Locale.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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\Intl\Locale;
  11. use Symfony\Component\Intl\Exception\MethodNotImplementedException;
  12. /**
  13. * Replacement for PHP's native {@link \Locale} class.
  14. *
  15. * The only methods supported in this class are `getDefault` and `canonicalize`.
  16. * All other methods will throw an exception when used.
  17. *
  18. * @author Eriksen Costa <eriksen.costa@infranology.com.br>
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @internal
  22. */
  23. abstract class Locale
  24. {
  25. public const DEFAULT_LOCALE = null;
  26. /* Locale method constants */
  27. public const ACTUAL_LOCALE = 0;
  28. public const VALID_LOCALE = 1;
  29. /* Language tags constants */
  30. public const LANG_TAG = 'language';
  31. public const EXTLANG_TAG = 'extlang';
  32. public const SCRIPT_TAG = 'script';
  33. public const REGION_TAG = 'region';
  34. public const VARIANT_TAG = 'variant';
  35. public const GRANDFATHERED_LANG_TAG = 'grandfathered';
  36. public const PRIVATE_TAG = 'private';
  37. /**
  38. * Not supported. Returns the best available locale based on HTTP "Accept-Language" header according to RFC 2616.
  39. *
  40. * @param string $header The string containing the "Accept-Language" header value
  41. *
  42. * @return string The corresponding locale code
  43. *
  44. * @see https://php.net/locale.acceptfromhttp
  45. *
  46. * @throws MethodNotImplementedException
  47. */
  48. public static function acceptFromHttp(string $header)
  49. {
  50. throw new MethodNotImplementedException(__METHOD__);
  51. }
  52. /**
  53. * Returns a canonicalized locale string.
  54. *
  55. * This polyfill doesn't implement the full-spec algorithm. It only
  56. * canonicalizes locale strings handled by the `LocaleBundle` class.
  57. *
  58. * @return string
  59. */
  60. public static function canonicalize(string $locale)
  61. {
  62. if ('' === $locale || '.' === $locale[0]) {
  63. return self::getDefault();
  64. }
  65. if (!preg_match('/^([a-z]{2})[-_]([a-z]{2})(?:([a-z]{2})(?:[-_]([a-z]{2}))?)?(?:\..*)?$/i', $locale, $m)) {
  66. return $locale;
  67. }
  68. if (!empty($m[4])) {
  69. return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])).'_'.strtoupper($m[4]);
  70. }
  71. if (!empty($m[3])) {
  72. return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3]));
  73. }
  74. return strtolower($m[1]).'_'.strtoupper($m[2]);
  75. }
  76. /**
  77. * Not supported. Returns a correctly ordered and delimited locale code.
  78. *
  79. * @param array $subtags A keyed array where the keys identify the particular locale code subtag
  80. *
  81. * @return string The corresponding locale code
  82. *
  83. * @see https://php.net/locale.composelocale
  84. *
  85. * @throws MethodNotImplementedException
  86. */
  87. public static function composeLocale(array $subtags)
  88. {
  89. throw new MethodNotImplementedException(__METHOD__);
  90. }
  91. /**
  92. * Not supported. Checks if a language tag filter matches with locale.
  93. *
  94. * @param string $langtag The language tag to check
  95. * @param string $locale The language range to check against
  96. *
  97. * @return string The corresponding locale code
  98. *
  99. * @see https://php.net/locale.filtermatches
  100. *
  101. * @throws MethodNotImplementedException
  102. */
  103. public static function filterMatches(string $langtag, string $locale, bool $canonicalize = false)
  104. {
  105. throw new MethodNotImplementedException(__METHOD__);
  106. }
  107. /**
  108. * Not supported. Returns the variants for the input locale.
  109. *
  110. * @param string $locale The locale to extract the variants from
  111. *
  112. * @return array The locale variants
  113. *
  114. * @see https://php.net/locale.getallvariants
  115. *
  116. * @throws MethodNotImplementedException
  117. */
  118. public static function getAllVariants(string $locale)
  119. {
  120. throw new MethodNotImplementedException(__METHOD__);
  121. }
  122. /**
  123. * Returns the default locale.
  124. *
  125. * @return string The default locale code. Always returns 'en'
  126. *
  127. * @see https://php.net/locale.getdefault
  128. */
  129. public static function getDefault()
  130. {
  131. return 'en';
  132. }
  133. /**
  134. * Not supported. Returns the localized display name for the locale language.
  135. *
  136. * @param string $locale The locale code to return the display language from
  137. * @param string $inLocale Optional format locale code to use to display the language name
  138. *
  139. * @return string The localized language display name
  140. *
  141. * @see https://php.net/locale.getdisplaylanguage
  142. *
  143. * @throws MethodNotImplementedException
  144. */
  145. public static function getDisplayLanguage(string $locale, string $inLocale = null)
  146. {
  147. throw new MethodNotImplementedException(__METHOD__);
  148. }
  149. /**
  150. * Not supported. Returns the localized display name for the locale.
  151. *
  152. * @param string $locale The locale code to return the display locale name from
  153. * @param string $inLocale Optional format locale code to use to display the locale name
  154. *
  155. * @return string The localized locale display name
  156. *
  157. * @see https://php.net/locale.getdisplayname
  158. *
  159. * @throws MethodNotImplementedException
  160. */
  161. public static function getDisplayName(string $locale, string $inLocale = null)
  162. {
  163. throw new MethodNotImplementedException(__METHOD__);
  164. }
  165. /**
  166. * Not supported. Returns the localized display name for the locale region.
  167. *
  168. * @param string $locale The locale code to return the display region from
  169. * @param string $inLocale Optional format locale code to use to display the region name
  170. *
  171. * @return string The localized region display name
  172. *
  173. * @see https://php.net/locale.getdisplayregion
  174. *
  175. * @throws MethodNotImplementedException
  176. */
  177. public static function getDisplayRegion(string $locale, string $inLocale = null)
  178. {
  179. throw new MethodNotImplementedException(__METHOD__);
  180. }
  181. /**
  182. * Not supported. Returns the localized display name for the locale script.
  183. *
  184. * @param string $locale The locale code to return the display script from
  185. * @param string $inLocale Optional format locale code to use to display the script name
  186. *
  187. * @return string The localized script display name
  188. *
  189. * @see https://php.net/locale.getdisplayscript
  190. *
  191. * @throws MethodNotImplementedException
  192. */
  193. public static function getDisplayScript(string $locale, string $inLocale = null)
  194. {
  195. throw new MethodNotImplementedException(__METHOD__);
  196. }
  197. /**
  198. * Not supported. Returns the localized display name for the locale variant.
  199. *
  200. * @param string $locale The locale code to return the display variant from
  201. * @param string $inLocale Optional format locale code to use to display the variant name
  202. *
  203. * @return string The localized variant display name
  204. *
  205. * @see https://php.net/locale.getdisplayvariant
  206. *
  207. * @throws MethodNotImplementedException
  208. */
  209. public static function getDisplayVariant(string $locale, string $inLocale = null)
  210. {
  211. throw new MethodNotImplementedException(__METHOD__);
  212. }
  213. /**
  214. * Not supported. Returns the keywords for the locale.
  215. *
  216. * @param string $locale The locale code to extract the keywords from
  217. *
  218. * @return array Associative array with the extracted variants
  219. *
  220. * @see https://php.net/locale.getkeywords
  221. *
  222. * @throws MethodNotImplementedException
  223. */
  224. public static function getKeywords(string $locale)
  225. {
  226. throw new MethodNotImplementedException(__METHOD__);
  227. }
  228. /**
  229. * Not supported. Returns the primary language for the locale.
  230. *
  231. * @param string $locale The locale code to extract the language code from
  232. *
  233. * @return string|null The extracted language code or null in case of error
  234. *
  235. * @see https://php.net/locale.getprimarylanguage
  236. *
  237. * @throws MethodNotImplementedException
  238. */
  239. public static function getPrimaryLanguage(string $locale)
  240. {
  241. throw new MethodNotImplementedException(__METHOD__);
  242. }
  243. /**
  244. * Not supported. Returns the region for the locale.
  245. *
  246. * @param string $locale The locale code to extract the region code from
  247. *
  248. * @return string|null The extracted region code or null if not present
  249. *
  250. * @see https://php.net/locale.getregion
  251. *
  252. * @throws MethodNotImplementedException
  253. */
  254. public static function getRegion(string $locale)
  255. {
  256. throw new MethodNotImplementedException(__METHOD__);
  257. }
  258. /**
  259. * Not supported. Returns the script for the locale.
  260. *
  261. * @param string $locale The locale code to extract the script code from
  262. *
  263. * @return string|null The extracted script code or null if not present
  264. *
  265. * @see https://php.net/locale.getscript
  266. *
  267. * @throws MethodNotImplementedException
  268. */
  269. public static function getScript(string $locale)
  270. {
  271. throw new MethodNotImplementedException(__METHOD__);
  272. }
  273. /**
  274. * Not supported. Returns the closest language tag for the locale.
  275. *
  276. * @param array $langtag A list of the language tags to compare to locale
  277. * @param string $locale The locale to use as the language range when matching
  278. * @param bool $canonicalize If true, the arguments will be converted to canonical form before matching
  279. * @param string $default The locale to use if no match is found
  280. *
  281. * @see https://php.net/locale.lookup
  282. *
  283. * @throws MethodNotImplementedException
  284. */
  285. public static function lookup(array $langtag, string $locale, bool $canonicalize = false, string $default = null)
  286. {
  287. throw new MethodNotImplementedException(__METHOD__);
  288. }
  289. /**
  290. * Not supported. Returns an associative array of locale identifier subtags.
  291. *
  292. * @param string $locale The locale code to extract the subtag array from
  293. *
  294. * @return array Associative array with the extracted subtags
  295. *
  296. * @see https://php.net/locale.parselocale
  297. *
  298. * @throws MethodNotImplementedException
  299. */
  300. public static function parseLocale(string $locale)
  301. {
  302. throw new MethodNotImplementedException(__METHOD__);
  303. }
  304. /**
  305. * Not supported. Sets the default runtime locale.
  306. *
  307. * @return bool true on success or false on failure
  308. *
  309. * @see https://php.net/locale.setdefault
  310. *
  311. * @throws MethodNotImplementedException
  312. */
  313. public static function setDefault(string $locale)
  314. {
  315. if ('en' !== $locale) {
  316. throw new MethodNotImplementedException(__METHOD__);
  317. }
  318. return true;
  319. }
  320. }