dongzhan5286 2015-07-06 11:25
浏览 17
已采纳

流明无法改变语言环境

I have an almost fresh installation of Lumen.

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');
}

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')}}"/>

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.

For example, if in my dotenv file I set:

APP_LOCALE=en

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.

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.

  • 写回答

1条回答 默认 最新

  • drwjv28028 2015-07-07 09:14
    关注

    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'));

    Edit

    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'));

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探