|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.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 |
|
|
|
12 |
namespace Symfony\Component\Locale\Stub; |
|
|
13 |
|
|
|
14 |
use Symfony\Component\Locale\Exception\NotImplementedException; |
|
|
15 |
use Symfony\Component\Locale\Exception\MethodNotImplementedException; |
|
|
16 |
|
|
|
17 |
/** |
|
|
18 |
* Provides a stub Locale for the 'en' locale. |
|
|
19 |
* |
|
|
20 |
* @author Eriksen Costa <eriksen.costa@infranology.com.br> |
|
|
21 |
*/ |
|
|
22 |
class StubLocale |
|
|
23 |
{ |
|
|
24 |
const DEFAULT_LOCALE = null; |
|
|
25 |
|
|
|
26 |
/** Locale method constants */ |
|
|
27 |
const ACTUAL_LOCALE = 0; |
|
|
28 |
const VALID_LOCALE = 1; |
|
|
29 |
|
|
|
30 |
/** Language tags constants */ |
|
|
31 |
const LANG_TAG = 'language'; |
|
|
32 |
const EXTLANG_TAG = 'extlang'; |
|
|
33 |
const SCRIPT_TAG = 'script'; |
|
|
34 |
const REGION_TAG = 'region'; |
|
|
35 |
const VARIANT_TAG = 'variant'; |
|
|
36 |
const GRANDFATHERED_LANG_TAG = 'grandfathered'; |
|
|
37 |
const PRIVATE_TAG = 'private'; |
|
|
38 |
|
|
|
39 |
/** |
|
|
40 |
* Caches the countries |
|
|
41 |
* @var array |
|
|
42 |
*/ |
|
|
43 |
protected static $countries = array(); |
|
|
44 |
|
|
|
45 |
/** |
|
|
46 |
* Caches the languages |
|
|
47 |
* @var array |
|
|
48 |
*/ |
|
|
49 |
protected static $languages = array(); |
|
|
50 |
|
|
|
51 |
/** |
|
|
52 |
* Caches the locales |
|
|
53 |
* @var array |
|
|
54 |
*/ |
|
|
55 |
protected static $locales = array(); |
|
|
56 |
|
|
|
57 |
/** |
|
|
58 |
* Caches the currencies |
|
|
59 |
* @var array |
|
|
60 |
*/ |
|
|
61 |
protected static $currencies = array(); |
|
|
62 |
|
|
|
63 |
/** |
|
|
64 |
* Caches the currencies names |
|
|
65 |
* @var array |
|
|
66 |
*/ |
|
|
67 |
protected static $currenciesNames = array(); |
|
|
68 |
|
|
|
69 |
/** |
|
|
70 |
* Returns the country names for a locale |
|
|
71 |
* |
|
|
72 |
* @param string $locale The locale to use for the country names |
|
|
73 |
* @return array The country names with their codes as keys |
|
|
74 |
* @throws InvalidArgumentException When the locale is different than 'en' |
|
|
75 |
*/ |
|
|
76 |
static public function getDisplayCountries($locale) |
|
|
77 |
{ |
|
|
78 |
return self::getStubData($locale, 'countries', 'region'); |
|
|
79 |
} |
|
|
80 |
|
|
|
81 |
/** |
|
|
82 |
* Returns all available country codes |
|
|
83 |
* |
|
|
84 |
* @return array The country codes |
|
|
85 |
*/ |
|
|
86 |
static public function getCountries() |
|
|
87 |
{ |
|
|
88 |
return array_keys(self::getDisplayCountries(self::getDefault())); |
|
|
89 |
} |
|
|
90 |
|
|
|
91 |
/** |
|
|
92 |
* Returns the language names for a locale |
|
|
93 |
* |
|
|
94 |
* @param string $locale The locale to use for the language names |
|
|
95 |
* @return array The language names with their codes as keys |
|
|
96 |
* @throws InvalidArgumentException When the locale is different than 'en' |
|
|
97 |
*/ |
|
|
98 |
static public function getDisplayLanguages($locale) |
|
|
99 |
{ |
|
|
100 |
return self::getStubData($locale, 'languages', 'lang'); |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
/** |
|
|
104 |
* Returns all available language codes |
|
|
105 |
* |
|
|
106 |
* @return array The language codes |
|
|
107 |
*/ |
|
|
108 |
static public function getLanguages() |
|
|
109 |
{ |
|
|
110 |
return array_keys(self::getDisplayLanguages(self::getDefault())); |
|
|
111 |
} |
|
|
112 |
|
|
|
113 |
/** |
|
|
114 |
* Returns the locale names for a locale |
|
|
115 |
* |
|
|
116 |
* @param string $locale The locale to use for the locale names |
|
|
117 |
* @return array The locale names with their codes as keys |
|
|
118 |
* @throws InvalidArgumentException When the locale is different than 'en' |
|
|
119 |
*/ |
|
|
120 |
static public function getDisplayLocales($locale) |
|
|
121 |
{ |
|
|
122 |
return self::getStubData($locale, 'locales', 'names'); |
|
|
123 |
} |
|
|
124 |
|
|
|
125 |
/** |
|
|
126 |
* Returns all available locale codes |
|
|
127 |
* |
|
|
128 |
* @return array The locale codes |
|
|
129 |
*/ |
|
|
130 |
static public function getLocales() |
|
|
131 |
{ |
|
|
132 |
return array_keys(self::getDisplayLocales(self::getDefault())); |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
/** |
|
|
136 |
* Returns the currencies data |
|
|
137 |
* |
|
|
138 |
* @param string $locale |
|
|
139 |
* |
|
|
140 |
* @return array The currencies data |
|
|
141 |
*/ |
|
|
142 |
static public function getCurrenciesData($locale) |
|
|
143 |
{ |
|
|
144 |
return self::getStubData($locale, 'currencies', 'curr'); |
|
|
145 |
} |
|
|
146 |
|
|
|
147 |
/** |
|
|
148 |
* Returns the currencies names for a locale |
|
|
149 |
* |
|
|
150 |
* @param string $locale The locale to use for the currencies names |
|
|
151 |
* @return array The currencies names with their codes as keys |
|
|
152 |
* @throws InvalidArgumentException When the locale is different than 'en' |
|
|
153 |
*/ |
|
|
154 |
static public function getDisplayCurrencies($locale) |
|
|
155 |
{ |
|
|
156 |
$currencies = self::getCurrenciesData($locale); |
|
|
157 |
|
|
|
158 |
if (!empty(self::$currenciesNames)) { |
|
|
159 |
return self::$currenciesNames; |
|
|
160 |
} |
|
|
161 |
|
|
|
162 |
foreach ($currencies as $code => $data) { |
|
|
163 |
self::$currenciesNames[$code] = $data['name']; |
|
|
164 |
} |
|
|
165 |
|
|
|
166 |
return self::$currenciesNames; |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
/** |
|
|
170 |
* Returns all available currencies codes |
|
|
171 |
* |
|
|
172 |
* @return array The currencies codes |
|
|
173 |
*/ |
|
|
174 |
static public function getCurrencies() |
|
|
175 |
{ |
|
|
176 |
return array_keys(self::getCurrenciesData(self::getDefault())); |
|
|
177 |
} |
|
|
178 |
|
|
|
179 |
/** |
|
|
180 |
* Returns the best available locale based on HTTP "Accept-Language" header according to RFC 2616 |
|
|
181 |
* |
|
|
182 |
* @param string $header The string containing the "Accept-Language" header value |
|
|
183 |
* @return string The corresponding locale code |
|
|
184 |
* @see http://www.php.net/manual/en/locale.acceptfromhttp.php |
|
|
185 |
* @throws MethodNotImplementedException |
|
|
186 |
*/ |
|
|
187 |
static public function acceptFromHttp($header) |
|
|
188 |
{ |
|
|
189 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
190 |
} |
|
|
191 |
|
|
|
192 |
/** |
|
|
193 |
* Returns a correctly ordered and delimited locale code |
|
|
194 |
* |
|
|
195 |
* @param array $subtags A keyed array where the keys identify the particular locale code subtag |
|
|
196 |
* @return string The corresponding locale code |
|
|
197 |
* @see http://www.php.net/manual/en/locale.composelocale.php |
|
|
198 |
* @throws MethodNotImplementedException |
|
|
199 |
*/ |
|
|
200 |
static public function composeLocale(array $subtags) |
|
|
201 |
{ |
|
|
202 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
203 |
} |
|
|
204 |
|
|
|
205 |
/** |
|
|
206 |
* Checks if a language tag filter matches with locale |
|
|
207 |
* |
|
|
208 |
* @param string $langtag The language tag to check |
|
|
209 |
* @param string $locale The language range to check against |
|
|
210 |
* @param Boolean $canonicalize |
|
|
211 |
* @return string The corresponding locale code |
|
|
212 |
* @see http://www.php.net/manual/en/locale.filtermatches.php |
|
|
213 |
* @throws MethodNotImplementedException |
|
|
214 |
*/ |
|
|
215 |
static public function filterMatches($langtag, $locale, $canonicalize = false) |
|
|
216 |
{ |
|
|
217 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
218 |
} |
|
|
219 |
|
|
|
220 |
/** |
|
|
221 |
* Returns the variants for the input locale |
|
|
222 |
* |
|
|
223 |
* @param string $locale The locale to extract the variants from |
|
|
224 |
* @return array The locale variants |
|
|
225 |
* @see http://www.php.net/manual/en/locale.getallvariants.php |
|
|
226 |
* @throws MethodNotImplementedException |
|
|
227 |
*/ |
|
|
228 |
static public function getAllVariants($locale) |
|
|
229 |
{ |
|
|
230 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
/** |
|
|
234 |
* Returns the default locale |
|
|
235 |
* |
|
|
236 |
* @return string The default locale code. Always returns 'en' |
|
|
237 |
* @see http://www.php.net/manual/en/locale.getdefault.php |
|
|
238 |
* @throws MethodNotImplementedException |
|
|
239 |
*/ |
|
|
240 |
static public function getDefault() |
|
|
241 |
{ |
|
|
242 |
return 'en'; |
|
|
243 |
} |
|
|
244 |
|
|
|
245 |
/** |
|
|
246 |
* Returns the localized display name for the locale language |
|
|
247 |
* |
|
|
248 |
* @param string $locale The locale code to return the display language from |
|
|
249 |
* @param string $inLocale Optional format locale code to use to display the language name |
|
|
250 |
* @return string The localized language display name |
|
|
251 |
* @see http://www.php.net/manual/en/locale.getdisplaylanguage.php |
|
|
252 |
* @throws MethodNotImplementedException |
|
|
253 |
*/ |
|
|
254 |
static public function getDisplayLanguage($locale, $inLocale = null) |
|
|
255 |
{ |
|
|
256 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
257 |
} |
|
|
258 |
|
|
|
259 |
/** |
|
|
260 |
* Returns the localized display name for the locale |
|
|
261 |
* |
|
|
262 |
* @param string $locale The locale code to return the display locale name from |
|
|
263 |
* @param string $inLocale Optional format locale code to use to display the locale name |
|
|
264 |
* @return string The localized locale display name |
|
|
265 |
* @see http://www.php.net/manual/en/locale.getdisplayname.php |
|
|
266 |
* @throws MethodNotImplementedException |
|
|
267 |
*/ |
|
|
268 |
static public function getDisplayName($locale, $inLocale = null) |
|
|
269 |
{ |
|
|
270 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
271 |
} |
|
|
272 |
|
|
|
273 |
/** |
|
|
274 |
* Returns the localized display name for the locale region |
|
|
275 |
* |
|
|
276 |
* @param string $locale The locale code to return the display region from |
|
|
277 |
* @param string $inLocale Optional format locale code to use to display the region name |
|
|
278 |
* @return string The localized region display name |
|
|
279 |
* @see http://www.php.net/manual/en/locale.getdisplayregion.php |
|
|
280 |
* @throws MethodNotImplementedException |
|
|
281 |
*/ |
|
|
282 |
static public function getDisplayRegion($locale, $inLocale = null) |
|
|
283 |
{ |
|
|
284 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
285 |
} |
|
|
286 |
|
|
|
287 |
/** |
|
|
288 |
* Returns the localized display name for the locale script |
|
|
289 |
* |
|
|
290 |
* @param string $locale The locale code to return the display script from |
|
|
291 |
* @param string $inLocale Optional format locale code to use to display the script name |
|
|
292 |
* @return string The localized script display name |
|
|
293 |
* @see http://www.php.net/manual/en/locale.getdisplayscript.php |
|
|
294 |
* @throws MethodNotImplementedException |
|
|
295 |
*/ |
|
|
296 |
static public function getDisplayScript($locale, $inLocale = null) |
|
|
297 |
{ |
|
|
298 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
299 |
} |
|
|
300 |
|
|
|
301 |
/** |
|
|
302 |
* Returns the localized display name for the locale variant |
|
|
303 |
* |
|
|
304 |
* @param string $locale The locale code to return the display variant from |
|
|
305 |
* @param string $inLocale Optional format locale code to use to display the variant name |
|
|
306 |
* @return string The localized variant display name |
|
|
307 |
* @see http://www.php.net/manual/en/locale.getdisplayvariant.php |
|
|
308 |
* @throws MethodNotImplementedException |
|
|
309 |
*/ |
|
|
310 |
static public function getDisplayVariant($locale, $inLocale = null) |
|
|
311 |
{ |
|
|
312 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
313 |
} |
|
|
314 |
|
|
|
315 |
/** |
|
|
316 |
* Returns the keywords for the locale |
|
|
317 |
* |
|
|
318 |
* @param string $locale The locale code to extract the keywords from |
|
|
319 |
* @return array Associative array with the extracted variants |
|
|
320 |
* @see http://www.php.net/manual/en/locale.getkeywords.php |
|
|
321 |
* @throws MethodNotImplementedException |
|
|
322 |
*/ |
|
|
323 |
static public function getKeywords($locale) |
|
|
324 |
{ |
|
|
325 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
326 |
} |
|
|
327 |
|
|
|
328 |
/** |
|
|
329 |
* Returns the primary language for the locale |
|
|
330 |
* |
|
|
331 |
* @param string $locale The locale code to extract the language code from |
|
|
332 |
* @return string|null The extracted language code or null in case of error |
|
|
333 |
* @see http://www.php.net/manual/en/locale.getprimarylanguage.php |
|
|
334 |
* @throws MethodNotImplementedException |
|
|
335 |
*/ |
|
|
336 |
static public function getPrimaryLanguage($locale) |
|
|
337 |
{ |
|
|
338 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
339 |
} |
|
|
340 |
|
|
|
341 |
/** |
|
|
342 |
* Returns the region for the locale |
|
|
343 |
* |
|
|
344 |
* @param string $locale The locale code to extract the region code from |
|
|
345 |
* @return string|null The extracted region code or null if not present |
|
|
346 |
* @see http://www.php.net/manual/en/locale.getregion.php |
|
|
347 |
* @throws MethodNotImplementedException |
|
|
348 |
*/ |
|
|
349 |
static public function getRegion($locale) |
|
|
350 |
{ |
|
|
351 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
352 |
} |
|
|
353 |
|
|
|
354 |
/** |
|
|
355 |
* Returns the script for the locale |
|
|
356 |
* |
|
|
357 |
* @param string $locale The locale code to extract the script code from |
|
|
358 |
* @return string|null The extracted script code or null if not present |
|
|
359 |
* @see http://www.php.net/manual/en/locale.getscript.php |
|
|
360 |
* @throws MethodNotImplementedException |
|
|
361 |
*/ |
|
|
362 |
static public function getScript($locale) |
|
|
363 |
{ |
|
|
364 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
365 |
} |
|
|
366 |
|
|
|
367 |
/** |
|
|
368 |
* Returns the closest language tag for the locale |
|
|
369 |
* |
|
|
370 |
* @param array $langtag A list of the language tags to compare to locale |
|
|
371 |
* @param string $locale The locale to use as the language range when matching |
|
|
372 |
* @param Boolean $canonicalize If true, the arguments will be converted to canonical form before matching |
|
|
373 |
* @param string $default The locale to use if no match is found |
|
|
374 |
* @see http://www.php.net/manual/en/locale.lookup.php |
|
|
375 |
* @throws RuntimeException When the intl extension is not loaded |
|
|
376 |
*/ |
|
|
377 |
static public function lookup(array $langtag, $locale, $canonicalize = false, $default = null) |
|
|
378 |
{ |
|
|
379 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
380 |
} |
|
|
381 |
|
|
|
382 |
/** |
|
|
383 |
* Returns an associative array of locale identifier subtags |
|
|
384 |
* |
|
|
385 |
* @param string $locale The locale code to extract the subtag array from |
|
|
386 |
* @return array Associative array with the extracted subtags |
|
|
387 |
* @see http://www.php.net/manual/en/locale.parselocale.php |
|
|
388 |
* @throws MethodNotImplementedException |
|
|
389 |
*/ |
|
|
390 |
static public function parseLocale($locale) |
|
|
391 |
{ |
|
|
392 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
393 |
} |
|
|
394 |
|
|
|
395 |
/** |
|
|
396 |
* Sets the default runtime locale |
|
|
397 |
* |
|
|
398 |
* @param string $locale The locale code |
|
|
399 |
* @return Boolean true on success or false on failure |
|
|
400 |
* @see http://www.php.net/manual/en/locale.parselocale.php |
|
|
401 |
* @throws MethodNotImplementedException |
|
|
402 |
*/ |
|
|
403 |
static public function setDefault($locale) |
|
|
404 |
{ |
|
|
405 |
throw new MethodNotImplementedException(__METHOD__); |
|
|
406 |
} |
|
|
407 |
|
|
|
408 |
/** |
|
|
409 |
* Returns the stub ICU data |
|
|
410 |
* |
|
|
411 |
* @param string $locale The locale code |
|
|
412 |
* @param string $cacheVariable The name of a static attribute to cache the data to |
|
|
413 |
* @param string $stubDataDir The stub data directory name |
|
|
414 |
* @return array |
|
|
415 |
* @throws InvalidArgumentException When the locale is different than 'en' |
|
|
416 |
*/ |
|
|
417 |
static private function getStubData($locale, $cacheVariable, $stubDataDir) |
|
|
418 |
{ |
|
|
419 |
if ('en' != $locale) { |
|
|
420 |
$message = 'Only the \'en\' locale is supported. '.NotImplementedException::INTL_INSTALL_MESSAGE; |
|
|
421 |
throw new \InvalidArgumentException($message); |
|
|
422 |
} |
|
|
423 |
|
|
|
424 |
if (empty(self::${$cacheVariable})) { |
|
|
425 |
self::${$cacheVariable} = include __DIR__.'/../Resources/data/stub/'.$stubDataDir.'/en.php'; |
|
|
426 |
} |
|
|
427 |
|
|
|
428 |
return self::${$cacheVariable}; |
|
|
429 |
} |
|
|
430 |
} |