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',
50 * Handle an incoming request.
52 * @param \Illuminate\Http\Request $request
53 * @param \Closure $next
56 public function handle($request, Closure $next)
58 $defaultLang = config('app.locale');
59 config()->set('app.default_locale', $defaultLang);
61 if (user()->isDefault() && config('app.auto_detect_locale')) {
62 $locale = $this->autoDetectLocale($request, $defaultLang);
64 $locale = setting()->getUser(user(), 'language', $defaultLang);
67 config()->set('app.lang', str_replace('_', '-', $this->getLocaleIso($locale)));
70 if (in_array($locale, $this->rtlLocales)) {
71 config()->set('app.rtl', true);
74 app()->setLocale($locale);
75 Carbon::setLocale($locale);
76 $this->setSystemDateLocale($locale);
77 return $next($request);
81 * Autodetect the visitors locale by matching locales in their headers
82 * against the locales supported by BookStack.
83 * @param Request $request
84 * @param string $default
87 protected function autoDetectLocale(Request $request, string $default)
89 $availableLocales = config('app.locales');
90 foreach ($request->getLanguages() as $lang) {
91 if (in_array($lang, $availableLocales)) {
99 * Get the ISO version of a BookStack language name
100 * @param string $locale
103 public function getLocaleIso(string $locale)
105 return $this->localeMap[$locale] ?? $locale;
109 * Set the system date locale for localized date formatting.
110 * Will try both the standard locale name and the UTF8 variant.
111 * @param string $locale
113 protected function setSystemDateLocale(string $locale)
115 $systemLocale = $this->getLocaleIso($locale);
116 $set = setlocale(LC_TIME, $systemLocale);
117 if ($set === false) {
118 setlocale(LC_TIME, $systemLocale . '.utf8');