i18n.js 6.7 KB

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