function locale_js_alter

Implements hook_js_alter().

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.

File

drupal/modules/locale/locale.module, line 900
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://' . variable_get('locale_js_directory', 'languages');
  $parsed = variable_get('javascript_parsed', array());
  $files = $new_files = FALSE;

  // Require because locale_js_alter() could be called without locale_init().
  require_once DRUPAL_ROOT . '/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->language])) {

    // Don't clear the refresh flag on failure, so that another try will
    // be performed later.
    if (_locale_rebuild_js()) {
      unset($parsed['refresh:' . $language->language]);
    }

    // Store any changes after refresh was attempted.
    variable_set('javascript_parsed', $parsed);
  }
  elseif ($new_files) {
    variable_set('javascript_parsed', $parsed);
  }

  // Add the translation JavaScript file to the page.
  if ($files && !empty($language->javascript)) {

    // Add the translation JavaScript file to the page.
    $file = $dir . '/' . $language->language . '_' . $language->javascript . '.js';
    $javascript[$file] = drupal_js_defaults($file);
  }
}