i18n.js 6.9 KB

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