You are here

function _potx_parse_js_file in Translation template extractor 5.2

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

Parse a JavaScript file for translatables. Only for 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 1022
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);
      $save_callback(_potx_parse_js_string($match[1]), $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);
      $save_callback(_potx_parse_js_string($match[1]) . "\0" . _potx_parse_js_string($match[2]), $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) {
      _potx_status(t("Invalid marker content in %filename\n* %marker\n\n", array(
        '%filename' => $file,
        '%marker' => $match[0],
      )), 'error');
    }
  }
}