You are here

function locale_js_translate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/locale/locale.module \locale_js_translate()

Returns a list of translation files given a list of JavaScript files.

This function checks all JavaScript files passed and invokes parsing if they have not yet been parsed for Drupal.t() and Drupal.formatPlural() calls. Also refreshes the JavaScript translation files if necessary, and returns the filepath to the translation file (if any).

Parameters

array $files: An array of local file paths.

Return value

string|null The filepath to the translation file or NULL if no translation is applicable.

2 calls to locale_js_translate()
CKEditor::getJSSettings in core/modules/ckeditor/src/Plugin/Editor/CKEditor.php
Returns JavaScript settings to be attached.
locale_js_alter in core/modules/locale/locale.module
Implements hook_js_alter().

File

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

Code

function locale_js_translate(array $files = array()) {
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();
  $dir = 'public://' . \Drupal::config('locale.settings')
    ->get('javascript.directory');
  $parsed = \Drupal::state()
    ->get('system.javascript_parsed') ?: array();
  $new_files = FALSE;
  foreach ($files as $filepath) {
    if (!in_array($filepath, $parsed)) {

      // Don't parse our own translations files.
      if (substr($filepath, 0, strlen($dir)) != $dir) {
        _locale_parse_js_file($filepath);
        $parsed[] = $filepath;
        $new_files = TRUE;
      }
    }
  }

  // If there are any new source files we parsed, invalidate existing
  // JavaScript translation files for all languages, adding the refresh
  // flags into the existing array.
  if ($new_files) {
    $parsed += _locale_invalidate_js();
  }

  // If necessary, rebuild the translation file for the current language.
  if (!empty($parsed['refresh:' . $language_interface
    ->getId()])) {

    // Don't clear the refresh flag on failure, so that another try will
    // be performed later.
    if (_locale_rebuild_js()) {
      unset($parsed['refresh:' . $language_interface
        ->getId()]);
    }

    // Store any changes after refresh was attempted.
    \Drupal::state()
      ->set('system.javascript_parsed', $parsed);
  }
  elseif ($new_files) {
    \Drupal::state()
      ->set('system.javascript_parsed', $parsed);
  }

  // Add the translation JavaScript file to the page.
  $locale_javascripts = \Drupal::state()
    ->get('locale.translation.javascript') ?: array();
  $translation_file = NULL;
  if (!empty($files) && !empty($locale_javascripts[$language_interface
    ->getId()])) {

    // Add the translation JavaScript file to the page.
    $translation_file = $dir . '/' . $language_interface
      ->getId() . '_' . $locale_javascripts[$language_interface
      ->getId()] . '.js';
  }
  return $translation_file;
}