Added option to disable public lang autodetect

Also cleaned up localization middleware a little.
Closes #944
This commit is contained in:
Dan Brown 2018-08-12 13:10:55 +01:00
parent fcdb39e428
commit fcb93dc7c8
No known key found for this signature in database
GPG key ID: 46D9F943C24A2EF9
2 changed files with 37 additions and 11 deletions

View file

@ -2,6 +2,7 @@
use Carbon\Carbon; use Carbon\Carbon;
use Closure; use Closure;
use Illuminate\Http\Request;
class Localization class Localization
{ {
@ -15,21 +16,33 @@ class Localization
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
$defaultLang = config('app.locale'); $defaultLang = config('app.locale');
if (user()->isDefault()) {
$locale = $defaultLang; if (user()->isDefault() && config('app.auto_detect_locale')) {
$availableLocales = config('app.locales'); $locale = $this->autoDetectLocale($request, $defaultLang);
foreach ($request->getLanguages() as $lang) {
if (!in_array($lang, $availableLocales)) {
continue;
}
$locale = $lang;
break;
}
} else { } else {
$locale = setting()->getUser(user(), 'language', $defaultLang); $locale = setting()->getUser(user(), 'language', $defaultLang);
} }
app()->setLocale($locale); app()->setLocale($locale);
Carbon::setLocale($locale); Carbon::setLocale($locale);
return $next($request); 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;
}
} }

View file

@ -77,8 +77,21 @@ return [
*/ */
'locale' => env('APP_LANG', 'en'), 'locale' => env('APP_LANG', 'en'),
'locales' => ['en', 'de', 'es', 'es_AR', 'fr', 'nl', 'pt_BR', 'sk', 'sv', 'ja', 'pl', 'it', 'ru', 'zh_CN', 'zh_TW'], 'locales' => ['en', 'de', 'es', 'es_AR', 'fr', 'nl', 'pt_BR', 'sk', 'sv', 'ja', 'pl', 'it', 'ru', 'zh_CN', 'zh_TW'],
/*
|--------------------------------------------------------------------------
| Auto-detect the locale for public users
|--------------------------------------------------------------------------
|
| For public users their locale can be guessed by headers sent by their
| browser. This is usually set by users in their browser settings.
| If not found the default app locale will be used.
|
*/
'auto_detect_locale' => env('APP_AUTO_LANG_PUBLIC', true),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Fallback Locale | Application Fallback Locale
@ -245,7 +258,7 @@ return [
'Activity' => BookStack\Services\Facades\Activity::class, 'Activity' => BookStack\Services\Facades\Activity::class,
'Setting' => BookStack\Services\Facades\Setting::class, 'Setting' => BookStack\Services\Facades\Setting::class,
'Views' => BookStack\Services\Facades\Views::class, 'Views' => BookStack\Services\Facades\Views::class,
'Images' => \BookStack\Services\Facades\Images::class, 'Images' => BookStack\Services\Facades\Images::class,
], ],