Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pluralization in laravel blade @lang() localization?

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])
like image 686
Hendrik Jan Avatar asked Nov 29 '22 08:11

Hendrik Jan


2 Answers

There is a @choice blade directive for this.

Course duration: {{ $course->days }} @choice('day|days', $course->days)
like image 59
Jerodev Avatar answered Dec 11 '22 09:12

Jerodev


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'
like image 35
imaginabit Avatar answered Dec 11 '22 09:12

imaginabit