doufu7464 2014-11-05 23:55
浏览 23
已采纳

每个用户的Laravel语言设置

I know how to set default language in application by using App::setLocale('es');

I'm thinking about setting individual language per user after an user logs in.

Currently, the only way I have in mind is to set a general language and use variable inside a Lang::get() command:

$user_language = 'gr';

Lang::get('messages.welcome'.$user_language);

Is there any other way of setting language setting per user?

  • 写回答

1条回答 默认 最新

  • duandou9931 2014-11-06 00:12
    关注

    Why not just use App::setLocale() to set the language according to the user preferences if the user is logged in. According to the Laravel Docs:

    You may change the active language at any time using the App::setLocale method.

    So you could do that maybe like this:

    App::before(function($request)
    {
        // If user is logged in
        if (Auth::check())
        {
            // Get the user specific language
            $lang = Auth::user()->language;
    
            // Set the language
            App::setLocale($lang);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法