PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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:

<?php
public function MyRule($number) {
    return (
$number == 10) ? 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:

<?php
Zend_Translate_Plural
::setPlural('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.

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.

Zend Framework