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