Forráskód Böngészése

Merge remote branch 'hason/twigtrans'

* hason/twigtrans:
  [Bridge][Twig] improved behaviour of the transchoice tag (support for variables)
  [Bridge][Twig] added transchoice filter
Fabien Potencier 14 éve
szülő
commit
9c80e7bcc4

+ 6 - 0
src/Symfony/Bridge/Twig/Extension/TranslationExtension.php

@@ -41,6 +41,7 @@ class TranslationExtension extends \Twig_Extension
     {
         return array(
             'trans' => new \Twig_Filter_Method($this, 'trans'),
+            'transchoice' => new \Twig_Filter_Method($this, 'transchoice'),
         );
     }
 
@@ -67,6 +68,11 @@ class TranslationExtension extends \Twig_Extension
         return $this->translator->trans($message, $arguments, $domain);
     }
 
+    public function transchoice($message, $count, array $arguments = array(), $domain = "messages")
+    {
+        return $this->translator->transChoice($message, $count, $arguments, $domain);
+    }
+
     /**
      * Returns the name of the extension.
      *

+ 2 - 3
src/Symfony/Bridge/Twig/Node/TransNode.php

@@ -12,13 +12,13 @@
 namespace Symfony\Bridge\Twig\Node;
 
 /**
- * 
+ *
  *
  * @author Fabien Potencier <fabien@symfony.com>
  */
 class TransNode extends \Twig_Node
 {
-    public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $lineno, $tag = null)
+    public function __construct(\Twig_NodeInterface $body, \Twig_NodeInterface $domain, \Twig_Node_Expression $count = null, \Twig_Node_Expression $vars = null, $lineno = 0, $tag = null)
     {
         parent::__construct(array('count' => $count, 'body' => $body, 'domain' => $domain, 'vars' => $vars), array(), $lineno, $tag);
     }
@@ -38,7 +38,6 @@ class TransNode extends \Twig_Node
             $defaults = $this->getNode('vars');
             $vars = null;
         }
-
         list($msg, $defaults) = $this->compileString($this->getNode('body'), $defaults);
 
         $method = null === $this->getNode('count') ? 'trans' : 'transChoice';

+ 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);

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

@@ -72,12 +72,20 @@ 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'),
             array('{{ name|trans }}', 'Symfony2', array('name' => 'Symfony2')),
             array('{{ hello|trans({ \'%name%\': \'Symfony2\' }) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
             array('{% set vars = { \'%name%\': \'Symfony2\' } %}{{ hello|trans(vars) }}', 'Hello Symfony2', array('hello' => 'Hello %name%')),
+
+            // transchoice filter
+            array('{{ "{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples"|transchoice(count, {\'%count%\': count}) }}', 'There is 5 apples', array('count' => 5)),
+            array('{{ text|transchoice(5, {\'%count%\': 5, \'%name%\': \'Symfony2\'}) }}', 'There is 5 apples (Symfony2)', array('text' => '{0} There is no apples|{1} There is one apple|]1,Inf] There is %count% apples (%name%)')),
         );
     }