LanguageManager.php 10.7 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 Symfony\Component\HttpFoundation\Request;
11
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
12 13

/**
14
 * Class responsible for initializing each language type.
15 16 17
 */
class LanguageManager {

18 19 20 21 22 23
  /**
   * A request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;
24

25 26 27 28 29 30 31
  /**
   * The Key/Value Store to use for state.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface
   */
  protected $state = NULL;

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  /**
   * An array of language objects keyed by language type.
   *
   * @var array
   */
  protected $languages;

  /**
   * Whether or not the language manager has been initialized.
   *
   * @var bool
   */
  protected $initialized = FALSE;

  /**
   * Whether already in the process of language initialization.
   *
   * @todo This is only needed due to the circular dependency between language
   *   and config. See http://drupal.org/node/1862202 for the plan to fix this.
   *
   * @var bool
   */
  protected $initializing = FALSE;

56 57 58 59
  /**
   * Constructs an LanguageManager object.
   *
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreInterface $state
60
   *   The state keyvalue store.
61
   */
62
  public function __construct(KeyValueStoreInterface $state = NULL) {
63 64 65
    $this->state = $state;
  }

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  /**
   * Initializes each language type to a language object.
   */
  public function init() {
    if ($this->initialized) {
      return;
    }
    if ($this->isMultilingual()) {
      foreach ($this->getLanguageTypes() as $type) {
        $this->getLanguage($type);
      }
    }
    $this->initialized = TRUE;
  }

  /**
   * Sets the $request property and resets all language types.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The HttpRequest object representing the current request.
   */
  public function setRequest(Request $request) {
88
    $this->request = $request;
89 90
    $this->reset();
    $this->init();
91 92
  }

93 94 95 96
  /**
   * Returns a language object for the given type.
   *
   * @param string $type
97 98
   *   (optional) The language type, e.g. the interface or the content language.
   *   Defaults to \Drupal\Core\Language\Language::TYPE_INTERFACE.
99 100 101 102
   *
   * @return \Drupal\Core\Language\Language
   *   A language object for the given type.
   */
103
  public function getLanguage($type = Language::TYPE_INTERFACE) {
104 105 106 107
    if (isset($this->languages[$type])) {
      return $this->languages[$type];
    }

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    if ($this->isMultilingual() && $this->request) {
      if (!$this->initializing) {
        $this->initializing = TRUE;
        // @todo Objectify the language system so that we don't have to load an
        //   include file and call out to procedural code. See
        //   http://drupal.org/node/1862202
        include_once DRUPAL_ROOT . '/core/includes/language.inc';
        $this->languages[$type] = language_types_initialize($type, $this->request);
        $this->initializing = FALSE;
      }
      else {
        // Config has called getLanguage() during initialization of a language
        // type. Simply return the default language without setting it on the
        // $this->languages property. See the TODO in the docblock for the
        // $initializing property.
        return $this->getLanguageDefault();
      }
125 126
    }
    else {
127
      $this->languages[$type] = $this->getLanguageDefault();
128
    }
129
    return $this->languages[$type];
130 131
  }

132 133 134 135 136
  /**
   * Resets the given language type or all types if none specified.
   *
   * @param string|null $type
   *   (optional) The language type to reset as a string, e.g.,
137 138
   *   Language::TYPE_INTERFACE, or NULL to reset all language types. Defaults
   *   to NULL.
139
   */
140 141 142
  public function reset($type = NULL) {
    if (!isset($type)) {
      $this->languages = array();
143
      $this->initialized = FALSE;
144 145 146 147
    }
    elseif (isset($this->languages[$type])) {
      unset($this->languages[$type]);
    }
148
  }
149 150 151 152 153 154 155

  /**
   * Returns whether or not the site has more than one language enabled.
   *
   * @return bool
   *   TRUE if more than one language is enabled, FALSE otherwise.
   */
156
  public function isMultilingual() {
157 158 159 160 161
    if (!isset($this->state)) {
      // No state service in install time.
      return FALSE;
    }
    return ($this->state->get('language_count') ?: 1) > 1;
162 163 164 165 166 167 168 169 170
  }

  /**
   * Returns an array of the available language types.
   *
   * @return array()
   *   An array of all language types.
   */
  protected function getLanguageTypes() {
171
    return language_types_get_all();
172 173 174 175 176
  }

  /**
   * Returns a language object representing the site's default language.
   *
177
   * @return \Drupal\Core\Language\Language
178 179 180 181
   *   A language object.
   */
  protected function getLanguageDefault() {
    $default_info = variable_get('language_default', array(
182
      'id' => 'en',
183 184 185 186 187
      'name' => 'English',
      'direction' => 0,
      'weight' => 0,
      'locked' => 0,
    ));
188 189
    $default_info['default'] = TRUE;
    return new Language($default_info);
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
  /**
   * 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'),
241 242
      'fy' => array('Frisian, Western', 'Frysk'),
      'ga' => array('Irish', 'Gaeilge'),
243 244 245 246 247 248 249 250 251
      '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'),
252
      'hy' => array('Armenian', 'Հայերեն'),
253 254 255 256 257 258 259
      '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', 'Қазақ'),
260
      'km' => array('Khmer', 'ភាសាខ្មែរ'),
261 262 263 264 265 266 267 268 269 270 271 272
      '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', 'मराठी'),
273
      'ms' => array('Bahasa Malaysia', 'بهاس ملايو'),
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
      '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'),
294
      'sw' => array('Swahili', 'Kiswahili'),
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
      '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
311
}