1 <?php namespace BookStack\Http\Middleware;
5 use Illuminate\Http\Request;
11 * Array of right-to-left locales
13 protected $rtlLocales = ['ar', 'he'];
16 * Map of BookStack locale names to best-estimate system locale names.
18 protected $localeMap = [
23 'de_informal' => 'de_DE',
49 * Handle an incoming request.
51 * @param \Illuminate\Http\Request $request
52 * @param \Closure $next
55 public function handle($request, Closure $next)
57 $defaultLang = config('app.locale');
58 config()->set('app.default_locale', $defaultLang);
60 if (user()->isDefault() && config('app.auto_detect_locale')) {
61 $locale = $this->autoDetectLocale($request, $defaultLang);
63 $locale = setting()->getUser(user(), 'language', $defaultLang);
66 config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
69 if (in_array($locale, $this->rtlLocales)) {
70 config()->set('app.rtl', true);
73 app()->setLocale($locale);
74 Carbon::setLocale($locale);
75 $this->setSystemDateLocale($locale);
76 return $next($request);
80 * Autodetect the visitors locale by matching locales in their headers
81 * against the locales supported by BookStack.
82 * @param Request $request
83 * @param string $default
86 protected function autoDetectLocale(Request $request, string $default)
88 $availableLocales = config('app.locales');
89 foreach ($request->getLanguages() as $lang) {
90 if (in_array($lang, $availableLocales)) {
98 * Get the ISO version of a BookStack language name
99 * @param string $locale
102 public function getLocaleIso(string $locale)
104 return $this->localeMap[$locale] ?? $locale;
108 * Set the system date locale for localized date formatting.
109 * Will try both the standard locale name and the UTF8 variant.
110 * @param string $locale
112 protected function setSystemDateLocale(string $locale)
114 $systemLocale = $this->getLocaleIso($locale);
115 $set = setlocale(LC_TIME, $systemLocale);
116 if ($set === false) {
117 setlocale(LC_TIME, $systemLocale . '.utf8');