1.20.x locale.module | locale_js_alter(&$javascript) |
Implements hook_js_alter().
This function checks all JavaScript files currently added via backdrop_add_js() and invokes parsing if they have not yet been parsed for Backdrop.t() and Backdrop.formatPlural() calls. Also refreshes the JavaScript translation file if necessary, and adds it to the page.
File
- modules/
locale/ locale.module, line 751 - Add language handling functionality and enables the translation of the user interface to languages other than English.
Code
function locale_js_alter(&$javascript) {
global $language;
$dir = 'public://' . settings_get('locale_js_directory', 'languages');
$parsed = state_get('locale_javascript_parsed', array());
$files = $new_files = FALSE;
// Require because locale_js_alter() could be called without locale_init().
require_once BACKDROP_ROOT . '/core/includes/locale.inc';
foreach ($javascript as $item) {
if ($item['type'] == 'file') {
$files = TRUE;
$filepath = $item['data'];
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->langcode])) {
// Don't clear the refresh flag on failure, so that another try will
// be performed later.
if (_locale_rebuild_js()) {
unset($parsed['refresh:' . $language->langcode]);
}
// Store any changes after refresh was attempted.
state_set('locale_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) {
state_set('locale_javascript_parsed', $parsed);
}
// Add the translation JavaScript file to the page.
$locale_javascripts = state_get('locale_translation_javascript', array());
if ($files && !empty($locale_javascripts[$language->langcode])) {
// Add the translation JavaScript file to the page.
$file = $dir . '/' . $language->langcode . '_' . $locale_javascripts[$language->langcode] . '.js';
$javascript[$file] = backdrop_js_defaults($file);
}
}