2017-01-19 04:04:29 +08:00
|
|
|
<?php namespace BookStack\Http\Middleware;
|
2017-01-16 00:27:24 +08:00
|
|
|
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Closure;
|
|
|
|
|
|
|
|
class Localization
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function handle($request, Closure $next)
|
|
|
|
{
|
|
|
|
$defaultLang = config('app.locale');
|
2017-04-29 23:47:41 +08:00
|
|
|
if (user()->isDefault()) {
|
|
|
|
$locale = $defaultLang;
|
|
|
|
$availableLocales = config('app.locales');
|
|
|
|
foreach ($request->getLanguages() as $lang) {
|
|
|
|
if (!in_array($lang, $availableLocales)) continue;
|
|
|
|
$locale = $lang;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$locale = setting()->getUser(user(), 'language', $defaultLang);
|
|
|
|
}
|
2017-01-16 00:27:24 +08:00
|
|
|
app()->setLocale($locale);
|
|
|
|
Carbon::setLocale($locale);
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|