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