lib.es2020.intl.d.ts 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. * The locale(s) to use
  69. *
  70. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument).
  71. */
  72. type LocalesArgument = UnicodeBCP47LocaleIdentifier | Locale | (UnicodeBCP47LocaleIdentifier | Locale)[] | undefined;
  73. /**
  74. * An object with some or all of properties of `options` parameter
  75. * of `Intl.RelativeTimeFormat` constructor.
  76. *
  77. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters).
  78. */
  79. interface RelativeTimeFormatOptions {
  80. /** 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). */
  81. localeMatcher?: RelativeTimeFormatLocaleMatcher;
  82. /** The format of output message. */
  83. numeric?: RelativeTimeFormatNumeric;
  84. /** The length of the internationalized message. */
  85. style?: RelativeTimeFormatStyle;
  86. }
  87. /**
  88. * An object with properties reflecting the locale
  89. * and formatting options computed during initialization
  90. * of the `Intl.RelativeTimeFormat` object
  91. *
  92. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/resolvedOptions#Description).
  93. */
  94. interface ResolvedRelativeTimeFormatOptions {
  95. locale: UnicodeBCP47LocaleIdentifier;
  96. style: RelativeTimeFormatStyle;
  97. numeric: RelativeTimeFormatNumeric;
  98. numberingSystem: string;
  99. }
  100. /**
  101. * An object representing the relative time format in parts
  102. * that can be used for custom locale-aware formatting.
  103. *
  104. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts#Using_formatToParts).
  105. */
  106. interface RelativeTimeFormatPart {
  107. type: string;
  108. value: string;
  109. unit?: RelativeTimeFormatUnit;
  110. }
  111. interface RelativeTimeFormat {
  112. /**
  113. * Formats a value and a unit according to the locale
  114. * and formatting options of the given
  115. * [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat)
  116. * object.
  117. *
  118. * While this method automatically provides the correct plural forms,
  119. * the grammatical form is otherwise as neutral as possible.
  120. *
  121. * It is the caller's responsibility to handle cut-off logic
  122. * such as deciding between displaying "in 7 days" or "in 1 week".
  123. * This API does not support relative dates involving compound units.
  124. * e.g "in 5 days and 4 hours".
  125. *
  126. * @param value - Numeric value to use in the internationalized relative time message
  127. *
  128. * @param unit - [Unit](https://tc39.es/ecma402/#sec-singularrelativetimeunit) to use in the relative time internationalized message.
  129. *
  130. * @throws `RangeError` if `unit` was given something other than `unit` possible values
  131. *
  132. * @returns {string} Internationalized relative time message as string
  133. *
  134. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/format).
  135. */
  136. format(value: number, unit: RelativeTimeFormatUnit): string;
  137. /**
  138. * Returns an array of objects representing the relative time format in parts that can be used for custom locale-aware formatting.
  139. *
  140. * @param value - Numeric value to use in the internationalized relative time message
  141. *
  142. * @param unit - [Unit](https://tc39.es/ecma402/#sec-singularrelativetimeunit) to use in the relative time internationalized message.
  143. *
  144. * @throws `RangeError` if `unit` was given something other than `unit` possible values
  145. *
  146. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/formatToParts).
  147. */
  148. formatToParts(value: number, unit: RelativeTimeFormatUnit): RelativeTimeFormatPart[];
  149. /**
  150. * Provides access to the locale and options computed during initialization of this `Intl.RelativeTimeFormat` object.
  151. *
  152. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/resolvedOptions).
  153. */
  154. resolvedOptions(): ResolvedRelativeTimeFormatOptions;
  155. }
  156. /**
  157. * The [`Intl.RelativeTimeFormat`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat)
  158. * object is a constructor for objects that enable language-sensitive relative time formatting.
  159. *
  160. * [Compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat#Browser_compatibility).
  161. */
  162. const RelativeTimeFormat: {
  163. /**
  164. * Creates [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat) objects
  165. *
  166. * @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
  167. * For the general form and interpretation of the locales argument,
  168. * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
  169. *
  170. * @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters)
  171. * with some or all of options of `RelativeTimeFormatOptions`.
  172. *
  173. * @returns [Intl.RelativeTimeFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RelativeTimeFormat) object.
  174. *
  175. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat).
  176. */
  177. new(
  178. locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],
  179. options?: RelativeTimeFormatOptions,
  180. ): RelativeTimeFormat;
  181. /**
  182. * Returns an array containing those of the provided locales
  183. * that are supported in date and time formatting
  184. * without having to fall back to the runtime's default locale.
  185. *
  186. * @param locales - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646), or an array of such strings.
  187. * For the general form and interpretation of the locales argument,
  188. * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
  189. *
  190. * @param options - An [object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/RelativeTimeFormat#Parameters)
  191. * with some or all of options of the formatting.
  192. *
  193. * @returns An array containing those of the provided locales
  194. * that are supported in date and time formatting
  195. * without having to fall back to the runtime's default locale.
  196. *
  197. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/RelativeTimeFormat/supportedLocalesOf).
  198. */
  199. supportedLocalesOf(
  200. locales?: UnicodeBCP47LocaleIdentifier | UnicodeBCP47LocaleIdentifier[],
  201. options?: RelativeTimeFormatOptions,
  202. ): UnicodeBCP47LocaleIdentifier[];
  203. };
  204. interface NumberFormatOptions {
  205. compactDisplay?: "short" | "long" | undefined;
  206. notation?: "standard" | "scientific" | "engineering" | "compact" | undefined;
  207. signDisplay?: "auto" | "never" | "always" | "exceptZero" | undefined;
  208. unit?: string | undefined;
  209. unitDisplay?: "short" | "long" | "narrow" | undefined;
  210. currencyDisplay?: string | undefined;
  211. currencySign?: string | undefined;
  212. }
  213. interface ResolvedNumberFormatOptions {
  214. compactDisplay?: "short" | "long";
  215. notation?: "standard" | "scientific" | "engineering" | "compact";
  216. signDisplay?: "auto" | "never" | "always" | "exceptZero";
  217. unit?: string;
  218. unitDisplay?: "short" | "long" | "narrow";
  219. currencyDisplay?: string;
  220. currencySign?: string;
  221. }
  222. interface DateTimeFormatOptions {
  223. calendar?: string | undefined;
  224. dayPeriod?: "narrow" | "short" | "long" | undefined;
  225. numberingSystem?: string | undefined;
  226. dateStyle?: "full" | "long" | "medium" | "short" | undefined;
  227. timeStyle?: "full" | "long" | "medium" | "short" | undefined;
  228. hourCycle?: "h11" | "h12" | "h23" | "h24" | undefined;
  229. }
  230. type LocaleHourCycleKey = "h12" | "h23" | "h11" | "h24";
  231. type LocaleCollationCaseFirst = "upper" | "lower" | "false";
  232. interface LocaleOptions {
  233. /** A string containing the language, and the script and region if available. */
  234. baseName?: string;
  235. /** The part of the Locale that indicates the locale's calendar era. */
  236. calendar?: string;
  237. /** Flag that defines whether case is taken into account for the locale's collation rules. */
  238. caseFirst?: LocaleCollationCaseFirst;
  239. /** The collation type used for sorting */
  240. collation?: string;
  241. /** The time keeping format convention used by the locale. */
  242. hourCycle?: LocaleHourCycleKey;
  243. /** The primary language subtag associated with the locale. */
  244. language?: string;
  245. /** The numeral system used by the locale. */
  246. numberingSystem?: string;
  247. /** Flag that defines whether the locale has special collation handling for numeric characters. */
  248. numeric?: boolean;
  249. /** The region of the world (usually a country) associated with the locale. Possible values are region codes as defined by ISO 3166-1. */
  250. region?: string;
  251. /** The script used for writing the particular language used in the locale. Possible values are script codes as defined by ISO 15924. */
  252. script?: string;
  253. }
  254. interface Locale extends LocaleOptions {
  255. /** A string containing the language, and the script and region if available. */
  256. baseName: string;
  257. /** The primary language subtag associated with the locale. */
  258. language: string;
  259. /** Gets the most likely values for the language, script, and region of the locale based on existing values. */
  260. maximize(): Locale;
  261. /** Attempts to remove information about the locale that would be added by calling `Locale.maximize()`. */
  262. minimize(): Locale;
  263. /** Returns the locale's full locale identifier string. */
  264. toString(): BCP47LanguageTag;
  265. }
  266. /**
  267. * Constructor creates [Intl.Locale](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale)
  268. * objects
  269. *
  270. * @param tag - A string with a [BCP 47 language tag](http://tools.ietf.org/html/rfc5646).
  271. * For the general form and interpretation of the locales argument,
  272. * see the [`Intl` page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_identification_and_negotiation).
  273. *
  274. * @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.
  275. *
  276. * @returns [Intl.Locale](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale) object.
  277. *
  278. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale).
  279. */
  280. const Locale: {
  281. new (tag: BCP47LanguageTag | Locale, options?: LocaleOptions): Locale;
  282. };
  283. type DisplayNamesFallback =
  284. | "code"
  285. | "none";
  286. type DisplayNamesType =
  287. | "language"
  288. | "region"
  289. | "script"
  290. | "calendar"
  291. | "dateTimeField"
  292. | "currency";
  293. type DisplayNamesLanguageDisplay =
  294. | "dialect"
  295. | "standard";
  296. interface DisplayNamesOptions {
  297. localeMatcher?: RelativeTimeFormatLocaleMatcher;
  298. style?: RelativeTimeFormatStyle;
  299. type: DisplayNamesType;
  300. languageDisplay?: DisplayNamesLanguageDisplay;
  301. fallback?: DisplayNamesFallback;
  302. }
  303. interface ResolvedDisplayNamesOptions {
  304. locale: UnicodeBCP47LocaleIdentifier;
  305. style: RelativeTimeFormatStyle;
  306. type: DisplayNamesType;
  307. fallback: DisplayNamesFallback;
  308. languageDisplay?: DisplayNamesLanguageDisplay;
  309. }
  310. interface DisplayNames {
  311. /**
  312. * Receives a code and returns a string based on the locale and options provided when instantiating
  313. * [`Intl.DisplayNames()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames)
  314. *
  315. * @param code The `code` to provide depends on the `type` passed to display name during creation:
  316. * - 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),
  317. * or a [three digits UN M49 Geographic Regions](https://unstats.un.org/unsd/methodology/m49/).
  318. * - If the type is `"script"`, code should be an [ISO-15924 four letters script code](https://unicode.org/iso15924/iso15924-codes.html).
  319. * - If the type is `"language"`, code should be a `languageCode` ["-" `scriptCode`] ["-" `regionCode` ] *("-" `variant` )
  320. * 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).
  321. * `languageCode` is either a two letters ISO 639-1 language code or a three letters ISO 639-2 language code.
  322. * - If the type is `"currency"`, code should be a [3-letter ISO 4217 currency code](https://www.iso.org/iso-4217-currency-codes.html).
  323. *
  324. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/of).
  325. */
  326. of(code: string): string | undefined;
  327. /**
  328. * Returns a new object with properties reflecting the locale and style formatting options computed during the construction of the current
  329. * [`Intl/DisplayNames`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames) object.
  330. *
  331. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/resolvedOptions).
  332. */
  333. resolvedOptions(): ResolvedDisplayNamesOptions;
  334. }
  335. /**
  336. * The [`Intl.DisplayNames()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames)
  337. * object enables the consistent translation of language, region and script display names.
  338. *
  339. * [Compatibility](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames#browser_compatibility).
  340. */
  341. const DisplayNames: {
  342. prototype: DisplayNames;
  343. /**
  344. * @param locales A string with a BCP 47 language tag, or an array of such strings.
  345. * 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)
  346. * page.
  347. *
  348. * @param options An object for setting up a display name.
  349. *
  350. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/DisplayNames).
  351. */
  352. new(locales: LocalesArgument, options: DisplayNamesOptions): DisplayNames;
  353. /**
  354. * 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.
  355. *
  356. * @param locales A string with a BCP 47 language tag, or an array of such strings.
  357. * 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)
  358. * page.
  359. *
  360. * @param options An object with a locale matcher.
  361. *
  362. * @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.
  363. *
  364. * [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DisplayNames/supportedLocalesOf).
  365. */
  366. supportedLocalesOf(locales?: LocalesArgument, options?: { localeMatcher?: RelativeTimeFormatLocaleMatcher }): BCP47LanguageTag[];
  367. };
  368. }