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',
43 * Handle an incoming request.
45 * @param \Illuminate\Http\Request $request
46 * @param \Closure $next
49 public function handle($request, Closure $next)
51 $defaultLang = config('app.locale');
52 config()->set('app.default_locale', $defaultLang);
54 if (user()->isDefault() && config('app.auto_detect_locale')) {
55 $locale = $this->autoDetectLocale($request, $defaultLang);
57 $locale = setting()->getUser(user(), 'language', $defaultLang);
60 config()->set('app.lang', $this->getLocaleIso($locale));
63 if (in_array($locale, $this->rtlLocales)) {
64 config()->set('app.rtl', true);
67 app()->setLocale($locale);
68 Carbon::setLocale($locale);
69 $this->setSystemDateLocale($locale);
70 return $next($request);
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
80 protected function autoDetectLocale(Request $request, string $default)
82 $availableLocales = config('app.locales');
83 foreach ($request->getLanguages() as $lang) {
84 if (in_array($lang, $availableLocales)) {
92 * Get the ISO version of a BookStack language name
93 * @param string $locale
96 public function getLocaleIso(string $locale)
98 return $this->localeMap[$locale] ?? $locale;
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
106 protected function setSystemDateLocale(string $locale)
108 $systemLocale = $this->getLocaleIso($locale);
109 $set = setlocale(LC_TIME, $systemLocale);
110 if ($set === false) {
111 setlocale(LC_TIME, $systemLocale . '.utf8');