]> BookStack Code Mirror - bookstack/blob - app/Translation/LocaleManager.php
WYSIWYG: Updated TinyMCE from 6.5.1 to 6.7.2
[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         'sv'          => 'sv_SE',
62         'tr'          => 'tr_TR',
63         'uk'          => 'uk_UA',
64         'uz'          => 'uz_UZ',
65         'vi'          => 'vi_VN',
66         'zh_CN'       => 'zh_CN',
67         'zh_TW'       => 'zh_TW',
68     ];
69
70     /**
71      * Get the BookStack locale string for the given user.
72      */
73     protected function getLocaleForUser(User $user): string
74     {
75         $default = config('app.default_locale');
76
77         if ($user->isGuest() && config('app.auto_detect_locale')) {
78             return $this->autoDetectLocale(request(), $default);
79         }
80
81         return setting()->getUser($user, 'language', $default);
82     }
83
84     /**
85      * Get a locale definition for the current user.
86      */
87     public function getForUser(User $user): LocaleDefinition
88     {
89         $localeString = $this->getLocaleForUser($user);
90
91         return new LocaleDefinition(
92             $localeString,
93             $this->localeMap[$localeString] ?? $localeString,
94             in_array($localeString, $this->rtlLocales),
95         );
96     }
97
98     /**
99      * Autodetect the visitors locale by matching locales in their headers
100      * against the locales supported by BookStack.
101      */
102     protected function autoDetectLocale(Request $request, string $default): string
103     {
104         $availableLocales = $this->getAllAppLocales();
105
106         foreach ($request->getLanguages() as $lang) {
107             if (in_array($lang, $availableLocales)) {
108                 return $lang;
109             }
110         }
111
112         return $default;
113     }
114
115     /**
116      * Get all the available app-specific level locale strings.
117      */
118     public function getAllAppLocales(): array
119     {
120         return array_keys($this->localeMap);
121     }
122 }