Re-import translation files in hook_update()

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

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 automatically within a hook_update_N().

Adapt the following code to your needs.

<?php
/**
 * 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:

<?php
/**
 * Implementation of hook_update_N().
 * Version 1.2
 *
 * @uses update_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 = update_locale_import_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');
}

A function definition to perform it

The following function makes the job.

Thanks to David Stosik for his help!

<?php
/**
 * Reloads translation file for a given module.
 *
 * @param mixed $module
 *   Module(s) name(s). An array of strings or a string.
 * @param mixed $languages
 * @return array
 *   An array with a "success" key which is a boolean and a "query" key which
 *   describes the action performed. It is compatible with hook_update and
 *   hook_install return values.
 */
function update_locale_import_module_po($modules=array(), $languages=array(), $mode=LOCALE_IMPORT_KEEP) {
  $ret = array();
  if (!is_array($modules)) {
    $modules = array($modules);
  }
  if (!is_array($languages)) {
    $languages = array($languages);
  }
  foreach ($modules as $module) {
    foreach ($languages as $language) {
      $file = new stdClass();
      $file->filename = 'translations/'. $module .'.'. $language .'.po';
      $file->filepath = drupal_get_path('module', $module) .'/'. $file->filename;
      $file->filemime = 'application/octet-stream';
      $success = locale_inc_callback('_locale_import_po', $file, 'fr', LOCALE_IMPORT_KEEP, 'default');
      $ret[] = array('success' => $success, 'query' => 'Reload '. $module .' translation file');
    }
  }
  return $ret;
}

You can call it like this:

<?php
update_locale_import_module_po('my_custom_module', 'fr', LOCALE_IMPORT_OVERWRITE);

Or like that, depending on your needs:

<?php
update_locale_import_module_po(array('my_custom_module', 'some_other_module'), array('fr', 'es'), LOCALE_IMPORT_OVERWRITE);

Useful inside hook_update() implementations !