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

40 lines
1018 B
PHP
Raw Normal View History

2017-01-18 22:31:35 +08:00
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class DetectLanguagePrefer
{
public function handle($request, \Closure $next)
{
$response = $next($request);
if ($response instanceof Response) {
$response->cookie('locale', config('app.locale'));
}
return $response;
}
public function detect(Request $request)
{
$locale = $request->input('lang') ?: ($request->cookie('locale') ?: $request->getPreferredLanguage());
2017-11-24 20:05:48 +08:00
if ($locale == 'zh_HANS_CN') { // For Microsoft Edge and IE
$locale = 'zh_CN';
}
2017-01-18 22:31:35 +08:00
// if current locale is an alias of other locale
if (($info = array_get(config('locales'), $locale)) && ($alias = array_get($info, 'alias'))) {
$locale = $alias;
}
app()->setLocale($locale);
AfterSessionBooted::$jobs[] = function () {
session(['locale' => config('app.locale')]);
};
}
}