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