You are here

function _potx_parse_js_file in Translation template extractor 7

Same name and namespace in other branches
  1. 8 potx.inc \_potx_parse_js_file()
  2. 5.2 potx.inc \_potx_parse_js_file()
  3. 5 potx.inc \_potx_parse_js_file()
  4. 6.3 potx.inc \_potx_parse_js_file()
  5. 6 potx.inc \_potx_parse_js_file()
  6. 6.2 potx.inc \_potx_parse_js_file()
  7. 7.3 potx.inc \_potx_parse_js_file()
  8. 7.2 potx.inc \_potx_parse_js_file()

Parse a JavaScript file for translatables. Only from Drupal 6.

Extracts strings wrapped in Drupal.t() and Drupal.formatPlural() calls and inserts them into potx storage.

Regex code lifted from _locale_parse_js_file().

1 call to _potx_parse_js_file()
_potx_process_file in ./potx.inc
Process a file and put extracted information to the given parameters.

File

./potx.inc, line 1352
Extraction API used by the web and command line interface.

Code

function _potx_parse_js_file($code, $file, $save_callback) {
  $js_string_regex = '(?:(?:\'(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*")(?:\\s*\\+\\s*)?)+';

  // Match all calls to Drupal.t() in an array.
  // Note: \s also matches newlines with the 's' modifier.
  preg_match_all('~[^\\w]Drupal\\s*\\.\\s*t\\s*\\(\\s*(' . $js_string_regex . ')\\s*[,\\)]~s', $code, $t_matches, PREG_SET_ORDER);
  if (isset($t_matches) && count($t_matches)) {
    foreach ($t_matches as $match) {

      // Remove match from code to help us identify faulty Drupal.t() calls.
      $code = str_replace($match[0], '', $code);

      // @todo: figure out how to parse out context, once Drupal supports it.
      $save_callback(_potx_parse_js_string($match[1]), POTX_CONTEXT_NONE, $file, 0);
    }
  }

  // Match all Drupal.formatPlural() calls in another array.
  preg_match_all('~[^\\w]Drupal\\s*\\.\\s*formatPlural\\s*\\(\\s*.+?\\s*,\\s*(' . $js_string_regex . ')\\s*,\\s*((?:(?:\'(?:\\\\\'|[^\'])*@count(?:\\\\\'|[^\'])*\'|"(?:\\\\"|[^"])*@count(?:\\\\"|[^"])*")(?:\\s*\\+\\s*)?)+)\\s*[,\\)]~s', $code, $plural_matches, PREG_SET_ORDER);
  if (isset($plural_matches) && count($plural_matches)) {
    foreach ($plural_matches as $index => $match) {

      // Remove match from code to help us identify faulty
      // Drupal.formatPlural() calls later.
      $code = str_replace($match[0], '', $code);

      // @todo: figure out how to parse out context, once Drupal supports it.
      $save_callback(_potx_parse_js_string($match[1]) . "\0" . _potx_parse_js_string($match[2]), POTX_CONTEXT_NONE, $file, 0);
    }
  }

  // Any remaining Drupal.t() or Drupal.formatPlural() calls are evil. This
  // regex is not terribly accurate (ie. code wrapped inside will confuse
  // the match), but we only need some unique part to identify the faulty calls.
  preg_match_all('~[^\\w]Drupal\\s*\\.\\s*(t|formatPlural)\\s*\\([^)]+\\)~s', $code, $faulty_matches, PREG_SET_ORDER);
  if (isset($faulty_matches) && count($faulty_matches)) {
    foreach ($faulty_matches as $index => $match) {
      $message = $match[1] == 't' ? t('Drupal.t() calls should have a single literal string as their first parameter.') : t('The singular and plural string parameters on Drupal.formatPlural() calls should be literal strings, plural containing a @count placeholder.');
      potx_status('error', $message, $file, NULL, $match[0], 'http://drupal.org/node/323109');
    }
  }
}