Browse Source

[Bridge][Twig] improved behaviour of the transchoice tag (support for variables)

Martin Hason 14 years ago
parent
commit
d39ea6ae79

+ 14 - 5
src/Symfony/Bridge/Twig/TokenParser/TransChoiceTokenParser.php

@@ -14,7 +14,7 @@ namespace Symfony\Bridge\Twig\TokenParser;
 use Symfony\Bridge\Twig\Node\TransNode;
 
 /**
- * 
+ *
  *
  * @author Fabien Potencier <fabien@symfony.com>
  */
@@ -34,10 +34,17 @@ class TransChoiceTokenParser extends TransTokenParser
 
         $vars = new \Twig_Node_Expression_Array(array(), $lineno);
 
+        $body = null;
         $count = $this->parser->getExpressionParser()->parseExpression();
-
         $domain = new \Twig_Node_Expression_Constant('messages', $lineno);
 
+        if (!$stream->test(\Twig_Token::BLOCK_END_TYPE) && $stream->test('for')) {
+            // {% transchoice count for "message" %}
+            // {% transchoice count for message %}
+            $stream->next();
+            $body = $this->parser->getExpressionParser()->parseExpression();
+        }
+
         if ($stream->test('with')) {
             // {% transchoice count with vars %}
             $stream->next();
@@ -50,9 +57,11 @@ class TransChoiceTokenParser extends TransTokenParser
             $domain = $this->parser->getExpressionParser()->parseExpression();
         }
 
-        $stream->expect(\Twig_Token::BLOCK_END_TYPE);
-
-        $body = $this->parser->subparse(array($this, 'decideTransChoiceFork'), true);
+        if (null === $body) {
+            // {% transchoice count %}message{% endtranschoice %}
+            $stream->expect(\Twig_Token::BLOCK_END_TYPE);
+            $body = $this->parser->subparse(array($this, 'decideTransChoiceFork'), true);
+        }
 
         if (!$body instanceof \Twig_Node_Text && !$body instanceof \Twig_Node_Expression) {
             throw new \Twig_Error_Syntax(sprintf('A message must be a simple text (line %s)', $lineno), -1);

+ 4 - 0
tests/Symfony/Tests/Bridge/Twig/Extension/TranslationExtensionTest.php

@@ -72,6 +72,10 @@ class TranslationExtensionTest extends TestCase
                 'There is 5 apples (Symfony2)', array('count' => 5, 'name' => 'Symfony2')),
             array('{% transchoice count with { \'%name%\': \'Symfony2\' } %}{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%){% endtranschoice %}',
                 'There is 5 apples (Symfony2)', array('count' => 5)),
+            array('{% transchoice count for "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples" from "messages" %}',
+                'There is one apple', array('count' => 1)),
+            array('{% set text = "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)" %}{% transchoice count for text with { \'%name%\': \'Symfony2\', \'%count%\': count } %}',
+                'There is 5 apples (Symfony2)', array('count' => 5)),
 
             // trans filter
             array('{{ "Hello"|trans }}', 'Hello'),