W3cubDocs

/Drupal 8

function locale_js_alter

locale_js_alter(&$javascript, AttachedAssetsInterface $assets)

Implements hook_js_alter().

File

core/modules/locale/locale.module, line 487
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 = array();
  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]);
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!locale!locale.module/function/locale_js_alter/8.1.x