]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/Localization.php
Adding APP_VIEWS_BOOKSHELF to .ENV
[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      */
13     protected $rtlLocales = ['ar', 'he'];
14
15     /**
16      * Map of BookStack locale names to best-estimate system locale names.
17      */
18     protected $localeMap = [
19         'ar' => 'ar',
20         'bg' => 'bg_BG',
21         'da' => 'da_DK',
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         'he' => 'he_IL',
29         'it' => 'it_IT',
30         'ja' => 'ja',
31         'ko' => 'ko_KR',
32         'nl' => 'nl_NL',
33         'nb' => 'nb_NO',
34         'pl' => 'pl_PL',
35         'pt' => 'pl_PT',
36         'pt_BR' => 'pt_BR',
37         'ru' => 'ru',
38         'sk' => 'sk_SK',
39         'sl' => 'sl_SI',
40         'sv' => 'sv_SE',
41         'uk' => 'uk_UA',
42         'vi' => 'vi_VN',
43         'zh_CN' => 'zh_CN',
44         'zh_TW' => 'zh_TW',
45         'tr' => 'tr_TR',
46     ];
47
48     /**
49      * Handle an incoming request.
50      *
51      * @param  \Illuminate\Http\Request  $request
52      * @param  \Closure  $next
53      * @return mixed
54      */
55     public function handle($request, Closure $next)
56     {
57         $defaultLang = config('app.locale');
58         config()->set('app.default_locale', $defaultLang);
59
60         $locale = $this->getUserLocale($request, $defaultLang);
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      * Get the locale specifically for the currently logged in user if available.
76      */
77     protected function getUserLocale(Request $request, string $default): string
78     {
79         try {
80             $user = user();
81         } catch (\Exception $exception) {
82             return $default;
83         }
84
85         if ($user->isDefault() && config('app.auto_detect_locale')) {
86             return $this->autoDetectLocale($request, $default);
87         }
88
89         return setting()->getUser($user, 'language', $default);
90     }
91
92     /**
93      * Autodetect the visitors locale by matching locales in their headers
94      * against the locales supported by BookStack.
95      */
96     protected function autoDetectLocale(Request $request, string $default): string
97     {
98         $availableLocales = config('app.locales');
99         foreach ($request->getLanguages() as $lang) {
100             if (in_array($lang, $availableLocales)) {
101                 return $lang;
102             }
103         }
104         return $default;
105     }
106
107     /**
108      * Get the ISO version of a BookStack language name
109      */
110     public function getLocaleIso(string $locale): string
111     {
112         return $this->localeMap[$locale] ?? $locale;
113     }
114
115     /**
116      * Set the system date locale for localized date formatting.
117      * Will try both the standard locale name and the UTF8 variant.
118      */
119     protected function setSystemDateLocale(string $locale)
120     {
121         $systemLocale = $this->getLocaleIso($locale);
122         $set = setlocale(LC_TIME, $systemLocale);
123         if ($set === false) {
124             setlocale(LC_TIME, $systemLocale . '.utf8');
125         }
126     }
127 }