【问题标题】:Pluralization is not working when using translation strings as keys in laravel localization在 laravel 本地化中使用翻译字符串作为键时,复数不起作用
【发布时间】:2021-10-19 04:58:03
【问题描述】:

我在 fr.json 中添加了这个

{
    "There is one apple|There are many apples": "Hay una manzana|Hay muchas manzanas"
}

在刀片文件中:

{{__("There is one apple")}}

然后显示

只有一个苹果

它应该显示 Hay una manzana。请帮我解决。

【问题讨论】:

    标签: php laravel localization laravel-localization


    【解决方案1】:

    应用程序的默认语言存储在config/app.php 配置文件的locale 配置选项中。您可以随意修改此值以满足您的应用程序的需要。

    在你的情况下应该是

    'locale' => 'fr',
    

    您可以在文档https://laravel.com/docs/8.x/localization#configuring-the-locale中找到更多详细信息

    【讨论】:

    • 我做了同样的事情,但没有工作。
    • @abhishekvbajaj 确保缓存配置是最新的 php artisan config:cache 并将语言环境转储到 cotroller dd(App::currentLocale(), true);
    【解决方案2】:

    首先确保将 config/app.php 设置为 'locale' => 'fr'。 您可以使用Illuminate\Support\Facades\App::setLocal('fr'); 设置每个请求 在路由中(Route::get())或者可能使用中间件。

    设置区域设置后,将此键:值对添加到 resources/lang/fr.json 文件: "There is one apple|There are many apples" : "{1} Hay una manzana|[2,*] Hay muchas manzanas".

    而不是__() 使用trans_choice() 方法。您还必须提供苹果/苹果的数量作为第二个参数。这就是它如何知道它是单数(1)还是复数(2+)。所以这就是你在刀片文件中使用它的方式:

    {{ trans_choice("There is one apple|There are many apples", 2) }}.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多