我想在Laravel 5.3上使用带变量的复数翻译

问题描述 投票:1回答:2

在Laravel 5.3中,我想使用翻译:

'items' => ':number item|:number items'

并使用trans():

trans('folder/file.items', ['number' => 0])

提供0项,1项和2项。

laravel translation laravel-5.3
2个回答
3
投票

阅读以下文档:https://laravel.com/docs/5.3/localization#pluralization

在Laravel 5.3中:

'items' => '[0,1] :count item |[2,Inf] :count items',

在Laravel 5.4(https://laravel.com/docs/5.4/localization#pluralization):

'items' => '[0,1] :count item |[2,*] :count items',

在视图中:

{{ trans_choice('app.items', 5) }}

计数占位符将自动设置数字5


1
投票

只需使用Laravel提供的str_plural()辅助函数即可!

{{ $items }} {{ str_plural('item', $items) }}

变量$ item必须是数字,而不是数组或集合。如果$ items是一个集合,您必须使用count($ items)来获取集合中的项目数。它会是这样的:

{{ $items }} {{ str_plural('item', count($items)) }}

参考文献(Laravel 5.3):qazxsw poi

© www.soinside.com 2019 - 2024. All rights reserved.