Core\Cldr::PluralCategory
Names the form a language uses for a given count, so one message can be written correctly in every language.
Core\Cldr::PluralCategory is declared in the class Core\Cldr. The methods pluralCategory and ordinalCategory return it.
English has two forms: "1 file" and "3 files". Russian has four, and picks between them by rules that put 21 with 1 and 11 with 5. Welsh uses six. Core\Cldr::pluralCategory answers which form a count selects in a language, as one of Zero, One, Two, Few, Many and Other, and your message catalog keeps one line per form.
In plain words: these names are labels for a language's forms, not amounts. Many does not mean "a lot", and One is whichever counts that language treats the way English treats 1 — in Russian that includes 21 and 101.
Good to know: every language has Other, and a language that makes no plural distinction at all uses only that one.
Examples
The right word for a count
<?nvs// A sentence that mentions a count needs the right word for that count. Ask// which form the language uses, then write the sentence for that form.
class Inbox{ public static function word(int $count): string { return match (Core\Cldr::pluralCategory($count, 'en')) { Core\Cldr::PluralCategory::One => 'message', default => 'messages', }; }}
array<int> $counts = [0, 1, 2, 5];foreach ($counts as int $count) { echo 'you have ', $count, ' ', Inbox::word($count), "\n";}you have 0 messagesyou have 1 messageyou have 2 messagesyou have 5 messagesA language with four forms
<?nvs// Russian has four forms, and which one a count takes is not what an English// speaker would guess: 21 goes with 1, and 11 goes with 5. A test on the number// itself cannot get this right, so ask which form the count selects.
class Ru{ public static function word(int $count): string { return match (Core\Cldr::pluralCategory($count, 'ru')) { Core\Cldr::PluralCategory::One => 'файл', Core\Cldr::PluralCategory::Few => 'файла', Core\Cldr::PluralCategory::Many => 'файлов', default => 'файла', }; }}
array<int> $counts = [1, 2, 5, 11, 21, 101];foreach ($counts as int $count) { echo $count, ' ', Ru::word($count), "\n";}1 файл2 файла5 файлов11 файлов21 файл101 файлA message catalog with one line per form
<?nvs// A translated application keeps one line per form for each message. This is// the lookup that picks the line, and it is the same code for every language// you add later.
class Catalog{ public static function pick(array<string> $forms, int $count, string $language): string { string $key = match (Core\Cldr::pluralCategory($count, $language)) { Core\Cldr::PluralCategory::Zero => 'zero', Core\Cldr::PluralCategory::One => 'one', Core\Cldr::PluralCategory::Two => 'two', Core\Cldr::PluralCategory::Few => 'few', Core\Cldr::PluralCategory::Many => 'many', Core\Cldr::PluralCategory::Other => 'other', };
// A translator only writes the forms their language uses, so fall back // to the one every language has. return $forms[$key] ?? $forms['other']; }}
array<string> $english = ['one' => 'day', 'other' => 'days'];array<string> $polish = ['one' => 'dzień', 'few' => 'dni', 'many' => 'dni', 'other' => 'dnia'];
array<int> $counts = [1, 2, 5, 22];foreach ($counts as int $count) { echo $count, ': ', Catalog::pick($english, $count, 'en'), ' / ', Catalog::pick($polish, $count, 'pl'), "\n";}1: day / dzień2: days / dni5: days / dni22: days / dni