function _locale_rebuild_js

(Re-)Creates the JavaScript translation file for a language.

Parameters

$langcode: The language, the translation file should be (re)created for.

3 calls to _locale_rebuild_js()
LocaleTranslationTest::testJavaScriptTranslation in drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleTranslationTest.php
LocaleUninstallTest::testUninstallProcess in drupal/core/modules/locale/lib/Drupal/locale/Tests/LocaleUninstallTest.php
Check if the values of the Locale variables are correct after uninstall.
locale_js_alter in drupal/core/modules/locale/locale.module
Implements hook_js_alter().

File

drupal/core/modules/locale/locale.module, line 943
Enables the translation of the user interface to languages other than English.

Code

function _locale_rebuild_js($langcode = NULL) {
  if (!isset($langcode)) {
    $language = language(LANGUAGE_TYPE_INTERFACE);
  }
  else {

    // Get information about the locale.
    $languages = language_list();
    $language = $languages[$langcode];
  }

  // Construct the array for JavaScript translations.
  // Only add strings with a translation to the translations array.
  $conditions = array(
    'type' => 'javascript',
    'language' => $language->langcode,
    'translated' => TRUE,
  );
  $translations = array();
  foreach (locale_storage()
    ->getTranslations($conditions) as $data) {
    $translations[$data->context][$data->source] = $data->translation;
  }

  // Construct the JavaScript file, if there are translations.
  $data_hash = NULL;
  $data = $status = '';
  if (!empty($translations)) {
    $data = "Drupal.locale = { ";
    $locale_plurals = variable_get('locale_translation_plurals', array());
    if (!empty($locale_plurals[$language->langcode])) {
      $data .= "'pluralFormula': function (\$n) { return Number({$locale_plurals[$language->langcode]['formula']}); }, ";
    }
    $data .= "'strings': " . drupal_json_encode($translations) . " };";
    $data_hash = drupal_hash_base64($data);
  }

  // Construct the filepath where JS translation files are stored.
  // There is (on purpose) no front end to edit that variable.
  $dir = 'public://' . variable_get('locale_js_directory', 'languages');

  // Delete old file, if we have no translations anymore, or a different file to be saved.
  $locale_javascripts = variable_get('locale_translation_javascript', array());
  $changed_hash = !isset($locale_javascripts[$language->langcode]) || $locale_javascripts[$language->langcode] != $data_hash;
  if (!empty($locale_javascripts[$language->langcode]) && (!$data || $changed_hash)) {
    file_unmanaged_delete($dir . '/' . $language->langcode . '_' . $locale_javascripts[$language->langcode] . '.js');
    $locale_javascripts[$language->langcode] = '';
    $status = 'deleted';
  }

  // Only create a new file if the content has changed or the original file got
  // lost.
  $dest = $dir . '/' . $language->langcode . '_' . $data_hash . '.js';
  if ($data && ($changed_hash || !file_exists($dest))) {

    // Ensure that the directory exists and is writable, if possible.
    file_prepare_directory($dir, FILE_CREATE_DIRECTORY);

    // Save the file.
    if (file_unmanaged_save_data($data, $dest)) {
      $locale_javascripts[$language->langcode] = $data_hash;

      // If we deleted a previous version of the file and we replace it with a
      // new one we have an update.
      if ($status == 'deleted') {
        $status = 'updated';
      }
      elseif ($changed_hash) {
        $status = 'created';
      }
      else {
        $status = 'rebuilt';
      }
    }
    else {
      $locale_javascripts[$language->langcode] = '';
      $status = 'error';
    }
  }

  // Save the new JavaScript hash (or an empty value if the file just got
  // deleted). Act only if some operation was executed that changed the hash
  // code.
  if ($status && $changed_hash) {
    variable_set('locale_translation_javascript', $locale_javascripts);
  }

  // Log the operation and return success flag.
  switch ($status) {
    case 'updated':
      watchdog('locale', 'Updated JavaScript translation file for the language %language.', array(
        '%language' => $language->name,
      ));
      return TRUE;
    case 'rebuilt':
      watchdog('locale', 'JavaScript translation file %file.js was lost.', array(
        '%file' => $locale_javascripts[$language->langcode],
      ), WATCHDOG_WARNING);

    // Proceed to the 'created' case as the JavaScript translation file has
    // been created again.
    case 'created':
      watchdog('locale', 'Created JavaScript translation file for the language %language.', array(
        '%language' => $language->name,
      ));
      return TRUE;
    case 'deleted':
      watchdog('locale', 'Removed JavaScript translation file for the language %language because no translations currently exist for that language.', array(
        '%language' => $language->name,
      ));
      return TRUE;
    case 'error':
      watchdog('locale', 'An error occurred during creation of the JavaScript translation file for the language %language.', array(
        '%language' => $language->name,
      ), WATCHDOG_ERROR);
      return FALSE;
    default:

      // No operation needed.
      return TRUE;
  }
}