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
-----------
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.'(')
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");