4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Bridge\Twig\TokenParser
;
14 use Symfony\Bridge\Twig\Node\TransNode
;
17 * Token Parser for the 'trans' tag.
19 * @author Fabien Potencier <fabien@symfony.com>
21 class TransTokenParser
extends \Twig_TokenParser
24 * Parses a token and returns a node.
26 * @param \Twig_Token $token A Twig_Token instance
28 * @return \Twig_NodeInterface A Twig_NodeInterface instance
30 * @throws \Twig_Error_Syntax
32 public function parse(\Twig_Token
$token)
34 $lineno = $token->getLine();
35 $stream = $this->parser
->getStream();
37 $vars = new \
Twig_Node_Expression_Array(array(), $lineno);
40 if (!$stream->test(\Twig_Token
::BLOCK_END_TYPE
)) {
41 if ($stream->test('with')) {
42 // {% trans with vars %}
44 $vars = $this->parser
->getExpressionParser()->parseExpression();
47 if ($stream->test('from')) {
48 // {% trans from "messages" %}
50 $domain = $this->parser
->getExpressionParser()->parseExpression();
53 if ($stream->test('into')) {
54 // {% trans into "fr" %}
56 $locale = $this->parser
->getExpressionParser()->parseExpression();
57 } elseif (!$stream->test(\Twig_Token
::BLOCK_END_TYPE
)) {
58 throw new \
Twig_Error_Syntax('Unexpected token. Twig was looking for the "with" or "from" keyword.');
62 // {% trans %}message{% endtrans %}
63 $stream->expect(\Twig_Token
::BLOCK_END_TYPE
);
64 $body = $this->parser
->subparse(array($this, 'decideTransFork'), true);
66 if (!$body instanceof \Twig_Node_Text
&& !$body instanceof \Twig_Node_Expression
) {
67 throw new \
Twig_Error_Syntax('A message inside a trans tag must be a simple text');
70 $stream->expect(\Twig_Token
::BLOCK_END_TYPE
);
72 return new TransNode($body, $domain, null, $vars, $locale, $lineno, $this->getTag());
75 public function decideTransFork($token)
77 return $token->test(array('endtrans'));
81 * Gets the tag name associated with this token parser.
83 * @return string The tag name
85 public function getTag()