]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Localization.php
Fix "Ubunto Mono" $mono type misspelling
[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      * @var array
13      */
14     protected $rtlLocales = ['ar', 'he'];
15
16     /**
17      * Map of BookStack locale names to best-estimate system locale names.
18      * @var array
19      */
20     protected $localeMap = [
21         'ar' => 'ar',
22         'bg' => 'bg_BG',
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         'it' => 'it_IT',
32         'ja' => 'ja',
33         'ko' => 'ko_KR',
34         'nl' => 'nl_NL',
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         if (user()->isDefault() && config('app.auto_detect_locale')) {
62             $locale = $this->autoDetectLocale($request, $defaultLang);
63         } else {
64             $locale = setting()->getUser(user(), 'language', $defaultLang);
65         }
66
67         config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
68
69         // Set text direction
70         if (in_array($locale, $this->rtlLocales)) {
71             config()->set('app.rtl', true);
72         }
73
74         app()->setLocale($locale);
75         Carbon::setLocale($locale);
76         $this->setSystemDateLocale($locale);
77         return $next($request);
78     }
79
80     /**
81      * Autodetect the visitors locale by matching locales in their headers
82      * against the locales supported by BookStack.
83      * @param Request $request
84      * @param string $default
85      * @return string
86      */
87     protected function autoDetectLocale(Request $request, string $default)
88     {
89         $availableLocales = config('app.locales');
90         foreach ($request->getLanguages() as $lang) {
91             if (in_array($lang, $availableLocales)) {
92                 return $lang;
93             }
94         }
95         return $default;
96     }
97
98     /**
99      * Get the ISO version of a BookStack language name
100      * @param  string $locale
101      * @return string
102      */
103     public function getLocaleIso(string $locale)
104     {
105         return $this->localeMap[$locale] ?? $locale;
106     }
107
108     /**
109      * Set the system date locale for localized date formatting.
110      * Will try both the standard locale name and the UTF8 variant.
111      * @param string $locale
112      */
113     protected function setSystemDateLocale(string $locale)
114     {
115         $systemLocale = $this->getLocaleIso($locale);
116         $set = setlocale(LC_TIME, $systemLocale);
117         if ($set === false) {
118             setlocale(LC_TIME, $systemLocale . '.utf8');
119         }
120     }
121 }