]> BookStack Code Mirror - bookstack/blob - app/Translation/LocaleManager.php
fix Sidebar scrolling at mid-range sceen
[bookstack] / app / Translation / LocaleManager.php
1 <?php
2
3 namespace BookStack\Translation;
4
5 use BookStack\Users\Models\User;
6 use Illuminate\Http\Request;
7
8 class LocaleManager
9 {
10     /**
11      * Array of right-to-left locale options.
12      */
13     protected array $rtlLocales = ['ar', 'fa', 'he'];
14
15     /**
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.
18      *
19      * @var array<string, string>
20      */
21     protected array $localeMap = [
22         'ar'          => 'ar',
23         'bg'          => 'bg_BG',
24         'bs'          => 'bs_BA',
25         'ca'          => 'ca',
26         'cs'          => 'cs_CZ',
27         'cy'          => 'cy_GB',
28         'da'          => 'da_DK',
29         'de'          => 'de_DE',
30         'de_informal' => 'de_DE',
31         'el'          => 'el_GR',
32         'en'          => 'en_GB',
33         'es'          => 'es_ES',
34         'es_AR'       => 'es_AR',
35         'et'          => 'et_EE',
36         'eu'          => 'eu_ES',
37         'fa'          => 'fa_IR',
38         'fr'          => 'fr_FR',
39         'he'          => 'he_IL',
40         'hr'          => 'hr_HR',
41         'hu'          => 'hu_HU',
42         'id'          => 'id_ID',
43         'it'          => 'it_IT',
44         'ja'          => 'ja',
45         'ka'          => 'ka_GE',
46         'ko'          => 'ko_KR',
47         'lt'          => 'lt_LT',
48         'lv'          => 'lv_LV',
49         'nb'          => 'nb_NO',
50         'nl'          => 'nl_NL',
51         'pl'          => 'pl_PL',
52         'pt'          => 'pt_PT',
53         'pt_BR'       => 'pt_BR',
54         'ro'          => 'ro_RO',
55         'ru'          => 'ru',
56         'sk'          => 'sk_SK',
57         'sl'          => 'sl_SI',
58         'sv'          => 'sv_SE',
59         'tr'          => 'tr_TR',
60         'uk'          => 'uk_UA',
61         'uz'          => 'uz_UZ',
62         'vi'          => 'vi_VN',
63         'zh_CN'       => 'zh_CN',
64         'zh_TW'       => 'zh_TW',
65     ];
66
67     /**
68      * Get the BookStack locale string for the given user.
69      */
70     protected function getLocaleForUser(User $user): string
71     {
72         $default = config('app.default_locale');
73
74         if ($user->isGuest() && config('app.auto_detect_locale')) {
75             return $this->autoDetectLocale(request(), $default);
76         }
77
78         return setting()->getUser($user, 'language', $default);
79     }
80
81     /**
82      * Get a locale definition for the current user.
83      */
84     public function getForUser(User $user): LocaleDefinition
85     {
86         $localeString = $this->getLocaleForUser($user);
87
88         return new LocaleDefinition(
89             $localeString,
90             $this->localeMap[$localeString] ?? $localeString,
91             in_array($localeString, $this->rtlLocales),
92         );
93     }
94
95     /**
96      * Autodetect the visitors locale by matching locales in their headers
97      * against the locales supported by BookStack.
98      */
99     protected function autoDetectLocale(Request $request, string $default): string
100     {
101         $availableLocales = $this->getAllAppLocales();
102
103         foreach ($request->getLanguages() as $lang) {
104             if (in_array($lang, $availableLocales)) {
105                 return $lang;
106             }
107         }
108
109         return $default;
110     }
111
112     /**
113      * Get all the available app-specific level locale strings.
114      */
115     public function getAllAppLocales(): array
116     {
117         return array_keys($this->localeMap);
118     }
119 }