mirrored from http://git.drupal.org/project/drupal.git
/
LanguageManager.php
422 lines (387 loc) · 14.8 KB
/
LanguageManager.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<?php
namespace Drupal\Core\Language;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
/**
* Class responsible for providing language support on language-unaware sites.
*/
class LanguageManager implements LanguageManagerInterface {
use DependencySerializationTrait;
/**
* A static cache of translated language lists.
*
* Array of arrays to cache the result of self::getLanguages() keyed by the
* language the list is translated to (first level) and the flags provided to
* the method (second level).
*
* @var \Drupal\Core\Language\LanguageInterface[]
*
* @see \Drupal\Core\Language\LanguageManager::getLanguages()
*/
protected $languages = [];
/**
* The default language object.
*
* @var \Drupal\Core\Language\LanguageDefault
*/
protected $defaultLanguage;
/**
* Constructs the language manager.
*
* @param \Drupal\Core\Language\LanguageDefault $default_language
* The default language.
*/
public function __construct(LanguageDefault $default_language) {
$this->defaultLanguage = $default_language;
}
/**
* {@inheritdoc}
*/
public function isMultilingual() {
return FALSE;
}
/**
* {@inheritdoc}
*/
public function getLanguageTypes() {
return [LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT, LanguageInterface::TYPE_URL];
}
/**
* Returns information about all defined language types.
*
* Defines the three core language types:
* - Interface language is the only configurable language type in core. It is
* used by t() as the default language if none is specified.
* - Content language is by default non-configurable and inherits the
* interface language negotiated value. It is used by the Field API to
* determine the display language for fields if no explicit value is
* specified.
* - URL language is by default non-configurable and is determined through the
* URL language negotiation method or the URL fallback language negotiation
* method if no language can be detected. It is used by l() as the default
* language if none is specified.
*
* @return array
* An associative array of language type information arrays keyed by
* language type machine name, in the format of
* hook_language_types_info().
*/
public function getDefinedLanguageTypesInfo() {
$this->definedLanguageTypesInfo = [
LanguageInterface::TYPE_INTERFACE => [
'name' => new TranslatableMarkup('Interface text'),
'description' => new TranslatableMarkup('Order of language detection methods for interface text. If a translation of interface text is available in the detected language, it will be displayed.'),
'locked' => TRUE,
],
LanguageInterface::TYPE_CONTENT => [
'name' => new TranslatableMarkup('Content'),
'description' => new TranslatableMarkup('Order of language detection methods for content. If a version of content is available in the detected language, it will be displayed.'),
'locked' => TRUE,
],
LanguageInterface::TYPE_URL => [
'locked' => TRUE,
],
];
return $this->definedLanguageTypesInfo;
}
/**
* {@inheritdoc}
*/
public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE) {
return $this->getDefaultLanguage();
}
/**
* {@inheritdoc}
*/
public function reset($type = NULL) {
return $this;
}
/**
* {@inheritdoc}
*/
public function getDefaultLanguage() {
return $this->defaultLanguage->get();
}
/**
* {@inheritdoc}
*/
public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) {
$static_cache_id = $this->getCurrentLanguage()->getId();
if (!isset($this->languages[$static_cache_id][$flags])) {
// If this language manager is used, there are no configured languages.
// The default language and locked languages comprise the full language
// list.
$default = $this->getDefaultLanguage();
$languages = [$default->getId() => $default];
$languages += $this->getDefaultLockedLanguages($default->getWeight());
// Filter the full list of languages based on the value of $flags.
$this->languages[$static_cache_id][$flags] = $this->filterLanguages($languages, $flags);
}
return $this->languages[$static_cache_id][$flags];
}
/**
* {@inheritdoc}
*/
public function getNativeLanguages() {
// In a language unaware site we don't have translated languages.
return $this->getLanguages();
}
/**
* {@inheritdoc}
*/
public function getLanguage($langcode) {
$languages = $this->getLanguages(LanguageInterface::STATE_ALL);
return isset($languages[$langcode]) ? $languages[$langcode] : NULL;
}
/**
* {@inheritdoc}
*/
public function getLanguageName($langcode) {
if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
return new TranslatableMarkup('None');
}
if ($language = $this->getLanguage($langcode)) {
return $language->getName();
}
if (empty($langcode)) {
return new TranslatableMarkup('Unknown');
}
return new TranslatableMarkup('Unknown (@langcode)', ['@langcode' => $langcode]);
}
/**
* {@inheritdoc}
*/
public function getDefaultLockedLanguages($weight = 0) {
$languages = [];
$locked_language = [
'default' => FALSE,
'locked' => TRUE,
'direction' => LanguageInterface::DIRECTION_LTR,
];
// This is called very early while initializing the language system. Prevent
// early t() calls by using the TranslatableMarkup.
$languages[LanguageInterface::LANGCODE_NOT_SPECIFIED] = new Language([
'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
'name' => new TranslatableMarkup('Not specified'),
'weight' => ++$weight,
] + $locked_language);
$languages[LanguageInterface::LANGCODE_NOT_APPLICABLE] = new Language([
'id' => LanguageInterface::LANGCODE_NOT_APPLICABLE,
'name' => new TranslatableMarkup('Not applicable'),
'weight' => ++$weight,
] + $locked_language);
return $languages;
}
/**
* {@inheritdoc}
*/
public function isLanguageLocked($langcode) {
$language = $this->getLanguage($langcode);
return ($language ? $language->isLocked() : FALSE);
}
/**
* {@inheritdoc}
*/
public function getFallbackCandidates(array $context = []) {
return [LanguageInterface::LANGCODE_DEFAULT];
}
/**
* {@inheritdoc}
*/
public function getLanguageSwitchLinks($type, Url $url) {
return [];
}
/**
* {@inheritdoc}
*/
public static function getStandardLanguageList() {
// This list is based on languages available from localize.drupal.org. See
// http://localize.drupal.org/issues for information on how to add languages
// there.
//
// The "Left-to-right marker" comments and the enclosed UTF-8 markers are to
// make otherwise strange looking PHP syntax natural (to not be displayed in
// right to left). See https://www.drupal.org/node/128866#comment-528929.
return [
'af' => ['Afrikaans', 'Afrikaans'],
'am' => ['Amharic', 'አማርኛ'],
'ar' => ['Arabic', /* Left-to-right marker "" */ 'العربية', LanguageInterface::DIRECTION_RTL],
'ast' => ['Asturian', 'Asturianu'],
'az' => ['Azerbaijani', 'Azərbaycanca'],
'be' => ['Belarusian', 'Беларуская'],
'bg' => ['Bulgarian', 'Български'],
'bn' => ['Bengali', 'বাংলা'],
'bo' => ['Tibetan', 'བོད་སྐད་'],
'bs' => ['Bosnian', 'Bosanski'],
'ca' => ['Catalan', 'Català'],
'cs' => ['Czech', 'Čeština'],
'cy' => ['Welsh', 'Cymraeg'],
'da' => ['Danish', 'Dansk'],
'de' => ['German', 'Deutsch'],
'dz' => ['Dzongkha', 'རྫོང་ཁ'],
'el' => ['Greek', 'Ελληνικά'],
'en' => ['English', 'English'],
'en-x-simple' => ['Simple English', 'Simple English'],
'eo' => ['Esperanto', 'Esperanto'],
'es' => ['Spanish', 'Español'],
'et' => ['Estonian', 'Eesti'],
'eu' => ['Basque', 'Euskera'],
'fa' => ['Persian, Farsi', /* Left-to-right marker "" */ 'فارسی', LanguageInterface::DIRECTION_RTL],
'fi' => ['Finnish', 'Suomi'],
'fil' => ['Filipino', 'Filipino'],
'fo' => ['Faeroese', 'Føroyskt'],
'fr' => ['French', 'Français'],
'fy' => ['Frisian, Western', 'Frysk'],
'ga' => ['Irish', 'Gaeilge'],
'gd' => ['Scots Gaelic', 'Gàidhlig'],
'gl' => ['Galician', 'Galego'],
'gsw-berne' => ['Swiss German', 'Schwyzerdütsch'],
'gu' => ['Gujarati', 'ગુજરાતી'],
'he' => ['Hebrew', /* Left-to-right marker "" */ 'עברית', LanguageInterface::DIRECTION_RTL],
'hi' => ['Hindi', 'हिन्दी'],
'hr' => ['Croatian', 'Hrvatski'],
'ht' => ['Haitian Creole', 'Kreyòl ayisyen'],
'hu' => ['Hungarian', 'Magyar'],
'hy' => ['Armenian', 'Հայերեն'],
'id' => ['Indonesian', 'Bahasa Indonesia'],
'is' => ['Icelandic', 'Íslenska'],
'it' => ['Italian', 'Italiano'],
'ja' => ['Japanese', '日本語'],
'jv' => ['Javanese', 'Basa Java'],
'ka' => ['Georgian', 'ქართული ენა'],
'kk' => ['Kazakh', 'Қазақ'],
'km' => ['Khmer', 'ភាសាខ្មែរ'],
'kn' => ['Kannada', 'ಕನ್ನಡ'],
'ko' => ['Korean', '한국어'],
'ku' => ['Kurdish', 'Kurdî'],
'ky' => ['Kyrgyz', 'Кыргызча'],
'lo' => ['Lao', 'ພາສາລາວ'],
'lt' => ['Lithuanian', 'Lietuvių'],
'lv' => ['Latvian', 'Latviešu'],
'mg' => ['Malagasy', 'Malagasy'],
'mk' => ['Macedonian', 'Македонски'],
'ml' => ['Malayalam', 'മലയാളം'],
'mn' => ['Mongolian', 'монгол'],
'mr' => ['Marathi', 'मराठी'],
'ms' => ['Bahasa Malaysia', 'بهاس ملايو'],
'my' => ['Burmese', 'ဗမာစကား'],
'ne' => ['Nepali', 'नेपाली'],
'nl' => ['Dutch', 'Nederlands'],
'nb' => ['Norwegian Bokmål', 'Norsk, bokmål'],
'nn' => ['Norwegian Nynorsk', 'Norsk, nynorsk'],
'oc' => ['Occitan', 'Occitan'],
'pa' => ['Punjabi', 'ਪੰਜਾਬੀ'],
'pl' => ['Polish', 'Polski'],
'pt-pt' => ['Portuguese, Portugal', 'Português, Portugal'],
'pt-br' => ['Portuguese, Brazil', 'Português, Brasil'],
'ro' => ['Romanian', 'Română'],
'ru' => ['Russian', 'Русский'],
'sco' => ['Scots', 'Scots'],
'se' => ['Northern Sami', 'Sámi'],
'si' => ['Sinhala', 'සිංහල'],
'sk' => ['Slovak', 'Slovenčina'],
'sl' => ['Slovenian', 'Slovenščina'],
'sq' => ['Albanian', 'Shqip'],
'sr' => ['Serbian', 'Српски'],
'sv' => ['Swedish', 'Svenska'],
'sw' => ['Swahili', 'Kiswahili'],
'ta' => ['Tamil', 'தமிழ்'],
'ta-lk' => ['Tamil, Sri Lanka', 'தமிழ், இலங்கை'],
'te' => ['Telugu', 'తెలుగు'],
'th' => ['Thai', 'ภาษาไทย'],
'tr' => ['Turkish', 'Türkçe'],
'tyv' => ['Tuvan', 'Тыва дыл'],
'ug' => ['Uyghur', /* Left-to-right marker "" */ 'ئۇيغۇرچە', LanguageInterface::DIRECTION_RTL],
'uk' => ['Ukrainian', 'Українська'],
'ur' => ['Urdu', /* Left-to-right marker "" */ 'اردو', LanguageInterface::DIRECTION_RTL],
'vi' => ['Vietnamese', 'Tiếng Việt'],
'xx-lolspeak' => ['Lolspeak', 'Lolspeak'],
'zh-hans' => ['Chinese, Simplified', '简体中文'],
'zh-hant' => ['Chinese, Traditional', '繁體中文'],
];
}
/**
* The 6 official languages used at the United Nations.
*
* This list is based on
* http://www.un.org/en/sections/about-un/official-languages/index.html and it
* uses the same format as getStandardLanguageList().
*
* @return array
* An array with language codes as keys, and English and native language
* names as values.
*/
public static function getUnitedNationsLanguageList() {
return [
'ar' => ['Arabic', /* Left-to-right marker "" */ 'العربية', LanguageInterface::DIRECTION_RTL],
'zh-hans' => ['Chinese, Simplified', '简体中文'],
'en' => ['English', 'English'],
'fr' => ['French', 'Français'],
'ru' => ['Russian', 'Русский'],
'es' => ['Spanish', 'Español'],
];
}
/**
* {@inheritdoc}
*
* This function is a noop since the configuration cannot be overridden by
* language unless the Language module is enabled. That replaces the default
* language manager with a configurable language manager.
*
* @see \Drupal\language\ConfigurableLanguageManager::setConfigOverrideLanguage()
*/
public function setConfigOverrideLanguage(LanguageInterface $language = NULL) {
return $this;
}
/**
* {@inheritdoc}
*/
public function getConfigOverrideLanguage() {
return $this->getCurrentLanguage();
}
/**
* Filters the full list of languages based on the value of the flag.
*
* The locked languages are removed by default.
*
* @param \Drupal\Core\Language\LanguageInterface[] $languages
* Array with languages to be filtered.
* @param int $flags
* (optional) Specifies the state of the languages that have to be returned.
* It can be: LanguageInterface::STATE_CONFIGURABLE,
* LanguageInterface::STATE_LOCKED, or LanguageInterface::STATE_ALL.
*
* @return \Drupal\Core\Language\LanguageInterface[]
* An associative array of languages, keyed by the language code.
*/
protected function filterLanguages(array $languages, $flags = LanguageInterface::STATE_CONFIGURABLE) {
// STATE_ALL means we don't actually filter, so skip the rest of the method.
if ($flags == LanguageInterface::STATE_ALL) {
return $languages;
}
$filtered_languages = [];
// Add the site's default language if requested.
if ($flags & LanguageInterface::STATE_SITE_DEFAULT) {
// Setup a language to have the defaults with data appropriate of the
// default language only for runtime.
$defaultLanguage = $this->getDefaultLanguage();
$default = new Language(
[
'id' => $defaultLanguage->getId(),
'name' => new TranslatableMarkup("Site's default language (@lang_name)",
['@lang_name' => $defaultLanguage->getName()]),
'direction' => $defaultLanguage->getDirection(),
'weight' => $defaultLanguage->getWeight(),
]
);
$filtered_languages[LanguageInterface::LANGCODE_SITE_DEFAULT] = $default;
}
foreach ($languages as $id => $language) {
if (($language->isLocked() && ($flags & LanguageInterface::STATE_LOCKED)) || (!$language->isLocked() && ($flags & LanguageInterface::STATE_CONFIGURABLE))) {
$filtered_languages[$id] = $language;
}
}
return $filtered_languages;
}
}