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 = [
24 'de_informal' => 'de_DE',
47 * Handle an incoming request.
49 * @param \Illuminate\Http\Request $request
50 * @param \Closure $next
53 public function handle($request, Closure $next)
55 $defaultLang = config('app.locale');
56 config()->set('app.default_locale', $defaultLang);
58 if (user()->isDefault() && config('app.auto_detect_locale')) {
59 $locale = $this->autoDetectLocale($request, $defaultLang);
61 $locale = setting()->getUser(user(), 'language', $defaultLang);
64 config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
67 if (in_array($locale, $this->rtlLocales)) {
68 config()->set('app.rtl', true);
71 app()->setLocale($locale);
72 Carbon::setLocale($locale);
73 $this->setSystemDateLocale($locale);
74 return $next($request);
78 * Autodetect the visitors locale by matching locales in their headers
79 * against the locales supported by BookStack.
80 * @param Request $request
81 * @param string $default
84 protected function autoDetectLocale(Request $request, string $default)
86 $availableLocales = config('app.locales');
87 foreach ($request->getLanguages() as $lang) {
88 if (in_array($lang, $availableLocales)) {
96 * Get the ISO version of a BookStack language name
97 * @param string $locale
100 public function getLocaleIso(string $locale)
102 return $this->localeMap[$locale] ?? $locale;
106 * Set the system date locale for localized date formatting.
107 * Will try both the standard locale name and the UTF8 variant.
108 * @param string $locale
110 protected function setSystemDateLocale(string $locale)
112 $systemLocale = $this->getLocaleIso($locale);
113 $set = setlocale(LC_TIME, $systemLocale);
114 if ($set === false) {
115 setlocale(LC_TIME, $systemLocale . '.utf8');