In zf2
you have the view helper translatePlural
. Which can be use as followed;
echo $this->translatePlural('car', 'cars', $num);
So when $num == 1
it says car
else cars
(depending on the plural rule but for now use the english rule).
To translate this, by file. You have to use an array like;
'car' => [
'auto',
'autos',
],
So now you get auto
if $num == 1
else autos
.
If you have some where else in your code to translate car
you could do translate('car')
but that will return to a notice array to string coverstion
, which is obvious not what we want.
So to be able to also translate just car
and use the plural I now have to modify the code to something like;
echo $this->translatePlural('car_plural', 'cars', $num);
And the language config;
'car_plural' => [
'auto',
'autos',
],
'car' => 'auto',
And you need to have to add an english translation for car_plural
. So the translatePlural
default messages become prety much useless in a default setup, unless you use translatePlural
not for translation but for just to choose between car
or cars
and not translating it.
But if instead the translatePlural
just looked at $num
then use the pluralRule
to choose between singular (car) or plural (cars). Then use the translate
helper to translate the singular or plural, you can have an easy config like and the default message all of sudden work again;
'car' => 'auto',
'cars' => 'autos',
No weird keys, you don't need english translation's or something else weird.
So the question /tldr;
Why does ZF2 plural uses an array and not just check which one to use and translate that one? Seems to me a way better method.
I, too, was baffled by this. After some assistance from a coworker and a lot of research, I found that there was a sound architectural reason for this. (I know! I'm surprised, too!)
It turns out that some languages have a different definition of singular vs plural. Some only have 1 form that serves as both singular and plural, and others have 3 or more plural forms.
The translatePlural method can't satisfy these other language forms, so instead it references the Gettext way of handling this. In a gettext file, plural translations for English are handled like this:
msgid "Found %d Result"
msgid_plural "Found %d Results"
msgstr[0] "Found %d Result"
msgstr[1] "Found %d Results"
So the singular/plural values are used to look up the array by the msgid, and then the language's plural index is used to choose either msgstr[0] or msgstr[1].
In a language that has 3 plural forms, the localization would look like this:
msgid "Found %d Result"
msgid_plural "Found %d Results"
msgstr[0] "singular translation"
msgstr[1] "plural translation"
msgstr[2] "super plural translation"
So this is why Zend uses an array, and why just flipping between $singular and $plural is insufficient. (I posted a version of this answer on my blog, but I don't know if it's kosher to link blogs in answers)