Pluralization
- Posted by thomasw at 21:35:27 // //
- Translate
I have great news for you…
As with release 1.9 Zend Framework supports pluralization.
Yes you heard correct… pluralization.
I added this amazing feature to Zend_Translate at 1.May and today it was accepted for core.
Support for plural forms depends on the used adapter. Therefor it is actually only available for
Array, Csv and Gettext. Until now I found no way to support it for other formats. When you know details for a unsupported format feel free to give them to me.
So how does this work…
First off, there is no change to existing applications and code.
When you take a look at plural support within php’s gettext extension you will see that you must use another method for it.
That’s not the case for Zend_Translate. You can use the existing translate() method and get a pluralized translation. Of course, when you like it the old fashion way you can still use a own method. :-)
Let’s go into code:
You can eighter do:
$translate->plural('Car', 'Cars', $number);
when you want to use a own method. Or you can use the translate() method like described here:
$translate->translate(array('Car', 'Cars', $number));
So when you want to use plural translation simply give an array with the original plural forms as messageid.
Easy, isn’t it ?
But Zend_Translate supports another feature with pluralization..
Actually pluralization is limited to languages which use english forms.
Look at php’s ngettext() method. It needs to have one plural and one singular form to be able to translate. But several languages have more than 1 plural forms. This means on the other side that ngettext can not be used with languages like russian or indian which have more than 2 plural forms.
With Zend_Translate your source code can be ANY language… you can do things like
$translate->translate(array('Auto', 'Autos', 'Auti', 'Autom', $number));
Not that I know the proper plural forms, but you get a feeling for what I meant.
For more details about how plural translations work take a look at the manual. I added a own chapter for it and hope you find it usefull.
As always I will keep you informed about my work.
Greetings
Thomas Weidner
I18N Team Leader, Zend Framework
Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework
Add comment
Fill out the form below to add your own comments

Monday, July 20, 2009 - 23:32:24
Regardless syntax errors in your examples, a great feature! :)
Monday, July 20, 2009 - 23:47:19
$translate->translate(array(’Auto’, ‘Autos’, ‘Auti’, ‘Autom’, $number); When is the result Auto, when Auti and when Autom?
Tuesday, July 21, 2009 - 00:54:59
Fantastic !
I’d love this feature for TMX adapter: http://www.lisa.org/Translation-Memory-e.34.0.html
Thank you very much and keep up the good work.
Tuesday, July 21, 2009 - 01:10:46
Which plural version is used for source and target language is dependend on the given number…
Example: Russia defined 1, 2, 5, 11… so when 5 is given we know that the 3rd plural form has to be used… So it searches for the 3rd translated plural form… when no translation is available it uses the 3rd source plural form, which you gave in your code.
Simple as is
Tuesday, July 21, 2009 - 01:13:35
I searched long for TMX beginning this year… in fact I just saw the notice that it “should” be supported but there was no example or definition how it “is” supported.
Even in several forums I found nothing mentioned for plural support within TMX.
The mentioned link is just the “should” which I also found but it is without any details.
Tuesday, July 21, 2009 - 01:36:37
Hi Tom,
thi si really great fueature for central europan languages, e.g. in Slovak or Czech, have singular and plural 6 forms.
Singular:
$translate->plural(’Auto’, ‘Auta’, ‘Autu’, ‘Auto’, ‘Aute’, ‘Autom’ $number);
Plural:
$translate->plural(’Autá’, ‘Áut’, ‘Autám’, ‘Autá’, ‘Autách’, ‘Autami’ $number);
There are 8 forms, but on form is veery similar to the another, so we are using only 7 forms.
- Nominatív
- Genitív
- Datív
- Akuzatív
- Lokál
- Inštrumentál
- (Vokatív) - this form is that 8
This is really very very imazing feature, which isn’t in any others FW (if im’ not mistake)
+100 score ;) ;) :)
I’m very happy, I like ZF i18n
Thank you
Tuesday, July 21, 2009 - 01:38:18
sry, not 6 but 7 forms and 8 we aren’t using.
Tuesday, July 21, 2009 - 01:45:01
Thx, it’s pretty nice feature, but can you provide a link to documentation?
Tuesday, July 21, 2009 - 02:28:52
@sNop: Note: Multi-Plural source support depends of course also on the used format. Actually the gettext format does NOT support it as gettext standard is 100% fixed to english source files. You can check this by looking into poEdit.
But Array and Csv support this feature out of the box.
@Alex: As said, the documentation can be found within trunk. When you don’t have access to SVN trunk you need to wait until 1.9 is released.
Tuesday, July 21, 2009 - 02:32:15
I can not say for other translation source formats but in my eyes any language which supports the usage of non-english source code should also support non-english plural forms.
PHP 6, which aims to support unicode written source code is one example in my eyes.
Tuesday, July 21, 2009 - 22:06:23
Is translate view helper yet updated to use plurals?
Thursday, July 23, 2009 - 20:06:21
Great feature, thank you!
I have a question though, I’m using Poedit tu generate translation files from source. Now, using this notation:
$translate->translate(array(’Car’, ‘Cars’, $number));
I can’t get Poedit to find it, and using this notation:
$translate->plural(’Car’, ‘Cars’, $number);
it finds only the first string, leaving me to manually enter plural versions into .po file. Am I missing something or is it the way it’s supposed to be?
Regards,
Goran Dodig
Thursday, July 23, 2009 - 21:00:55
@marko: Simply try… I don’t see why it should not be able to use arrays as parameter. The new plural method is not supported as the time between acceptance and the new release was too short.
@goran: Simply configure your poEdit to find the proper notation you are using. But I can’t help with poEdit as it’s no ZF code ;-) A g***g* search should help.
Saturday, February 13, 2010 - 20:21:48
thank you for tutorial…