/src/Symfony/Component/Intl/Locale/Locale.php

https://github.com/ruudk/symfony · PHP · 351 lines · 103 code · 29 blank · 219 comment · 6 complexity · 023ea53d116049cab4544f5981d21c97 MD5 · raw file

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