Same name and namespace in other branches
  1. 7.x modules/locale/locale.module \locale_js_alter()
  2. 8.9.x core/modules/locale/locale.module \locale_js_alter()
  3. 9 core/modules/locale/locale.module \locale_js_alter()

Implements hook_js_alter().

File

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

Code

function locale_js_alter(&$javascript, AttachedAssetsInterface $assets, LanguageInterface $language) {
  $files = [];
  foreach ($javascript as $item) {
    if (isset($item['type']) && $item['type'] == 'file') {

      // Ignore the JS translation placeholder file.
      if ($item['data'] === 'core/modules/locale/locale.translation.js') {
        continue;
      }
      $files[] = $item['data'];
    }
  }

  // Replace the placeholder file with the actual JS translation file.
  $placeholder_file = 'core/modules/locale/locale.translation.js';
  if (isset($javascript[$placeholder_file])) {
    if ($translation_file = locale_js_translate($files, $language)) {
      $js_translation_asset =& $javascript[$placeholder_file];
      $js_translation_asset['data'] = $translation_file;

      // @todo Remove this when https://www.drupal.org/node/1945262 lands.
      // Decrease the weight so that the translation file is loaded first.
      $js_translation_asset['weight'] = $javascript['core/misc/drupal.js']['weight'] - 0.001;
    }
    else {

      // If no translation file exists, then remove the placeholder JS asset.
      unset($javascript[$placeholder_file]);
    }
  }
}