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

[Bridge][Twig] added transchoice filter

Martin Hason 14 éve
szülő
commit
d300b94745

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

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

@@ -78,6 +78,10 @@ class TranslationExtensionTest extends TestCase
             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%)')),
         );
     }