3 namespace BookStack\Translation;
5 use BookStack\Users\Models\User;
6 use Illuminate\Http\Request;
11 * Array of right-to-left locale options.
13 protected array $rtlLocales = ['ar', 'fa', 'he'];
16 * Map of BookStack locale names to best-estimate ISO locale names.
17 * Locales can often be found by running `locale -a` on a linux system.
19 * @var array<string, string>
21 protected array $localeMap = [
31 'de_informal' => 'de_DE',
77 * Get the BookStack locale string for the given user.
79 protected function getLocaleForUser(User $user): string
81 $default = config('app.default_locale');
83 if ($user->isGuest() && config('app.auto_detect_locale')) {
84 return $this->autoDetectLocale(request(), $default);
87 return setting()->getUser($user, 'language', $default);
91 * Get a locale definition for the current user.
93 public function getForUser(User $user): LocaleDefinition
95 $localeString = $this->getLocaleForUser($user);
97 return new LocaleDefinition(
99 $this->localeMap[$localeString] ?? $localeString,
100 in_array($localeString, $this->rtlLocales),
105 * Autodetect the visitors locale by matching locales in their headers
106 * against the locales supported by BookStack.
108 protected function autoDetectLocale(Request $request, string $default): string
110 $availableLocales = $this->getAllAppLocales();
112 foreach ($request->getLanguages() as $lang) {
113 if (in_array($lang, $availableLocales)) {
122 * Get all the available app-specific level locale strings.
124 public function getAllAppLocales(): array
126 return array_keys($this->localeMap);