如何在控制器中使用 Laravel 语言翻译作为常量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47674869/
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
How use Laravel Language Translation in controller as constant?
提问by Adam Kozlowski
Is it possible to use Laravel Language Translator (Localization) in controller as constant? Below attempt, example that does not work:
是否可以在控制器中使用 Laravel 语言翻译器(本地化)作为常量?下面的尝试,不起作用的例子:
class SearchApproval extends Controller
{
private $request;
const stage_1 = Lang::get('message.failed');
回答by timod
trans it's a global function, so you can use it directly from your controller
trans 它是一个全局函数,因此您可以直接从控制器中使用它
trans('messages.failed');
but this won't work as constant, so you can use it like this:
但这不会像常数一样工作,因此您可以像这样使用它:
class SearchApproval extends Controller
{
private $request;
const stage_1 = 'message.failed';
public function xxx(){
$whatever = trans(self::stage_1);
}
}
UPDATED:
更新:
You can use this __('Your Text') inside the controller.
您可以在控制器内使用这个 __('Your Text') 。
回答by Amando Vledder
Use trans('message.failed')instead of Lang::get('message.failed')https://laravel.com/docs/5.5/helpers#method-trans
使用trans('message.failed')而不是Lang::get('message.failed')https://laravel.com/docs/5.5/helpers#method-trans

