function locale_js_translate

Same name in other branches
  1. 8.9.x core/modules/locale/locale.module \locale_js_translate()
  2. 10 core/modules/locale/locale.module \locale_js_translate()
  3. 11.x 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 514

Code

function locale_js_translate(array $files = []) {
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
    $dir = 'public://' . \Drupal::config('locale.settings')->get('javascript.directory');
    $parsed = \Drupal::state()->get('system.javascript_parsed', []);
    $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', []);
    $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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.