Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. locale.module

function 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.

\Drupal\Core\Language\LanguageInterface $language_interface: The interface language the files should be translated into.

Return value

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

1 call to locale_js_translate()
LocaleHooks::jsAlter in core/modules/locale/src/Hook/LocaleHooks.php
Implements hook_js_alter().

File

core/modules/locale/locale.module, line 316

Code

function locale_js_translate(array $files = [], $language_interface = NULL) {
    if (!isset($language_interface)) {
        $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 (!str_starts_with($filepath, $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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal