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);
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化