2ad4b89d5850912b8a73fb28a5bf8ba0b1bdafea
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Site / Entity / SiteLanguage.php
1 <?php
2 declare(strict_types = 1);
3
4 namespace TYPO3\CMS\Core\Site\Entity;
5
6 /*
7 * This file is part of the TYPO3 CMS project.
8 *
9 * It is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License, either version 2
11 * of the License, or any later version.
12 *
13 * For the full copyright and license information, please read the
14 * LICENSE.txt file that was distributed with this source code.
15 *
16 * The TYPO3 project - inspiring people to share!
17 */
18
19 /**
20 * Entity representing a site_language configuration of a site object.
21 */
22 class SiteLanguage
23 {
24 /**
25 * The language mapped to the sys_language DB entry.
26 *
27 * @var int
28 */
29 protected $languageId;
30
31 /**
32 * Locale, like 'de_CH' or 'en_GB'
33 *
34 * @var string
35 */
36 protected $locale;
37
38 /**
39 * The Base URL for this language
40 *
41 * @var string
42 */
43 protected $base;
44
45 /**
46 * Label to be used within TYPO3 to identify the language
47 * @var string
48 */
49 protected $title = 'Default';
50
51 /**
52 * Label to be used within language menus
53 * @var string
54 */
55 protected $navigationTitle = '';
56
57 /**
58 * The flag key (like "gb" or "fr") used to be used in TYPO3's Backend.
59 * @var string
60 */
61 protected $flagIdentifier = 'us';
62
63 /**
64 * The iso code for this language (two letter) ISO-639-1
65 * @var string
66 */
67 protected $twoLetterIsoCode = 'en';
68
69 /**
70 * Language tag for this language defined by RFC 1766 / 3066 for "lang"
71 * and "hreflang" attributes
72 *
73 * @var string
74 */
75 protected $hreflang = 'en-US';
76
77 /**
78 * The direction for this language
79 * @var string
80 */
81 protected $direction = '';
82
83 /**
84 * Prefix for TYPO3's language files
85 * "default" for english, otherwise one of TYPO3's internal language keys.
86 * Previously configured via TypoScript config.language = fr
87 *
88 * @var string
89 */
90 protected $typo3Language = 'default';
91
92 /**
93 * @var string
94 */
95 protected $fallbackType = 'strict';
96
97 /**
98 * @var array
99 */
100 protected $fallbackLanguageIds = [];
101
102 /**
103 * @var bool
104 */
105 protected $enabled = true;
106
107 /**
108 * Additional parameters configured for this site language
109 * @var array
110 */
111 protected $attributes = [];
112
113 /**
114 * SiteLanguage constructor.
115 *
116 * @param int $languageId
117 * @param string $locale
118 * @param string $base
119 * @param array $attributes
120 */
121 public function __construct(int $languageId, string $locale, string $base, array $attributes)
122 {
123 $this->languageId = $languageId;
124 $this->locale = $locale;
125 $this->base = $base;
126 $this->attributes = $attributes;
127 if (!empty($attributes['title'])) {
128 $this->title = $attributes['title'];
129 }
130 if (!empty($attributes['navigationTitle'])) {
131 $this->navigationTitle = $attributes['navigationTitle'];
132 }
133 if (!empty($attributes['flag'])) {
134 $this->flagIdentifier = $attributes['flag'];
135 }
136 if (!empty($attributes['typo3Language'])) {
137 $this->typo3Language = $attributes['typo3Language'];
138 }
139 if (!empty($attributes['iso-639-1'])) {
140 $this->twoLetterIsoCode = $attributes['iso-639-1'];
141 }
142 if (!empty($attributes['hreflang'])) {
143 $this->hreflang = $attributes['hreflang'];
144 }
145 if (!empty($attributes['direction'])) {
146 $this->direction = $attributes['direction'];
147 }
148 if (!empty($attributes['fallbackType'])) {
149 $this->fallbackType = $attributes['fallbackType'];
150 }
151 if (!empty($attributes['fallbacks'])) {
152 $this->fallbackLanguageIds = $attributes['fallbacks'];
153 }
154 if (isset($attributes['enabled'])) {
155 $this->enabled = (bool)$attributes['enabled'];
156 }
157 }
158
159 /**
160 * Returns the SiteLanguage in an array representation for e.g. the usage
161 * in TypoScript.
162 *
163 * @return array
164 */
165 public function toArray(): array
166 {
167 return [
168 'languageId' => $this->getLanguageId(),
169 'locale' => $this->getLocale(),
170 'base' => $this->getBase(),
171 'title' => $this->getTitle(),
172 'navigationTitle' => $this->getNavigationTitle(),
173 'twoLetterIsoCode' => $this->getTwoLetterIsoCode(),
174 'hreflang' => $this->getHreflang(),
175 'direction' => $this->getDirection(),
176 'typo3Language' => $this->getTypo3Language(),
177 'flagIdentifier' => $this->getFlagIdentifier(),
178 'fallbackType' => $this->getFallbackType(),
179 'enabled' => $this->enabled(),
180 'fallbackLanguageIds' => $this->getFallbackLanguageIds(),
181 ];
182 }
183
184 /**
185 * @return int
186 */
187 public function getLanguageId(): int
188 {
189 return $this->languageId;
190 }
191
192 /**
193 * @return string
194 */
195 public function getLocale(): string
196 {
197 return $this->locale;
198 }
199
200 /**
201 * @return string
202 */
203 public function getBase(): string
204 {
205 return $this->base;
206 }
207
208 /**
209 * @return string
210 */
211 public function getTitle(): string
212 {
213 return $this->title;
214 }
215
216 /**
217 * @return string
218 */
219 public function getNavigationTitle(): string
220 {
221 return $this->navigationTitle ?: $this->getTitle();
222 }
223
224 /**
225 * @return string
226 */
227 public function getFlagIdentifier(): string
228 {
229 return $this->flagIdentifier;
230 }
231
232 /**
233 * @return string
234 */
235 public function getTypo3Language(): string
236 {
237 return $this->typo3Language;
238 }
239
240 /**
241 * Returns the ISO-639-1 language ISO code
242 *
243 * @return string
244 */
245 public function getTwoLetterIsoCode(): string
246 {
247 return $this->twoLetterIsoCode;
248 }
249
250 /**
251 * Returns the RFC 1766 / 3066 language tag
252 *
253 * @return string
254 */
255 public function getHreflang(): string
256 {
257 return $this->hreflang;
258 }
259
260 /**
261 * Returns the language direction
262 *
263 * @return string
264 */
265 public function getDirection(): string
266 {
267 return $this->direction;
268 }
269
270 /**
271 * Returns true if the language is available in frontend usage
272 *
273 * @return bool
274 */
275 public function enabled(): bool
276 {
277 return $this->enabled;
278 }
279
280 /**
281 * @return string
282 */
283 public function getFallbackType(): string
284 {
285 return $this->fallbackType;
286 }
287
288 /**
289 * @return array
290 */
291 public function getFallbackLanguageIds(): array
292 {
293 return $this->fallbackLanguageIds;
294 }
295 }