You are here

function _potx_parse_js_file in Translation template extractor 6.3

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 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 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 1602
Extraction API used by the web and command line interface.

Code

function _potx_parse_js_file($code, $file, $save_callback) {

  // 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*                     # match "Drupal.t" with whitespace
    \\(\\s*                                       # match "(" argument list start
    (' . POTX_JS_STRING . ')\\s*                 # capture string argument
    (?:,\\s*' . POTX_JS_OBJECT . '\\s*            # optionally capture str args
      (?:,\\s*' . POTX_JS_OBJECT_CONTEXT . '\\s*) # optionally capture context
    ?)?                                         # close optional args
    \\)                                          # match ")" to finish
    ~sx', $code, $t_matches, PREG_SET_ORDER);

  // Add strings from Drupal.t().
  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);

      // Get context
      if (!empty($match[2])) {

        // Remove wrapping quotes
        $context = preg_replace('~^[\'"]|[\'"]$~', '', $match[2]);
        $context = _potx_parse_js_string($match[2]);
      }
      else {

        // Set context to null
        $context = POTX_CONTEXT_NONE;
      }
      $save_callback(_potx_parse_js_string($match[1]), $context, $file, 0);
    }
  }

  // Match all Drupal.formatPlural() calls in another array.
  preg_match_all('~
    [^\\w]Drupal\\s*\\.\\s*formatPlural\\s*  # match "Drupal.formatPlural" with whitespace
    \\(                                  # match "(" argument list start
    \\s*.+?\\s*,\\s*                       # match count argument
    (' . POTX_JS_STRING . ')\\s*,\\s*     # match singular string argument
    (                                   # capture plural string argument
      (?:                               # non-capturing group to repeat string pieces
        (?:
          \'                            # match start of single-quoted string
          (?:\\\\\'|[^\'])*             # match any character except unescaped single-quote
          @count                        # match "@count"
          (?:\\\\\'|[^\'])*             # match any character except unescaped single-quote
          \'                            # match end of single-quoted string
          |
          "                             # match start of double-quoted string
          (?:\\\\"|[^"])*               # match any character except unescaped double-quote
          @count                        # match "@count"
          (?:\\\\"|[^"])*               # match any character except unescaped double-quote
          "                             # match end of double-quoted string
        )
        (?:\\s*\\+\\s*)?                   # match "+" with possible whitespace, for str concat
      )+                                # match multiple because we supports concatenating strs
    )\\s*                                # end capturing of plural string argument
    (?:,\\s*' . POTX_JS_OBJECT . '\\s*              # optionally capture string args
      (?:,\\s*' . POTX_JS_OBJECT_CONTEXT . '\\s*)?  # optionally capture context
    )?
    \\)                                            # match ")" to finish
    ~sx', $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);

      // Get context
      if (!empty($match[3])) {

        // Remove wrapping quotes
        $context = preg_replace('~^[\'"]|[\'"]$~', '', $match[3]);
        $context = _potx_parse_js_string($match[3]);
      }
      else {

        // Set context to null
        $context = POTX_CONTEXT_NONE;
      }
      $save_callback(_potx_parse_js_string($match[1]) . "\0" . _potx_parse_js_string($match[2]), $context, $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');
    }
  }
}