Skip to content

Pluralization

Michael Spiss edited this page Aug 17, 2017 · 1 revision

To enable pluralization for a string simply add a pipe-character between singular and plural form in your translation file:

message.php

<?php

return [
    'simple_pluralization' => 'Dog | Dogs'
];

The second option is a bit more advanced and actually allows you to set your own rules:

message.php

<?php

return [
    'advanced_pluralization' => '{0} None | [1,10] Some | [11,*] Many'
];

You can use the following expressions:

Expression Meaning
{3} match exactly 3
[1,10] match everything from 1 to 10, including 1 and 10
[11,*] match everything from 11 to infinite including 11
]10,20] match everything from 10 to 20, excluding 10, including 20
]10,20[ match everything from 10 to 20, excluding 10 and 20
[10,20[ match everything from 10 to 20, including 10, excluding 20

Expressions also support integers and floats!

To get the string use the transChoice() method, which takes an additional number argument (type int or float):

$translator->transChoice('message.simple_pluralization', 4);
// returns "Dogs"

$translator->transChoice('message.advanced_pluralization', 8);
// returns "Some"
Clone this wiki locally