]> BookStack Code Mirror - bookstack/blob - app/Translation/LocaleManager.php
Testing: Fixed issues during pre-release testing
[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         'bn'          => 'bn_BD',
25         'bs'          => 'bs_BA',
26         'ca'          => 'ca',
27         'cs'          => 'cs_CZ',
28         'cy'          => 'cy_GB',
29         'da'          => 'da_DK',
30         'de'          => 'de_DE',
31         'de_informal' => 'de_DE',
32         'el'          => 'el_GR',
33         'en'          => 'en_GB',
34         'es'          => 'es_ES',
35         'es_AR'       => 'es_AR',
36         'et'          => 'et_EE',
37         'eu'          => 'eu_ES',
38         'fa'          => 'fa_IR',
39         'fi'          => 'fi_FI',
40         'fr'          => 'fr_FR',
41         'he'          => 'he_IL',
42         'hr'          => 'hr_HR',
43         'hu'          => 'hu_HU',
44         'id'          => 'id_ID',
45         'is'          => 'is_IS',
46         'it'          => 'it_IT',
47         'ja'          => 'ja',
48         'ka'          => 'ka_GE',
49         'ko'          => 'ko_KR',
50         'ku'          => 'ku_TR',
51         'lt'          => 'lt_LT',
52         'lv'          => 'lv_LV',
53         'nb'          => 'nb_NO',
54         'nl'          => 'nl_NL',
55         'nn'          => 'nn_NO',
56         'pl'          => 'pl_PL',
57         'pt'          => 'pt_PT',
58         'pt_BR'       => 'pt_BR',
59         'ro'          => 'ro_RO',
60         'ru'          => 'ru',
61         'sk'          => 'sk_SK',
62         'sl'          => 'sl_SI',
63         'sq'          => 'sq_AL',
64         'sr'          => 'sr_RS',
65         'sv'          => 'sv_SE',
66         'tk'          => 'tk_TM',
67         'tr'          => 'tr_TR',
68         'uk'          => 'uk_UA',
69         'uz'          => 'uz_UZ',
70         'vi'          => 'vi_VN',
71         'zh_CN'       => 'zh_CN',
72         'zh_TW'       => 'zh_TW',
73     ];
74
75     /**
76      * Get the BookStack locale string for the given user.
77      */
78     protected function getLocaleForUser(User $user): string
79     {
80         $default = config('app.default_locale');
81
82         if ($user->isGuest() && config('app.auto_detect_locale')) {
83             return $this->autoDetectLocale(request(), $default);
84         }
85
86         return setting()->getUser($user, 'language', $default);
87     }
88
89     /**
90      * Get a locale definition for the current user.
91      */
92     public function getForUser(User $user): LocaleDefinition
93     {
94         $localeString = $this->getLocaleForUser($user);
95
96         return new LocaleDefinition(
97             $localeString,
98             $this->localeMap[$localeString] ?? $localeString,
99             in_array($localeString, $this->rtlLocales),
100         );
101     }
102
103     /**
104      * Autodetect the visitors locale by matching locales in their headers
105      * against the locales supported by BookStack.
106      */
107     protected function autoDetectLocale(Request $request, string $default): string
108     {
109         $availableLocales = $this->getAllAppLocales();
110
111         foreach ($request->getLanguages() as $lang) {
112             if (in_array($lang, $availableLocales)) {
113                 return $lang;
114             }
115         }
116
117         return $default;
118     }
119
120     /**
121      * Get all the available app-specific level locale strings.
122      */
123     public function getAllAppLocales(): array
124     {
125         return array_keys($this->localeMap);
126     }
127 }