mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-11-29 04:10:09 +08:00
fcb93dc7c8
Also cleaned up localization middleware a little. Closes #944
49 lines
1.3 KiB
PHP
49 lines
1.3 KiB
PHP
<?php namespace BookStack\Http\Middleware;
|
|
|
|
use Carbon\Carbon;
|
|
use Closure;
|
|
use Illuminate\Http\Request;
|
|
|
|
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');
|
|
|
|
if (user()->isDefault() && config('app.auto_detect_locale')) {
|
|
$locale = $this->autoDetectLocale($request, $defaultLang);
|
|
} else {
|
|
$locale = setting()->getUser(user(), 'language', $defaultLang);
|
|
}
|
|
|
|
app()->setLocale($locale);
|
|
Carbon::setLocale($locale);
|
|
return $next($request);
|
|
}
|
|
|
|
/**
|
|
* Autodetect the visitors locale by matching locales in their headers
|
|
* against the locales supported by BookStack.
|
|
* @param Request $request
|
|
* @param string $default
|
|
* @return string
|
|
*/
|
|
protected function autoDetectLocale(Request $request, string $default)
|
|
{
|
|
$availableLocales = config('app.locales');
|
|
foreach ($request->getLanguages() as $lang) {
|
|
if (in_array($lang, $availableLocales)) {
|
|
return $lang;
|
|
}
|
|
}
|
|
return $default;
|
|
}
|
|
}
|