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