fixed the plural with a better patch than the previous one (closes #27)
authorFabien Potencier <fabien.potencier@gmail.com>
Fri, 19 Mar 2010 08:12:26 +0000 (09:12 +0100)
committerFabien Potencier <fabien.potencier@gmail.com>
Fri, 19 Mar 2010 08:12:26 +0000 (09:12 +0100)
doc/02-Twig-for-Template-Designers.markdown
lib/Twig/Node/Trans.php

index 60625c9..d8f76b1 100644 (file)
@@ -736,14 +736,15 @@ result to a variable:
 To pluralize a translatable string, use the `plural` block:
 
     [twig]
-    {% trans count %}
-    I have one apple.
+    {% trans apple_count %}
+    Hey {{ name }}, I have one apple.
     {% plural %}
-    I have {{ count }} apples.
+    Hey {{ name }}, I have {{ count }} apples.
     {% endtrans %}
 
-The `trans` block uses the `count` argument to select the right string. The
-`count` variable is available in the translatable string.
+The `trans` block first argument is the `count` used to select the right
+string. Within the translatable string, the special `count` variable always
+contain the count value (here the value of `apple_count`).
 
 Expressions
 -----------
index efb1edb..2ebc2d0 100644 (file)
@@ -43,11 +43,13 @@ class Twig_Node_Trans extends Twig_Node
     if (false !== $this->plural)
     {
       list($msg1, $vars1) = $this->compileString($this->plural);
+
+      $vars = array_merge($vars, $vars1);
     }
 
     $function = false === $this->plural ? 'gettext' : 'ngettext';
 
-    if ($vars || false !== $this->plural)
+    if ($vars)
     {
       $compiler
         ->write('echo strtr('.$function.'(')
@@ -69,22 +71,24 @@ class Twig_Node_Trans extends Twig_Node
 
       foreach ($vars as $var)
       {
-        $compiler
-          ->string('%'.$var->getName().'%')
-          ->raw(' => ')
-          ->subcompile($var)
-          ->raw(', ')
-        ;
-      }
-
-      if (false !== $this->plural)
-      {
-        $compiler
-          ->string('%count%')
-          ->raw(' => abs(')
-          ->subcompile($this->count)
-          ->raw('), ')
-        ;
+        if ('count' === $var->getName())
+        {
+          $compiler
+            ->string('%count%')
+            ->raw(' => abs(')
+            ->subcompile($this->count)
+            ->raw('), ')
+          ;
+        }
+        else
+        {
+          $compiler
+            ->string('%'.$var->getName().'%')
+            ->raw(' => ')
+            ->subcompile($var)
+            ->raw(', ')
+          ;
+        }
       }
 
       $compiler->raw("));\n");