控制器中的流明(laravel)翻译

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37655880/
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 13:56:34  来源:igfitidea点击:

Lumen (laravel) translation in controller

phplaraveltranslationlumen

提问by Peter

I need to translate strings which are used in (email-)jobs and controllers.

我需要翻译(电子邮件)作业和控制器中使用的字符串。

I read this: https://laravel.com/docs/5.2/localizationso I know you can get in in the view with:

我读了这个:https: //laravel.com/docs/5.2/localization所以我知道你可以通过以下方式进入视图:

echo trans("messages.welcome");

But it's an Lumen api and I do not use the view.

但它是一个 Lumen api,我不使用该视图。

How can i get the translator in a controller or a job.

我怎样才能在控制器或工作中找到翻译。

Thanks in advance!

提前致谢!

回答by huuuk

transit's a global function, so you can use it directly from your controller or Closure

trans它是一个全局函数,因此您可以直接从控制器或 Closure

Route::get('/', function () {

    echo trans('messages.welcome');

});

回答by KuKeC

To get the translator in controller you need to use it like below

要在控制器中使用翻译器,您需要像下面这样使用它

//include in your controller
use Lang;

//in code you get values like
Lang::get('messages.error');

Hope it helps

希望能帮助到你