function _potx_process_twig_trans_tag in Translation template extractor 6.3
Same name and namespace in other branches
- 8 potx.inc \_potx_process_twig_trans_tag()
- 7.3 potx.inc \_potx_process_twig_trans_tag()
- 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:
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 1777 - Extraction API used by the web and command line interface.
Code
function _potx_process_twig_trans_tag($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 = array();
$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 = array();
// Skip past the 'count' token.
$stream
->next();
}
elseif ($token
->test(Twig_Token::VAR_START_TYPE)) {
$name = array();
while ($stream
->look(1)
->test(Twig_Token::PUNCTUATION_TYPE, '.')) {
$token = $stream
->next();
$name[] = $token
->getValue();
$stream
->next();
}
$token = $stream
->next();
$name[] = $token
->getValue();
$name = implode('.', $name);
// Figure out if it's escaped, passthrough, or placeholder.
$token = $stream
->next();
// If the next thing we see is }}, this is escaped.
if ($token
->test(Twig_Token::VAR_END_TYPE)) {
$text[] = "@{$name}";
}
elseif ($token
->test(Twig_Token::PUNCTUATION_TYPE, '|')) {
$token = $stream
->next();
if ($token
->getValue() == 'passthrough') {
$text[] = "!{$name}";
}
elseif ($token
->getValue() == 'placeholder') {
$text[] = "%{$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);
}
}