You are here

function _potx_find_context in Translation template extractor 6.3

Same name and namespace in other branches
  1. 8 potx.inc \_potx_find_context()
  2. 7.3 potx.inc \_potx_find_context()
  3. 7 potx.inc \_potx_find_context()
  4. 7.2 potx.inc \_potx_find_context()

Helper to find the value for 'context' on t() and format_plural().

Parameters

$tf: Start position of the original function.

$ti: Start position where we should search from.

$file: Full path name of file parsed.

function_name: The name of the function to look for. Either 'format_plural' or 't' given that Drupal 7 only supports context on these.

2 calls to _potx_find_context()
_potx_find_format_plural_calls in ./potx.inc
Detect all occurances of format_plural calls.
_potx_find_t_calls_with_context in ./potx.inc
Detect all occurances of t()-like calls from Drupal 7 (with context).

File

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

Code

function _potx_find_context($tf, $ti, $file, $function_name) {
  global $_potx_tokens;

  // Start from after the comma and skip the possible arguments for the function
  // so we can look for the context.
  if (($ti = _potx_skip_args($ti)) && $_potx_tokens[$ti] == ',') {

    // Now we actually might have some definition for a context. The $options
    // argument is coming up, which might have a key for context.
    list($com, $arr, $par) = array(
      $_potx_tokens[$ti],
      $_potx_tokens[$ti + 1],
      $_potx_tokens[$ti + 2],
    );
    if ($com == ',' && $arr[1] == 'array' && $par == '(') {
      $nesting = 0;
      $ti += 3;

      // Go through to either the end of the array or to the key definition of
      // context on the same nesting level.
      while (!(is_array($_potx_tokens[$ti]) && in_array($_potx_tokens[$ti][1], array(
        '"context"',
        "'context'",
      )) && $_potx_tokens[$ti][0] == T_CONSTANT_ENCAPSED_STRING && $nesting == 0 || $_potx_tokens[$ti] == ')' && $nesting == -1)) {
        $ti++;
        if (!is_array($_potx_tokens[$ti])) {
          if ($_potx_tokens[$ti] == ')') {
            $nesting--;
          }
          if ($_potx_tokens[$ti] == '(') {
            $nesting++;
          }
        }
      }
      if ($nesting == 0) {

        // Found the 'context' key on the top level of the $options array.
        list($arw, $str) = array(
          $_potx_tokens[$ti + 1],
          $_potx_tokens[$ti + 2],
        );
        if (is_array($arw) && $arw[1] == '=>' && is_array($str) && $str[0] == T_CONSTANT_ENCAPSED_STRING) {
          return _potx_format_quoted_string($str[1]);
        }
        else {
          list($type, $string, $line) = $_potx_tokens[$ti];

          // @todo: fix error reference.
          _potx_marker_error($file, $line, $function_name, $tf, t('The context element in the options array argument to @function() should be a literal string. There should be no variables, concatenation, constants or other non-literal strings there.', array(
            '@function' => $function_name,
          )), 'http://drupal.org/node/322732');

          // Return with error.
          return POTX_CONTEXT_ERROR;
        }
      }
      else {

        // Did not found 'context' key in $options array.
        return POTX_CONTEXT_NONE;
      }
    }
  }

  // After skipping args, we did not find a comma to look for $options.
  return POTX_CONTEXT_NONE;
}