LanguageManager.php 11.9 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Language\LanguageManager.
6 7 8 9
 */

namespace Drupal\Core\Language;

10
use Drupal\Component\Utility\String;
11
use Drupal\Core\DependencyInjection\DependencySerialization;
12
use Drupal\Core\StringTranslation\TranslationInterface;
13 14

/**
15
 * Class responsible for providing language support on language-unaware sites.
16
 */
17
class LanguageManager extends DependencySerialization implements LanguageManagerInterface {
18

19
  /**
20
   * The string translation service.
21
   *
22
   * @var \Drupal\Core\StringTranslation\TranslationInterface
23
   */
24
  protected $translation;
25

26
  /**
27
   * An array of all the available languages keyed by language code.
28
   *
29
   * @var array
30
   */
31
  protected $languages;
32

33
  /**
34
   * The default language object.
35
   *
36
   * @var \Drupal\Core\Language\LanguageDefault
37
   */
38
  protected $defaultLanguage;
39

40 41 42
  /**
   * Constructs the language manager.
   *
43
   * @param \Drupal\Core\Language\LanguageDefault $default_language
44 45 46 47 48 49
   *   The default language.
   */
  public function __construct(LanguageDefault $default_language) {
    $this->defaultLanguage = $default_language;
  }

50
  /**
51
   * {@inheritdoc}
52
   */
53
  public function setTranslation(TranslationInterface $translation) {
54 55
    $this->translation = $translation;
  }
56 57

  /**
58
   * Translates a string to the current language or to a given language.
59
   *
60
   * @see \Drupal\Core\StringTranslation\TranslationInterface()
61
   */
62 63 64
  protected function t($string, array $args = array(), array $options = array()) {
    return $this->translation ? $this->translation->translate($string, $args, $options) : String::format($string, $args);
  }
65 66

  /**
67
   * {@inheritdoc}
68
   */
69 70
  public function init() {
  }
71

72
  /**
73
   * {@inheritdoc}
74
   */
75 76
  public function isMultilingual() {
    return FALSE;
77 78
  }

79
  /**
80
   * {@inheritdoc}
81
   */
82
  public function getLanguageTypes() {
83
    return array(LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT, LanguageInterface::TYPE_URL);
84 85 86
  }

  /**
87
   * {@inheritdoc}
88
   */
89
  public function getCurrentLanguage($type = LanguageInterface::TYPE_INTERFACE) {
90
    return $this->getDefaultLanguage();
91 92
  }

93
  /**
94
   * {@inheritdoc}
95
   */
96
  public function reset($type = NULL) {
97
    return $this;
98
  }
99

100 101 102 103
  /**
   * {@inheritdoc}
   */
  public function getDefaultLanguage() {
104
    return $this->defaultLanguage->get();
105 106
  }

107
  /**
108
   * {@inheritdoc}
109
   */
110
  public function getLanguages($flags = LanguageInterface::STATE_CONFIGURABLE) {
111 112 113 114 115 116 117
    // Initialize master language list.
    if (!isset($this->languages)) {
      // No language module, so use the default language only.
      $default = $this->getDefaultLanguage();
      $this->languages = array($default->id => $default);
      // Add the special languages, they will be filtered later if needed.
      $this->languages += $this->getDefaultLockedLanguages($default->weight);
118
    }
119 120 121 122 123 124 125

    // Filter the full list of languages based on the value of the $all flag. By
    // default we remove the locked languages, but the caller may request for
    // those languages to be added as well.
    $filtered_languages = array();

    // Add the site's default language if flagged as allowed value.
126
    if ($flags & LanguageInterface::STATE_SITE_DEFAULT) {
127 128 129 130
      $default = isset($default) ? $default : $this->getDefaultLanguage();
      // Rename the default language.
      $default->name = $this->t("Site's default language (@lang_name)", array('@lang_name' => $default->name));
      $filtered_languages['site_default'] = $default;
131
    }
132 133

    foreach ($this->languages as $id => $language) {
134
      if (($language->locked && ($flags & LanguageInterface::STATE_LOCKED)) || (!$language->locked && ($flags & LanguageInterface::STATE_CONFIGURABLE))) {
135 136 137 138 139
        $filtered_languages[$id] = $language;
      }
    }

    return $filtered_languages;
140
  }
141 142

  /**
143
   * {@inheritdoc}
144
   */
145
  public function getLanguage($langcode) {
146
    $languages = $this->getLanguages(LanguageInterface::STATE_ALL);
147
    return isset($languages[$langcode]) ? $languages[$langcode] : NULL;
148 149
  }

150
  /**
151
   * {@inheritdoc}
152
   */
153
  public function getLanguageName($langcode) {
154
    if ($langcode == LanguageInterface::LANGCODE_NOT_SPECIFIED) {
155 156 157 158
      return $this->t('None');
    }
    if ($language = $this->getLanguage($langcode)) {
      return $language->name;
159
    }
160 161
    if (empty($langcode)) {
      return $this->t('Unknown');
162
    }
163 164 165 166 167 168 169 170 171 172 173 174
    return $this->t('Unknown (@langcode)', array('@langcode' => $langcode));
  }

  /**
   * {@inheritdoc}
   */
  public function getDefaultLockedLanguages($weight = 0) {
    $languages = array();

    $locked_language = array(
      'default' => FALSE,
      'locked' => TRUE,
175
    );
176 177
    $languages[LanguageInterface::LANGCODE_NOT_SPECIFIED] = new Language(array(
      'id' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
178 179 180 181
      'name' => $this->t('Not specified'),
      'weight' => ++$weight,
    ) + $locked_language);

182 183
    $languages[LanguageInterface::LANGCODE_NOT_APPLICABLE] = new Language(array(
      'id' => LanguageInterface::LANGCODE_NOT_APPLICABLE,
184 185 186 187 188 189
      'name' => $this->t('Not applicable'),
      'weight' => ++$weight,
    ) + $locked_language);

    return $languages;
  }
190

191 192 193 194 195 196
  /**
   * {@inheritdoc}
   */
  public function isLanguageLocked($langcode) {
    $language = $this->getLanguage($langcode);
    return ($language ? $language->locked : FALSE);
197 198
  }

199
  /**
200
   * {@inheritdoc}
201
   */
202
  public function getFallbackCandidates($langcode = NULL, array $context = array()) {
203
    return array(LanguageInterface::LANGCODE_DEFAULT);
204 205 206
  }

