You are here

function _potx_find_context in Translation template extractor 8

Same name and namespace in other branches
  1. 6.3 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

int $tf: Start position of the original function.

int $ti: Start position where we should search from.

string $file: Full path name of file parsed.

string $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 1457
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) = [
      $_potx_tokens[$ti],
      $_potx_tokens[$ti + 1],
      $_potx_tokens[$ti + 2],
    ];
    if ($com == ',' && ($forward = _potx_is_beginning_of_array($arr, $par))) {
      $nesting = 0;
      $ti += $forward;

      // 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], [
        '"context"',
        "'context'",
      ]) && $_potx_tokens[$ti][0] == T_CONSTANT_ENCAPSED_STRING && $nesting == 0 || _potx_is_end_of_array($_potx_tokens[$ti]) && $nesting == -1)) {
        $ti++;
        if (!is_array($_potx_tokens[$ti])) {

          // Treat each () and [] pair as going into / out of nesting levels.
          // There may be function or method calls as well as nested short
          // arrays within the arguments list. The list may be similar to:
          // array('langcode' => $obj->someMethod([])[2], 'context' =>
          // 'Long month name')
          // or
          // ['langcode' => $obj->someMethod(array())[2], 'context' =>
          // 'Long month name'].
          if ($_potx_tokens[$ti] == ')' || $_potx_tokens[$ti] == ']') {
            $nesting--;
          }
          if ($_potx_tokens[$ti] == '(' || $_potx_tokens[$ti] == '[') {
            $nesting++;
          }
        }
      }
      if ($nesting == 0) {

        // Found the 'context' key on the top level of the $options array.
        list($arw, $str) = [
          $_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.', [
            '@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;
}