blessing-skin-server/app/Http/Middleware/DetectLanguagePrefer.php

23 lines
594 B
PHP
Raw Normal View History

2017-01-18 22:31:35 +08:00
<?php
namespace App\Http\Middleware;
2019-04-26 00:01:32 +08:00
use Cookie;
2019-02-27 23:44:50 +08:00
use Illuminate\Support\Arr;
2017-01-18 22:31:35 +08:00
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class DetectLanguagePrefer
{
public function handle($request, \Closure $next)
{
2019-04-26 00:01:32 +08:00
$locale = $request->input('lang') ?? $request->cookie('locale') ?? $request->getPreferredLanguage();
2019-02-27 23:44:50 +08:00
if (($info = Arr::get(config('locales'), $locale)) && ($alias = Arr::get($info, 'alias'))) {
2017-01-18 22:31:35 +08:00
$locale = $alias;
}
app()->setLocale($locale);
2019-04-26 00:01:32 +08:00
Cookie::queue('locale', $locale);
2019-04-25 13:01:39 +08:00
return $next($request);
2017-01-18 22:31:35 +08:00
}
}