]> BookStack Code Mirror - bookstack/blob - app/Translation/LocaleManager.php
PHP: Updated DOMPDF version
[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         'lt'          => 'lt_LT',
51         'lv'          => 'lv_LV',
52         'nb'          => 'nb_NO',
53         'nl'          => 'nl_NL',
54         'nn'          => 'nn_NO',
55         'pl'          => 'pl_PL',
56         'pt'          => 'pt_PT',
57         'pt_BR'       => 'pt_BR',
58         'ro'          => 'ro_RO',
59         'ru'          => 'ru',
60         'sk'          => 'sk_SK',
61         'sl'          => 'sl_SI',
62         'sq'          => 'sq_AL',
63         'sr'          => 'sr_RS',
64         'sv'          => 'sv_SE',
65         'tk'          => 'tk_TM',
66         'tr'          => 'tr_TR',
67         'uk'          => 'uk_UA',
68         'uz'          => 'uz_UZ',
69         'vi'          => 'vi_VN',
70         'zh_CN'       => 'zh_CN',
71         'zh_TW'       => 'zh_TW',
72     ];
73
74     /**
75      * Get the BookStack locale string for the given user.
76      */
77     protected function getLocaleForUser(User $user): string
78     {
79         $default = config('app.default_locale');
80
81         if ($user->isGuest() && config('app.auto_detect_locale')) {
82             return $this->autoDetectLocale(request(), $default);
83         }
84
85         return setting()->getUser($user, 'language', $default);
86     }
87
88     /**
89      * Get a locale definition for the current user.
90      */
91     public function getForUser(User $user): LocaleDefinition
92     {
93         $localeString = $this->getLocaleForUser($user);
94
95         return new LocaleDefinition(
96             $localeString,
97             $this->localeMap[$localeString] ?? $localeString,
98             in_array($localeString, $this->rtlLocales),
99         );
100     }
101
102     /**
103      * Autodetect the visitors locale by matching locales in their headers
104      * against the locales supported by BookStack.
105      */
106     protected function autoDetectLocale(Request $request, string $default): string
107     {
108         $availableLocales = $this->getAllAppLocales();
109
110         foreach ($request->getLanguages() as $lang) {
111             if (in_array($lang, $availableLocales)) {
112                 return $lang;
113             }
114         }
115
116         return $default;
117     }
118
119     /**
120      * Get all the available app-specific level locale strings.
121      */
122     public function getAllAppLocales(): array
123     {
124         return array_keys($this->localeMap);
125     }
126 }