locale_js_translate

locale_js_translate(array $files = array())

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.

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);
  }
  // If no refresh was attempted, but we have new source files, we need
  // to store them too. This occurs if current page is in English.
  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;
}
doc_Drupal
2016-10-29 09:24:17
Comments
Leave a Comment

Please login to continue.