2018-09-03 10:18
浏览 291


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 found 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 mee, 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])
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doumaque6551 2018-09-03 10:23

    There is a @choice blade directive for this.

    Course duration: {{ $course->days }} @choice('day|days', $course->days)
    打赏 评论
  • duanchi0883649 2018-09-03 10:23

    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
    打赏 评论

相关推荐 更多相似问题