StubLocale.php 14 KB

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