2017-01-18 21:04:29 +01:00
|
|
|
<?php namespace BookStack\Http\Middleware;
|
2017-01-15 17:27:24 +01: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 17:47:41 +02:00
|
|
|
if (user()->isDefault()) {
|
|
|
|
$locale = $defaultLang;
|
|
|
|
$availableLocales = config('app.locales');
|
|
|
|
foreach ($request->getLanguages() as $lang) {
|
2018-01-28 17:58:52 +01:00
|
|
|
if (!in_array($lang, $availableLocales)) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-04-29 17:47:41 +02:00
|
|
|
$locale = $lang;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$locale = setting()->getUser(user(), 'language', $defaultLang);
|
|
|
|
}
|
2017-01-15 17:27:24 +01:00
|
|
|
app()->setLocale($locale);
|
|
|
|
Carbon::setLocale($locale);
|
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
}
|