Laravel - Localization - Pluralization
Pluralization is a complex problem, as different languages have a variety of complex rules for pluralization; however, Laravel can help you translate strings differently based on pluralization rules that you define. Using a |
character, you may distinguish singular and plural forms of a string:
'apples' => 'There is one apple|There are many apples',
Of course, pluralization is also supported when using translation strings as keys:
{
"There is one apple|There are many apples": "Hay una manzana|Hay muchas manzanas"
}
You may even create more complex pluralization rules which specify translation strings for multiple ranges of values:
'apples' => '{0} There are none|[1,19] There are some|[20,*] There are many',
After defining a translation string that has pluralization options, you may use the trans_choice
function to retrieve the line for a given "count". In this example, since the count is greater than one, the plural form of the translation string is returned:
echo trans_choice('messages.apples', 10);
You may also define placeholder attributes in pluralization strings. These placeholders may be replaced by passing an array as the third argument to the trans_choice
function:
'minutes_ago' => '{1} :value minute ago|[2,*] :value minutes ago',
echo trans_choice('time.minutes_ago', 5, ['value' => 5]);
If you would like to display the integer value that was passed to the trans_choice
function, you may use the built-in :count
placeholder:
'apples' => '{0} There are none|{1} There is one|[2,*] There are :count',