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
