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