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