Language.php 5.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php

/**
 * @file
 * Definition of Drupal\Core\Language\Language.
 */

namespace Drupal\Core\Language;

/**
 * An object containing the information for an interface language.
 *
 * @todo To keep backwards compatibility with stdClass, we currently use
 * public scopes for the Language class's variables. We will change these to
 * full get/set functions in a follow-up issue: http://drupal.org/node/1512424
 *
 * @see language_default()
 */
class Language {
20

21
  // Properties within the Language are set up as the default language.
22 23 24 25 26 27

  /**
   * The human readable English name.
   *
   * @var string
   */
28
  public $name = '';
29 30 31 32 33 34

  /**
   * The ID, langcode.
   *
   * @var string
   */
35
  public $id = '';
36 37 38 39 40 41 42 43

  /**
   * The direction, left-to-right, or right-to-left.
   *
   * Defined using constants, either DIRECTION_LTR or const DIRECTION_RTL.
   *
   * @var int
   */
44
  public $direction = Language::DIRECTION_LTR;
45 46 47 48 49 50

  /**
   * The weight, used for ordering languages in lists, like selects or tables.
   *
   * @var int
   */
51
  public $weight = 0;
52 53 54 55 56 57

  /**
   * Flag indicating if this is the only site default language.
   *
   * @var bool
   */
58
  public $default = FALSE;
59 60 61 62 63 64 65 66

  /**
   * The language negotiation method used when a language was detected.
   *
   * @var bool
   *
   * @see language_types_initialize()
   */
67
  public $method_id = NULL;
68 69 70 71 72 73 74 75 76 77

  /**
   * Locked indicates a language used by the system, not an actual language.
   *
   * Examples of locked languages are, LANGCODE_NOT_SPECIFIED, und, and
   * LANGCODE_NOT_APPLICABLE, zxx, which are usually shown in language selects
   * but hidden in places like the Language configuration and cannot be deleted.
   *
   * @var bool
   */
78
  public $locked = FALSE;
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
  /**
   * Special system language code (only applicable to UI language).
   *
   * Refers to the language used in Drupal and module/theme source code. Drupal
   * uses the built-in text for English by default, but if configured to allow
   * translation/customization of English, we need to differentiate between the
   * built-in language and the English translation.
   */
  const LANGCODE_SYSTEM = 'system';

  /**
   * The language code used when no language is explicitly assigned (yet).
   *
   * Should be used when language information is not available or cannot be
   * determined. This special language code is useful when we know the data
   * might have linguistic information, but we don't know the language.
   *
   * See http://www.w3.org/International/questions/qa-no-language#undetermined.
   */
  const LANGCODE_NOT_SPECIFIED = 'und';

  /**
   * The language code used when the marked object has no linguistic content.
   *
   * Should be used when we explicitly know that the data referred has no
   * linguistic content.
   *
   * See http://www.w3.org/International/questions/qa-no-language#nonlinguistic.
   */
  const LANGCODE_NOT_APPLICABLE = 'zxx';

  /**
   * Language code referring to the default language of data, e.g. of an entity.
   *
114 115
   * See the BCP 47 syntax for defining private language tags:
   * http://www.rfc-editor.org/rfc/bcp/bcp47.txt
116
   */
117
  const LANGCODE_DEFAULT = 'x-default';
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

  /**
   * The language state when referring to configurable languages.
   */
  const STATE_CONFIGURABLE = 1;

  /**
   * The language state when referring to locked languages.
   */
  const STATE_LOCKED = 2;

  /**
   * The language state used when referring to all languages.
   */
  const STATE_ALL = 3;

  /**
   * The language state used when referring to the site's default language.
   */
  const STATE_SITE_DEFAULT = 4;

  /**
   * The type of language used to define the content language.
   */
  const TYPE_CONTENT = 'language_content';

  /**
   * The type of language used to select the user interface.
   */
  const TYPE_INTERFACE = 'language_interface';

  /**
   * The type of language used for URLs.
   */
  const TYPE_URL = 'language_url';

  /**
   * Language written left to right. Possible value of $language->direction.
   */
  const DIRECTION_LTR = 0;

  /**
   * Language written right to left. Possible value of $language->direction.
   */
  const DIRECTION_RTL = 1;

164 165 166 167 168 169 170
  /**
   * Language constructor builds the default language object.
   *
   * @param array $options
   *   The properties used to construct the language.
   */
  public function __construct(array $options = array()) {
171
    // Set all the provided properties for the language.
172
    foreach ($options as $name => $value) {
173
      $this->{$name} = $value;
174
    }
175 176
    // If some options were not set, set sane defaults of a predefined language.
    if (!isset($options['name']) || !isset($options['direction'])) {
177
      $predefined = LanguageManager::getStandardLanguageList();
178
      if (isset($predefined[$this->id])) {
179
        if (!isset($options['name'])) {
180
          $this->name = $predefined[$this->id][0];
181
        }
182 183
        if (!isset($options['direction']) && isset($predefined[$this->id][2])) {
          $this->direction = $predefined[$this->id][2];
184 185 186
        }
      }
    }
187 188 189 190 191 192 193 194
  }

  /**
   * Extend $this with properties from the given object.
   *
   * @todo Remove this function once $GLOBALS['language'] is gone.
   */
  public function extend($obj) {
195 196 197
    $variables = get_object_vars($obj);
    foreach ($variables as $variable => $value) {
      $this->$variable = $value;
198 199
    }
  }
200 201 202 203 204 205 206

  /**
   * Sort language objects.
   *
   * @param array $languages
   *   The array of language objects keyed by langcode.
   */
207
  public static function sort(&$languages) {
208 209 210 211 212 213 214 215 216 217
    uasort($languages, function ($a, $b) {
      $a_weight = isset($a->weight) ? $a->weight : 0;
      $b_weight = isset($b->weight) ? $b->weight : 0;
      if ($a_weight == $b_weight) {
        return strnatcasecmp($a->name, $b->name);
      }
      return ($a_weight < $b_weight) ? -1 : 1;
    });
  }

218
}