Core\Cldr::pluralCategory
Tells you which form of a word a number needs in a language, so a translated message reads correctly.
English has two forms: "1 message" and "2 messages". Other languages have more. Polish uses three forms, Welsh uses six, and Japanese uses one. pluralCategory returns the form the language uses for the count you give it: One, Two, Few, Many, Zero or Other. These are names for the forms of a language, not amounts, so Many does not mean a large number.
The second argument is a language tag such as "en", "de" or "pt-BR". Only the language part is read, and upper and lower case are the same. A language Novis carries no rules for throws a LogicError, because a message shown with another language's forms is wrong.
Good to know: 1 and 1.0 can select different forms. In English 1 is One and 1.0 is Other, because the rules read the digits a reader sees.
The examples below show one English message, then the three forms Polish uses, then a message catalog that picks its line by form.
Parameters
$countint|float|decimalThe number the message is about. Adecimalkeeps its digits after the point. So1.0and1can give different forms in a language whose rules read the fraction. Aninthas no fraction. Afloatis read with the digits it prints.$localestringA BCP 47 tag. Only the language part is read, and case is ignored. Soen-GB,en_USandENall give the result foren.
Return value
Returns Core\Cldr::PluralCategory. The plural category of $count in this language. It is Other for every count in a language that has no plural forms.
Errors
LogicErrorThe tag has no language part, or Novis has no rules for its language. Novis never uses the rules of another language instead.RuntimeError$countis afloatthat is not finite. It is also thrown for afloatthat prints more digits than the rules can check.
Examples
One message in english
<?nvs// Shows how to pick the right wording for a count in English.
array<int> $counts = [0, 1, 2];foreach ($counts as int $count) { // `Core\Cldr::pluralCategory` returns the form English uses for this // count. English uses `One` for 1 and `Other` for every other number. if (Core\Cldr::pluralCategory($count, 'en') == Core\Cldr::PluralCategory::One) { echo 'You have ', $count, " new message.\n"; } else { echo 'You have ', $count, " new messages.\n"; }}You have 0 new messages.You have 1 new message.You have 2 new messages.The three forms polish uses
<?nvs// Shows a language that has more than two forms. Polish has three words for// "file", and the count decides which one you need.
class Polish{ public static function files(int $count): string { return match (Core\Cldr::pluralCategory($count, 'pl')) { Core\Cldr::PluralCategory::One => 'plik', Core\Cldr::PluralCategory::Few => 'pliki', Core\Cldr::PluralCategory::Many => 'plików', // Polish has one more form, for counts that show a fraction. default => 'pliku', }; }}
// This prints 1 plik, 2 pliki, 5 plików and 22 pliki. A test for `== 1` gets// the last three wrong.array<int> $counts = [1, 2, 5, 22];foreach ($counts as int $count) { echo $count, ' ', Polish::files($count), "\n";}1 plik2 pliki5 plików22 plikiA message catalog
<?nvs// Shows how a message catalog picks its line for a count. A translated// application does this for every message that names a number.
class Catalog{ // The six form names. A catalog keys its lines on these. public static function form(int $count, string $locale): string { return match (Core\Cldr::pluralCategory($count, $locale)) { 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', }; }
public static function line(array<string> $lines, int $count, string $locale): string { string $key = Catalog::form($count, $locale); // A catalog does not always write a line for every form. Every // language has `other`, so use that line when the form is missing. if (!Core\Arr::hasKey($lines, $key)) { $key = 'other'; } return Core\Str::replace($lines[$key], '{n}', $count as string); }}
// Russian has three forms for "file".array<string> $russian = [ 'one' => '{n} файл', 'few' => '{n} файла', 'many' => '{n} файлов', 'other' => '{n} файла',];
// 21 takes the same form as 1 in Russian, so the last line is "21 файл".array<int> $counts = [1, 2, 5, 21];foreach ($counts as int $count) { echo Catalog::line($russian, $count, 'ru'), "\n";}1 файл2 файла5 файлов21 файл