Laravel 本地化字符串中的参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30242119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:33:34  来源:igfitidea点击:

Parameters inside Laravel localized string

phplaravel

提问by JasonGenX

I'd like have something like this in the lang/en/mymsgs.php

我想在 lang/en/mymsgs.php

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

I would provide the content for %1 and %2 when getting the 'string1' from my code.

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

I couldn't find a way to do this. Any pointers?

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

回答by bishop

Laravel message localization uses named, not numeric, parameters.

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

Rewriting your example message:

重写您的示例消息:

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

You can now use, for example:

您现在可以使用,例如:

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

回答by Tamim

If anyone use string translation for Laravel. They can use like this -> __('Some translatable string with :attribute',['attribute' => $attribute_var])

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

check the documentation for more https://laravel.com/docs/5.7/localization#using-short-keys

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