]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Localization.php
Merge pull request #2820 from BookStackApp/analysis-6470L9
[bookstack] / app / Http / Middleware / Localization.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use Carbon\Carbon;
6 use Closure;
7 use Illuminate\Http\Request;
8
9 class Localization
10 {
11     /**
12      * Array of right-to-left locales.
13      */
14     protected $rtlLocales = ['ar', 'he'];
15
16     /**
17      * Map of BookStack locale names to best-estimate system locale names.
18      */
19     protected $localeMap = [
20         'ar'          => 'ar',
21         'bg'          => 'bg_BG',
22         'bs'          => 'bs_BA',
23         'ca'          => 'ca',
24         'da'          => 'da_DK',
25         'de'          => 'de_DE',
26         'de_informal' => 'de_DE',
27         'en'          => 'en_GB',
28         'es'          => 'es_ES',
29         'es_AR'       => 'es_AR',
30         'fr'          => 'fr_FR',
31         'he'          => 'he_IL',
32         'hr'          => 'hr_HR',
33         'id'          => 'id_ID',
34         'it'          => 'it_IT',
35         'ja'          => 'ja',
36         'ko'          => 'ko_KR',
37         'lv'          => 'lv_LV',
38         'nl'          => 'nl_NL',
39         'nb'          => 'nb_NO',
40         'pl'          => 'pl_PL',
41         'pt'          => 'pt_PT',
42         'pt_BR'       => 'pt_BR',
43         'ru'          => 'ru',
44         'sk'          => 'sk_SK',
45         'sl'          => 'sl_SI',
46         'sv'          => 'sv_SE',
47         'uk'          => 'uk_UA',
48         'vi'          => 'vi_VN',
49         'zh_CN'       => 'zh_CN',
50         'zh_TW'       => 'zh_TW',
51         'tr'          => 'tr_TR',
52     ];
53
54     /**
55      * Handle an incoming request.
56      *
57      * @param \Illuminate\Http\Request $request
58      * @param \Closure                 $next
59      *
60      * @return mixed
61      */
62     public function handle($request, Closure $next)
63     {
64         $defaultLang = config('app.locale');
65         config()->set('app.default_locale', $defaultLang);
66
67         $locale = $this->getUserLocale($request, $defaultLang);
68         config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
69
70         // Set text direction
71         if (in_array($locale, $this->rtlLocales)) {
72             config()->set('app.rtl', true);
73         }
74
75         app()->setLocale($locale);
76         Carbon::setLocale($locale);
77         $this->setSystemDateLocale($locale);
78
79         return $next($request);
80     }
81
82     /**
83      * Get the locale specifically for the currently logged in user if available.
84      */
85     protected function getUserLocale(Request $request, string $default): string
86     {
87         try {
88             $user = user();
89         } catch (\Exception $exception) {
90             return $default;
91         }
92
93         if ($user->isDefault() && config('app.auto_detect_locale')) {
94             return $this->autoDetectLocale($request, $default);
95         }
96
97         return setting()->getUser($user, 'language', $default);
98     }
99
100     /**
101      * Autodetect the visitors locale by matching locales in their headers
102      * against the locales supported by BookStack.
103      */
104     protected function autoDetectLocale(Request $request, string $default): string
105     {
106         $availableLocales = config('app.locales');
107         foreach ($request->getLanguages() as $lang) {
108             if (in_array($lang, $availableLocales)) {
109                 return $lang;
110             }
111         }
112
113         return $default;
114     }
115
116     /**
117      * Get the ISO version of a BookStack language name.
118      */
119     public function getLocaleIso(string $locale): string
120     {
121         return $this->localeMap[$locale] ?? $locale;
122     }
123
124     /**
125      * Set the system date locale for localized date formatting.
126      * Will try both the standard locale name and the UTF8 variant.
127      */
128     protected function setSystemDateLocale(string $locale)
129     {
130         $systemLocale = $this->getLocaleIso($locale);
131         $set = setlocale(LC_TIME, $systemLocale);
132         if ($set === false) {
133             setlocale(LC_TIME, $systemLocale . '.utf8');
134         }
135     }
136 }