Locale.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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\Polyfill\Intl\Icu;
  11. use Symfony\Polyfill\Intl\Icu\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. * @return string The corresponding locale code
  41. *
  42. * @see https://php.net/locale.acceptfromhttp
  43. *
  44. * @throws MethodNotImplementedException
  45. */
  46. public static function acceptFromHttp(string $header)
  47. {
  48. throw new MethodNotImplementedException(__METHOD__);
  49. }
  50. /**
  51. * Returns a canonicalized locale string.
  52. *
  53. * This polyfill doesn't implement the full-spec algorithm. It only
  54. * canonicalizes locale strings handled by the `LocaleBundle` class.
  55. *
  56. * @return string
  57. */
  58. public static function canonicalize(string $locale)
  59. {
  60. if ('' === $locale || '.' === $locale[0]) {
  61. return self::getDefault();
  62. }
  63. if (!preg_match('/^([a-z]{2})[-_]([a-z]{2})(?:([a-z]{2})(?:[-_]([a-z]{2}))?)?(?:\..*)?$/i', $locale, $m)) {
  64. return $locale;
  65. }
  66. if (!empty($m[4])) {
  67. return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3])).'_'.strtoupper($m[4]);
  68. }
  69. if (!empty($m[3])) {
  70. return strtolower($m[1]).'_'.ucfirst(strtolower($m[2].$m[3]));
  71. }
  72. return strtolower($m[1]).'_'.strtoupper($m[2]);
  73. }
  74. /**
  75. * Not supported. Returns a correctly ordered and delimited locale code.
  76. *
  77. * @return string The corresponding locale code
  78. *
  79. * @see https://php.net/locale.composelocale
  80. *
  81. * @throws MethodNotImplementedException
  82. */
  83. public static function composeLocale(array $subtags)
  84. {
  85. throw new MethodNotImplementedException(__METHOD__);
  86. }
  87. /**
  88. * Not supported. Checks if a language tag filter matches with locale.
  89. *
  90. * @return string The corresponding locale code
  91. *
  92. * @see https://php.net/locale.filtermatches
  93. *
  94. * @throws MethodNotImplementedException
  95. */
  96. public static function filterMatches(string $languageTag, string $locale, bool $canonicalize = false)
  97. {
  98. throw new MethodNotImplementedException(__METHOD__);
  99. }
  100. /**
  101. * Not supported. Returns the variants for the input locale.
  102. *
  103. * @return array The locale variants
  104. *
  105. * @see https://php.net/locale.getallvariants
  106. *
  107. * @throws MethodNotImplementedException
  108. */
  109. public static function getAllVariants(string $locale)
  110. {
  111. throw new MethodNotImplementedException(__METHOD__);
  112. }
  113. /**
  114. * Returns the default locale.
  115. *
  116. * @return string The default locale code. Always returns 'en'
  117. *
  118. * @see https://php.net/locale.getdefault
  119. */
  120. public static function getDefault()
  121. {
  122. return 'en';
  123. }
  124. /**
  125. * Not supported. Returns the localized display name for the locale language.
  126. *
  127. * @return string The localized language display name
  128. *
  129. * @see https://php.net/locale.getdisplaylanguage
  130. *
  131. * @throws MethodNotImplementedException
  132. */
  133. public static function getDisplayLanguage(string $locale, string $displayLocale = null)
  134. {
  135. throw new MethodNotImplementedException(__METHOD__);
  136. }
  137. /**
  138. * Not supported. Returns the localized display name for the locale.
  139. *
  140. * @return string The localized locale display name
  141. *
  142. * @see https://php.net/locale.getdisplayname
  143. *
  144. * @throws MethodNotImplementedException
  145. */
  146. public static function getDisplayName(string $locale, string $displayLocale = null)
  147. {
  148. throw new MethodNotImplementedException(__METHOD__);
  149. }
  150. /**
  151. * Not supported. Returns the localized display name for the locale region.
  152. *
  153. * @return string The localized region display name
  154. *
  155. * @see https://php.net/locale.getdisplayregion
  156. *
  157. * @throws MethodNotImplementedException
  158. */
  159. public static function getDisplayRegion(string $locale, string $displayLocale = null)
  160. {
  161. throw new MethodNotImplementedException(__METHOD__);
  162. }
  163. /**
  164. * Not supported. Returns the localized display name for the locale script.
  165. *
  166. * @return string The localized script display name
  167. *
  168. * @see https://php.net/locale.getdisplayscript
  169. *
  170. * @throws MethodNotImplementedException
  171. */
  172. public static function getDisplayScript(string $locale, string $displayLocale = null)
  173. {
  174. throw new MethodNotImplementedException(__METHOD__);
  175. }
  176. /**
  177. * Not supported. Returns the localized display name for the locale variant.
  178. *
  179. * @return string The localized variant display name
  180. *
  181. * @see https://php.net/locale.getdisplayvariant
  182. *
  183. * @throws MethodNotImplementedException
  184. */
  185. public static function getDisplayVariant(string $locale, string $displayLocale = null)
  186. {
  187. throw new MethodNotImplementedException(__METHOD__);
  188. }
  189. /**
  190. * Not supported. Returns the keywords for the locale.
  191. *
  192. * @return array Associative array with the extracted variants
  193. *
  194. * @see https://php.net/locale.getkeywords
  195. *
  196. * @throws MethodNotImplementedException
  197. */
  198. public static function getKeywords(string $locale)
  199. {
  200. throw new MethodNotImplementedException(__METHOD__);
  201. }
  202. /**
  203. * Not supported. Returns the primary language for the locale.
  204. *
  205. * @return string|null The extracted language code or null in case of error
  206. *
  207. * @see https://php.net/locale.getprimarylanguage
  208. *
  209. * @throws MethodNotImplementedException
  210. */
  211. public static function getPrimaryLanguage(string $locale)
  212. {
  213. throw new MethodNotImplementedException(__METHOD__);
  214. }
  215. /**
  216. * Not supported. Returns the region for the locale.
  217. *
  218. * @return string|null The extracted region code or null if not present
  219. *
  220. * @see https://php.net/locale.getregion
  221. *
  222. * @throws MethodNotImplementedException
  223. */
  224. public static function getRegion(string $locale)
  225. {
  226. throw new MethodNotImplementedException(__METHOD__);
  227. }
  228. /**
  229. * Not supported. Returns the script for the locale.
  230. *
  231. * @return string|null The extracted script code or null if not present
  232. *
  233. * @see https://php.net/locale.getscript
  234. *
  235. * @throws MethodNotImplementedException
  236. */
  237. public static function getScript(string $locale)
  238. {
  239. throw new MethodNotImplementedException(__METHOD__);
  240. }
  241. /**
  242. * Not supported. Returns the closest language tag for the locale.
  243. *
  244. * @see https://php.net/locale.lookup
  245. *
  246. * @throws MethodNotImplementedException
  247. */
  248. public static function lookup(array $languageTag, string $locale, bool $canonicalize = false, string $defaultLocale = null)
  249. {
  250. throw new MethodNotImplementedException(__METHOD__);
  251. }
  252. /**
  253. * Not supported. Returns an associative array of locale identifier subtags.
  254. *
  255. * @return array Associative array with the extracted subtags
  256. *
  257. * @see https://php.net/locale.parselocale
  258. *
  259. * @throws MethodNotImplementedException
  260. */
  261. public static function parseLocale(string $locale)
  262. {
  263. throw new MethodNotImplementedException(__METHOD__);
  264. }
  265. /**
  266. * Not supported. Sets the default runtime locale.
  267. *
  268. * @return bool true on success or false on failure
  269. *
  270. * @see https://php.net/locale.setdefault
  271. *
  272. * @throws MethodNotImplementedException
  273. */
  274. public static function setDefault(string $locale)
  275. {
  276. if ('en' !== $locale) {
  277. throw new MethodNotImplementedException(__METHOD__);
  278. }
  279. return true;
  280. }
  281. }