TransTokenParser.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  27. * {@inheritdoc}
  28. */
  29. public function parse(Token $token): Node
  30. {
  31. $lineno = $token->getLine();
  32. $stream = $this->parser->getStream();
  33. $count = null;
  34. $vars = new ArrayExpression([], $lineno);
  35. $domain = null;
  36. $locale = null;
  37. if (!$stream->test(Token::BLOCK_END_TYPE)) {
  38. if ($stream->test('count')) {
  39. // {% trans count 5 %}
  40. $stream->next();
  41. $count = $this->parser->getExpressionParser()->parseExpression();
  42. }
  43. if ($stream->test('with')) {
  44. // {% trans with vars %}
  45. $stream->next();
  46. $vars = $this->parser->getExpressionParser()->parseExpression();
  47. }
  48. if ($stream->test('from')) {
  49. // {% trans from "messages" %}
  50. $stream->next();
  51. $domain = $this->parser->getExpressionParser()->parseExpression();
  52. }
  53. if ($stream->test('into')) {
  54. // {% trans into "fr" %}
  55. $stream->next();
  56. $locale = $this->parser->getExpressionParser()->parseExpression();
  57. } elseif (!$stream->test(Token::BLOCK_END_TYPE)) {
  58. throw new SyntaxError('Unexpected token. Twig was looking for the "with", "from", or "into" keyword.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
  59. }
  60. }
  61. // {% trans %}message{% endtrans %}
  62. $stream->expect(Token::BLOCK_END_TYPE);
  63. $body = $this->parser->subparse([$this, 'decideTransFork'], true);
  64. if (!$body instanceof TextNode && !$body instanceof AbstractExpression) {
  65. throw new SyntaxError('A message inside a trans tag must be a simple text.', $body->getTemplateLine(), $stream->getSourceContext());
  66. }
  67. $stream->expect(Token::BLOCK_END_TYPE);
  68. return new TransNode($body, $domain, $count, $vars, $locale, $lineno, $this->getTag());
  69. }
  70. public function decideTransFork(Token $token): bool
  71. {
  72. return $token->test(['endtrans']);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getTag(): string
  78. {
  79. return 'trans';
  80. }
  81. }