]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Localization.php
Merge branch 'master' of git://github.com/rondaa/BookStack into rondaa-master
[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         'da' => 'da_DK',
22         'de' => 'de_DE',
23         'de_informal' => 'de_DE',
24         'en' => 'en_GB',
25         'es' => 'es_ES',
26         'es_AR' => 'es_AR',
27         'fr' => 'fr_FR',
28         'he' => 'he_IL',
29         'it' => 'it_IT',
30         'ja' => 'ja',
31         'ko' => 'ko_KR',
32         'nl' => 'nl_NL',
33         'nb' => 'nb_NO',
34         'pl' => 'pl_PL',
35         'pt' => 'pl_PT',
36         'pt_BR' => 'pt_BR',
37         'ru' => 'ru',
38         'sk' => 'sk_SK',
39         'sl' => 'sl_SI',
40         'sv' => 'sv_SE',
41         'uk' => 'uk_UA',
42         'vi' => 'vi_VN',
43         'zh_CN' => 'zh_CN',
44         'zh_TW' => 'zh_TW',
45         'tr' => 'tr_TR',
46     ];
47
48     /**
49      * Handle an incoming request.
50      *
51      * @param  \Illuminate\Http\Request  $request
52      * @param  \Closure  $next
53      * @return mixed
54      */
55     public function handle($request, Closure $next)
56     {
57         $defaultLang = config('app.locale');
58         config()->set('app.default_locale', $defaultLang);
59
60         if (user()->isDefault() && config('app.auto_detect_locale')) {
61             $locale = $this->autoDetectLocale($request, $defaultLang);
62         } else {
63             $locale = setting()->getUser(user(), 'language', $defaultLang);
64         }
65
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      * Autodetect the visitors locale by matching locales in their headers
81      * against the locales supported by BookStack.
82      * @param Request $request
83      * @param string $default
84      * @return string
85      */
86     protected function autoDetectLocale(Request $request, string $default)
87     {
88         $availableLocales = config('app.locales');
89         foreach ($request->getLanguages() as $lang) {
90             if (in_array($lang, $availableLocales)) {
91                 return $lang;
92             }
93         }
94         return $default;
95     }
96
97     /**
98      * Get the ISO version of a BookStack language name
99      * @param  string $locale
100      * @return string
101      */
102     public function getLocaleIso(string $locale)
103     {
104         return $this->localeMap[$locale] ?? $locale;
105     }
106
107     /**
108      * Set the system date locale for localized date formatting.
109      * Will try both the standard locale name and the UTF8 variant.
110      * @param string $locale
111      */
112     protected function setSystemDateLocale(string $locale)
113     {
114         $systemLocale = $this->getLocaleIso($locale);
115         $set = setlocale(LC_TIME, $systemLocale);
116         if ($set === false) {
117             setlocale(LC_TIME, $systemLocale . '.utf8');
118         }
119     }
120 }