1 <?php namespace BookStack\Http\Middleware;
5 use Illuminate\Http\Request;
11 * Array of right-to-left locales
14 protected $rtlLocales = ['ar', 'he'];
17 * Map of BookStack locale names to best-estimate system locale names.
20 protected $localeMap = [
25 'de_informal' => 'de_DE',
51 * Handle an incoming request.
53 * @param \Illuminate\Http\Request $request
54 * @param \Closure $next
57 public function handle($request, Closure $next)
59 $defaultLang = config('app.locale');
60 config()->set('app.default_locale', $defaultLang);
62 if (user()->isDefault() && config('app.auto_detect_locale')) {
63 $locale = $this->autoDetectLocale($request, $defaultLang);
65 $locale = setting()->getUser(user(), 'language', $defaultLang);
68 config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
71 if (in_array($locale, $this->rtlLocales)) {
72 config()->set('app.rtl', true);
75 app()->setLocale($locale);
76 Carbon::setLocale($locale);
77 $this->setSystemDateLocale($locale);
78 return $next($request);
82 * Autodetect the visitors locale by matching locales in their headers
83 * against the locales supported by BookStack.
84 * @param Request $request
85 * @param string $default
88 protected function autoDetectLocale(Request $request, string $default)
90 $availableLocales = config('app.locales');
91 foreach ($request->getLanguages() as $lang) {
92 if (in_array($lang, $availableLocales)) {
100 * Get the ISO version of a BookStack language name
101 * @param string $locale
104 public function getLocaleIso(string $locale)
106 return $this->localeMap[$locale] ?? $locale;
110 * Set the system date locale for localized date formatting.
111 * Will try both the standard locale name and the UTF8 variant.
112 * @param string $locale
114 protected function setSystemDateLocale(string $locale)
116 $systemLocale = $this->getLocaleIso($locale);
117 $set = setlocale(LC_TIME, $systemLocale);
118 if ($set === false) {
119 setlocale(LC_TIME, $systemLocale . '.utf8');