diff options
author | Gábor Hojtsy <gabor@hojtsy.hu> | 2007-11-26 22:34:09 +0000 |
---|---|---|
committer | Gábor Hojtsy <gabor@hojtsy.hu> | 2007-11-26 22:34:09 +0000 |
commit | efbd1db550a62a282faa5a7d8ca733dea68dc046 (patch) | |
tree | 5f00ab268d2c2d9d3ec039b56871f0ad130ba4b6 /modules | |
parent | 4c040de89b719da3f66e2ea7f5a8caca4a3e27e4 (diff) | |
download | brdo-efbd1db550a62a282faa5a7d8ca733dea68dc046.tar.gz brdo-efbd1db550a62a282faa5a7d8ca733dea68dc046.tar.bz2 |
#193320 by JirkaRybka: _locale_rebuild_js() was invoked on every page view, now optimized
Diffstat (limited to 'modules')
-rw-r--r-- | modules/locale/locale.module | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/modules/locale/locale.module b/modules/locale/locale.module index a07e5afdd..d6a94a0d6 100644 --- a/modules/locale/locale.module +++ b/modules/locale/locale.module @@ -458,6 +458,74 @@ function locale_system_update($components) { } } +/** + * Update JavaScript translation file, if required, and add it to the page. + * + * This function checks all JavaScript files currently added via drupal_add_js() + * and invokes parsing if they have not yet been parsed for Drupal.t() + * and Drupal.formatPlural() calls. Also refreshes the JavaScript translation + * file if necessary, and adds it to the page. + */ +function locale_update_js_files() { + global $language; + + $dir = file_create_path(variable_get('locale_js_directory', 'languages')); + $parsed = variable_get('javascript_parsed', array()); + + // The first three parameters are NULL in order to get an array with all + // scopes. This is necessary to prevent recreation of JS translation files + // when new files are added for example in the footer. + $javascript = drupal_add_js(NULL, NULL, NULL); + $files = $new_files = FALSE; + + foreach ($javascript as $scope) { + foreach ($scope as $type => $data) { + if ($type != 'setting' && $type != 'inline') { + foreach ($data as $filepath => $info) { + $files = TRUE; + if (!in_array($filepath, $parsed)) { + // Don't parse our own translations files. + if (substr($filepath, 0, strlen($dir)) != $dir) { + locale_inc_callback('_locale_parse_js_file', $filepath); + watchdog('locale', 'Parsed JavaScript file %file.', array('%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_inc_callback('_locale_invalidate_js'); + } + + // If necessary, rebuild the translation file for the current language. + if (!empty($parsed['refresh:'. $language->language])) { + // Don't clear the refresh flag on failure, so that another try will + // be performed later. + if (locale_inc_callback('_locale_rebuild_js')) { + unset($parsed['refresh:'. $language->language]); + } + // Store any changes after refresh was attempted. + variable_set('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. + else if ($new_files) { + variable_set('javascript_parsed', $parsed); + } + + // Add the translation JavaScript file to the page. + if ($files && !empty($language->javascript)) { + drupal_add_js($dir .'/'. $language->language .'_'. $language->javascript .'.js', 'core'); + } +} + // --------------------------------------------------------------------------------- // Language switcher block |