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