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