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