vendor/symfony/twig-bridge/TokenParser/TransTokenParser.php line 77

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Bridge\Twig\TokenParser;
  11. use Symfony\Bridge\Twig\Node\TransNode;
  12. use Twig\Error\SyntaxError;
  13. use Twig\Node\Expression\AbstractExpression;
  14. use Twig\Node\Expression\ArrayExpression;
  15. use Twig\Node\Node;
  16. use Twig\Node\TextNode;
  17. use Twig\Token;
  18. use Twig\TokenParser\AbstractTokenParser;
  19. /**
  20.  * Token Parser for the 'trans' tag.
  21.  *
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  */
  24. final class TransTokenParser extends AbstractTokenParser
  25. {
  26.     public function parse(Token $token): Node
  27.     {
  28.         $lineno $token->getLine();
  29.         $stream $this->parser->getStream();
  30.         $count null;
  31.         $vars = new ArrayExpression([], $lineno);
  32.         $domain null;
  33.         $locale null;
  34.         if (!$stream->test(Token::BLOCK_END_TYPE)) {
  35.             if ($stream->test('count')) {
  36.                 // {% trans count 5 %}
  37.                 $stream->next();
  38.                 $count $this->parser->getExpressionParser()->parseExpression();
  39.             }
  40.             if ($stream->test('with')) {
  41.                 // {% trans with vars %}
  42.                 $stream->next();
  43.                 $vars $this->parser->getExpressionParser()->parseExpression();
  44.             }
  45.             if ($stream->test('from')) {
  46.                 // {% trans from "messages" %}
  47.                 $stream->next();
  48.                 $domain $this->parser->getExpressionParser()->parseExpression();
  49.             }
  50.             if ($stream->test('into')) {
  51.                 // {% trans into "fr" %}
  52.                 $stream->next();
  53.                 $locale $this->parser->getExpressionParser()->parseExpression();
  54.             } elseif (!$stream->test(Token::BLOCK_END_TYPE)) {
  55.                 throw new SyntaxError('Unexpected token. Twig was looking for the "with", "from", or "into" keyword.'$stream->getCurrent()->getLine(), $stream->getSourceContext());
  56.             }
  57.         }
  58.         // {% trans %}message{% endtrans %}
  59.         $stream->expect(Token::BLOCK_END_TYPE);
  60.         $body $this->parser->subparse($this->decideTransFork(...), true);
  61.         if (!$body instanceof TextNode && !$body instanceof AbstractExpression) {
  62.             throw new SyntaxError('A message inside a trans tag must be a simple text.'$body->getTemplateLine(), $stream->getSourceContext());
  63.         }
  64.         $stream->expect(Token::BLOCK_END_TYPE);
  65.         return new TransNode($body$domain$count$vars$locale$lineno$this->getTag());
  66.     }
  67.     public function decideTransFork(Token $token): bool
  68.     {
  69.         return $token->test(['endtrans']);
  70.     }
  71.     public function getTag(): string
  72.     {
  73.         return 'trans';
  74.     }
  75. }