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',
75 * Get the BookStack locale string for the given user.
77 protected function getLocaleForUser(User $user): string
79 $default = config('app.default_locale');
81 if ($user->isGuest() && config('app.auto_detect_locale')) {
82 return $this->autoDetectLocale(request(), $default);
85 return setting()->getUser($user, 'language', $default);
89 * Get a locale definition for the current user.
91 public function getForUser(User $user): LocaleDefinition
93 $localeString = $this->getLocaleForUser($user);
95 return new LocaleDefinition(
97 $this->localeMap[$localeString] ?? $localeString,
98 in_array($localeString, $this->rtlLocales),
103 * Autodetect the visitors locale by matching locales in their headers
104 * against the locales supported by BookStack.
106 protected function autoDetectLocale(Request $request, string $default): string
108 $availableLocales = $this->getAllAppLocales();
110 foreach ($request->getLanguages() as $lang) {
111 if (in_array($lang, $availableLocales)) {
120 * Get all the available app-specific level locale strings.
122 public function getAllAppLocales(): array
124 return array_keys($this->localeMap);