]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Localization.php
added rtl support for hebrew + added to localMap
[bookstack] / app / Http / Middleware / Localization.php
1 <?php namespace BookStack\Http\Middleware;
2
3 use Carbon\Carbon;
4 use Closure;
5 use Illuminate\Http\Request;
6
7 class Localization
8 {
9
10     /**
11      * Array of right-to-left locales
12      * @var array
13      */
14     protected $rtlLocales = ['ar', 'he'];
15
16     /**
17      * Map of BookStack locale names to best-estimate system locale names.
18      * @var array
19      */
20     protected $localeMap = [
21         'ar' => 'ar',
22         'de' => 'de_DE',
23         'de_informal' => 'de_DE',
24         'en' => 'en_GB',
25         'es' => 'es_ES',
26         'es_AR' => 'es_AR',
27         'fr' => 'fr_FR',
28         'he' => 'he_IL',
29         'it' => 'it_IT',
30         'ja' => 'ja',
31         'kr' => 'ko_KR',
32         'nl' => 'nl_NL',
33         'pl' => 'pl_PL',
34         'pt_BR' => 'pt_BR',
35         'ru' => 'ru',
36         'sk' => 'sk_SK',
37         'sv' => 'sv_SE',
38         'uk' => 'uk_UA',
39         'zh_CN' => 'zh_CN',
40         'zh_TW' => 'zh_TW',
41     ];
42
43     /**
44      * Handle an incoming request.
45      *
46      * @param  \Illuminate\Http\Request  $request
47      * @param  \Closure  $next
48      * @return mixed
49      */
50     public function handle($request, Closure $next)
51     {
52         $defaultLang = config('app.locale');
53         config()->set('app.default_locale', $defaultLang);
54
55         if (user()->isDefault() && config('app.auto_detect_locale')) {
56             $locale = $this->autoDetectLocale($request, $defaultLang);
57         } else {
58             $locale = setting()->getUser(user(), 'language', $defaultLang);
59         }
60
61         // Set text direction
62         if (in_array($locale, $this->rtlLocales)) {
63             config()->set('app.rtl', true);
64         }
65
66         app()->setLocale($locale);
67         Carbon::setLocale($locale);
68         $this->setSystemDateLocale($locale);
69         return $next($request);
70     }
71
72     /**
73      * Autodetect the visitors locale by matching locales in their headers
74      * against the locales supported by BookStack.
75      * @param Request $request
76      * @param string $default
77      * @return string
78      */
79     protected function autoDetectLocale(Request $request, string $default)
80     {
81         $availableLocales = config('app.locales');
82         foreach ($request->getLanguages() as $lang) {
83             if (in_array($lang, $availableLocales)) {
84                 return $lang;
85             }
86         }
87         return $default;
88     }
89
90     /**
91      * Set the system date locale for localized date formatting.
92      * Will try both the standard locale name and the UTF8 variant.
93      * @param string $locale
94      */
95     protected function setSystemDateLocale(string $locale)
96     {
97         $systemLocale = $this->localeMap[$locale] ?? $locale;
98         $set = setlocale(LC_TIME, $systemLocale);
99         if ($set === false) {
100             setlocale(LC_TIME, $systemLocale . '.utf8');
101         }
102     }
103 }