laravel 5.4 session app setlocale不能正常工作

时间:2017-12-12 04:31:34

标签: session multilingual

在我的应用程序中,我尝试更新app文件中的区域设置:在Locale Middleware文件中:

public function handle($request, Closure $next) 
{
    if(Session::has('locale')) 
    {
        $locale = Session::get('locale'); 
        App::setLocale($locale); 
    }
    return $next($request);
}

但是,它不起作用

我已经注册了Kernel.php

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
    \App\Http\Middleware\TrimStrings::class,
    \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
    \App\Http\Middleware\LanguageChange::class,

];

我创建路线网

Route::get('lang/{locale}', function ($locale) {
//App::setLocale($locale);
Session::put('locale', $locale); 
return Redirect::back(); 

});

我的刀片

<li class="dropdown">
          <a class=" dropdown-toggle" id="dropdownLang"
                           data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
                            @if(\Illuminate\Support\Facades\Session::get('locale') == "th")
                                THAI
                            @elseif(\Illuminate\Support\Facades\Session::get('locale') == "en")
                                English
                            @endif
                            <span class="caret"></span></a>
                        <ul class="dropdown-menu" aria-labelledby="dropdownLang">
                            <li>
                                <a href="{{url('lang/en')}}">
                                    ภาษาอังกฤษ (English)
                                </a>
                            </li>
                            <li>
                                <a href="{{url('lang/th')}}">
                                    ภาษาไทย (Thai)
                                </a>
                            </li>
                        </ul>
                    </li>

1 个答案:

答案 0 :(得分:1)

我认为您应该在“ StartSession”中间件之后添加中间件。

protected $middlewareGroups = [
    ...
    'web' => [
        ...
        \Illuminate\Session\Middleware\StartSession::class,
        \App\Http\Middleware\Locale::class,
        ...
    ],
    ...
];

检查此处以查看laravel 5.7 https://gist.github.com/ricardocanelas/08bec2e801226af4fe8e3180a8de466d

中的示例