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