You are here

function _potx_find_t_calls_with_context in Translation template extractor 8

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

Detect all occurances of t()-like calls from Drupal 7 (with context).

These sequences are searched for: T_STRING("$function_name") + "(" + T_CONSTANT_ENCAPSED_STRING + ")" T_STRING("$function_name") + "(" + T_CONSTANT_ENCAPSED_STRING + "," and then an optional value for the replacements and an optional array for the options with an optional context key.

Parameters

string $file: Name of file parsed.

string $save_callback: Callback function used to save strings.

string $function_name: The name of the function to look for (could be 't', '$t', 'st' or any other t-like function). Drupal 7 only supports context on t(), st() and $t().

string $string_mode: String mode to use: POTX_STRING_INSTALLER, POTX_STRING_RUNTIME or POTX_STRING_BOTH.

1 call to _potx_find_t_calls_with_context()
_potx_parse_php_file in ./potx.inc
Parse a PHP file for translatables.

File

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

Code

function _potx_find_t_calls_with_context($file, $save_callback, $function_name = 't', $string_mode = POTX_STRING_RUNTIME) {
  global $_potx_tokens, $_potx_lookup;

  // Lookup tokens by function name.
  if (isset($_potx_lookup[$function_name])) {
    foreach ($_potx_lookup[$function_name] as $ti) {
      if (count($_potx_tokens) <= $ti + 3 || $_potx_tokens[$ti + 1] != '(') {

        // This is not a t() call or similar, e.g. "TranslatableMarkup" in
        // "class TranslationWrapper extends TranslatableMarkup {}".
        continue;
      }
      list($prev, $ctok, $par, $mid, $rig) = [
        $_potx_tokens[$ti - 1],
        $_potx_tokens[$ti],
        $_potx_tokens[$ti + 1],
        $_potx_tokens[$ti + 2],
        $_potx_tokens[$ti + 3],
      ];
      list($type, $string, $line) = $ctok;
      if (is_array($prev) && $prev[0] == T_FUNCTION) {
        continue;
      }
      if ($par == "(") {
        if (in_array($rig, [
          ")",
          ",",
        ]) && (is_array($mid) && $mid[0] == T_CONSTANT_ENCAPSED_STRING)) {

          // By default, there is no context.
          $context = POTX_CONTEXT_NONE;
          if ($rig == ',') {

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

            // Only save if there was no error in context parsing.
            $save_callback(_potx_format_quoted_string($mid[1]), $context, $file, $line, $string_mode);
          }
        }
        else {

          // $function_name() found, but inside is something which is not a
          // string literal.
          _potx_marker_error($file, $line, $function_name, $ti, t('The first parameter 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');
        }
      }
    }
  }
}