  /**
207
   * {@inheritdoc}
208
   */
209 210
  public function getLanguageSwitchLinks($type, $path) {
    return array();
211 212
  }

213 214 215 216
  /**
   * Some common languages with their English and native names.
   *
   * Language codes are defined by the W3C language tags document for
217 218 219 220
   * interoperability. Language codes typically have a language and, optionally,
   * a script or regional variant name. See:
   * http://www.w3.org/International/articles/language-tags/ for more
   * information.
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
   *
   * 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 http://drupal.org/node/128866#comment-528929.
   *
   * @return array
   *   An array of language code to language name information.
   *   Language name information itself is an array of English and native names.
   */
  public static function getStandardLanguageList() {
    return array(
      'af' => array('Afrikaans', 'Afrikaans'),
      'am' => array('Amharic', 'አማርኛ'),
238
      'ar' => array('Arabic', /* Left-to-right marker "‭" */ 'العربية', LanguageInterface::DIRECTION_RTL),
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
      'ast' => array('Asturian', 'Asturianu'),
      'az' => array('Azerbaijani', 'Azərbaycanca'),
      'be' => array('Belarusian', 'Беларуская'),
      'bg' => array('Bulgarian', 'Български'),
      'bn' => array('Bengali', 'বাংলা'),
      'bo' => array('Tibetan', 'བོད་སྐད་'),
      'bs' => array('Bosnian', 'Bosanski'),
      'ca' => array('Catalan', 'Català'),
      'cs' => array('Czech', 'Čeština'),
      'cy' => array('Welsh', 'Cymraeg'),
      'da' => array('Danish', 'Dansk'),
      'de' => array('German', 'Deutsch'),
      'dz' => array('Dzongkha', 'རྫོང་ཁ'),
      'el' => array('Greek', 'Ελληνικά'),
      'en' => array('English', 'English'),
      'eo' => array('Esperanto', 'Esperanto'),
      'es' => array('Spanish', 'Español'),
      'et' => array('Estonian', 'Eesti'),
      'eu' => array('Basque', 'Euskera'),
258
      'fa' => array('Persian, Farsi', /* Left-to-right marker "‭" */ 'فارسی', LanguageInterface::DIRECTION_RTL),
259 260 261 262
      'fi' => array('Finnish', 'Suomi'),
      'fil' => array('Filipino', 'Filipino'),
      'fo' => array('Faeroese', 'Føroyskt'),
      'fr' => array('French', 'Français'),
263 264
      'fy' => array('Frisian, Western', 'Frysk'),
      'ga' => array('Irish', 'Gaeilge'),
265 266 267 268
      'gd' => array('Scots Gaelic', 'Gàidhlig'),
      'gl' => array('Galician', 'Galego'),
      'gsw-berne' => array('Swiss German', 'Schwyzerdütsch'),
      'gu' => array('Gujarati', 'ગુજરાતી'),
269
      'he' => array('Hebrew', /* Left-to-right marker "‭" */ 'עברית', LanguageInterface::DIRECTION_RTL),
270 271 272 273
      'hi' => array('Hindi', 'हिन्दी'),
      'hr' => array('Croatian', 'Hrvatski'),
      'ht' => array('Haitian Creole', 'Kreyòl ayisyen'),
      'hu' => array('Hungarian', 'Magyar'),
274
      'hy' => array('Armenian', 'Հայերեն'),
275 276 277 278 279 280 281
      'id' => array('Indonesian', 'Bahasa Indonesia'),
      'is' => array('Icelandic', 'Íslenska'),
      'it' => array('Italian', 'Italiano'),
      'ja' => array('Japanese', '日本語'),
      'jv' => array('Javanese', 'Basa Java'),
      'ka' => array('Georgian', 'ქართული ენა'),
      'kk' => array('Kazakh', 'Қазақ'),
282
      'km' => array('Khmer', 'ភាសាខ្មែរ'),
283 284 285 286 287 288 289 290 291 292 293 294
      'kn' => array('Kannada', 'ಕನ್ನಡ'),
      'ko' => array('Korean', '한국어'),
      'ku' => array('Kurdish', 'Kurdî'),
      'ky' => array('Kyrgyz', 'Кыргызча'),
      'lo' => array('Lao', 'ພາສາລາວ'),
      'lt' => array('Lithuanian', 'Lietuvių'),
      'lv' => array('Latvian', 'Latviešu'),
      'mg' => array('Malagasy', 'Malagasy'),
      'mk' => array('Macedonian', 'Македонски'),
      'ml' => array('Malayalam', 'മലയാളം'),
      'mn' => array('Mongolian', 'монгол'),
      'mr' => array('Marathi', 'मराठी'),
295
      'ms' => array('Bahasa Malaysia', 'بهاس ملايو'),
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
      'my' => array('Burmese', 'ဗမာစကား'),
      'ne' => array('Nepali', 'नेपाली'),
      'nl' => array('Dutch', 'Nederlands'),
      'nb' => array('Norwegian Bokmål', 'Bokmål'),
      'nn' => array('Norwegian Nynorsk', 'Nynorsk'),
      'oc' => array('Occitan', 'Occitan'),
      'pa' => array('Punjabi', 'ਪੰਜਾਬੀ'),
      'pl' => array('Polish', 'Polski'),
      'pt-pt' => array('Portuguese, Portugal', 'Português, Portugal'),
      'pt-br' => array('Portuguese, Brazil', 'Português, Brasil'),
      'ro' => array('Romanian', 'Română'),
      'ru' => array('Russian', 'Русский'),
      'sco' => array('Scots', 'Scots'),
      'se' => array('Northern Sami', 'Sámi'),
      'si' => array('Sinhala', 'සිංහල'),
      'sk' => array('Slovak', 'Slovenčina'),
      'sl' => array('Slovenian', 'Slovenščina'),
      'sq' => array('Albanian', 'Shqip'),
      'sr' => array('Serbian', 'Српски'),
      'sv' => array('Swedish', 'Svenska'),
316
      'sw' => array('Swahili', 'Kiswahili'),
317 318 319 320 321 322 323 324
      'ta' => array('Tamil', 'தமிழ்'),
      'ta-lk' => array('Tamil, Sri Lanka', 'தமிழ், இலங்கை'),
      'te' => array('Telugu', 'తెలుగు'),
      'th' => array('Thai', 'ภาษาไทย'),
      'tr' => array('Turkish', 'Türkçe'),
      'tyv' => array('Tuvan', 'Тыва дыл'),
      'ug' => array('Uyghur', 'Уйғур'),
      'uk' => array('Ukrainian', 'Українська'),
325
      'ur' => array('Urdu', /* Left-to-right marker "‭" */ 'اردو', LanguageInterface::DIRECTION_RTL),
326 327 328 329 330 331 332
      'vi' => array('Vietnamese', 'Tiếng Việt'),
      'xx-lolspeak' => array('Lolspeak', 'Lolspeak'),
      'zh-hans' => array('Chinese, Simplified', '简体中文'),
      'zh-hant' => array('Chinese, Traditional', '繁體中文'),
    );
  }

333 334 335
  /**
   * {@inheritdoc}
   *
336
   * This function is a noop since the configuration cannot be overridden by
337
   * language unless the Language module is enabled. That replaces the default
338
   * language manager with a configurable language manager.
339 340 341
   *
   * @see \Drupal\language\ConfigurableLanguageManager::setConfigOverrideLanguage()
   */
342
  public function setConfigOverrideLanguage(LanguageInterface $language = NULL) {
343 344 345 346 347 348 349 350 351 352
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfigOverrideLanguage() {
    return $this->getCurrentLanguage();
  }

Crell's avatar
Crell committed
353
}