|
Size: | 14348 |
Storage flags: | no_autoload,compress/gzip (28%) |
Most languages have specific rules for handling plurals. For instance, in English, we say “0 cars” and “2 cars” (plural) while we say “1 car” (singular). On the other hand, French uses the singular form for 0 and 1 (“0 voiture” and “1 voiture”) and uses the plural form otherwise (“3 voitures”).
Therefore, we often need to handle those plural cases even without using translation (mono-lingual application). The Plural helper was created for this. Please remember that, if you need to both handle translation and plural, you must use the TranslatePlural helper for that. Plural does not deal with translation.
Internally, the Plural helper uses the Zend\I18n\Translator\Plural\Rule class to handle rules.
In Zend Framework 1, there was a similar helper. However, this helper hardcoded rules for mostly every languages. The problem with this approach is that languages are alive and can evolve over time. Therefore, we would need to change the rules and hence break current applications that may (or may not) want those new rules.
That’s why defining rules is now up to the developer. To help you with this process, here are some links with up-to-date plural rules for tons of languages:
The first thing to do is to defining rule. You may want to add this in your Module.php file, for example:
1 2 3 4 5 6 7 | // Get the ViewHelperPlugin Manager from Service manager, so we can fetch the ``Plural``
// helper and add the plural rule for the application's language
$viewHelperManager = $serviceManager->get('ViewHelperManager');
$pluralHelper = $viewHelperManager->get('Plural');
// Here is the rule for French
$pluralHelper->setPluralRule('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)');
|
The string reads like that:
As we said earlier, English consider “1” as singular, and “0/other” as plural. Here is such a rule:
1 2 | // Here is the rule for English
$pluralHelper->setPluralRule('nplurals=2; plural=(n==1 ? 0 : 1)');
|
Now that we have defined the rule, we can use it in our views:
1 2 3 4 5 6 7 8 9 10 11 | <?php
// If the rule defined in Module.php is the English one:
echo $this->plural(array('car', 'cars'), 0); // prints "cars"
echo $this->plural(array('car', 'cars'), 1); // prints "car"
// If the rule defined in Module.php is the French one:
echo $this->plural(array('voiture', 'voitures'), 0); // prints "voiture"
echo $this->plural(array('voiture', 'voitures'), 1); // prints "voiture"
echo $this->plural(array('voiture', 'voitures'), 2); // prints "voitures"
?>
|
The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.