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