Additional features for translation

Plural notations for Translation

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.

Traditional plural 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.

  1. span style="color: #ff0000;">'adapter' => 'gettext',
  2.         'content' => '/path/to/german.mo',
  3.         'locale'  => 'de'
  4.     )
  5. );
  6. $translate->plural('Car', 'Cars', $number);

Modern plural translations

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.

  1. span style="color: #ff0000;">'adapter' => 'gettext',
  2.         'content' => '/path/to/german.mo',
  3.         'locale'  => 'de''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.

  1. span style="color: #ff0000;">'adapter' => 'gettext',
  2.         'content' => '/path/to/german.mo',
  3.         'locale'  => 'de''Car',
  4.         'Cars first plural',
  5.         'Cars second plural',
  6.         $number,
  7.         'ru'
  8.     )
  9. );

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.

Plural source files

Not all source formats support plural forms. Look into this list for details:

Plural support
Adapter Plurals supported
Array yes
Csv yes
Gettext yes
Ini no
Qt no
Tbx no
Tmx no
Xliff no
XmlTm no

Below you can find examples of plural defined source files.

Array source with plural definitions

An array with plural definitions has to look like the following example.

  1. span style="color: #ff0000;">'plural_0''plural_0 (ru)',
  2.         'plural_1 (ru)',
  3.         'plural_2 (ru)',
  4.         'plural_3 (ru)'
  5.     ),
  6.     'plural_1' => ''
  7. );

In the above example 'plural_0' and 'plural_1' are the plural definitions from the source code. And the array at 'plural_0' has all translated plural forms available. Take a look at the following example with real content and translation from English source to German.

  1. span style="color: #ff0000;">'Car''Auto',
  2.         'Autos'
  3.     ),
  4.     'Cars' => ''
  5. );

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.

Csv source with plural definitions

A csv file with plural definitions has to look like the following example.

  1. "plural_0";"plural_0 (ru)";"plural_1 (ru)";"plural_2 (ru)";"plural_3 (ru)"
  2. "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 source with plural definitions

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.

Custom plural rules

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:

  1.  

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:

  1. span style="color: #ff0000;">'MyPlural', 'zh');

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.


Additional features for translation