forked from Rudloff/alltube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Locale.php
117 lines (104 loc) · 2.1 KB
/
Locale.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
113
114
115
116
117
<?php
/**
* Locale class.
*/
namespace Alltube;
use Locale as PHPLocale;
use Rinvex\Country\Country;
use Teto\HTTP\AcceptLanguage;
/**
* Class used to represent locales.
*/
class Locale
{
/**
* Locale language.
*
* @var string
*/
private $language;
/**
* Locale region.
*
* @var string
*/
private $region;
/**
* Locale constructor.
*
* @param string $locale ISO 15897 code
*/
public function __construct(string $locale)
{
$parse = AcceptLanguage::parse($locale);
$this->language = $parse[1]['language'];
if (!empty($parse[1]['region'])) {
$this->region = $parse[1]['region'];
}
}
/**
* Convert the locale to a string.
*
* @return string ISO 15897 code
*/
public function __toString(): string
{
return $this->getIso15897();
}
/**
* Get the full name of the locale.
*
* @return string
*/
public function getFullName(): string
{
return PHPLocale::getDisplayName($this->getIso15897(), $this->getIso15897());
}
/**
* Get the ISO 15897 code.
*
* @return string
*/
public function getIso15897(): string
{
if (isset($this->region)) {
return $this->language . '_' . $this->region;
} else {
return $this->language;
}
}
/**
* Get the BCP 47 code.
*
* @return string
*/
public function getBcp47(): string
{
if (isset($this->region)) {
return $this->language . '-' . $this->region;
} else {
return $this->language;
}
}
/**
* Get the ISO 3166 code.
*
* @return string
*/
public function getIso3166(): string
{
return strtolower($this->region);
}
/**
* Get country information from locale.
*
* @return Country|Country[]|null
*/
public function getCountry()
{
if (isset($this->region)) {
return country($this->getIso3166());
}
return null;
}
}