Language.php 4.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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 {
  // Properties within the Language are set up as the default language.
21
  public $name = '';
22
  public $id = '';
23
  public $direction = Language::DIRECTION_LTR;
24
25
26
  public $weight = 0;
  public $default = FALSE;
  public $method_id = NULL;
27
  public $locked = FALSE;
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
70
71
72
73
74
75
76
77
78
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
  /**
   * 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.
   *
   * @todo: Change value to differ from Language::LANGCODE_NOT_SPECIFIED once
   * field API leverages the property API.
   */
  const LANGCODE_DEFAULT = 'und';

  /**
   * 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;

113
114
115
116
117
118
119
  /**
   * Language constructor builds the default language object.
   *
   * @param array $options
   *   The properties used to construct the language.
   */
  public function __construct(array $options = array()) {
120
    // Set all the provided properties for the language.
121
    foreach ($options as $name => $value) {
122
      $this->{$name} = $value;
123
    }
124
125
    // If some options were not set, set sane defaults of a predefined language.
    if (!isset($options['name']) || !isset($options['direction'])) {
126
      $predefined = LanguageManager::getStandardLanguageList();
127
      if (isset($predefined[$this->id])) {
128
        if (!isset($options['name'])) {
129
          $this->name = $predefined[$this->id][0];
130
        }
131
132
        if (!isset($options['direction']) && isset($predefined[$this->id][2])) {
          $this->direction = $predefined[$this->id][2];
133
134
135
        }
      }
    }
136
137
138
139
140
141
142
143
  }

  /**
   * Extend $this with properties from the given object.
   *
   * @todo Remove this function once $GLOBALS['language'] is gone.
   */
  public function extend($obj) {
144
145
146
    $variables = get_object_vars($obj);
    foreach ($variables as $variable => $value) {
      $this->$variable = $value;
147
148
    }
  }
149
150
151
152
153
154
155

  /**
   * Sort language objects.
   *
   * @param array $languages
   *   The array of language objects keyed by langcode.
   */
156
  public static function sort(&$languages) {
157
158
159
160
161
162
163
164
165
166
    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;
    });
  }

167
}