1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@link Localization_Locale} class. |
4
|
|
|
* @package Localization |
5
|
|
|
* @subpackage Core |
6
|
|
|
* @see Localization_Locale |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace AppLocalize; |
10
|
|
|
|
11
|
|
|
use AppUtils\FileHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Individual locale representation with information about |
15
|
|
|
* the country and currency. |
16
|
|
|
* |
17
|
|
|
* @package Localization |
18
|
|
|
* @subpackage Core |
19
|
|
|
* @author Sebastian Mordziol <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class Localization_Locale |
22
|
|
|
{ |
23
|
|
|
const ERROR_LOCALE_LABEL_MISSING = 39102; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $localeName; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var Localization_Country |
32
|
|
|
*/ |
33
|
|
|
protected $country; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $countryCode; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $languageCode; |
44
|
|
|
|
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
$localeName = explode('\\', get_class($this)); |
48
|
|
|
$localeName = array_pop($localeName); |
49
|
|
|
$tokens = explode('_', $localeName); |
50
|
|
|
|
51
|
|
|
$this->localeName = $localeName; |
52
|
|
|
$this->countryCode = strtolower($tokens[1]); |
53
|
|
|
$this->languageCode = strtolower($tokens[0]); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Retrieves the two-letter language code of the locale. |
58
|
|
|
* |
59
|
|
|
* @return string Language code, e.g. "en", "de" |
60
|
|
|
*/ |
61
|
|
|
public function getLanguageCode() : string |
62
|
|
|
{ |
63
|
|
|
return $this->languageCode; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Checks whether the specified locale name is known |
68
|
|
|
* (supported by the application). |
69
|
|
|
* |
70
|
|
|
* @param string $localeName |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
public static function isLocaleKnown(string $localeName) : bool |
74
|
|
|
{ |
75
|
|
|
return Localization::isLocaleSupported($localeName); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the locale name, e.g. "en_US" |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getName() : string |
83
|
|
|
{ |
84
|
|
|
return $this->localeName; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retrieves the shortened version of the locale name, |
89
|
|
|
* e.g. "en" or "de". |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
* @deprecated |
93
|
|
|
* @see Localization_Locale::getLanguageCode() |
94
|
|
|
*/ |
95
|
|
|
public function getShortName() : string |
96
|
|
|
{ |
97
|
|
|
return $this->getLanguageCode(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Retrieves the two-letter country code of |
102
|
|
|
* the locale. |
103
|
|
|
* |
104
|
|
|
* @return string Lowercase code, e.g. "uk" |
105
|
|
|
*/ |
106
|
|
|
public function getCountryCode() : string |
107
|
|
|
{ |
108
|
|
|
return $this->countryCode; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Checks if this locale is the builtin application locale |
113
|
|
|
* (the one in which application strings are written). |
114
|
|
|
* |
115
|
|
|
* @return boolean |
116
|
|
|
* @see Localization::BUILTIN_LOCALE_NAME |
117
|
|
|
*/ |
118
|
|
|
public function isNative() : bool |
119
|
|
|
{ |
120
|
|
|
return $this->getName() == Localization::BUILTIN_LOCALE_NAME; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns the localized label for the locale, e.g. "German" |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
abstract public function getLabel() : string; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Retrieves the country object for this locale |
132
|
|
|
* |
133
|
|
|
* @return Localization_Country |
134
|
|
|
* |
135
|
|
|
* @throws Localization_Exception |
136
|
|
|
* @see Localization::ERROR_COUNTRY_NOT_FOUND |
137
|
|
|
*/ |
138
|
|
|
public function getCountry() : Localization_Country |
139
|
|
|
{ |
140
|
|
|
if(!isset($this->country)) { |
141
|
|
|
$this->country = Localization::createCountry($this->countryCode); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->country; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Retrieves the currency object for this locale |
149
|
|
|
* |
150
|
|
|
* @return Localization_Currency |
151
|
|
|
* |
152
|
|
|
* @throws Localization_Exception |
153
|
|
|
* @see Localization::ERROR_COUNTRY_NOT_FOUND |
154
|
|
|
*/ |
155
|
|
|
public function getCurrency() : Localization_Currency |
156
|
|
|
{ |
157
|
|
|
return $this->getCountry()->getCurrency(); |
158
|
|
|
} |
159
|
|
|
} |