laravel blade @lang() 本地化中的复数化?

Pluralization in laravel blade @lang() localization?

Laravel 5 使用@lang 助手提供翻译

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

Laravel 5 也可以根据变量对字符串进行复数化。

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

翻译文件将包含以下行来翻译苹果:

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

现在,我想在 blade 模板中使用复数形式,但我不知道如何使用它。我尝试了以下方法:

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

这对我来说是合乎逻辑的语法,但这只会给我一个关于输入参数 2 需要是数组的错误。我也试过这个:

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

还有这个:

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

为此有一个 @choice blade 指令。

Course duration: {{ $course->days }} @choice('day|days', $course->days)

您必须在其中一个翻译文件中注册一个新的键控条目,比方说 plurals.php。那么正确的做法是:

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

然后您可以像

一样检索条目
{{trans_choice('plurals.day', $course->days)}} //assuming the arrow syntax is how you retrieve a property in php :P

您可以像这样将它与变量一起使用

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

您可以在 blade 文件中执行此操作:

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

你甚至可以使用这个

{{ 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'