i18n.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. declare var Intl: any;
  2. declare type Path = string;
  3. declare type Locale = string;
  4. declare type FallbackLocale = string | string[] | false | { [locale: string]: string[] };
  5. declare type LocaleMessage = string | LocaleMessageObject | LocaleMessageArray;
  6. declare type LocaleMessageObject = { [key: Path]: LocaleMessage };
  7. declare type LocaleMessageArray = Array<LocaleMessage>;
  8. declare type LocaleMessages = { [key: Locale]: LocaleMessageObject };
  9. // This options is the same as Intl.DateTimeFormat constructor options:
  10. // http://www.ecma-international.org/ecma-402/2.0/#sec-intl-datetimeformat-constructor
  11. declare type DateTimeFormatOptions = {
  12. year?: 'numeric' | '2-digit',
  13. month?: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long',
  14. day?: 'numeric' | '2-digit',
  15. hour?: 'numeric' | '2-digit',
  16. minute?: 'numeric' | '2-digit',
  17. second?: 'numeric' | '2-digit',
  18. weekday?: 'narrow' | 'short' | 'long',
  19. hour12?: boolean,
  20. era?: 'narrow' | 'short' | 'long',
  21. timeZone?: string, // IANA time zone
  22. timeZoneName?: 'short' | 'long',
  23. localeMatcher?: 'lookup' | 'best fit',
  24. formatMatcher?: 'basic' | 'best fit'
  25. };
  26. declare type DateTimeFormat = { [key: string]: DateTimeFormatOptions };
  27. declare type DateTimeFormats = { [key: Locale]: DateTimeFormat };
  28. // This options is the same as Intl.NumberFormat constructor options:
  29. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
  30. declare type NumberFormatOptions = {
  31. style?: 'decimal' | 'currency' | 'percent',
  32. currency?: string, // ISO 4217 currency codes
  33. currencyDisplay?: 'symbol' | 'code' | 'name',
  34. useGrouping?: boolean,
  35. minimumIntegerDigits?: number,
  36. minimumFractionDigits?: number,
  37. maximumFractionDigits?: number,
  38. minimumSignificantDigits?: number,
  39. maximumSignificantDigits?: number,
  40. localeMatcher?: 'lookup' | 'best fit',
  41. formatMatcher?: 'basic' | 'best fit'
  42. };
  43. declare type NumberFormat = { [key: string]: NumberFormatOptions };
  44. declare type NumberFormats = { [key: Locale]: NumberFormat };
  45. declare type Modifiers = { [key: string]: (str: string) => string };
  46. declare type TranslateResult = string | LocaleMessages;
  47. declare type DateTimeFormatResult = string;
  48. declare type NumberFormatResult = string;
  49. declare type MissingHandler = (locale: Locale, key: Path, vm?: any) => string | void;
  50. declare type PostTranslationHandler = (str: string, key?: string) => string;
  51. declare type GetChoiceIndex = (choice: number, choicesLength: number) => number
  52. declare type ComponentInstanceCreatedListener = (newI18n: I18n, rootI18n: I18n) => void;
  53. declare type FormattedNumberPartType = 'currency' | 'decimal' | 'fraction' | 'group' | 'infinity' | 'integer' | 'literal' | 'minusSign' | 'nan' | 'plusSign' | 'percentSign';
  54. declare type FormattedNumberPart = {
  55. type: FormattedNumberPartType,
  56. value: string,
  57. };
  58. // This array is the same as Intl.NumberFormat.formatToParts() return value:
  59. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/formatToParts#Return_value
  60. declare type NumberFormatToPartsResult = Array<FormattedNumberPart>;
  61. declare type WarnHtmlInMessageLevel = 'off' | 'warn' | 'error';
  62. declare type I18nOptions = {
  63. locale?: Locale,
  64. fallbackLocale?: FallbackLocale,
  65. messages?: LocaleMessages,
  66. dateTimeFormats?: DateTimeFormats,
  67. numberFormats?: NumberFormats,
  68. formatter?: Formatter,
  69. missing?: MissingHandler,
  70. modifiers?: Modifiers,
  71. root?: I18n, // for internal
  72. fallbackRoot?: boolean,
  73. formatFallbackMessages?: boolean,
  74. sync?: boolean,
  75. silentTranslationWarn?: boolean | RegExp,
  76. silentFallbackWarn?: boolean | RegExp,
  77. pluralizationRules?: PluralizationRules,
  78. preserveDirectiveContent?: boolean,
  79. warnHtmlInMessage?: WarnHtmlInMessageLevel,
  80. sharedMessages?: LocaleMessage,
  81. postTranslation?: PostTranslationHandler,
  82. componentInstanceCreatedListener?: ComponentInstanceCreatedListener,
  83. };
  84. declare type IntlAvailability = {
  85. dateTimeFormat: boolean,
  86. numberFormat: boolean
  87. };
  88. declare type PluralizationRules = {
  89. [lang: string]: GetChoiceIndex,
  90. }
  91. declare interface I18n {
  92. static install: () => void, // for Vue plugin interface
  93. static version: string,
  94. static availabilities: IntlAvailability,
  95. get vm (): any, // for internal
  96. get locale (): Locale,
  97. set locale (locale: Locale): void,
  98. get fallbackLocale (): FallbackLocale,
  99. set fallbackLocale (locale: FallbackLocale): void,
  100. get messages (): LocaleMessages,
  101. get dateTimeFormats (): DateTimeFormats,
  102. get numberFormats (): NumberFormats,
  103. get availableLocales (): Locale[],
  104. get missing (): ?MissingHandler,
  105. set missing (handler: MissingHandler): void,
  106. get formatter (): Formatter,
  107. set formatter (formatter: Formatter): void,
  108. get formatFallbackMessages (): boolean,
  109. set formatFallbackMessages (fallback: boolean): void,
  110. get silentTranslationWarn (): boolean | RegExp,
  111. set silentTranslationWarn (silent: boolean | RegExp): void,
  112. get silentFallbackWarn (): boolean | RegExp,
  113. set silentFallbackWarn (slient: boolean | RegExp): void,
  114. get pluralizationRules (): PluralizationRules,
  115. set pluralizationRules (rules: PluralizationRules): void,
  116. get preserveDirectiveContent (): boolean,
  117. set preserveDirectiveContent (preserve: boolean): void,
  118. get warnHtmlInMessage (): WarnHtmlInMessageLevel,
  119. set warnHtmlInMessage (level: WarnHtmlInMessageLevel): void,
  120. get postTranslation (): ?PostTranslationHandler,
  121. set postTranslation (handler: PostTranslationHandler): void,
  122. getLocaleMessage (locale: Locale): LocaleMessageObject,
  123. setLocaleMessage (locale: Locale, message: LocaleMessageObject): void,
  124. mergeLocaleMessage (locale: Locale, message: LocaleMessageObject): void,
  125. t (key: Path, ...values: any): TranslateResult,
  126. i (key: Path, locale: Locale, values: Object): TranslateResult,
  127. tc (key: Path, choice?: number, ...values: any): TranslateResult,
  128. te (key: Path, locale?: Locale): boolean,
  129. getDateTimeFormat (locale: Locale): DateTimeFormat,
  130. setDateTimeFormat (locale: Locale, format: DateTimeFormat): void,
  131. mergeDateTimeFormat (locale: Locale, format: DateTimeFormat): void,
  132. d (value: number | Date, ...args: any): DateTimeFormatResult,
  133. getNumberFormat (locale: Locale): NumberFormat,
  134. setNumberFormat (locale: Locale, format: NumberFormat): void,
  135. mergeNumberFormat (locale: Locale, format: NumberFormat): void,
  136. n (value: number, ...args: any): NumberFormatResult,
  137. getChoiceIndex: GetChoiceIndex,
  138. pluralizationRules: PluralizationRules,
  139. preserveDirectiveContent: boolean
  140. };
  141. declare interface Formatter {
  142. interpolate (message: string, values: any, path: string): (Array<any> | null)
  143. };