Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Core\Language;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\StringTranslation\TranslatableMarkup;
|
Chris@0
|
6
|
Chris@0
|
7 /**
|
Chris@0
|
8 * An object containing the information for an interface language.
|
Chris@0
|
9 *
|
Chris@0
|
10 * @see \Drupal\Core\Language\LanguageManager::getLanguage()
|
Chris@0
|
11 */
|
Chris@0
|
12 class Language implements LanguageInterface {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * The values to use to instantiate the default language.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var array
|
Chris@0
|
18 */
|
Chris@0
|
19 public static $defaultValues = [
|
Chris@0
|
20 'id' => 'en',
|
Chris@0
|
21 'name' => 'English',
|
Chris@0
|
22 'direction' => self::DIRECTION_LTR,
|
Chris@0
|
23 'weight' => 0,
|
Chris@0
|
24 'locked' => FALSE,
|
Chris@0
|
25 ];
|
Chris@0
|
26
|
Chris@0
|
27 // Properties within the Language are set up as the default language.
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * The human readable English name.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var string
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $name = '';
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * The ID, langcode.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @var string
|
Chris@0
|
40 */
|
Chris@0
|
41 protected $id = '';
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * The direction, left-to-right, or right-to-left.
|
Chris@0
|
45 *
|
Chris@0
|
46 * Defined using constants, either self::DIRECTION_LTR or self::DIRECTION_RTL.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @var int
|
Chris@0
|
49 */
|
Chris@0
|
50 protected $direction = self::DIRECTION_LTR;
|
Chris@0
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * The weight, used for ordering languages in lists, like selects or tables.
|
Chris@0
|
54 *
|
Chris@0
|
55 * @var int
|
Chris@0
|
56 */
|
Chris@0
|
57 protected $weight = 0;
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * Locked indicates a language used by the system, not an actual language.
|
Chris@0
|
61 *
|
Chris@0
|
62 * Examples of locked languages are, LANGCODE_NOT_SPECIFIED, und, and
|
Chris@0
|
63 * LANGCODE_NOT_APPLICABLE, zxx, which are usually shown in language selects
|
Chris@0
|
64 * but hidden in places like the Language configuration and cannot be deleted.
|
Chris@0
|
65 *
|
Chris@0
|
66 * @var bool
|
Chris@0
|
67 */
|
Chris@0
|
68 protected $locked = FALSE;
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Constructs a new class instance.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @param array $values
|
Chris@0
|
74 * An array of property values, keyed by property name, used to construct
|
Chris@0
|
75 * the language.
|
Chris@0
|
76 */
|
Chris@0
|
77 public function __construct(array $values = []) {
|
Chris@0
|
78 // Set all the provided properties for the language.
|
Chris@0
|
79 foreach ($values as $key => $value) {
|
Chris@0
|
80 if (property_exists($this, $key)) {
|
Chris@0
|
81 $this->{$key} = $value;
|
Chris@0
|
82 }
|
Chris@0
|
83 }
|
Chris@0
|
84 // If some values were not set, set sane defaults of a predefined language.
|
Chris@0
|
85 if (!isset($values['name']) || !isset($values['direction'])) {
|
Chris@0
|
86 $predefined = LanguageManager::getStandardLanguageList();
|
Chris@0
|
87 if (isset($predefined[$this->id])) {
|
Chris@0
|
88 if (!isset($values['name'])) {
|
Chris@0
|
89 $this->name = $predefined[$this->id][0];
|
Chris@0
|
90 }
|
Chris@0
|
91 if (!isset($values['direction']) && isset($predefined[$this->id][2])) {
|
Chris@0
|
92 $this->direction = $predefined[$this->id][2];
|
Chris@0
|
93 }
|
Chris@0
|
94 }
|
Chris@0
|
95 }
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 /**
|
Chris@0
|
99 * {@inheritdoc}
|
Chris@0
|
100 */
|
Chris@0
|
101 public function getName() {
|
Chris@0
|
102 return $this->name;
|
Chris@0
|
103 }
|
Chris@0
|
104
|
Chris@0
|
105 /**
|
Chris@0
|
106 * {@inheritdoc}
|
Chris@0
|
107 */
|
Chris@0
|
108 public function getId() {
|
Chris@0
|
109 return $this->id;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 /**
|
Chris@0
|
113 * {@inheritdoc}
|
Chris@0
|
114 */
|
Chris@0
|
115 public function getDirection() {
|
Chris@0
|
116 return $this->direction;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * {@inheritdoc}
|
Chris@0
|
121 */
|
Chris@0
|
122 public function getWeight() {
|
Chris@0
|
123 return $this->weight;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 public function isDefault() {
|
Chris@0
|
130 return static::getDefaultLangcode() == $this->getId();
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * {@inheritdoc}
|
Chris@0
|
135 */
|
Chris@0
|
136 public function isLocked() {
|
Chris@0
|
137 return (bool) $this->locked;
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * Sort language objects.
|
Chris@0
|
142 *
|
Chris@0
|
143 * @param \Drupal\Core\Language\LanguageInterface[] $languages
|
Chris@0
|
144 * The array of language objects keyed by langcode.
|
Chris@0
|
145 */
|
Chris@0
|
146 public static function sort(&$languages) {
|
Chris@0
|
147 uasort($languages, function (LanguageInterface $a, LanguageInterface $b) {
|
Chris@0
|
148 $a_weight = $a->getWeight();
|
Chris@0
|
149 $b_weight = $b->getWeight();
|
Chris@0
|
150 if ($a_weight == $b_weight) {
|
Chris@0
|
151 $a_name = $a->getName();
|
Chris@0
|
152 $b_name = $b->getName();
|
Chris@0
|
153 // If either name is a TranslatableMarkup object it can not be converted
|
Chris@0
|
154 // to a string. This is because translation requires a sorted list of
|
Chris@0
|
155 // languages thereby causing an infinite loop. Determine the order based
|
Chris@0
|
156 // on ID if this is the case.
|
Chris@0
|
157 if ($a_name instanceof TranslatableMarkup || $b_name instanceof TranslatableMarkup) {
|
Chris@0
|
158 $a_name = $a->getId();
|
Chris@0
|
159 $b_name = $b->getId();
|
Chris@0
|
160 }
|
Chris@0
|
161 return strnatcasecmp($a_name, $b_name);
|
Chris@0
|
162 }
|
Chris@0
|
163 return ($a_weight < $b_weight) ? -1 : 1;
|
Chris@0
|
164 });
|
Chris@0
|
165 }
|
Chris@0
|
166
|
Chris@0
|
167 /**
|
Chris@0
|
168 * Gets the default langcode.
|
Chris@0
|
169 *
|
Chris@0
|
170 * @return string
|
Chris@0
|
171 * The current default langcode.
|
Chris@0
|
172 */
|
Chris@0
|
173 protected static function getDefaultLangcode() {
|
Chris@0
|
174 $language = \Drupal::service('language.default')->get();
|
Chris@0
|
175 return $language->getId();
|
Chris@0
|
176 }
|
Chris@0
|
177
|
Chris@0
|
178 }
|