TransTokenParser.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. class TransTokenParser extends AbstractTokenParser
  25. {
  26. /**
  27. * Parses a token and returns a node.
  28. *
  29. * @param Token $token
  30. *
  31. * @return Node
  32. *
  33. * @throws SyntaxError
  34. */
  35. public function parse(Token $token)
  36. {
  37. $lineno = $token->getLine();
  38. $stream = $this->parser->getStream();
  39. $vars = new ArrayExpression(array(), $lineno);
  40. $domain = null;
  41. $locale = null;
  42. if (!$stream->test(Token::BLOCK_END_TYPE)) {
  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()->getName());
  59. }
  60. }
  61. // {% trans %}message{% endtrans %}
  62. $stream->expect(Token::BLOCK_END_TYPE);
  63. $body = $this->parser->subparse(array($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()->getName());
  66. }
  67. $stream->expect(Token::BLOCK_END_TYPE);
  68. return new TransNode($body, $domain, null, $vars, $locale, $lineno, $this->getTag());
  69. }
  70. public function decideTransFork($token)
  71. {
  72. return $token->test(array('endtrans'));
  73. }
  74. /**
  75. * Gets the tag name associated with this token parser.
  76. *
  77. * @return string The tag name
  78. */
  79. public function getTag()
  80. {
  81. return 'trans';
  82. }
  83. }