+
+ /**
+ * Get the locale specifically for the currently logged in user if available.
+ */
+ protected function getUserLocale(Request $request, string $default): string
+ {
+ try {
+ $user = user();
+ } catch (\Exception $exception) {
+ return $default;
+ }
+
+ if ($user->isDefault() && config('app.auto_detect_locale')) {
+ return $this->autoDetectLocale($request, $default);
+ }
+
+ return setting()->getUser($user, 'language', $default);
+ }
+
+ /**
+ * Autodetect the visitors locale by matching locales in their headers
+ * against the locales supported by BookStack.
+ */
+ protected function autoDetectLocale(Request $request, string $default): string
+ {
+ $availableLocales = config('app.locales');
+ foreach ($request->getLanguages() as $lang) {
+ if (in_array($lang, $availableLocales)) {
+ return $lang;
+ }
+ }
+ return $default;
+ }
+
+ /**
+ * Get the ISO version of a BookStack language name
+ */
+ public function getLocaleIso(string $locale): string
+ {
+ return $this->localeMap[$locale] ?? $locale;
+ }
+
+ /**
+ * Set the system date locale for localized date formatting.
+ * Will try both the standard locale name and the UTF8 variant.
+ */
+ protected function setSystemDateLocale(string $locale)
+ {
+ $systemLocale = $this->getLocaleIso($locale);
+ $set = setlocale(LC_TIME, $systemLocale);
+ if ($set === false) {
+ setlocale(LC_TIME, $systemLocale . '.utf8');
+ }
+ }