流明无法更改区域设置 [英] Lumen fails to change locale

查看:105
本文介绍了流明无法更改区域设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个几乎全新的流明装置.

I have an almost fresh installation of Lumen.

我有一条POST路线通向一个控制器.在请求中,我发送带有字符串值的lang参数.

I have a POST route which leads to one Controller. In the request I send lang parameter with a string value.

public function form(Request $request)
{
    if ($request->has('lang')) {
        app('config')->set('app.locale', $request->input('lang'));
    }
    return view('form');
}

我的/resources/lang/文件夹中还有一个名为bg的文件夹.在此文件夹中,我有一个简单的文件,该文件具有与resources/lang/en/validation.php中相同的数组-称为static.php.

I have one folder more in my /resources/lang/ folder called bg. In this folder I have simple file which has an array just like in resources/lang/en/validation.php - called static.php.

在我显示的视图中,我有几行显示:

In the view I'm displaying I have few lines that say:

<input type="text" name="email" value="" class="input-default" placeholder="{{trans('static.enter_email')}}"/>

我在resources/lang/...中的那些lang文件中设置的文本可以正确显示,但是在显示表单之前,我无法更改区域设置.

The text I've set in those lang files in resources/lang/... are properly displayed, but I cannot change the locale before displaying the form.

例如,如果在我的 dotenv 文件中设置:

For example, if in my dotenv file I set:

APP_LOCALE=en

并加载视图,并加载resources/lang/en/static.php中的字符串. 如果将APP_LOCALE更改为bg并再次加载视图,则会从resources/lang/bg/static.php获取字符串.

And load the view the strings in resources/lang/en/static.php are loaded. If I change the APP_LOCALE to bg and load the view again, I get the strings from resources/lang/bg/static.php.

我只想在渲染视图之前设置语言环境,这似乎与上面提供的代码(在我的控制器中)不兼容.

All I want is to set the locale before rendering the view, which doesn't seem to work with the code (in my controller) I provided above.

我在官方网站上也找不到有关流明的本地化的任何信息,但我想它支持这样,因为trans()正在工作.

Also I couldn't find any information about localization in Lumen on the official site, but I suppose it supports such, since trans() is working.

推荐答案

要在加载配置后更改配置,该配置无效.要在运行时更改语言环境,请在应用程序实例上使用setLocale()方法:

You are changing the config after it is loaded, which has no effect. To change the locale at runtime use the setLocale() method on the app instance:

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

第一个答案似乎仅适用于Laravel.要在运行时使用Lumen更改语言环境,请使用以下命令:

The first answer seem to only work on Laravel. To change the locale at runtime with Lumen use this:

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

这篇关于流明无法更改区域设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