diff --git a/src/Symfony/Bundle/TwigBundle/Node/TransNode.php b/src/Symfony/Bundle/TwigBundle/Node/TransNode.php index a9a8d2bddf5d..da43ea8c35e1 100644 --- a/src/Symfony/Bundle/TwigBundle/Node/TransNode.php +++ b/src/Symfony/Bundle/TwigBundle/Node/TransNode.php @@ -18,9 +18,9 @@ */ class TransNode extends \Twig_Node { - public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, $lineno, $tag = null) + public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $lineno, $tag = null) { - parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain), array(), $lineno, $tag); + parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $lineno, $tag); } /** @@ -32,7 +32,12 @@ public function compile($compiler) { $compiler->addDebugInfo($this); - list($msg, $vars) = $this->compileString($this->body); + if ($this->isSimpleString($this->body)) { + list($msg, $vars) = $this->compileString($this->body); + } else { + $msg = $this->body; + $vars = $this->vars; + } $method = null === $this->count ? 'trans' : 'transChoice'; @@ -52,13 +57,19 @@ public function compile($compiler) $compiler->raw('array('); - foreach ($vars as $var) { - $compiler - ->string('{{ '.$var['name'].' }}') - ->raw(' => ') - ->subcompile($var) - ->raw(', ') - ; + if (is_array($vars)) { + foreach ($vars as $var) { + $compiler + ->string('{{ '.$var['name'].' }}') + ->raw(' => ') + ->subcompile($var) + ->raw(', ') + ; + } + } elseif (null !== $vars) { + $compiler->subcompile($vars); + } else { + $compiler->raw('array()'); } $compiler @@ -91,4 +102,21 @@ protected function compileString(\Twig_NodeInterface $body) return array(new \Twig_Node(array(new \Twig_Node_Expression_Constant(trim($msg), $node->getLine()))), $vars); } + + protected function isSimpleString(\Twig_NodeInterface $body) + { + foreach ($body as $i => $node) { + if ( + $node instanceof \Twig_Node_Text + || + ($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name) + ) { + continue; + } + + return false; + } + + return true; + } } diff --git a/src/Symfony/Bundle/TwigBundle/TokenParser/TransChoiceTokenParser.php b/src/Symfony/Bundle/TwigBundle/TokenParser/TransChoiceTokenParser.php index c740432f1a5a..3fc1f83d9efb 100644 --- a/src/Symfony/Bundle/TwigBundle/TokenParser/TransChoiceTokenParser.php +++ b/src/Symfony/Bundle/TwigBundle/TokenParser/TransChoiceTokenParser.php @@ -32,10 +32,20 @@ public function parse(\Twig_Token $token) $lineno = $token->getLine(); $stream = $this->parser->getStream(); + $vars = null; + $count = $this->parser->getExpressionParser()->parseExpression(); $domain = new \Twig_Node_Expression_Constant('messages', $lineno); - if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test('from')) { + + if ($stream->test('with')) { + // {% transchoice count with vars %} + $stream->next(); + $vars = $this->parser->getExpressionParser()->parseExpression(); + } + + if ($stream->test('from')) { + // {% transchoice count from "messages" %} $stream->next(); $domain = $this->parser->getExpressionParser()->parseExpression(); } @@ -46,9 +56,7 @@ public function parse(\Twig_Token $token) $stream->expect(\Twig_Token::BLOCK_END_TYPE); - $this->checkTransString($body, $lineno); - - return new TransNode($body, $domain, $count, $lineno, $this->getTag()); + return new TransNode($body, $domain, $count, $vars, $lineno, $this->getTag()); } public function decideTransChoiceFork($token) diff --git a/src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php b/src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php index ce7f17e53034..d32176133cea 100644 --- a/src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php +++ b/src/Symfony/Bundle/TwigBundle/TokenParser/TransTokenParser.php @@ -32,14 +32,22 @@ public function parse(\Twig_Token $token) $lineno = $token->getLine(); $stream = $this->parser->getStream(); + $vars = null; $domain = new \Twig_Node_Expression_Constant('messages', $lineno); if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { $body = null; if (!$stream->test('from')) { // {% trans "message" %} + // {% trans message %} $body = $this->parser->getExpressionParser()->parseExpression(); } + if ($stream->test('with')) { + // {% trans "message" with vars %} + $stream->next(); + $vars = $this->parser->getExpressionParser()->parseExpression(); + } + if ($stream->test('from')) { // {% trans "message" from "messages" %} $stream->next(); @@ -61,9 +69,7 @@ public function parse(\Twig_Token $token) $stream->expect(\Twig_Token::BLOCK_END_TYPE); - $this->checkTransString($body, $lineno); - - return new TransNode($body, $domain, null, $lineno, $this->getTag()); + return new TransNode($body, $domain, null, $vars, $lineno, $this->getTag()); } public function decideTransFork($token) @@ -80,19 +86,4 @@ public function getTag() { return 'trans'; } - - protected function checkTransString(\Twig_NodeInterface $body, $lineno) - { - foreach ($body as $i => $node) { - if ( - $node instanceof \Twig_Node_Text - || - ($node instanceof \Twig_Node_Print && $node->expr instanceof \Twig_Node_Expression_Name) - ) { - continue; - } - - throw new \Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variables'), $lineno); - } - } }