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