Laravel 本地化字符串中的参数

2022-08-30 12:12:12

我希望在lang/en/mymsgs.php

'string1' => 'Welcome %1, Please meet %2'

当从我的代码中获取“string1”时,我会提供%1和%2的内容。

我找不到办法做到这一点。任何指针?


答案 1

Laravel 消息本地化使用命名参数,而不是数字参数。

重写示例消息:

'string1' => 'Welcome :user, Please meet :other',

例如,您现在可以使用:

trans('string1', [ 'user' => 'Ainsley', 'other' => 'Hayden' ]);

答案 2

如果有人使用Laravel的字符串翻译。他们可以像这样使用-> __('Some translatable string with :attribute',['attribute' => $attribute_var])

查看文档以获取更多 https://laravel.com/docs/5.7/localization#using-short-keys


推荐