]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Middleware/Localization.php
Update errors.php
[bookstack] / app / Http / Middleware / Localization.php
index 528ff40478dbeb92f436ee747b0cb9f07b9071d8..b5e702781ef53b6d7bd28b76cbe5ca42728a283b 100644 (file)
@@ -7,8 +7,38 @@ use Illuminate\Http\Request;
 class Localization
 {
 
+    /**
+     * Array of right-to-left locales
+     * @var array
+     */
     protected $rtlLocales = ['ar'];
 
+    /**
+     * Map of BookStack locale names to best-estimate system locale names.
+     * @var array
+     */
+    protected $localeMap = [
+        'ar' => 'ar',
+        'de' => 'de_DE',
+        'de_informal' => 'de_DE',
+        'en' => 'en_GB',
+        'es' => 'es_ES',
+        'es_AR' => 'es_AR',
+        'fr' => 'fr_FR',
+        'it' => 'it_IT',
+        'ja' => 'ja',
+        'kr' => 'ko_KR',
+        'nl' => 'nl_NL',
+        'pl' => 'pl_PL',
+        'pt_BR' => 'pt_BR',
+        'ru' => 'ru',
+        'sk' => 'sk_SK',
+        'sv' => 'sv_SE',
+        'uk' => 'uk_UA',
+        'zh_CN' => 'zh_CN',
+        'zh_TW' => 'zh_TW',
+    ];
+
     /**
      * Handle an incoming request.
      *
@@ -19,6 +49,7 @@ class Localization
     public function handle($request, Closure $next)
     {
         $defaultLang = config('app.locale');
+        config()->set('app.default_locale', $defaultLang);
 
         if (user()->isDefault() && config('app.auto_detect_locale')) {
             $locale = $this->autoDetectLocale($request, $defaultLang);
@@ -26,6 +57,8 @@ class Localization
             $locale = setting()->getUser(user(), 'language', $defaultLang);
         }
 
+        config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
+
         // Set text direction
         if (in_array($locale, $this->rtlLocales)) {
             config()->set('app.rtl', true);
@@ -33,6 +66,7 @@ class Localization
 
         app()->setLocale($locale);
         Carbon::setLocale($locale);
+        $this->setSystemDateLocale($locale);
         return $next($request);
     }
 
@@ -53,4 +87,28 @@ class Localization
         }
         return $default;
     }
+
+    /**
+     * Get the ISO version of a BookStack language name
+     * @param  string $locale
+     * @return string
+     */
+    public function getLocaleIso(string $locale)
+    {
+        return $this->localeMap[$locale] ?? $locale;
+    }
+
+    /**
+     * 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)
+    {
+        $systemLocale = $this->getLocaleIso($locale);
+        $set = setlocale(LC_TIME, $systemLocale);
+        if ($set === false) {
+            setlocale(LC_TIME, $systemLocale . '.utf8');
+        }
+    }
 }