X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/1f202f6dbc151eadd83f9e162d10f215e132c007..refs/pull/2700/head:/app/Http/Middleware/Localization.php diff --git a/app/Http/Middleware/Localization.php b/app/Http/Middleware/Localization.php index c0ac7a7c4..cd084746c 100644 --- a/app/Http/Middleware/Localization.php +++ b/app/Http/Middleware/Localization.php @@ -9,17 +9,17 @@ class Localization /** * Array of right-to-left locales - * @var array */ protected $rtlLocales = ['ar', 'he']; /** * Map of BookStack locale names to best-estimate system locale names. - * @var array */ protected $localeMap = [ 'ar' => 'ar', 'bg' => 'bg_BG', + 'bs' => 'bs_BA', + 'ca' => 'ca', 'da' => 'da_DK', 'de' => 'de_DE', 'de_informal' => 'de_DE', @@ -28,12 +28,15 @@ class Localization 'es_AR' => 'es_AR', 'fr' => 'fr_FR', 'he' => 'he_IL', + 'id' => 'id_ID', 'it' => 'it_IT', 'ja' => 'ja', 'ko' => 'ko_KR', + 'lv' => 'lv_LV', 'nl' => 'nl_NL', + 'nb' => 'nb_NO', 'pl' => 'pl_PL', - 'pt' => 'pl_PT', + 'pt' => 'pt_PT', 'pt_BR' => 'pt_BR', 'ru' => 'ru', 'sk' => 'sk_SK', @@ -58,12 +61,7 @@ class Localization $defaultLang = config('app.locale'); config()->set('app.default_locale', $defaultLang); - if (user()->isDefault() && config('app.auto_detect_locale')) { - $locale = $this->autoDetectLocale($request, $defaultLang); - } else { - $locale = setting()->getUser(user(), 'language', $defaultLang); - } - + $locale = $this->getUserLocale($request, $defaultLang); config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale))); // Set text direction @@ -77,14 +75,29 @@ class Localization return $next($request); } + /** + * Get the locale specifically for the currently logged in user if available. + */ + protected function getUserLocale(Request $request, string $default): string + { + try { + $user = user(); + } catch (\Exception $exception) { + return $default; + } + + if ($user->isDefault() && config('app.auto_detect_locale')) { + return $this->autoDetectLocale($request, $default); + } + + return setting()->getUser($user, 'language', $default); + } + /** * 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) + protected function autoDetectLocale(Request $request, string $default): string { $availableLocales = config('app.locales'); foreach ($request->getLanguages() as $lang) { @@ -97,10 +110,8 @@ class Localization /** * Get the ISO version of a BookStack language name - * @param string $locale - * @return string */ - public function getLocaleIso(string $locale) + public function getLocaleIso(string $locale): string { return $this->localeMap[$locale] ?? $locale; } @@ -108,7 +119,6 @@ class Localization /** * Set the system date locale for localized date formatting. * Will try both the standard locale name and the UTF8 variant. - * @param string $locale */ protected function setSystemDateLocale(string $locale) {