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 = [
30 'de_informal' => 'de_DE',
71 * Get the BookStack locale string for the given user.
73 protected function getLocaleForUser(User $user): string
75 $default = config('app.default_locale');
77 if ($user->isGuest() && config('app.auto_detect_locale')) {
78 return $this->autoDetectLocale(request(), $default);
81 return setting()->getUser($user, 'language', $default);
85 * Get a locale definition for the current user.
87 public function getForUser(User $user): LocaleDefinition
89 $localeString = $this->getLocaleForUser($user);
91 return new LocaleDefinition(
93 $this->localeMap[$localeString] ?? $localeString,
94 in_array($localeString, $this->rtlLocales),
99 * Autodetect the visitors locale by matching locales in their headers
100 * against the locales supported by BookStack.
102 protected function autoDetectLocale(Request $request, string $default): string
104 $availableLocales = $this->getAllAppLocales();
106 foreach ($request->getLanguages() as $lang) {
107 if (in_array($lang, $availableLocales)) {
116 * Get all the available app-specific level locale strings.
118 public function getAllAppLocales(): array
120 return array_keys($this->localeMap);