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