Caution: The documentation you are viewing is
for an older version of Zend Framework.
You can find the documentation of the current version at docs.zendframework.com
Plural notations for Translation - Zend_Translate
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 imagine 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 ways 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 fourth parameter can be used to give a locale which will be used to return the translation.
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.
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.
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.
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.
When your translated language supports more plural forms then simply add them to the array below the first plural form. When your source language supports more plural forms, than simply add a new empty translation.
A csv file with plural definitions has to look like the following example.
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: 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.
In rare cases it could be useful to be able to define own plural rules. See Chinese for example. This language defines two plural rules. Per default it does not use plurals. But in rare cases it uses a rule like (number == 1) ? 0 : 1.
Also when you want to use a language which has no known plural rules, and would want to define your own rules.
This can be done by using Zend_Translate_Plural::setRule(). The method expects two parameters which must be given. A rule, which is simply a callback to a self defined method. And a locale for which the rule will be used.
Your rule could look like this:
As you see, your rule must accept one parameter. It is the number which you will use to return which plural the translation has to use. In our example we defined that when we get a '10' the plural definition 0 has to be used, in all other cases we're using 1.
Your rules can be as simple or as complicated as you want. You must only return an integer value. The plural definition 0 stands for singular translation, and 1 stands for the first plural rule.
To activate your rule, and to link it to the wished locale, you have to call it like this:
Now we linked our plural definition to the Chinese language.
You can define one plural rule for every language. But you should be aware that you set the plural rules before you are doing translations.
Note: Define custom plurals only when needed
Zend_Translate defines plurals for most known languages. You should not define own plurals when you are not in need. The default rules work most of time.