From: Fabien Potencier Date: Fri, 19 Mar 2010 08:12:26 +0000 (+0100) Subject: fixed the plural with a better patch than the previous one (closes #27) X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=6a8e2d5d24e5dfd003c728e282f019bd43ed7c35;p=konrad%2Ftwig.git fixed the plural with a better patch than the previous one (closes #27) --- diff --git a/doc/02-Twig-for-Template-Designers.markdown b/doc/02-Twig-for-Template-Designers.markdown index 60625c9..d8f76b1 100644 --- a/doc/02-Twig-for-Template-Designers.markdown +++ b/doc/02-Twig-for-Template-Designers.markdown @@ -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 ----------- diff --git a/lib/Twig/Node/Trans.php b/lib/Twig/Node/Trans.php index efb1edb..2ebc2d0 100644 --- a/lib/Twig/Node/Trans.php +++ b/lib/Twig/Node/Trans.php @@ -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");