-
Notifications
You must be signed in to change notification settings - Fork 642
/
Copy pathI18N.php
350 lines (301 loc) · 10.1 KB
/
I18N.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\i18n;
use Craft;
use ResourceBundle;
use yii\base\Exception;
/**
* @inheritdoc
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0.0
*/
class I18N extends \yii\i18n\I18N
{
/**
* @var array|null All of the known locales
* @see getAllLocales()
*/
private ?array $_allLocaleIds = null;
/**
* @var bool[]
* @see getAppLocaleIds()
*/
private array $_appLocaleIds;
/**
* @var Locale[]
* @see getAppLocales()
*/
private array $_appLocales;
/**
* @var bool|null Whether [[translate()]] should wrap translations with `@` characters
*/
private ?bool $_translationDebugOutput = null;
/**
* Returns whether the [Intl extension](https://php.net/manual/en/book.intl.php) is loaded.
*
* @return bool Whether the Intl extension is loaded.
* @deprecated in 4.0.0. The Intl extension is now required.
*/
public function getIsIntlLoaded(): bool
{
return true;
}
/**
* Returns a locale by its ID.
*
* @param string $localeId
* @return Locale
*/
public function getLocaleById(string $localeId): Locale
{
$generalConfig = Craft::$app->getConfig()->getGeneral();
return new Locale($localeId, $generalConfig->localeAliases[$localeId] ?? []);
}
/**
* Returns an array of all known locale IDs, according to the Intl extension.
*
* @return array An array of locale IDs.
* @link https://php.net/manual/en/resourcebundle.locales.php
*/
public function getAllLocaleIds(): array
{
if (!isset($this->_allLocaleIds)) {
$this->_allLocaleIds = ResourceBundle::getLocales('');
// Hyphens, not underscores
foreach ($this->_allLocaleIds as $i => $locale) {
$this->_allLocaleIds[$i] = str_replace('_', '-', $locale);
}
// Merge in any custom aliases
$generalConfig = Craft::$app->getConfig()->getGeneral();
if (!empty($generalConfig->localeAliases)) {
$this->_allLocaleIds = array_merge($this->_allLocaleIds, array_keys($generalConfig->localeAliases));
$this->_allLocaleIds = array_unique($this->_allLocaleIds);
sort($this->_allLocaleIds);
}
}
return $this->_allLocaleIds;
}
/**
* Returns an array of all known locales.
*
* @return Locale[] An array of [[Locale]] objects.
* @see getAllLocaleIds()
*/
public function getAllLocales(): array
{
$locales = [];
$localeIds = $this->getAllLocaleIds();
$generalConfig = Craft::$app->getConfig()->getGeneral();
foreach ($localeIds as $localeId) {
$locales[] = new Locale($localeId, $generalConfig->localeAliases[$localeId] ?? []);
}
return $locales;
}
// Application Locales
// -------------------------------------------------------------------------
/**
* Returns an array of locales that Craft is translated into. The list of locales is based on whatever files exist
* in `vendor/craftcms/cms/src/translations/`.
*
* @return Locale[] An array of [[Locale]] objects.
* @throws Exception in case of failure
*/
public function getAppLocales(): array
{
if (isset($this->_appLocales)) {
return $this->_appLocales;
}
$this->_appLocales = [];
$generalConfig = Craft::$app->getConfig()->getGeneral();
foreach ($this->getAppLocaleIds() as $localeId) {
$this->_appLocales[] = new Locale($localeId, $generalConfig->localeAliases[$localeId] ?? []);
}
return $this->_appLocales;
}
/**
* Returns an array of the locale IDs which Craft has been translated into. The list of locales is based on whatever
* files exist in `vendor/craftcms/cms/src/translations/`.
*
* @return array An array of locale IDs.
* @throws Exception in case of failure
*/
public function getAppLocaleIds(): array
{
$this->_defineAppLocales();
return array_keys($this->_appLocaleIds);
}
/**
* Defines the list of supported app locale IDs.
*
*/
private function _defineAppLocales(): void
{
if (isset($this->_appLocaleIds)) {
return;
}
$this->_appLocaleIds = [
Craft::$app->sourceLanguage => true,
];
// Scan the translations/ dir for the others
$dir = Craft::$app->getPath()->getCpTranslationsPath();
$handle = opendir($dir);
if ($handle === false) {
throw new Exception("Unable to open directory: $dir");
}
while (($subDir = readdir($handle)) !== false) {
if ($subDir !== '.' && $subDir !== '..' && is_dir($dir . DIRECTORY_SEPARATOR . $subDir)) {
$this->_appLocaleIds[$subDir] = true;
}
}
closedir($handle);
// Add in any extra locales defined by the config
$generalConfig = Craft::$app->getConfig()->getGeneral();
if (!empty($generalConfig->extraAppLocales)) {
foreach ($generalConfig->extraAppLocales as $localeId) {
$this->_appLocaleIds[$localeId] = true;
}
}
if ($generalConfig->defaultCpLanguage) {
$this->_appLocaleIds[$generalConfig->defaultCpLanguage] = true;
}
}
/**
* Returns whether the given locale ID is a supported app locale ID.
*
* @param string $localeId
* @return bool
* @since 3.6.0
*/
public function validateAppLocaleId(string $localeId): bool
{
$this->_defineAppLocales();
return isset($this->_appLocaleIds[$localeId]);
}
// Site Locales
// -------------------------------------------------------------------------
/**
* Returns an array of the site locales.
*
* @return Locale[] An array of [[Locale]] objects.
*/
public function getSiteLocales(): array
{
$locales = [];
$generalConfig = Craft::$app->getConfig()->getGeneral();
foreach ($this->getSiteLocaleIds() as $localeId) {
$locales[] = new Locale($localeId, $generalConfig->localeAliases[$localeId] ?? []);
}
return $locales;
}
/**
* Returns the site's primary locale. The primary locale is whatever is listed first in Settings > Locales in the
* control panel.
*
* @return Locale A [[Locale]] object representing the primary locale.
* @deprecated in 5.0.0. [[\craft\models\Site::getLocale()]] should be used instead.
*/
public function getPrimarySiteLocale(): Locale
{
return Craft::$app->getSites()->getPrimarySite()->getLocale();
}
/**
* Returns the site's primary locale ID. The primary locale is whatever is listed first in Settings > Locales in the
* control panel.
*
* @return string The primary locale ID.
* @deprecated in 5.0.0. [[\craft\models\Site::$language]] should be used instead.
*/
public function getPrimarySiteLocaleId(): string
{
return Craft::$app->getSites()->getPrimarySite()->language;
}
/**
* Returns an array of the site locale IDs.
*
* @return array An array of locale IDs.
*/
public function getSiteLocaleIds(): array
{
$localeIds = [];
foreach (Craft::$app->getSites()->getAllSites() as $site) {
// Make sure it's unique
if (!in_array($site->language, $localeIds, true)) {
$localeIds[] = $site->language;
}
}
return $localeIds;
}
/**
* Returns a list of locales that are editable by the current user.
*
* @return array
*/
public function getEditableLocales(): array
{
if (Craft::$app->getIsMultiSite()) {
$locales = $this->getSiteLocales();
$editableLocales = [];
foreach ($locales as $locale) {
if (Craft::$app->getUser()->checkPermission('editLocale:' . $locale->id)) {
$editableLocales[] = $locale;
}
}
return $editableLocales;
}
return $this->getSiteLocales();
}
/**
* Returns an array of the editable locale IDs.
*
* @return array
*/
public function getEditableLocaleIds(): array
{
$locales = $this->getEditableLocales();
$localeIds = [];
foreach ($locales as $locale) {
$localeIds[] = $locale->id;
}
return $localeIds;
}
/**
* @inheritdoc
*/
public function translate($category, $message, $params, $language): ?string
{
$translation = parent::translate($category, $message, $params, $language);
// If $message is a key and came back identical to the input, translate it into the source language
if ($translation === $message && !in_array($category, ['yii', 'site'], true)) {
$messageSource = $this->getMessageSource($category);
if ($messageSource->sourceLanguage !== $language) {
$translation = parent::translate($category, $message, $params, $messageSource->sourceLanguage);
}
}
if ($this->_shouldAddTranslationDebugOutput()) {
$char = match ($category) {
'site' => '$',
'app' => '@',
default => '%',
};
$translation = $char . $translation . $char;
}
return $translation;
}
/**
* Returns whether [[translate()]] should wrap translations with `@` characters,
* per the `translationDebugOutput` config setting.
*
* @return bool
*/
private function _shouldAddTranslationDebugOutput(): bool
{
if (!isset($this->_translationDebugOutput)) {
$this->_translationDebugOutput = Craft::$app->getConfig()->getGeneral()->translationDebugOutput;
}
return $this->_translationDebugOutput;
}
}