PageRenderTime 52ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/hphp/runtime/ext/icu/ext_icu_locale.php

http://github.com/facebook/hiphop-php
PHP | 539 lines | 125 code | 37 blank | 377 comment | 7 complexity | 3f41d28422ec6bcde7954a4d3c81632a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, BSD-3-Clause, MPL-2.0-no-copyleft-exception, MIT, LGPL-2.0, Apache-2.0
  1. <?hh // partial
  2. /**
  3. * A "Locale" is an identifier used to get language, culture, or
  4. * regionally-specific behavior from an API. PHP locales are organized and
  5. * identified the same way that the CLDR locales used by ICU (and many vendors
  6. * of Unix-like operating systems, the Mac, Java, and so forth) use. Locales
  7. * are identified using RFC 4646 language tags (which use hyphen, not
  8. * underscore) in addition to the more traditional underscore-using
  9. * identifiers. Unless otherwise noted the functions in this class are
  10. * tolerant of both formats. Examples of identifiers include: en-US
  11. * (English, United States) zh-Hant-TW (Chinese, Traditional Script, Taiwan)
  12. * fr-CA, fr-FR (French for Canada and France respectively) The Locale
  13. * class (and related procedural functions) are used to interact with locale
  14. * identifiers--to verify that an ID is well-formed, valid, etc. The
  15. * extensions used by CLDR in UAX #35 (and inherited by ICU) are valid and
  16. * used wherever they would be in ICU normally. Locales cannot be
  17. * instantiated as objects. All of the functions/methods provided are static.
  18. * The null or empty string obtains the "root" locale. The "root" locale is
  19. * equivalent to "en_US_POSIX" in CLDR. Language tags (and thus locale
  20. * identifiers) are case insensitive. There exists a canonicalization function
  21. * to make case match the specification.
  22. */
  23. class Locale {
  24. /**
  25. * Tries to find out best available locale based on HTTP "Accept-Language"
  26. * header
  27. *
  28. * @param string $header - The string containing the "Accept-Language"
  29. * header according to format in RFC 2616.
  30. *
  31. * @return string - The corresponding locale identifier.
  32. */
  33. <<__Native>>
  34. public static function acceptFromHttp(string $header): mixed;
  35. /**
  36. * Canonicalize the locale string
  37. *
  38. * @param string $locale -
  39. *
  40. * @return string -
  41. */
  42. <<__Native>>
  43. public static function canonicalize(string $locale): mixed;
  44. /**
  45. * Returns a correctly ordered and delimited locale ID
  46. *
  47. * @param array $subtags - an array containing a list of key-value
  48. * pairs, where the keys identify the particular locale ID subtags, and
  49. * the values are the associated subtag values. The 'variant' and
  50. * 'private' subtags can take maximum 15 values whereas 'extlang' can
  51. * take maximum 3 values.e.g. Variants are allowed with the suffix
  52. * ranging from 0-14. Hence the keys for the input array can be
  53. * variant0, variant1, ...,variant14. In the returned locale id, the
  54. * subtag is ordered by suffix resulting in variant0 followed by
  55. * variant1 followed by variant2 and so on. The 'variant', 'private'
  56. * and 'extlang' multiple values can be specified both as array under
  57. * specific key (e.g. 'variant') and as multiple numbered keys (e.g.
  58. * 'variant0', 'variant1', etc.).
  59. *
  60. * @return string - The corresponding locale identifier.
  61. */
  62. <<__Native>>
  63. public static function composeLocale(darray $subtags): mixed;
  64. /**
  65. * Checks if a language tag filter matches with locale
  66. *
  67. * @param string $langtag - The language tag to check
  68. * @param string $locale - The language range to check against
  69. * @param bool $canonicalize - If true, the arguments will be converted
  70. * to canonical form before matching.
  71. *
  72. * @return bool - TRUE if $locale matches $langtag FALSE otherwise.
  73. */
  74. public static function filterMatches($langtag,
  75. $locale,
  76. $canonicalize = false): bool {
  77. if ($locale == '*') return true;
  78. if (!($locale ?? false)) $locale = self::getDefault();
  79. if ($canonicalize) {
  80. $locale = self::canonicalize($locale);
  81. $langtag = self::canonicalize($langtag);
  82. }
  83. $locale = strtolower(strtr($locale, '-', '_'));
  84. $langtag = strtolower(strtr($langtag, '-', '_'));
  85. if ($locale == $langtag) return true;
  86. return ((strlen($locale) < strlen($langtag)) &&
  87. ($langtag[strlen($locale)] == '_') &&
  88. !strncmp($locale, $langtag, strlen($locale)));
  89. }
  90. /**
  91. * Gets the variants for the input locale
  92. *
  93. * @param string $locale - The locale to extract the variants from
  94. *
  95. * @return array - The array containing the list of all variants subtag
  96. * for the locale or NULL if not present
  97. */
  98. <<__Native>>
  99. public static function getAllVariants(string $locale): varray<string>;
  100. /**
  101. * Gets the default locale value from the INTL global 'default_locale'
  102. *
  103. * @return string - The current runtime locale
  104. */
  105. <<__Native>>
  106. public static function getDefault(): string;
  107. /**
  108. * Returns an appropriately localized display name for language of the
  109. * inputlocale
  110. *
  111. * @param string $locale - The locale to return a display language for
  112. * @param string $in_locale - Optional format locale to use to display
  113. * the language name
  114. *
  115. * @return string - display name of the language for the $locale in the
  116. * format appropriate for $in_locale.
  117. */
  118. <<__Native>>
  119. public static function getDisplayLanguage(string $locale,
  120. string $in_locale): string;
  121. /**
  122. * Returns an appropriately localized display name for the input locale
  123. *
  124. * @param string $locale - The locale to return a display name for.
  125. * @param string $in_locale - optional format locale
  126. *
  127. * @return string - Display name of the locale in the format
  128. * appropriate for $in_locale.
  129. */
  130. <<__Native>>
  131. public static function getDisplayName(string $locale,
  132. string $in_locale): string;
  133. /**
  134. * Returns an appropriately localized display name for region of the input
  135. * locale
  136. *
  137. * @param string $locale - The locale to return a display region for.
  138. * @param string $in_locale - Optional format locale to use to display
  139. * the region name
  140. *
  141. * @return string - display name of the region for the $locale in the
  142. * format appropriate for $in_locale.
  143. */
  144. <<__Native>>
  145. public static function getDisplayRegion(string $locale,
  146. string $in_locale): string;
  147. /**
  148. * Returns an appropriately localized display name for script of the input
  149. * locale
  150. *
  151. * @param string $locale - The locale to return a display script for
  152. * @param string $in_locale - Optional format locale to use to display
  153. * the script name
  154. *
  155. * @return string - Display name of the script for the $locale in the
  156. * format appropriate for $in_locale.
  157. */
  158. <<__Native>>
  159. public static function getDisplayScript(string $locale,
  160. string $in_locale): string;
  161. /**
  162. * Returns an appropriately localized display name for variants of the input
  163. * locale
  164. *
  165. * @param string $locale - The locale to return a display variant for
  166. * @param string $in_locale - Optional format locale to use to display
  167. * the variant name
  168. *
  169. * @return string - Display name of the variant for the $locale in the
  170. * format appropriate for $in_locale.
  171. */
  172. <<__Native>>
  173. public static function getDisplayVariant(string $locale,
  174. string $in_locale): string;
  175. /**
  176. * Gets the keywords for the input locale
  177. *
  178. * @param string $locale - The locale to extract the keywords from
  179. *
  180. * @return array - Associative array containing the keyword-value pairs
  181. * for this locale
  182. */
  183. <<__Native>>
  184. public static function getKeywords(string $locale): darray<string,string>;
  185. /**
  186. * Gets the primary language for the input locale
  187. *
  188. * @param string $locale - The locale to extract the primary language
  189. * code from
  190. *
  191. * @return string - The language code associated with the language or
  192. * NULL in case of error.
  193. */
  194. <<__Native>>
  195. public static function getPrimaryLanguage(string $locale): string;
  196. /**
  197. * Gets the region for the input locale
  198. *
  199. * @param string $locale - The locale to extract the region code from
  200. *
  201. * @return string - The region subtag for the locale or NULL if not
  202. * present
  203. */
  204. <<__Native>>
  205. public static function getRegion(string $locale): mixed;
  206. /**
  207. * Gets the script for the input locale
  208. *
  209. * @param string $locale - The locale to extract the script code from
  210. *
  211. * @return string - The script subtag for the locale or NULL if not
  212. * present
  213. */
  214. <<__Native>>
  215. public static function getScript(string $locale): mixed;
  216. /**
  217. * Searches the language tag list for the best match to the language
  218. *
  219. * @param array $langtag - An array containing a list of language tags
  220. * to compare to locale. Maximum 100 items allowed.
  221. * @param string $locale - The locale to use as the language range when
  222. * matching.
  223. * @param bool $canonicalize - If true, the arguments will be converted
  224. * to canonical form before matching.
  225. * @param string $default - The locale to use if no match is found.
  226. *
  227. * @return string - The closest matching language tag or default
  228. * value.
  229. */
  230. <<__Native>>
  231. public static function lookup(varray $langtag,
  232. string $locale,
  233. bool $canonicalize = false,
  234. string $default = ""): string;
  235. /**
  236. * Returns a key-value array of locale ID subtag elements.
  237. *
  238. * @param string $locale - The locale to extract the subtag array from.
  239. * Note: The 'variant' and 'private' subtags can take maximum 15 values
  240. * whereas 'extlang' can take maximum 3 values.
  241. *
  242. * @return array - Returns an array containing a list of key-value
  243. * pairs, where the keys identify the particular locale ID subtags, and
  244. * the values are the associated subtag values. The array will be
  245. * ordered as the locale id subtags e.g. in the locale id if variants
  246. * are '-varX-varY-varZ' then the returned array will have
  247. * variant0=varX , variant1=varY , variant2=varZ
  248. */
  249. <<__Native>>
  250. public static function parseLocale(string $locale): darray<string,string>;
  251. /**
  252. * sets the default runtime locale
  253. *
  254. * @param string $locale - Is a BCP 47 compliant language tag
  255. * containing the
  256. *
  257. * @return bool -
  258. */
  259. <<__Native>>
  260. public static function setDefault(string $locale): bool;
  261. }
  262. /**
  263. * Tries to find out best available locale based on HTTP "Accept-Language"
  264. * header
  265. *
  266. * @param string $header - The string containing the "Accept-Language"
  267. * header according to format in RFC 2616.
  268. *
  269. * @return string - The corresponding locale identifier.
  270. */
  271. function locale_accept_from_http(string $header): mixed {
  272. return Locale::acceptFromHttp($header);
  273. }
  274. /**
  275. * Canonicalize the locale string
  276. *
  277. * @param string $locale -
  278. *
  279. * @return string -
  280. */
  281. function locale_canonicalize(string $locale): mixed {
  282. return Locale::canonicalize($locale);
  283. }
  284. /**
  285. * Returns a correctly ordered and delimited locale ID
  286. *
  287. * @param array $subtags - an array containing a list of key-value pairs,
  288. * where the keys identify the particular locale ID subtags, and the
  289. * values are the associated subtag values. The 'variant' and 'private'
  290. * subtags can take maximum 15 values whereas 'extlang' can take maximum
  291. * 3 values.e.g. Variants are allowed with the suffix ranging from 0-14.
  292. * Hence the keys for the input array can be variant0, variant1,
  293. * ...,variant14. In the returned locale id, the subtag is ordered by
  294. * suffix resulting in variant0 followed by variant1 followed by variant2
  295. * and so on. The 'variant', 'private' and 'extlang' multiple values
  296. * can be specified both as array under specific key (e.g. 'variant') and
  297. * as multiple numbered keys (e.g. 'variant0', 'variant1', etc.).
  298. *
  299. * @return string - The corresponding locale identifier.
  300. */
  301. function locale_compose(darray $subtags) {
  302. return Locale::composeLocale($subtags);
  303. }
  304. /**
  305. * Checks if a language tag filter matches with locale
  306. *
  307. * @param string $langtag - The language tag to check
  308. * @param string $locale - The language range to check against
  309. * @param bool $canonicalize - If true, the arguments will be converted
  310. * to canonical form before matching.
  311. *
  312. * @return bool - TRUE if $locale matches $langtag FALSE otherwise.
  313. */
  314. function locale_filter_matches($langtag,
  315. $locale,
  316. $canonicalize = false): bool {
  317. return Locale::filterMatches($langtag, $locale, $canonicalize);
  318. }
  319. /**
  320. * Gets the variants for the input locale
  321. *
  322. * @param string $locale - The locale to extract the variants from
  323. *
  324. * @return array - The array containing the list of all variants subtag
  325. * for the locale or NULL if not present
  326. */
  327. function locale_get_all_variants(string $locale) {
  328. return Locale::getAllVariants($locale);
  329. }
  330. /**
  331. * Gets the default locale value from the INTL global 'default_locale'
  332. *
  333. * @return string - The current runtime locale
  334. */
  335. function locale_get_default(): string {
  336. return Locale::getDefault();
  337. }
  338. /**
  339. * Returns an appropriately localized display name for language of the
  340. * inputlocale
  341. *
  342. * @param string $locale - The locale to return a display language for
  343. * @param string $in_locale - Optional format locale to use to display
  344. * the language name
  345. *
  346. * @return string - display name of the language for the $locale in the
  347. * format appropriate for $in_locale.
  348. */
  349. function locale_get_display_language(string $locale,
  350. string $in_locale): string {
  351. return Locale::getDisplayLanguage($locale, $in_locale);
  352. }
  353. /**
  354. * Returns an appropriately localized display name for the input locale
  355. *
  356. * @param string $locale - The locale to return a display name for.
  357. * @param string $in_locale - optional format locale
  358. *
  359. * @return string - Display name of the locale in the format appropriate
  360. * for $in_locale.
  361. */
  362. function locale_get_display_name(string $locale,
  363. string $in_locale): string {
  364. return Locale::getDisplayName($locale, $in_locale);
  365. }
  366. /**
  367. * Returns an appropriately localized display name for region of the input
  368. * locale
  369. *
  370. * @param string $locale - The locale to return a display region for.
  371. * @param string $in_locale - Optional format locale to use to display
  372. * the region name
  373. *
  374. * @return string - display name of the region for the $locale in the
  375. * format appropriate for $in_locale.
  376. */
  377. function locale_get_display_region(string $locale,
  378. string $in_locale): string {
  379. return Locale::getDisplayRegion($locale, $in_locale);
  380. }
  381. /**
  382. * Returns an appropriately localized display name for script of the input
  383. * locale
  384. *
  385. * @param string $locale - The locale to return a display script for
  386. * @param string $in_locale - Optional format locale to use to display
  387. * the script name
  388. *
  389. * @return string - Display name of the script for the $locale in the
  390. * format appropriate for $in_locale.
  391. */
  392. function locale_get_display_script(string $locale,
  393. string $in_locale): string {
  394. return Locale::getDisplayScript($locale, $in_locale);
  395. }
  396. /**
  397. * Returns an appropriately localized display name for variants of the input
  398. * locale
  399. *
  400. * @param string $locale - The locale to return a display variant for
  401. * @param string $in_locale - Optional format locale to use to display
  402. * the variant name
  403. *
  404. * @return string - Display name of the variant for the $locale in the
  405. * format appropriate for $in_locale.
  406. */
  407. function locale_get_display_variant(string $locale,
  408. string $in_locale): string {
  409. return Locale::getDisplayVariant($locale, $in_locale);
  410. }
  411. /**
  412. * Gets the keywords for the input locale
  413. *
  414. * @param string $locale - The locale to extract the keywords from
  415. *
  416. * @return array - Associative array containing the keyword-value pairs
  417. * for this locale
  418. */
  419. function locale_get_keywords(string $locale) {
  420. return Locale::getKeywords($locale);
  421. }
  422. /**
  423. * Gets the primary language for the input locale
  424. *
  425. * @param string $locale - The locale to extract the primary language
  426. * code from
  427. *
  428. * @return string - The language code associated with the language or
  429. * NULL in case of error.
  430. */
  431. function locale_get_primary_language(string $locale): string {
  432. return Locale::getPrimaryLanguage($locale);
  433. }
  434. /**
  435. * Gets the region for the input locale
  436. *
  437. * @param string $locale - The locale to extract the region code from
  438. *
  439. * @return string - The region subtag for the locale or NULL if not
  440. * present
  441. */
  442. function locale_get_region(string $locale): mixed {
  443. return Locale::getRegion($locale);
  444. }
  445. /**
  446. * Gets the script for the input locale
  447. *
  448. * @param string $locale - The locale to extract the script code from
  449. *
  450. * @return string - The script subtag for the locale or NULL if not
  451. * present
  452. */
  453. function locale_get_script(string $locale): mixed {
  454. return Locale::getScript($locale);
  455. }
  456. /**
  457. * Searches the language tag list for the best match to the language
  458. *
  459. * @param array $langtag - An array containing a list of language tags to
  460. * compare to locale. Maximum 100 items allowed.
  461. * @param string $locale - The locale to use as the language range when
  462. * matching.
  463. * @param bool $canonicalize - If true, the arguments will be converted
  464. * to canonical form before matching.
  465. * @param string $default - The locale to use if no match is found.
  466. *
  467. * @return string - The closest matching language tag or default value.
  468. */
  469. function locale_lookup(varray $langtag,
  470. string $locale,
  471. bool $canonicalize = false,
  472. string $default = ""): string {
  473. return Locale::lookup($langtag, $locale, $canonicalize, $default);
  474. }
  475. /**
  476. * Returns a key-value array of locale ID subtag elements.
  477. *
  478. * @param string $locale - The locale to extract the subtag array from.
  479. * Note: The 'variant' and 'private' subtags can take maximum 15 values
  480. * whereas 'extlang' can take maximum 3 values.
  481. *
  482. * @return array - Returns an array containing a list of key-value pairs,
  483. * where the keys identify the particular locale ID subtags, and the
  484. * values are the associated subtag values. The array will be ordered as
  485. * the locale id subtags e.g. in the locale id if variants are
  486. * '-varX-varY-varZ' then the returned array will have variant0=varX ,
  487. * variant1=varY , variant2=varZ
  488. */
  489. function locale_parse(string $locale): darray<string,string> {
  490. return Locale::parseLocale($locale);
  491. }
  492. /**
  493. * sets the default runtime locale
  494. *
  495. * @param string $locale - Is a BCP 47 compliant language tag containing
  496. * the
  497. *
  498. * @return bool -
  499. */
  500. function locale_set_default(string $locale): bool {
  501. return Locale::setDefault($locale);
  502. }