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

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

namespace Drupal\Core\Language;

use Symfony\Component\HttpFoundation\Request;

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

17 18 19 20 21 22
  /**
   * A request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;
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
  /**
   * 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;

  /**
   * 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) {
70
    $this->request = $request;
71 72
    $this->reset();
    $this->init();
73 74
  }

75 76 77 78 79 80 81 82 83
  /**
   * Returns a language object for the given type.
   *
   * @param string $type
   *   The language type, e.g. LANGUAGE_TYPE_INTERFACE.
   *
   * @return \Drupal\Core\Language\Language
   *   A language object for the given type.
   */
84
  public function getLanguage($type) {
85 86 87 88
    if (isset($this->languages[$type])) {
      return $this->languages[$type];
    }

89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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();
      }
106 107
    }
    else {
108
      $this->languages[$type] = $this->getLanguageDefault();
109
    }
110
    return $this->languages[$type];
111 112
  }

113 114 115 116 117 118 119 120
  /**
   * 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.,
   *   LANGUAGE_TYPE_INTERFACE, or NULL to reset all language types. Defaults to
   *   NULL.
   */
121 122 123
  public function reset($type = NULL) {
    if (!isset($type)) {
      $this->languages = array();
124
      $this->initialized = FALSE;
125 126 127 128
    }
    elseif (isset($this->languages[$type])) {
      unset($this->languages[$type]);
    }
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

  /**
   * Returns whether or not the site has more than one language enabled.
   *
   * @return bool
   *   TRUE if more than one language is enabled, FALSE otherwise.
   */
  protected function isMultilingual() {
    return variable_get('language_count', 1) > 1;
  }

  /**
   * Returns an array of the available language types.
   *
   * @return array()
   *   An array of all language types.
   */
  protected function getLanguageTypes() {
    return array_keys(variable_get('language_types', language_types_get_default()));
  }

  /**
   * Returns a language object representing the site's default language.
   *
   * @return Drupal\Core\Language\Language
   *   A language object.
   */
  protected function getLanguageDefault() {
    $default_info = variable_get('language_default', array(
      'langcode' => 'en',
      'name' => 'English',
      'direction' => 0,
      'weight' => 0,
      'locked' => 0,
    ));
    return new Language($default_info + array('default' => TRUE));
  }

Crell's avatar
Crell committed
168
}