i18n.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. declare var Intl: any;
  2. declare type Path = string;
  3. declare type Locale = string;
  4. declare type LocaleMessage = string | LocaleMessageObject | LocaleMessageArray;
  5. declare type LocaleMessageObject = { [key: Path]: LocaleMessage };
  6. declare type LocaleMessageArray = Array<LocaleMessage>;
  7. declare type LocaleMessages = { [key: Locale]: LocaleMessageObject };
  8. // This options is the same as Intl.DateTimeFormat constructor options:
  9. // http://www.ecma-international.org/ecma-402/2.0/#sec-intl-datetimeformat-constructor
  10. declare type DateTimeFormatOptions = {
  11. year?: 'numeric' | '2-digit',
  12. month?: 'numeric' | '2-digit' | 'narrow' | 'short' | 'long',
  13. day?: 'numeric' | '2-digit',
  14. hour?: 'numeric' | '2-digit',
  15. minute?: 'numeric' | '2-digit',
  16. second?: 'numeric' | '2-digit',
  17. weekday?: 'narrow' | 'short' | 'long',
  18. hour12?: boolean,
  19. era?: 'narrow' | 'short' | 'long',
  20. timeZone?: string, // IANA time zone
  21. timeZoneName?: 'short' | 'long',
  22. localeMatcher?: 'lookup' | 'best fit',
  23. formatMatcher?: 'basic' | 'best fit'
  24. };
  25. declare type DateTimeFormat = { [key: string]: DateTimeFormatOptions };
  26. declare type DateTimeFormats = { [key: Locale]: DateTimeFormat };
  27. // This options is the same as Intl.NumberFormat constructor options:
  28. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat
  29. declare type NumberFormatOptions = {
  30. style?: 'decimal' | 'currency' | 'percent',
  31. currency?: string, // ISO 4217 currency codes
  32. currencyDisplay?: 'symbol' | 'code' | 'name',
  33. useGrouping?: boolean,
  34. minimumIntegerDigits?: number,
  35. minimumFractionDigits?: number,
  36. maximumFractionDigits?: number,
  37. minimumSignificantDigits?: number,
  38. maximumSignificantDigits?: number,
  39. localeMatcher?: 'lookup' | 'best fit',
  40. formatMatcher?: 'basic' | 'best fit'
  41. };
  42. declare type NumberFormat = { [key: string]: NumberFormatOptions };
  43. declare type NumberFormats = { [key: Locale]: NumberFormat };
  44. declare type TranslateResult = string | Array<any>;
  45. declare type DateTimeFormatResult = string;
  46. declare type NumberFormatResult = string;
  47. declare type MissingHandler = (locale: Locale, key: Path, vm?: any) => void;
  48. declare type I18nOptions = {
  49. locale?: Locale,
  50. fallbackLocale?: Locale,
  51. messages?: LocaleMessages,
  52. dateTimeFormats?: DateTimeFormats,
  53. numberFormats?: NumberFormats,
  54. formatter?: Formatter,
  55. missing?: MissingHandler,
  56. root?: I18n, // for internal
  57. fallbackRoot?: boolean,
  58. sync?: boolean,
  59. silentTranslationWarn?: boolean
  60. };
  61. declare type IntlAvailability = {
  62. dateTimeFormat: boolean,
  63. numberFormat: boolean
  64. };
  65. declare interface I18n {
  66. static install: () => void, // for Vue plugin interface
  67. static version: string,
  68. static availabilities: IntlAvailability,
  69. get vm (): any, // for internal
  70. get locale (): Locale,
  71. set locale (locale: Locale): void,
  72. get fallbackLocale (): Locale,
  73. set fallbackLocale (locale: Locale): void,
  74. get messages (): LocaleMessages,
  75. get dateTimeFormats (): DateTimeFormats,
  76. get missing (): ?MissingHandler,
  77. set missing (handler: MissingHandler): void,
  78. get formatter (): Formatter,
  79. set formatter (formatter: Formatter): void,
  80. get silentTranslationWarn (): boolean,
  81. set silentTranslationWarn (silent: boolean): void,
  82. getLocaleMessage (locale: Locale): LocaleMessageObject,
  83. setLocaleMessage (locale: Locale, message: LocaleMessageObject): void,
  84. mergeLocaleMessage (locale: Locale, message: LocaleMessageObject): void,
  85. t (key: Path, ...values: any): TranslateResult,
  86. i (key: Path, locale: Locale, values: Object): TranslateResult,
  87. tc (key: Path, choice?: number, ...values: any): TranslateResult,
  88. te (key: Path, locale?: Locale): boolean,
  89. getDateTimeFormat (locale: Locale): DateTimeFormat,
  90. setDateTimeFormat (locale: Locale, format: DateTimeFormat): void,
  91. mergeDateTimeFormat (locale: Locale, format: DateTimeFormat): void,
  92. d (value: number | Date, ...args: any): DateTimeFormatResult,
  93. getNumberFormat (locale: Locale): NumberFormat,
  94. setNumberFormat (locale: Locale, format: NumberFormat): void,
  95. mergeNumberFormat (locale: Locale, format: NumberFormat): void,
  96. n (value: number, ...args: any): NumberFormatResult
  97. };
  98. declare interface Formatter {
  99. interpolate (message: string, values: any): Array<any>
  100. };