duanhao1004 2017-08-17 15:26
浏览 46
已采纳

多语言网站,使用语言操作?

I use laravel 5.3 framework and have a middleware to check for languages, redirects are correct and localization works, my question is, it is recommended to save selected language in a cookie? So I will be able to redirect the user every time to the selected language? Can it be also good for performance...

At the moment if I call App::getLocale() is get the right language.

I'm generally interested to know is this way correct what I do?

  • 写回答

2条回答 默认 最新

  • douhui7136 2017-08-17 15:38
    关注

    I use this middleware to check/set the language in session for every request:

    <?php
    
    namespace App\Http\Middleware;
    
    use App;
    use Auth;
    use Config;
    use Session;
    use Closure;
    
    class SetLocale
    {
        public function handle($request, Closure $next)
        {
            // If the session doesn't have already a locale
            if (!Session::has('locale')) {
                // Set the logged in user language
                if (Auth::check() && Auth::user()->lang->code) {
                    Session::put('locale', Auth::user()->lang->code);
                } else {
                    // Else get the http header language and set it
                    $requestLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
                    if (App\Lang::where('code', $requestLanguage)->exists()) {
                        Session::put('locale', $requestLanguage);
                    } else {
                        // If none of the above worked use the app deafult language
                        Session::put('locale', Config::get('app.locale'));
                    }
                }
            }
            // Set the output locale as app locale
            App::setLocale(Session::get('locale'));
    
            return $next($request);
        }
    }
    

    Hope this helps you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog