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