blob: 1fb5bafdc84a0f42643d00dadfc19d91199612a0 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/**
4 * This file is part of the Carbon package.
5 *
6 * (c) Brian Nesbitt <brian@nesbot.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010011
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020012namespace Carbon;
13
14use JsonSerializable;
15use ReturnTypeWillChange;
16
17class Language implements JsonSerializable
18{
19 /**
20 * @var array
21 */
22 protected static $languagesNames;
23
24 /**
25 * @var array
26 */
27 protected static $regionsNames;
28
29 /**
30 * @var string
31 */
32 protected $id;
33
34 /**
35 * @var string
36 */
37 protected $code;
38
39 /**
40 * @var string|null
41 */
42 protected $variant;
43
44 /**
45 * @var string|null
46 */
47 protected $region;
48
49 /**
50 * @var array
51 */
52 protected $names;
53
54 /**
55 * @var string
56 */
57 protected $isoName;
58
59 /**
60 * @var string
61 */
62 protected $nativeName;
63
64 public function __construct(string $id)
65 {
66 $this->id = str_replace('-', '_', $id);
67 $parts = explode('_', $this->id);
68 $this->code = $parts[0];
69
70 if (isset($parts[1])) {
71 if (!preg_match('/^[A-Z]+$/', $parts[1])) {
72 $this->variant = $parts[1];
73 $parts[1] = $parts[2] ?? null;
74 }
75 if ($parts[1]) {
76 $this->region = $parts[1];
77 }
78 }
79 }
80
81 /**
82 * Get the list of the known languages.
83 *
84 * @return array
85 */
86 public static function all()
87 {
88 if (!static::$languagesNames) {
89 static::$languagesNames = require __DIR__.'/List/languages.php';
90 }
91
92 return static::$languagesNames;
93 }
94
95 /**
96 * Get the list of the known regions.
97 *
98 * @return array
99 */
100 public static function regions()
101 {
102 if (!static::$regionsNames) {
103 static::$regionsNames = require __DIR__.'/List/regions.php';
104 }
105
106 return static::$regionsNames;
107 }
108
109 /**
110 * Get both isoName and nativeName as an array.
111 *
112 * @return array
113 */
114 public function getNames(): array
115 {
116 if (!$this->names) {
117 $this->names = static::all()[$this->code] ?? [
118 'isoName' => $this->code,
119 'nativeName' => $this->code,
120 ];
121 }
122
123 return $this->names;
124 }
125
126 /**
127 * Returns the original locale ID.
128 *
129 * @return string
130 */
131 public function getId(): string
132 {
133 return $this->id;
134 }
135
136 /**
137 * Returns the code of the locale "en"/"fr".
138 *
139 * @return string
140 */
141 public function getCode(): string
142 {
143 return $this->code;
144 }
145
146 /**
147 * Returns the variant code such as cyrl/latn.
148 *
149 * @return string|null
150 */
151 public function getVariant(): ?string
152 {
153 return $this->variant;
154 }
155
156 /**
157 * Returns the variant such as Cyrillic/Latin.
158 *
159 * @return string|null
160 */
161 public function getVariantName(): ?string
162 {
163 if ($this->variant === 'Latn') {
164 return 'Latin';
165 }
166
167 if ($this->variant === 'Cyrl') {
168 return 'Cyrillic';
169 }
170
171 return $this->variant;
172 }
173
174 /**
175 * Returns the region part of the locale.
176 *
177 * @return string|null
178 */
179 public function getRegion(): ?string
180 {
181 return $this->region;
182 }
183
184 /**
185 * Returns the region name for the current language.
186 *
187 * @return string|null
188 */
189 public function getRegionName(): ?string
190 {
191 return $this->region ? (static::regions()[$this->region] ?? $this->region) : null;
192 }
193
194 /**
195 * Returns the long ISO language name.
196 *
197 * @return string
198 */
199 public function getFullIsoName(): string
200 {
201 if (!$this->isoName) {
202 $this->isoName = $this->getNames()['isoName'];
203 }
204
205 return $this->isoName;
206 }
207
208 /**
209 * Set the ISO language name.
210 *
211 * @param string $isoName
212 */
213 public function setIsoName(string $isoName): self
214 {
215 $this->isoName = $isoName;
216
217 return $this;
218 }
219
220 /**
221 * Return the full name of the language in this language.
222 *
223 * @return string
224 */
225 public function getFullNativeName(): string
226 {
227 if (!$this->nativeName) {
228 $this->nativeName = $this->getNames()['nativeName'];
229 }
230
231 return $this->nativeName;
232 }
233
234 /**
235 * Set the name of the language in this language.
236 *
237 * @param string $nativeName
238 */
239 public function setNativeName(string $nativeName): self
240 {
241 $this->nativeName = $nativeName;
242
243 return $this;
244 }
245
246 /**
247 * Returns the short ISO language name.
248 *
249 * @return string
250 */
251 public function getIsoName(): string
252 {
253 $name = $this->getFullIsoName();
254
255 return trim(strstr($name, ',', true) ?: $name);
256 }
257
258 /**
259 * Get the short name of the language in this language.
260 *
261 * @return string
262 */
263 public function getNativeName(): string
264 {
265 $name = $this->getFullNativeName();
266
267 return trim(strstr($name, ',', true) ?: $name);
268 }
269
270 /**
271 * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
272 *
273 * @return string
274 */
275 public function getIsoDescription()
276 {
277 $region = $this->getRegionName();
278 $variant = $this->getVariantName();
279
280 return $this->getIsoName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
281 }
282
283 /**
284 * Get a string with short native name, region in parentheses if applicable, variant in parentheses if applicable.
285 *
286 * @return string
287 */
288 public function getNativeDescription()
289 {
290 $region = $this->getRegionName();
291 $variant = $this->getVariantName();
292
293 return $this->getNativeName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
294 }
295
296 /**
297 * Get a string with long ISO name, region in parentheses if applicable, variant in parentheses if applicable.
298 *
299 * @return string
300 */
301 public function getFullIsoDescription()
302 {
303 $region = $this->getRegionName();
304 $variant = $this->getVariantName();
305
306 return $this->getFullIsoName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
307 }
308
309 /**
310 * Get a string with long native name, region in parentheses if applicable, variant in parentheses if applicable.
311 *
312 * @return string
313 */
314 public function getFullNativeDescription()
315 {
316 $region = $this->getRegionName();
317 $variant = $this->getVariantName();
318
319 return $this->getFullNativeName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
320 }
321
322 /**
323 * Returns the original locale ID.
324 *
325 * @return string
326 */
327 public function __toString()
328 {
329 return $this->getId();
330 }
331
332 /**
333 * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
334 *
335 * @return string
336 */
337 #[ReturnTypeWillChange]
338 public function jsonSerialize()
339 {
340 return $this->getIsoDescription();
341 }
342}