Pluralization in Laravel translations
source link: https://barryvanveen.nl/articles/29-pluralization-in-laravel-translations
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Pluralization in Laravel translations
Jul 15, 2016
Laravel translations can depend on the number of subjects: one apple, two apples. This article will explain more complex pluralizations: no apples, one apple, two apples, tons of apples.
Define and use translations
In Laravel you can define translations in the /resources/lang/
folder. Each folder contains files with the translations for a specific language. An example of these translations:
// en/comments.php
'title' => 'Comments',
// nl/comments.php
'title' => 'Reacties',
In your views you can use these with a helper like so:
trans('comments.title')
Replacing parameters
Parameters can be used in translations like so:
// en/comments.php
'number-of-comments' => ':count comments',
// in your views
trans('comments.number-of-comments', ['count' => $count])
Pluralization
Pluralizations give you the ability to express the difference between 1 comment and 2 comments. This time, we must use the trans_choice
helper function.
// en/comments.php
'number-of-comments' => ':count comment|:count comments',
// in your views
trans_choice('comments.number-of-comments', ['count' => $count])
The downside of these pluralizations is they only define the difference between a single object and multiple objects. If we want to overcome this problem we must be specific about interval that each string belongs to.
Advanced pluralizations
Because Laravel translations are powered by the Symfony Translation component we can define even more powerful translations like this.
// en/comments.php
'number-of-comments' => '{0} no comments yet|{1} 1 comment|[2, Inf] :count comments',
// in your views
trans_choice('comments.number-of-comments', $count, ['count' => $count])
Each translation is a list of separated strings. Each string belongs to an interval that is denoted in the ISO 31-11 form.
A few examples of intervals:
{0} = 0
{1,2,3} = 1, 2, 3
[0, 3] = 0, 1, 2, 3
]0, 3] = 1, 2, 3
]0, 3[ = 1, 2
[-2, Inf] = -2, -1, 0, 1, 2, ..., n
[-Inf, Inf] = -n, ..., -1, 0, 1, ..., n
Further reading
Related articles
Comments (2)
Got a question? Liked the article or got a suggestion? Leave a comment to let us know.
Name
Email address (not visible to others)
Message
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK