]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Middleware/Localization.php
Added tests and translations for dark-mode components
[bookstack] / app / Http / Middleware / Localization.php
index bdbf6ccbd9c57832cb29c560770900683e5c7938..d24e8a9b5b88c8f8cb0b08f60a05506942561021 100644 (file)
@@ -6,6 +6,45 @@ use Illuminate\Http\Request;
 
 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',
+        'da' => 'da_DK',
+        'de' => 'de_DE',
+        'de_informal' => 'de_DE',
+        'en' => 'en_GB',
+        'es' => 'es_ES',
+        'es_AR' => 'es_AR',
+        'fr' => 'fr_FR',
+        'he' => 'he_IL',
+        'it' => 'it_IT',
+        'ja' => 'ja',
+        'ko' => 'ko_KR',
+        'nl' => 'nl_NL',
+        'pl' => 'pl_PL',
+        'pt' => 'pl_PT',
+        'pt_BR' => 'pt_BR',
+        'ru' => 'ru',
+        'sk' => 'sk_SK',
+        'sl' => 'sl_SI',
+        'sv' => 'sv_SE',
+        'uk' => 'uk_UA',
+        'vi' => 'vi_VN',
+        'zh_CN' => 'zh_CN',
+        'zh_TW' => 'zh_TW',
+        'tr' => 'tr_TR',
+    ];
+
     /**
      * Handle an incoming request.
      *
@@ -16,6 +55,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);
@@ -23,8 +63,16 @@ 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);
+        }
+
         app()->setLocale($locale);
         Carbon::setLocale($locale);
+        $this->setSystemDateLocale($locale);
         return $next($request);
     }
 
@@ -45,4 +93,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');
+        }
+    }
 }