Accueil / Blog / Métier / Archives / How to automate the re-import of a translation file with hook_update()

How to automate the re-import of a translation file with hook_update()

Par Benoit Bryon — publié 25/03/2011
You worked on a module of an existing website and made changes in the related translation file

You worked on a module of an existing website and made changes in the related translation file. So, at upgrade, the translation file needs to be reloaded. Here is a recipe to perform it automatiqcally within a hook_update_N().

Adapt the following code to your needs.

/**
 * Implementation of hook_update_N().
 * Version 1.2
 */
function my_custom_module_update_6102(&$sandbox) {
  // reload the "fr" translation file for module "my_custom_module"
  $module_name = 'my_custom_module';
  $language = 'fr';
  include_once './includes/locale.inc';
  $file = new stdClass();
  $file->filename = 'translations/'. $module_name .'.'. $language .'.po';
  $file->filepath = drupal_get_path('module', $module_name) .'/'. $file->filename;
  $file->filemime = file_get_mimetype($file->filename);
  $success = _locale_import_po($file, $language, LOCALE_IMPORT_KEEP, 'default'); // You may use LOCALE_IMPORT_OVERWRITE here
  $ret[] = array('success' => $success, 'query' => 'Reload '. $module_name .' translation file for '. $language .' language');
}

Is there a simpler solution? I don't know right know.

If not, maybe this one should be put in a function and submitted as a contrib. It would be great to simplify the code this way:

/**
 * Implementation of hook_update_N().
 * Version 1.2
 *
 * @uses locale_import_module_po() which does not exist by now.
 */
function my_custom_module_update_6102(&$sandbox) {
  // reload the "fr" translation file for module "my_custom_module"
  include_once './includes/locale.inc';
  $success = locale_reload_module_po('my_custom_module', 'fr', LOCALE_IMPORT_KEEP, 'default'); // You may use LOCALE_IMPORT_OVERWRITE here
  $ret[] = array('success' => $success, 'query' => 'Reload '. $module_name .' translation file for '. $language .' language');
}
ABONNEZ-VOUS À LA NEWSLETTER !