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