lib.es2020.intl.d.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*! *****************************************************************************
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  4. this file except in compliance with the License. You may obtain a copy of the
  5. License at http://www.apache.org/licenses/LICENSE-2.0
  6. THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  7. KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  8. WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  9. MERCHANTABLITY OR NON-INFRINGEMENT.
  10. See the Apache Version 2.0 License for specific language governing permissions
  11. and limitations under the License.
  12. ***************************************************************************** */
  13. /// <reference no-default-lib="true"/>
  14. declare namespace Intl {
  15. /**
  16. * [Unicode BCP 47 Locale Identifiers](https://unicode.org/reports/tr35/#Unicode_Language_and_Locale_Identifiers) definition.
  17. *
  18. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
  19. */
  20. type UnicodeBCP47LocaleIdentifier = string;
  21. /**
  22. * Unit to use in the relative time internationalized message.
  23. *
  24. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/format#Parameters).
  25. */
  26. type RelativeTimeFormatUnit =
  27. | "year"
  28. | "years"
  29. | "quarter"
  30. | "quarters"
  31. | "month"
  32. | "months"
  33. | "week"
  34. | "weeks"
  35. | "day"
  36. | "days"
  37. | "hour"
  38. | "hours"
  39. | "minute"
  40. | "minutes"
  41. | "second"
  42. | "seconds";
  43. /**
  44. * The locale matching algorithm to use.
  45. *
  46. * [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation).
  47. */
  48. type RelativeTimeFormatLocaleMatcher = "lookup" | "best fit";
  49. /**
  50. * The format of output message.
  51. *
  52. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters).
  53. */
  54. type RelativeTimeFormatNumeric = "always" | "auto";
  55. /**
  56. * The length of the internationalized message.
  57. *
  58. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters).
  59. */
  60. type RelativeTimeFormatStyle = "long" | "short" | "narrow";
  61. /**
  62. * [BCP 47 language tag](http://tools.ietf.org/html/rfc5646) definition.
  63. *
  64. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
  65. */
  66. type BCP47LanguageTag = string;
  67. /**
  68. * An object with some or all of properties of `options` parameter
  69. * of `Intl.RelativeTimeFormat` constructor.
  70. *
  71. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters).
  72. */
  73. interface RelativeTimeFormatOptions {
  74. /** The locale matching algorithm to use. For information about this option, see [Intl page](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation). */
  75. localeMatcher?: RelativeTimeFormatLocaleMatcher;
  76. /** The format of output message. */
  77. numeric?: RelativeTimeFormatNumeric;
  78. /** The length of the internationalized message. */
  79. style?: RelativeTimeFormatStyle;
  80. }
  81. /**
  82. * An object with properties reflecting the locale
  83. * and formatting options computed during initialization
  84. * of the `Intl.RelativeTimeFormat` object
  85. *
  86. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/resolvedOptions#Description).
  87. */
  88. interface ResolvedRelativeTimeFormatOptions {
  89. locale: UnicodeBCP47LocaleIdentifier;
  90. style: RelativeTimeFormatStyle;
  91. numeric: RelativeTimeFormatNumeric;
  92. numberingSystem: string;
  93. }
  94. /**
  95. * An object representing the relative time format in parts
  96. * that can be used for custom locale-aware formatting.
  97. *
  98. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
  99. */
  100. interface RelativeTimeFormatPart {
  101. type: string;
  102. value: string;
  103. unit?: RelativeTimeFormatUnit;
  104. }
  105. interface RelativeTimeFormat {
  106. /**
  107. * Formats a value and a unit according to the locale
  108. * and formatting options of the given
  109. * [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat)
  110. * object.
  111. *
  112. * While this method automatically provides the correct plural forms,
  113. * the grammatical form is otherwise as neutral as possible.
  114. *
  115. * It is the caller's responsibility to handle cut-off logic
  116. * such as deciding between displaying "in 7 days" or "in 1 week".
  117. * This API does not support relative dates involving compound units.
  118. * e.g "in 5 days and 4 hours".
  119. *
  120. * @param value - Numeric value to use in the internationalized relative time message
  121. *
  122. * @param unit - [Unit](https://tc39.es/ecma402/#sec-singularrelativetimeunit) to use in the relative time internationalized message.
  123. *
  124. * @throws `RangeError` if `unit` was given something other than `unit` possible values
  125. *
  126. * @returns {string} Internationalized relative time message as string
  127. *
  128. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/format).
  129. */
  130. format(value: number, unit: RelativeTimeFormatUnit): string;
  131. /**
  132. * Returns an array of objects representing the relative time format in parts that can be used for custom locale-aware formatting.
  133. *
  134. * @param value - Numeric value to use in the internationalized relative time message
  135. *
  136. * @param unit - [Unit](https://tc39.es/ecma402/#sec-singularrelativetimeunit) to use in the relative time internationalized message.
  137. *
  138. * @throws `RangeError` if `unit` was given something other than `unit` possible values
  139. *
  140. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts).
  141. */
  142. formatToParts(value: number, unit: RelativeTimeFormatUnit): RelativeTimeFormatPart[];
  143. /**
  144. * Provides access to the locale and options computed during initialization of this `Intl.RelativeTimeFormat` object.
  145. *
  146. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/resolvedOptions).
  147. */
  148. resolvedOptions(): ResolvedRelativeTimeFormatOptions;
  149. }
  150. /**
  151. * The [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat)
  152. * object is a constructor for objects that enable language-sensitive relative time formatting.
  153. *
  154. * [Compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat#Browser_compatibility).
  155. */
  156. const RelativeTimeFormat: {
  157. /**
  158. * Creates [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat) objects
  159. *
  160. * @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
  161. * For the general form and interpretation of the locales argument,
  162. * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
  163. *
  164. * @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters)
  165. * with some or all of options of `RelativeTimeFormatOptions`.
  166. *
  167. * @returns [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat) object.
  168. *
  169. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat).
  170. */
  171. new(
  172. locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],
  173. options?: RelativeTimeFormatOptions,
  174. ): RelativeTimeFormat;
  175. /**
  176. * Returns an array containing those of the provided locales
  177. * that are supported in date and time formatting
  178. * without having to fall back to the runtime's default locale.
  179. *
  180. * @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
  181. * For the general form and interpretation of the locales argument,
  182. * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
  183. *
  184. * @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters)
  185. * with some or all of options of the formatting.
  186. *
  187. * @returns An array containing those of the provided locales
  188. * that are supported in date and time formatting
  189. * without having to fall back to the runtime's default locale.
  190. *
  191. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/supportedLocalesOf).
  192. */
  193. supportedLocalesOf(
  194. locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],
  195. options?: RelativeTimeFormatOptions,
  196. ): UnicodeBCP47LocaleIdentifier[];
  197. };
  198. interface NumberFormatOptions {
  199. compactDisplay?: "short" | "long" | undefined;
  200. notation?: "standard" | "scientific" | "engineering" | "compact" | undefined;
  201. signDisplay?: "auto" | "never" | "always" | undefined;
  202. unit?: string | undefined;
  203. unitDisplay?: "short" | "long" | "narrow" | undefined;
  204. }
  205. interface ResolvedNumberFormatOptions {
  206. compactDisplay?: "short" | "long";
  207. notation?: "standard" | "scientific" | "engineering" | "compact";
  208. signDisplay?: "auto" | "never" | "always";
  209. unit?: string;
  210. unitDisplay?: "short" | "long" | "narrow";
  211. }
  212. interface DateTimeFormatOptions {
  213. calendar?: string | undefined;
  214. dayPeriod?: "narrow" | "short" | "long" | undefined;
  215. numberingSystem?: string | undefined;
  216. dateStyle?: "full" | "long" | "medium" | "short" | undefined;
  217. timeStyle?: "full" | "long" | "medium" | "short" | undefined;
  218. hourCycle?: "h11" | "h12" | "h23" | "h24" | undefined;
  219. }
  220. type LocaleHourCycleKey = "h12" | "h23" | "h11" | "h24";
  221. type LocaleCollationCaseFirst = "upper" | "lower" | "false";
  222. interface LocaleOptions {
  223. /** A string containing the language, and the script and region if available. */
  224. baseName?: string;
  225. /** The part of the Locale that indicates the locale's calendar era. */
  226. calendar?: string;
  227. /** Flag that defines whether case is taken into account for the locale's collation rules. */
  228. caseFirst?: LocaleCollationCaseFirst;
  229. /** The collation type used for sorting */
  230. collation?: string;
  231. /** The time keeping format convention used by the locale. */
  232. hourCycle?: LocaleHourCycleKey;
  233. /** The primary language subtag associated with the locale. */
  234. language?: string;
  235. /** The numeral system used by the locale. */
  236. numberingSystem?: string;
  237. /** Flag that defines whether the locale has special collation handling for numeric characters. */
  238. numeric?: boolean;
  239. /** The region of the world (usually a country) associated with the locale. Possible values are region codes as defined by ISO 3166-1. */
  240. region?: string;
  241. /** The script used for writing the particular language used in the locale. Possible values are script codes as defined by ISO 15924. */
  242. script?: string;
  243. }
  244. interface Locale extends LocaleOptions {
  245. /** Gets the most likely values for the language, script, and region of the locale based on existing values. */
  246. maximize(): Locale;
  247. /** Attempts to remove information about the locale that would be added by calling `Locale.maximize()`. */
  248. minimize(): Locale;
  249. /** Returns the locale's full locale identifier string. */
  250. toString(): BCP47LanguageTag;
  251. }
  252. /**
  253. * Constructor creates [Intl.Locale](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale)
  254. * objects
  255. *
  256. * @param tag - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646).
  257. * For the general form and interpretation of the locales argument,
  258. * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
  259. *
  260. * @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/Locale#Parameters) with some or all of options of the locale.
  261. *
  262. * @returns [Intl.Locale](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale) object.
  263. *
  264. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale).
  265. */
  266. const Locale: {
  267. new (tag?: BCP47LanguageTag, options?: LocaleOptions): Locale;
  268. };
  269. interface DisplayNamesOptions {
  270. localeMatcher: RelativeTimeFormatLocaleMatcher;
  271. style: RelativeTimeFormatStyle;
  272. type: "language" | "region" | "script" | "currency";
  273. fallback: "code" | "none";
  274. }
  275. interface DisplayNames {
  276. /**
  277. * Receives a code and returns a string based on the locale and options provided when instantiating
  278. * [`Intl.DisplayNames()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames)
  279. *
  280. * @param code The `code` to provide depends on the `type` passed to display name during creation:
  281. * - If the type is `"region"`, code should be either an [ISO-3166 two letters region code](https://www.iso.org/iso-3166-country-codes.html),
  282. * or a [three digits UN M49 Geographic Regions](https://unstats.un.org/unsd/methodology/m49/).
  283. * - If the type is `"script"`, code should be an [ISO-15924 four letters script code](https://unicode.org/iso15924/iso15924-codes.html).
  284. * - If the type is `"language"`, code should be a `languageCode` ["-" `scriptCode`] ["-" `regionCode` ] *("-" `variant` )
  285. * subsequence of the unicode_language_id grammar in [UTS 35's Unicode Language and Locale Identifiers grammar](https://unicode.org/reports/tr35/#Unicode_language_identifier).
  286. * `languageCode` is either a two letters ISO 639-1 language code or a three letters ISO 639-2 language code.
  287. * - If the type is `"currency"`, code should be a [3-letter ISO 4217 currency code](https://www.iso.org/iso-4217-currency-codes.html).
  288. *
  289. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/of).
  290. */
  291. of(code: string): string;
  292. /**
  293. * Returns a new object with properties reflecting the locale and style formatting options computed during the construction of the current
  294. * [`Intl/DisplayNames`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames) object.
  295. *
  296. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/resolvedOptions).
  297. */
  298. resolvedOptions(): DisplayNamesOptions;
  299. }
  300. /**
  301. * The [`Intl.DisplayNames()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames)
  302. * object enables the consistent translation of language, region and script display names.
  303. *
  304. * [Compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames#browser_compatibility).
  305. */
  306. const DisplayNames: {
  307. prototype: DisplayNames;
  308. /**
  309. * @param locales A string with a BCP 47 language tag, or an array of such strings.
  310. * For the general form and interpretation of the `locales` argument, see the [Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)
  311. * page.
  312. *
  313. * @param options An object for setting up a display name.
  314. *
  315. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames).
  316. */
  317. new(locales?: BCP47LanguageTag | BCP47LanguageTag[], options?: Partial<DisplayNamesOptions>): DisplayNames;
  318. /**
  319. * Returns an array containing those of the provided locales that are supported in display names without having to fall back to the runtime's default locale.
  320. *
  321. * @param locales A string with a BCP 47 language tag, or an array of such strings.
  322. * For the general form and interpretation of the `locales` argument, see the [Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)
  323. * page.
  324. *
  325. * @param options An object with a locale matcher.
  326. *
  327. * @returns An array of strings representing a subset of the given locale tags that are supported in display names without having to fall back to the runtime's default locale.
  328. *
  329. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/supportedLocalesOf).
  330. */
  331. supportedLocalesOf(locales: BCP47LanguageTag | BCP47LanguageTag[], options?: {localeMatcher: RelativeTimeFormatLocaleMatcher}): BCP47LanguageTag[];
  332. };
  333. }