As of Zend Framework 1.9,
Zend_Translate is able to provide plural
support. Professional translation will always have the need to use plurals as they are
native in almost all languages.
So what are plurals? Generally spoken plurals are words which take into account numeric meanings. But as you may imaging each language has it's own definition of plurals. English, for example, supports one plural. We have a singular definition, for example "car", which means implicit one car, and we have the plural definition, "cars" which could mean more than one car but also zero cars. Other languages like russian or polish have more plurals and also the rules for plurals are different.
When you want to use plurals with
Zend_Translate you must not need
to know how the plurals are defined, only the translator must know as he does the
translation. The only information you need to have is the language.
There are two way for using plurals... the traditional one, which means that you use a own method, and a modern one, which allows you to do plural translations with the same method as normal translations.
People who worked with gettext in past will be more common with traditional plural translations. There is a own plural() method which can be used for plural translations.
Example #1 Example of traditional plural translations
The plural() method accepts 4 parameters. The first parameter is the singular messageId, the second is the plural messageId and the third is the number or amount.
The number will be used to detect the plural which has to be returned. A optional forth parameter can be used to give a locale which will be used to return the translation.
$translate = new Zend_Translate('gettext', '/path/to/german.mo', 'de'); $translate->plural('Car', 'Cars', $number);
As traditional plural translations are restricted to source code using english plurals we added a new way for plural translations. It allows to use the same translate() for standard and for plural translations.
To use plural translations with translate() you need to give an array as messageId instead of an string. This array must have the original plural messageId's, then the amount and at last an optional locale when your given messageId's are not in english notation.
Example #2 Example of modern plural translations
When we want to translate the same plural definitions like in the previous our example would have to be defined like below.
$translate = new Zend_Translate('gettext', '/path/to/german.mo', 'de'); $translate->translate(array('Car', 'Cars', $number));
Using modern plural translations it is also possible to use any language as source for messageId's.
Example #3 Example of modern plural translations using a different source language
Let's expect we want to use russian and let's also expect that the given messageId's are russian and not english.
$translate = new Zend_Translate('gettext', '/path/to/german.mo', 'de'); $translate->translate(array('Car', 'Cars first plural', 'Cars second plural', $number, 'ru'));
As you can see you can give more than just the one english plural. But you must give
the source language in this case so
Zend_Translate knows which
plural rules it has to apply.
When you omit the plural language then english will be used per default and any additional plural definition will be ignored.
Not all source formats support plural forms. Look into this list for details:
Below you can find examples of plural defined source files.
An array with plural definitions has to look like the following example.
array( 'plural_0' => array( 'plural_0 (ru)', 'plural_1 (ru)', 'plural_2 (ru)', 'plural_3 (ru)' ), 'plural_1' => '' );
In the above example
plural_1 are the
plural definitions from the source code. And the array at
has all translated plural forms available. Take a look at the following example
with real content and translation from english source to german.
array( 'Car' => array( 'Auto', 'Autos' ), 'Cars' => '' );
When your translated language supports more plural forms then simply add them to the array below the first plural form. When your source language suppors more plural forms, than simply add a new empty translation.
A csv file with plural definitions has to look like the following example.
"plural_0";"plural_0 (ru)";"plural_1 (ru)";"plural_2 (ru)";"plural_3 (ru)" "plural_1";
All translated plural forms have to be added after the first plural of the source language. And all further plural forms of the source language have to be added below but without translation. Note that you must add a delimiter to empty source plurals.
Gettext sources support plural forms out of the box. There is no need for adoption as the *.mo file will contain all necessary data.
Note that gettext does not support the usage of source languages which are not using english plural forms. When you plan to use a source language which supports other plural forms like russian for example, then you can not use gettext sources.