Laravel本地化字符串中的参数

Jas*_*enX 17 php laravel

我想在这里有类似的东西 lang/en/mymsgs.php

'string1' => 'Welcome %1, Please meet %2'
Run Code Online (Sandbox Code Playgroud)

从我的代码中获取'string1'时,我会提供%1和%2的内容.

我找不到办法做到这一点.有什么指针吗?

bis*_*hop 34

Laravel消息本地化使用命名而非数字参数.

重写您的示例消息:

'string1' => 'Welcome :user, Please meet :other',
Run Code Online (Sandbox Code Playgroud)

您现在可以使用,例如:

trans('string1', [ 'user' => 'Ainsley', 'other' => 'Hayden' ]);
Run Code Online (Sandbox Code Playgroud)


小智 15

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

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