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