Novis\Intl\PluralRules::cardinal
Novis\Intl\PluralRules::cardinal(
array<int|float|decimal> $numbers,
string $locale,
): array<Core\Cldr::PluralCategory>Gives the plural category of each number, so you can choose the right form of a word.
PluralRules::cardinal returns one Core\Cldr::PluralCategory for each number, in the same order as the list. The category tells you which form of a word goes with the number, as in "1 file" and "2 files". The locale you pass decides the rules. English has two forms: One and Other. Russian has four: One, Few, Many and Other.
The digits a number shows count. In English, 1 is One, but the decimal value 1.0 is Other, because people write "1.0 files".
PluralRules::cardinal throws a RuntimeError when a float is not finite. It throws a LogicError when the locale tag is not valid.
Good to know: Core\Cldr::pluralCategory gives the same category for one number in the languages it knows. PluralRules::cardinal knows every language and takes a whole list in one call. For places, such as "1st" and "2nd", use PluralRules::ordinal.
Parameters
$numbersarray<int|float|decimal>$localestring
Return value
Returns array<Core\Cldr::PluralCategory>.
Examples
Choose the word form
<?nvs// Shows how to choose between "file" and "files" for each count.use Novis\Intl\PluralRules;
array<int|float|decimal> $counts = [1, 2, 0];array<Core\Cldr::PluralCategory> $categories = PluralRules::cardinal($counts, 'en');
// In English, only `One` takes the singular word.// This prints 1 file, 2 files and 0 files.for (int $i = 0; $i < 3; $i++) { string $word = $categories[$i] == Core\Cldr::PluralCategory::One ? 'file' : 'files'; echo $counts[$i], ' ', $word, "\n";}1 file2 files0 filesRussian has more forms
<?nvs// Shows that Russian needs three forms of a word where English needs two.use Novis\Intl\PluralRules;
array<int|float|decimal> $counts = [1, 3, 5, 21];array<Core\Cldr::PluralCategory> $categories = PluralRules::cardinal($counts, 'ru');
// Each category has its own form of the word for "file".// This prints 1 файл, 3 файла, 5 файлов and 21 файл.for (int $i = 0; $i < 4; $i++) { string $word = match ($categories[$i]) { Core\Cldr::PluralCategory::One => 'файл', Core\Cldr::PluralCategory::Few => 'файла', default => 'файлов', }; echo $counts[$i], ' ', $word, "\n";}1 файл3 файла5 файлов21 файлThe digits count
<?nvs// Shows that the digits a number shows change its category.use Novis\Intl\PluralRules;
array<int|float|decimal> $sizes = [1, '1.0' as decimal];array<Core\Cldr::PluralCategory> $categories = PluralRules::cardinal($sizes, 'en');
// `1` is `One`, but `1.0` is `Other`. This prints 1 megabyte and 1.0 megabytes.for (int $i = 0; $i < 2; $i++) { string $word = $categories[$i] == Core\Cldr::PluralCategory::One ? 'megabyte' : 'megabytes'; echo $sizes[$i], ' ', $word, "\n";}1 megabyte1.0 megabytes