Plural Forms

Plural forms are important because you can pass a number of objects to the gettext() method and return the correct pluralized version. The plural forms can be different for each language. For example, in French, you typically say 0 pomme and not 0 pommes. In English you would say 0 apples and not 0 apple.

When creating your locale files, you can specify an array of values. The first element is the singular form, and the second is the plural form.

1
2
3
4
5
<?php
// from en_CA.php
return [
    'TEXT_PAGE'=>['page', 'pages']
];

Plural forms are defined by the format for zero, 1, and more than one objects. The default looks like this

1
2
3
4
| 0       | 1        | 2+      |
+---------+----------+---------+
| plural  | singular | plural  |
+---------+----------+---------+

So, 0 apples, 1 apple, 4 apples.

You can set the plural form for a given language using the setPluralForm() method. See below.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php

$form = [
    'singular', 'plural', 'plural'
];
$locale->setCode('fr_CA');
$locale->setPluralForm('fr_CA', $form);

echo $locale->gettext('TEXT_APPLE', 0);
// displays pomme

echo $locale->gettext('TEXT_APPLE', 2);
// displays pommes

// 0 pomme, 1 pommes, 2 pommes