1 <?php namespace BookStack\Http\Middleware;
5 use Illuminate\Http\Request;
11 * Array of right-to-left locales
14 protected $rtlLocales = ['ar'];
17 * Map of BookStack locale names to best-estimate system locale names.
20 protected $localeMap = [
23 'de_informal' => 'de_DE',
44 * Handle an incoming request.
46 * @param \Illuminate\Http\Request $request
47 * @param \Closure $next
50 public function handle($request, Closure $next)
52 $defaultLang = config('app.locale');
53 config()->set('app.default_locale', $defaultLang);
55 if (user()->isDefault() && config('app.auto_detect_locale')) {
56 $locale = $this->autoDetectLocale($request, $defaultLang);
58 $locale = setting()->getUser(user(), 'language', $defaultLang);
61 config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
64 if (in_array($locale, $this->rtlLocales)) {
65 config()->set('app.rtl', true);
68 app()->setLocale($locale);
69 Carbon::setLocale($locale);
70 $this->setSystemDateLocale($locale);
71 return $next($request);
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
81 protected function autoDetectLocale(Request $request, string $default)
83 $availableLocales = config('app.locales');
84 foreach ($request->getLanguages() as $lang) {
85 if (in_array($lang, $availableLocales)) {
93 * Get the ISO version of a BookStack language name
94 * @param string $locale
97 public function getLocaleIso(string $locale)
99 return $this->localeMap[$locale] ?? $locale;
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
107 protected function setSystemDateLocale(string $locale)
109 $systemLocale = $this->getLocaleIso($locale);
110 $set = setlocale(LC_TIME, $systemLocale);
111 if ($set === false) {
112 setlocale(LC_TIME, $systemLocale . '.utf8');