You are here

function _potx_find_format_plural_calls in Translation template extractor 7.2

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

Detect all occurances of format_plural calls.

These sequences are searched for: T_STRING("$function_name") + "(" + ..anything (might be more tokens).. + "," + T_CONSTANT_ENCAPSED_STRING + "," + T_CONSTANT_ENCAPSED_STRING + parenthesis (or comma allowed from Drupal 6)

Parameters

$file: Name of file parsed.

$save_callback: Callback function used to save strings.

$function_name: The name of the function to look for (could be 'format_plural' or 'formatPlural').

$api_version: Drupal API version to work with.

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

File

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

Code

function _potx_find_format_plural_calls($file, $save_callback, $function_name = 'format_plural', $api_version = POTX_API_CURRENT) {
  global $_potx_tokens, $_potx_lookup;
  if (isset($_potx_lookup[$function_name])) {
    foreach ($_potx_lookup[$function_name] as $ti) {
      list($prev, $ctok, $par1) = array(
        $_potx_tokens[$ti - 1],
        $_potx_tokens[$ti],
        $_potx_tokens[$ti + 1],
      );
      list($type, $string, $line) = $ctok;
      if (is_array($prev) && $prev[0] == T_FUNCTION) {
        continue;
      }
      if ($par1 == "(") {

        // Eat up everything that is used as the first parameter
        $tn = $ti + 2;
        $depth = 0;
        while (!($_potx_tokens[$tn] == "," && $depth == 0)) {
          if ($_potx_tokens[$tn] == "(") {
            $depth++;
          }
          elseif ($_potx_tokens[$tn] == ")") {
            $depth--;
          }
          $tn++;
          if ($depth < 0) {

            // There is no second argument. This $function_name() call was a
            // false positive, continue with the next one.
            continue 2;
          }
        }

        // Get further parameters
        list($comma1, $singular, $comma2, $plural, $par2) = array(
          $_potx_tokens[$tn],
          $_potx_tokens[$tn + 1],
          $_potx_tokens[$tn + 2],
          $_potx_tokens[$tn + 3],
          $_potx_tokens[$tn + 4],
        );
        if ($comma2 == ',' && ($par2 == ')' || $par2 == ',' && $api_version > POTX_API_5) && (is_array($singular) && $singular[0] == T_CONSTANT_ENCAPSED_STRING) && (is_array($plural) && $plural[0] == T_CONSTANT_ENCAPSED_STRING)) {

          // By default, there is no context.
          $context = POTX_CONTEXT_NONE;
          if ($par2 == ',' && $api_version > POTX_API_6) {

            // If there was a comma after the plural, we need to look forward
            // to try and find the context.
            $context = _potx_find_context($ti, $tn + 5, $file, $function_name);
          }
          if ($context !== POTX_CONTEXT_ERROR) {

            // Only save if there was no error in context parsing.
            $save_callback(_potx_format_quoted_string($singular[1]) . "\0" . _potx_format_quoted_string($plural[1]), $context, $file, $line);
          }
        }
        else {

          // $function_name() found, but the parameters are not correct.
          _potx_marker_error($file, $line, $function_name, $ti, t('In @function(), the singular and plural strings should be literal strings. There should be no variables, concatenation, constants or even a t() call there.', array(
            '@function' => $function_name,
          )), 'http://drupal.org/node/323072');
        }
      }
    }
  }
}