Fix switching i18n

This commit is contained in:
Pig Fang 2019-04-26 00:01:32 +08:00
parent 6918a46ae8
commit 262d48e1f7
3 changed files with 21 additions and 4 deletions

View File

@ -27,7 +27,7 @@ class Kernel extends HttpKernel
*/
protected $middlewareGroups = [
'web' => [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,

View File

@ -2,6 +2,7 @@
namespace App\Http\Middleware;
use Cookie;
use Illuminate\Support\Arr;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
@ -10,13 +11,12 @@ class DetectLanguagePrefer
{
public function handle($request, \Closure $next)
{
$locale = $request->input('lang') ?? session('locale') ?? $request->getPreferredLanguage();
$locale = $request->input('lang') ?? $request->cookie('locale') ?? $request->getPreferredLanguage();
if (($info = Arr::get(config('locales'), $locale)) && ($alias = Arr::get($info, 'alias'))) {
$locale = $alias;
}
app()->setLocale($locale);
session(['locale' => $locale]);
Cookie::queue('locale', $locale);
return $next($request);
}
}

View File

@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array
*/
protected $except = [
'locale',
];
}