You are here

function locale_js_alter in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/locale/locale.module \locale_js_alter()
  2. 7 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 493
Enables the translation of the user interface to languages other than English.

Code

function locale_js_alter(&$javascript, AttachedAssetsInterface $assets) {

  // @todo Remove this in https://www.drupal.org/node/2421323.
  $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)) {
      $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]);
    }
  }
}