4 * This file is part of Twig.
6 * (c) 2010 Fabien Potencier
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
11 class Twig_Extensions_TokenParser_Trans
extends Twig_TokenParser
14 * Parses a token and returns a node.
16 * @param Twig_Token $token A Twig_Token instance
18 * @return Twig_NodeInterface A Twig_NodeInterface instance
20 public function parse(Twig_Token
$token)
22 $lineno = $token->getLine();
23 $stream = $this->parser
->getStream();
27 if (!$stream->test(Twig_Token
::BLOCK_END_TYPE
)) {
28 $body = $this->parser
->getExpressionParser()->parseExpression();
30 $stream->expect(Twig_Token
::BLOCK_END_TYPE
);
31 $body = $this->parser
->subparse(array($this, 'decideForFork'));
32 if ('plural' === $stream->next()->getValue()) {
33 $count = $this->parser
->getExpressionParser()->parseExpression();
34 $stream->expect(Twig_Token
::BLOCK_END_TYPE
);
35 $plural = $this->parser
->subparse(array($this, 'decideForEnd'), true);
39 $stream->expect(Twig_Token
::BLOCK_END_TYPE
);
41 $this->checkTransString($body, $lineno);
43 return new Twig_Extensions_Node_Trans($body, $plural, $count, $lineno, $this->getTag());
46 public function decideForFork(Twig_Token
$token)
48 return $token->test(array('plural', 'endtrans'));
51 public function decideForEnd(Twig_Token
$token)
53 return $token->test('endtrans');
57 * Gets the tag name associated with this token parser.
59 * @param string The tag name
61 public function getTag()
66 protected function checkTransString(Twig_NodeInterface
$body, $lineno)
68 foreach ($body as $i => $node) {
70 $node instanceof Twig_Node_Text
72 ($node instanceof Twig_Node_Print
&& $node->getNode('expr') instanceof Twig_Node_Expression_Name
)
77 throw new Twig_Error_Syntax(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno);