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