1
<?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
 */
11
namespace Carbon;
12

13
use JsonSerializable;
14

15
class Language implements JsonSerializable
16
{
17
    /**
18
     * @var array
19
     */
20
    protected static $languagesNames;
21

22
    /**
23
     * @var array
24
     */
25
    protected static $regionsNames;
26

27
    /**
28
     * @var string
29
     */
30
    protected $id;
31

32
    /**
33
     * @var string
34
     */
35
    protected $code;
36

37
    /**
38
     * @var string|null
39
     */
40
    protected $variant;
41

42
    /**
43
     * @var string|null
44
     */
45
    protected $region;
46

47
    /**
48
     * @var array
49
     */
50
    protected $names;
51

52
    /**
53
     * @var string
54
     */
55
    protected $isoName;
56

57
    /**
58
     * @var string
59
     */
60
    protected $nativeName;
61

62 1
    public function __construct(string $id)
63
    {
64 1
        $this->id = str_replace('-', '_', $id);
65 1
        $parts = explode('_', $this->id);
66 1
        $this->code = $parts[0];
67

68 1
        if (isset($parts[1])) {
69 1
            if (!preg_match('/^[A-Z]+$/', $parts[1])) {
70 1
                $this->variant = $parts[1];
71 1
                $parts[1] = $parts[2] ?? null;
72
            }
73 1
            if ($parts[1]) {
74 1
                $this->region = $parts[1];
75
            }
76
        }
77
    }
78

79
    /**
80
     * Get the list of the known languages.
81
     *
82
     * @return array
83
     */
84 1
    public static function all()
85
    {
86 1
        if (!static::$languagesNames) {
87 1
            static::$languagesNames = require __DIR__.'/List/languages.php';
88
        }
89

90 1
        return static::$languagesNames;
91
    }
92

93
    /**
94
     * Get the list of the known regions.
95
     *
96
     * @return array
97
     */
98 1
    public static function regions()
99
    {
100 1
        if (!static::$regionsNames) {
101 1
            static::$regionsNames = require __DIR__.'/List/regions.php';
102
        }
103

104 1
        return static::$regionsNames;
105
    }
106

107
    /**
108
     * Get both isoName and nativeName as an array.
109
     *
110
     * @return array
111
     */
112 1
    public function getNames(): array
113
    {
114 1
        if (!$this->names) {
115 1
            $this->names = static::all()[$this->code] ?? [
116 1
                'isoName' => $this->code,
117 1
                'nativeName' => $this->code,
118
            ];
119
        }
120

121 1
        return $this->names;
122
    }
123

124
    /**
125
     * Returns the original locale ID.
126
     *
127
     * @return string
128
     */
129 1
    public function getId(): string
130
    {
131 1
        return $this->id;
132
    }
133

134
    /**
135
     * Returns the code of the locale "en"/"fr".
136
     *
137
     * @return string
138
     */
139 1
    public function getCode(): string
140
    {
141 1
        return $this->code;
142
    }
143

144
    /**
145
     * Returns the variant code such as cyrl/latn.
146
     *
147
     * @return string|null
148
     */
149 1
    public function getVariant(): ?string
150
    {
151 1
        return $this->variant;
152
    }
153

154
    /**
155
     * Returns the variant such as Cyrillic/Latin.
156
     *
157
     * @return string|null
158
     */
159 1
    public function getVariantName(): ?string
160
    {
161 1
        if ($this->variant === 'Latn') {
162 1
            return 'Latin';
163
        }
164

165 1
        if ($this->variant === 'Cyrl') {
166 1
            return 'Cyrillic';
167
        }
168

169 1
        return $this->variant;
170
    }
171

172
    /**
173
     * Returns the region part of the locale.
174
     *
175
     * @return string|null
176
     */
177 1
    public function getRegion(): ?string
178
    {
179 1
        return $this->region;
180
    }
181

182
    /**
183
     * Returns the region name for the current language.
184
     *
185
     * @return string|null
186
     */
187 1
    public function getRegionName(): ?string
188
    {
189 1
        return $this->region ? (static::regions()[$this->region] ?? $this->region) : null;
190
    }
191

192
    /**
193
     * Returns the long ISO language name.
194
     *
195
     * @return string
196
     */
197 1
    public function getFullIsoName(): string
198
    {
199 1
        if (!$this->isoName) {
200 1
            $this->isoName = $this->getNames()['isoName'];
201
        }
202

203 1
        return $this->isoName;
204
    }
205

206
    /**
207
     * Set the ISO language name.
208
     *
209
     * @param string $isoName
210
     */
211 1
    public function setIsoName(string $isoName): self
212
    {
213 1
        $this->isoName = $isoName;
214

215 1
        return $this;
216
    }
217

218
    /**
219
     * Return the full name of the language in this language.
220
     *
221
     * @return string
222
     */
223 1
    public function getFullNativeName(): string
224
    {
225 1
        if (!$this->nativeName) {
226 1
            $this->nativeName = $this->getNames()['nativeName'];
227
        }
228

229 1
        return $this->nativeName;
230
    }
231

232
    /**
233
     * Set the name of the language in this language.
234
     *
235
     * @param string $nativeName
236
     */
237 1
    public function setNativeName(string $nativeName): self
238
    {
239 1
        $this->nativeName = $nativeName;
240

241 1
        return $this;
242
    }
243

244
    /**
245
     * Returns the short ISO language name.
246
     *
247
     * @return string
248
     */
249 1
    public function getIsoName(): string
250
    {
251 1
        $name = $this->getFullIsoName();
252

253 1
        return trim(strstr($name, ',', true) ?: $name);
254
    }
255

256
    /**
257
     * Get the short name of the language in this language.
258
     *
259
     * @return string
260
     */
261 1
    public function getNativeName(): string
262
    {
263 1
        $name = $this->getFullNativeName();
264

265 1
        return trim(strstr($name, ',', true) ?: $name);
266
    }
267

268
    /**
269
     * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
270
     *
271
     * @return string
272
     */
273 1
    public function getIsoDescription()
274
    {
275 1
        $region = $this->getRegionName();
276 1
        $variant = $this->getVariantName();
277

278 1
        return $this->getIsoName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
279
    }
280

281
    /**
282
     * Get a string with short native name, region in parentheses if applicable, variant in parentheses if applicable.
283
     *
284
     * @return string
285
     */
286 1
    public function getNativeDescription()
287
    {
288 1
        $region = $this->getRegionName();
289 1
        $variant = $this->getVariantName();
290

291 1
        return $this->getNativeName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
292
    }
293

294
    /**
295
     * Get a string with long ISO name, region in parentheses if applicable, variant in parentheses if applicable.
296
     *
297
     * @return string
298
     */
299 1
    public function getFullIsoDescription()
300
    {
301 1
        $region = $this->getRegionName();
302 1
        $variant = $this->getVariantName();
303

304 1
        return $this->getFullIsoName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
305
    }
306

307
    /**
308
     * Get a string with long native name, region in parentheses if applicable, variant in parentheses if applicable.
309
     *
310
     * @return string
311
     */
312 1
    public function getFullNativeDescription()
313
    {
314 1
        $region = $this->getRegionName();
315 1
        $variant = $this->getVariantName();
316

317 1
        return $this->getFullNativeName().($region ? ' ('.$region.')' : '').($variant ? ' ('.$variant.')' : '');
318
    }
319

320
    /**
321
     * Returns the original locale ID.
322
     *
323
     * @return string
324
     */
325 1
    public function __toString()
326
    {
327 1
        return $this->getId();
328
    }
329

330
    /**
331
     * Get a string with short ISO name, region in parentheses if applicable, variant in parentheses if applicable.
332
     *
333
     * @return string
334
     */
335 1
    public function jsonSerialize()
336
    {
337 1
        return $this->getIsoDescription();
338
    }
339
}

Read our documentation on viewing source code .

Loading