You are here

function _potx_process_twig_trans_tag in Translation template extractor 8

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

Parses a single {% trans %} tag, and extracts the translatable string.

A {% trans %} tag can have the following formats: 1. {% trans 'string' %} 2. {% trans %} string {% endtrans %} 3. {% trans %} singular string {% plural %} plural string {% endtrans %}

Parameters

Twig_TokenStream $stream: The twig token stream.

1 call to _potx_process_twig_trans_tag()
_potx_parse_twig_file in ./potx.inc
Parse a Twig template for translatables. Drupal 8+.

File

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

Code

function _potx_process_twig_trans_tag(Twig_TokenStream $stream, $file, $save_callback) {
  $is_plural = FALSE;
  $context = POTX_CONTEXT_NONE;

  // If the current token in the stream is a string, this trans tag
  // has a simple string argument to be translated.
  $token = $stream
    ->next();
  if ($token
    ->test(Twig_Token::STRING_TYPE)) {
    $text = $token
      ->getValue();

    // Check for context.
    $token = $stream
      ->next();
    if ($token
      ->test(Twig_Token::NAME_TYPE, 'with')) {
      $context = _potx_find_twig_trans_context($stream);
    }
    $save_callback(_potx_format_quoted_string('"' . trim($text) . '"'), $context, $file, $token
      ->getLine());
    return;
  }

  // Otherwise, we are in a trans/endtrans structure.
  $text = [];
  $line = $token
    ->getLine();

  // Process the stream until we reach the endtrans tag.
  while (!$stream
    ->isEOF() && !$token
    ->test('endtrans')) {

    // If it's text, add it to the translation.
    if ($token
      ->test(Twig_Token::TEXT_TYPE)) {
      $text[] = $token
        ->getValue();
    }
    elseif ($token
      ->test(Twig_Token::NAME_TYPE, 'with')) {
      $context = _potx_find_twig_trans_context($stream);
    }
    elseif ($token
      ->test('plural')) {
      $singular = implode('', $text);
      $is_plural = TRUE;
      $text = [];

      // Skip past the 'count' token.
      $stream
        ->next();
    }
    elseif ($token
      ->test(Twig_Token::VAR_START_TYPE)) {
      $name = [];
      while ($stream
        ->look(1)
        ->test(Twig_Token::PUNCTUATION_TYPE, '.')) {
        $token = $stream
          ->next();
        $name[] = $token
          ->getValue();
        $stream
          ->next();
      }
      $token = $stream
        ->next();
      $name[] = $token
        ->getValue();

      // Figure out if it's escaped or placeholder.
      $prefix = '@';
      $token = $stream
        ->next();

      // If the next thing we see is |, this may be a placeholder. The only
      // "filter" supported is placeholder, so if there is any other filter
      // then we keep assuming it is to be escaped. (Even if later a
      // placeholder filter was attempted).
      if ($token
        ->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
        $token = $stream
          ->next();
        if ($token
          ->getValue() == 'placeholder') {
          $prefix = '%';
        }
      }
      $text[] = $prefix . implode('.', $name);
    }
    $token = $stream
      ->next();
  }
  if ($is_plural) {
    $plural = implode('', $text);
    $save_callback(_potx_format_quoted_string('"' . trim($singular) . '"') . "\0" . _potx_format_quoted_string('"' . trim($plural) . '"'), $context, $file, $line);
  }
  else {
    $save_callback(_potx_format_quoted_string('"' . trim(implode('', $text)) . '"'), $context, $file, $line);
  }
}