TwigTransTokenParser.php in Drupal 9
File
core/lib/Drupal/Core/Template/TwigTransTokenParser.php
View source
<?php
namespace Drupal\Core\Template;
use Twig\Error\SyntaxError;
use Twig\Node\Expression\FilterExpression;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\NameExpression;
use Twig\Node\Node;
use Twig\Node\PrintNode;
use Twig\Node\TextNode;
use Twig\Token;
use Twig\TokenParser\AbstractTokenParser;
class TwigTransTokenParser extends AbstractTokenParser {
public function parse(Token $token) {
$lineno = $token
->getLine();
$stream = $this->parser
->getStream();
$body = NULL;
$options = NULL;
$count = NULL;
$plural = NULL;
if (!$stream
->test(Token::BLOCK_END_TYPE) && $stream
->test(Token::STRING_TYPE)) {
$body = $this->parser
->getExpressionParser()
->parseExpression();
}
if (!$stream
->test(Token::BLOCK_END_TYPE) && $stream
->test(Token::NAME_TYPE, 'with')) {
$stream
->next();
$options = $this->parser
->getExpressionParser()
->parseExpression();
}
if (!$body) {
$stream
->expect(Token::BLOCK_END_TYPE);
$body = $this->parser
->subparse([
$this,
'decideForFork',
]);
if ('plural' === $stream
->next()
->getValue()) {
$count = $this->parser
->getExpressionParser()
->parseExpression();
$stream
->expect(Token::BLOCK_END_TYPE);
$plural = $this->parser
->subparse([
$this,
'decideForEnd',
], TRUE);
}
}
$stream
->expect(Token::BLOCK_END_TYPE);
$this
->checkTransString($body, $lineno);
$node = new TwigNodeTrans($body, $plural, $count, $options, $lineno, $this
->getTag());
return $node;
}
public function decideForFork($token) {
return $token
->test([
'plural',
'endtrans',
]);
}
public function decideForEnd($token) {
return $token
->test('endtrans');
}
public function getTag() {
return 'trans';
}
protected function checkTransString(Node $body, $lineno) {
foreach ($body as $node) {
if ($node instanceof TextNode || $node instanceof PrintNode && $node
->getNode('expr') instanceof NameExpression || $node instanceof PrintNode && $node
->getNode('expr') instanceof GetAttrExpression || $node instanceof PrintNode && $node
->getNode('expr') instanceof FilterExpression) {
continue;
}
throw new SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);
}
}
}