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