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

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

namespace Drupal\Core\Language;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

106
  /**
107
   * {@inheritdoc}
108
   */
109 110 111 112 113 114 115 116
  public function getLanguages($flags = Language::STATE_CONFIGURABLE) {
    // 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);
117
    }
118 119 120 121 122 123 124 125 126 127 128 129

    // 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.
    if ($flags & Language::STATE_SITE_DEFAULT) {
      $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;
130
    }
131 132 133 134 135 136 137 138

    foreach ($this->languages as $id => $language) {
      if (($language->locked && ($flags & Language::STATE_LOCKED)) || (!$language->locked && ($flags & Language::STATE_CONFIGURABLE))) {
        $filtered_languages[$id] = $language;
      }
    }

    return $filtered_languages;
139
  }
140 141

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

149
  /**
150
   * {@inheritdoc}
151
   */
152 153 154 155 156 157
  function getLanguageName($langcode) {
    if ($langcode == Language::LANGCODE_NOT_SPECIFIED) {
      return $this->t('None');
    }
    if ($language = $this->getLanguage($langcode)) {
      return $language->name;
158
    }
159 160
    if (empty($langcode)) {
      return $this->t('Unknown');
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
    return $this->t('Unknown (@langcode)', array('@langcode' => $langcode));
  }

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

    $locked_language = array(
      'default' => FALSE,
      'locked' => TRUE,
     );
    $languages[Language::LANGCODE_NOT_SPECIFIED] = new Language(array(
      'id' => Language::LANGCODE_NOT_SPECIFIED,
      'name' => $this->t('Not specified'),
      'weight' => ++$weight,
    ) + $locked_language);

    $languages[Language::LANGCODE_NOT_APPLICABLE] = new Language(array(
      'id' => Language::LANGCODE_NOT_APPLICABLE,
      'name' => $this->t('Not applicable'),
      'weight' => ++$weight,
    ) + $locked_language);

    return $languages;
  }
189

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

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

  /**
206
   * {@inheritdoc}
207
   */
208 209
  public function getLanguageSwitchLinks($type, $path) {
    return array();
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
  /**
   * Some common languages with their English and native names.
   *
   * Language codes are defined by the W3C language tags document for
   * 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.
   *
   * 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', 'አማርኛ'),
      'ar' => array('Arabic', /* Left-to-right marker "‭" */ 'العربية', Language::DIRECTION_RTL),
      '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'),
      'fa' => array('Persian, Farsi', /* Left-to-right marker "‭" */ 'فارسی', Language::DIRECTION_RTL),
      'fi' => array('Finnish', 'Suomi'),
      'fil' => array('Filipino', 'Filipino'),
      'fo' => array('Faeroese', 'Føroyskt'),
      'fr' => array('French', 'Français'),
261 262
      'fy' => array('Frisian, Western', 'Frysk'),
      'ga' => array('Irish', 'Gaeilge'),
263 264 265 266 267 268 269 270 271
      'gd' => array('Scots Gaelic', 'Gàidhlig'),
      'gl' => array('Galician', 'Galego'),
      'gsw-berne' => array('Swiss German', 'Schwyzerdütsch'),
      'gu' => array('Gujarati', 'ગુજરાતી'),
      'he' => array('Hebrew', /* Left-to-right marker "‭" */ 'עברית', Language::DIRECTION_RTL),
      'hi' => array('Hindi', 'हिन्दी'),
      'hr' => array('Croatian', 'Hrvatski'),
      'ht' => array('Haitian Creole', 'Kreyòl ayisyen'),
      'hu' => array('Hungarian', 'Magyar'),
272
      'hy' => array('Armenian', 'Հայերեն'),
273 274 275 276 277 278 279
      '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', 'Қазақ'),
280
      'km' => array('Khmer', 'ភាសាខ្មែរ'),
281 282 283 284 285 286 287 288 289 290 291 292
      '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', 'मराठी'),
293
      'ms' => array('Bahasa Malaysia', 'بهاس ملايو'),
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
      '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'),
314
      'sw' => array('Swahili', 'Kiswahili'),
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      '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', 'Українська'),
      'ur' => array('Urdu', /* Left-to-right marker "‭" */ 'اردو', Language::DIRECTION_RTL),
      'vi' => array('Vietnamese', 'Tiếng Việt'),
      'xx-lolspeak' => array('Lolspeak', 'Lolspeak'),
      'zh-hans' => array('Chinese, Simplified', '简体中文'),
      'zh-hant' => array('Chinese, Traditional', '繁體中文'),
    );
  }

Crell's avatar
Crell committed
331
}