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);
61 if (in_array($locale, $this->rtlLocales)) {
62 config()->set('app.rtl', true);
65 app()->setLocale($locale);
66 Carbon::setLocale($locale);
67 $this->setSystemDateLocale($locale);
68 return $next($request);
72 * Autodetect the visitors locale by matching locales in their headers
73 * against the locales supported by BookStack.
74 * @param Request $request
75 * @param string $default
78 protected function autoDetectLocale(Request $request, string $default)
80 $availableLocales = config('app.locales');
81 foreach ($request->getLanguages() as $lang) {
82 if (in_array($lang, $availableLocales)) {
90 * Set the system date locale for localized date formatting.
91 * Will try both the standard locale name and the UTF8 variant.
92 * @param string $locale
94 protected function setSystemDateLocale(string $locale)
96 $systemLocale = $this->localeMap[$locale] ?? $locale;
97 $set = setlocale(LC_TIME, $systemLocale);
99 setlocale(LC_TIME, $systemLocale . '.utf8');