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 = [
24 'de_informal' => 'de_DE',
48 * Handle an incoming request.
50 * @param \Illuminate\Http\Request $request
51 * @param \Closure $next
54 public function handle($request, Closure $next)
56 $defaultLang = config('app.locale');
57 config()->set('app.default_locale', $defaultLang);
59 if (user()->isDefault() && config('app.auto_detect_locale')) {
60 $locale = $this->autoDetectLocale($request, $defaultLang);
62 $locale = setting()->getUser(user(), 'language', $defaultLang);
65 config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
68 if (in_array($locale, $this->rtlLocales)) {
69 config()->set('app.rtl', true);
72 app()->setLocale($locale);
73 Carbon::setLocale($locale);
74 $this->setSystemDateLocale($locale);
75 return $next($request);
79 * Autodetect the visitors locale by matching locales in their headers
80 * against the locales supported by BookStack.
81 * @param Request $request
82 * @param string $default
85 protected function autoDetectLocale(Request $request, string $default)
87 $availableLocales = config('app.locales');
88 foreach ($request->getLanguages() as $lang) {
89 if (in_array($lang, $availableLocales)) {
97 * Get the ISO version of a BookStack language name
98 * @param string $locale
101 public function getLocaleIso(string $locale)
103 return $this->localeMap[$locale] ?? $locale;
107 * Set the system date locale for localized date formatting.
108 * Will try both the standard locale name and the UTF8 variant.
109 * @param string $locale
111 protected function setSystemDateLocale(string $locale)
113 $systemLocale = $this->getLocaleIso($locale);
114 $set = setlocale(LC_TIME, $systemLocale);
115 if ($set === false) {
116 setlocale(LC_TIME, $systemLocale . '.utf8');