简体   繁体   English

controller 中的 Lumen(laravel)翻译

[英]Lumen (laravel) translation in controller

I need to translate strings which are used in (email-)jobs and controllers.我需要翻译在(电子邮件)作业和控制器中使用的字符串。

I read this: https://laravel.com/docs/5.2/localization so 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.我怎样才能在 controller 或工作中找到翻译。

Thanks in advance!提前致谢!

trans it's a global function, so you can use it directly from your controller or Closure trans它是一个全局函数,因此您可以直接从控制器或Closure使用它

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

    echo trans('messages.welcome');

});

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 希望能帮助到你

I am using Lumen 8.0 version:我使用的是 Lumen 8.0 版本:

To set new local language:设置新的本地语言:

$request->input('lang') can have any language code eg. $request->input('lang')可以有任何语言代码,例如。 en or you can set it manully by passing language code as parameter in setLocale function. en或者您可以通过将语言代码作为参数传递给setLocale function 来手动设置它。

app('translator')->setLocale($request->input('lang'));

To get current local language:要获取当前的本地语言:

app('translator')->getLocale();

output: en output:英文

This worked for me.这对我有用。 I hope, this will also help you:)我希望,这也会对你有所帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM