Pluralization in laravel blade @lang() localization?
Asked Answered
R

4

13

Laravel 5 provides translations using the @lang helper

<!-- file: template.blade.php -->
@lang('some text')

Laravel 5 also has the possibility to pluralize strings depending on a variable.

// file: controller.php
echo trans_choice('messages.apples', 10);

The translation file would then contain the following line to translate apples:

// file: /resources/lang/en
'apples' => 'There is one apple|There are many apples',

Now, I would like to use pluralization inside a blade template and I cannot find out how to use this. I tried the following:

<!-- file: template.blade.php -->
Course duration: {{ $course.days }} @lang('day|days', $course.days)

which feels to be the logical syntax to me, but this only give me an error about input argument 2 needing to be an array. I also tried this:

<!-- file: template.blade.php -->
Course duration: {{ $course.days }} @lang('day|days', [$course.days])

And this:

<!-- file: template.blade.php -->
Course duration: {{ $course.days }} @lang(['day|days', $course.days])
Riegel answered 3/9, 2018 at 10:18 Comment(0)
S
30

There is a @choice blade directive for this.

Course duration: {{ $course->days }} @choice('day|days', $course->days)
Seaman answered 3/9, 2018 at 10:23 Comment(6)
Is that included by default? I thought something like that would be useful but never found it in documentations...Boucher
This works great. I combined it with the answer of D. Petrov. @choice('plurals.days', $course->days)Riegel
It would be interesting if you could choose between two translations ;)Hippocras
On Laravel 7, using the resources/lang/lang.json I use: @choice('day',2) and this choice means get the string after the pipe so it shows 'days' on the translation languageIndelicate
Laravel 8.69.0, @choice directive doesn't work ? Always returns first parameter before pipe, any idea ??Clayberg
You can use {{ $course->days }} {{ Str::plural('day', $course->days }} instead of @choice.Ounce
R
7

You can use it with variables like this

//plurals.php
'day' => 'one day| :n days',

you can do this in blade file:

{{ trans_choice('plurals.day', $course->days), ['n' => $course->days] }} 

you can even use this

{{ trans_choice('plurals.like', $post->likes), ['n' => $post->likes] }} 
'like' => '{0} Nobody likes this|[1,19] :n users like this|[20,*] Many users like this'
Ritaritardando answered 21/7, 2021 at 7:26 Comment(0)
B
1

You'll have to register a new keyed entry in one of your translation files, let's say plurals.php. Then the proper approach would be:

//in plurals.php
//...
'day' => 'day|days',
//...

Then you can retrieve the entry like

{{trans_choice('plurals.day', $course->days)}} //assuming the arrow syntax is how you retrieve a property in php :P
Boucher answered 3/9, 2018 at 10:23 Comment(0)
O
0

You can use plural method from Illuminate\Support\Str;

Course duration: {{ $course->days }} {{ Str::plural('day', $course->days) }}
Ounce answered 27/8, 2024 at 10:11 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.