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',
76 * Get the BookStack locale string for the given user.
78 protected function getLocaleForUser(User $user): string
80 $default = config('app.default_locale');
82 if ($user->isGuest() && config('app.auto_detect_locale')) {
83 return $this->autoDetectLocale(request(), $default);
86 return setting()->getUser($user, 'language', $default);
90 * Get a locale definition for the current user.
92 public function getForUser(User $user): LocaleDefinition
94 $localeString = $this->getLocaleForUser($user);
96 return new LocaleDefinition(
98 $this->localeMap[$localeString] ?? $localeString,
99 in_array($localeString, $this->rtlLocales),
104 * Autodetect the visitors locale by matching locales in their headers
105 * against the locales supported by BookStack.
107 protected function autoDetectLocale(Request $request, string $default): string
109 $availableLocales = $this->getAllAppLocales();
111 foreach ($request->getLanguages() as $lang) {
112 if (in_array($lang, $availableLocales)) {
121 * Get all the available app-specific level locale strings.
123 public function getAllAppLocales(): array
125 return array_keys($this->localeMap);