index.d.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. import Vue, { PluginFunction } from 'vue';
  2. declare namespace VueI18n {
  3. type Path = string;
  4. type Locale = string;
  5. type FallbackLocale = string | string[] | false | { [locale: string]: string[] }
  6. type Values = any[] | { [key: string]: any };
  7. type Choice = number;
  8. interface MessageContext {
  9. list(index: number): unknown
  10. named(key: string): unknown
  11. }
  12. type MessageFunction = (ctx: MessageContext) => string;
  13. type LocaleMessage = string | MessageFunction | LocaleMessageObject | LocaleMessageArray;
  14. interface LocaleMessageObject { [key: string]: LocaleMessage; }
  15. interface LocaleMessageArray { [index: number]: LocaleMessage; }
  16. interface LocaleMessages { [key: string]: LocaleMessageObject; }
  17. type TranslateResult = string | LocaleMessages;
  18. type LocaleMatcher = 'lookup' | 'best-fit';
  19. type FormatMatcher = 'basic' | 'best-fit';
  20. type DateTimeHumanReadable = 'long' | 'short' | 'narrow';
  21. type DateTimeDigital = 'numeric' | '2-digit';
  22. interface SpecificDateTimeFormatOptions extends Intl.DateTimeFormatOptions {
  23. year?: DateTimeDigital;
  24. month?: DateTimeDigital | DateTimeHumanReadable;
  25. day?: DateTimeDigital;
  26. hour?: DateTimeDigital;
  27. minute?: DateTimeDigital;
  28. second?: DateTimeDigital;
  29. weekday?: DateTimeHumanReadable;
  30. era?: DateTimeHumanReadable;
  31. timeZoneName?: 'long' | 'short';
  32. localeMatcher?: LocaleMatcher;
  33. formatMatcher?: FormatMatcher;
  34. }
  35. type DateTimeFormatOptions = Intl.DateTimeFormatOptions | SpecificDateTimeFormatOptions;
  36. interface DateTimeFormat { [key: string]: DateTimeFormatOptions; }
  37. interface DateTimeFormats { [locale: string]: DateTimeFormat; }
  38. type DateTimeFormatResult = string;
  39. type CurrencyDisplay = 'symbol' | 'code' | 'name';
  40. interface SpecificNumberFormatOptions extends Intl.NumberFormatOptions {
  41. style?: 'decimal' | 'percent';
  42. currency?: string;
  43. currencyDisplay?: CurrencyDisplay;
  44. localeMatcher?: LocaleMatcher;
  45. formatMatcher?: FormatMatcher;
  46. }
  47. interface CurrencyNumberFormatOptions extends Intl.NumberFormatOptions {
  48. style: 'currency';
  49. currency: string; // Obligatory if style is 'currency'
  50. currencyDisplay?: CurrencyDisplay;
  51. localeMatcher?: LocaleMatcher;
  52. formatMatcher?: FormatMatcher;
  53. }
  54. type NumberFormatOptions = Intl.NumberFormatOptions | SpecificNumberFormatOptions | CurrencyNumberFormatOptions;
  55. interface NumberFormat { [key: string]: NumberFormatOptions; }
  56. interface NumberFormats { [locale: string]: NumberFormat; }
  57. type NumberFormatResult = string;
  58. type PluralizationRulesMap = {
  59. /**
  60. * @param choice {number} a choice index given by the input to $tc: `$tc('path.to.rule', choiceIndex)`
  61. * @param choicesLength {number} an overall amount of available choices
  62. * @returns a final choice index
  63. */
  64. [lang: string]: (choice: number, choicesLength: number) => number;
  65. };
  66. type Modifiers = { [key: string]: (str : string) => string };
  67. type FormattedNumberPartType = 'currency' | 'decimal' | 'fraction' | 'group' | 'infinity' | 'integer' | 'literal' | 'minusSign' | 'nan' | 'plusSign' | 'percentSign';
  68. type WarnHtmlInMessageLevel = 'off' | 'warn' | 'error';
  69. interface FormattedNumberPart {
  70. type: FormattedNumberPartType;
  71. value: string;
  72. }
  73. interface NumberFormatToPartsResult { [index: number]: FormattedNumberPart; }
  74. interface Formatter {
  75. interpolate(message: string, values: Values | undefined, path: string): (any[] | null);
  76. }
  77. type MissingHandler = (locale: Locale, key: Path, vm: Vue | null, values: any) => string | void;
  78. type PostTranslationHandler = (str: string, key?: string) => string;
  79. type ComponentInstanceCreatedListener = (newVm: VueI18n & IVueI18n, rootVm: VueI18n & IVueI18n) => void;
  80. interface IntlAvailability {
  81. dateTimeFormat: boolean;
  82. numberFormat: boolean;
  83. }
  84. // tslint:disable-next-line:interface-name
  85. interface I18nOptions {
  86. locale?: Locale;
  87. fallbackLocale?: FallbackLocale;
  88. messages?: LocaleMessages;
  89. dateTimeFormats?: DateTimeFormats;
  90. numberFormats?: NumberFormats;
  91. formatter?: Formatter;
  92. modifiers?: Modifiers,
  93. missing?: MissingHandler;
  94. fallbackRoot?: boolean;
  95. formatFallbackMessages?: boolean;
  96. sync?: boolean;
  97. silentTranslationWarn?: boolean | RegExp;
  98. silentFallbackWarn?: boolean | RegExp;
  99. preserveDirectiveContent?: boolean;
  100. pluralizationRules?: PluralizationRulesMap;
  101. warnHtmlInMessage?: WarnHtmlInMessageLevel;
  102. sharedMessages?: LocaleMessages;
  103. postTranslation?: PostTranslationHandler;
  104. componentInstanceCreatedListener?: ComponentInstanceCreatedListener;
  105. }
  106. }
  107. export type Path = VueI18n.Path;
  108. export type Locale = VueI18n.Locale;
  109. export type FallbackLocale = VueI18n.FallbackLocale;
  110. export type Values = VueI18n.Values;
  111. export type Choice = VueI18n.Choice;
  112. export type MessageContext = VueI18n.MessageContext;
  113. export type MessageFunction = VueI18n.MessageFunction;
  114. export type LocaleMessage = VueI18n.LocaleMessage;
  115. export type LocaleMessageObject = VueI18n.LocaleMessageObject;
  116. export type LocaleMessageArray = VueI18n.LocaleMessageArray;
  117. export type LocaleMessages = VueI18n.LocaleMessages;
  118. export type TranslateResult = VueI18n.TranslateResult;
  119. export type DateTimeFormatOptions = VueI18n.DateTimeFormatOptions;
  120. export type DateTimeFormat = VueI18n.DateTimeFormat;
  121. export type DateTimeFormats = VueI18n.DateTimeFormats;
  122. export type DateTimeFormatResult = VueI18n.DateTimeFormatResult;
  123. export type NumberFormatOptions = VueI18n.NumberFormatOptions;
  124. export type NumberFormat = VueI18n.NumberFormat;
  125. export type NumberFormats = VueI18n.NumberFormats;
  126. export type NumberFormatResult = VueI18n.NumberFormatResult;
  127. export type NumberFormatToPartsResult = VueI18n.NumberFormatToPartsResult;
  128. export type WarnHtmlInMessageLevel = VueI18n.WarnHtmlInMessageLevel;
  129. export type Formatter = VueI18n.Formatter;
  130. export type MissingHandler = VueI18n.MissingHandler;
  131. export type PostTranslationHandler = VueI18n.PostTranslationHandler;
  132. export type IntlAvailability = VueI18n.IntlAvailability;
  133. export type I18nOptions = VueI18n.I18nOptions;
  134. export declare interface IVueI18n {
  135. readonly messages: VueI18n.LocaleMessages;
  136. readonly dateTimeFormats: VueI18n.DateTimeFormats;
  137. readonly numberFormats: VueI18n.NumberFormats;
  138. locale: VueI18n.Locale;
  139. fallbackLocale: VueI18n.FallbackLocale;
  140. missing: VueI18n.MissingHandler;
  141. formatter: VueI18n.Formatter;
  142. formatFallbackMessages: boolean;
  143. silentTranslationWarn: boolean | RegExp;
  144. silentFallbackWarn: boolean | RegExp;
  145. preserveDirectiveContent: boolean;
  146. pluralizationRules: VueI18n.PluralizationRulesMap;
  147. warnHtmlInMessage: VueI18n.WarnHtmlInMessageLevel;
  148. postTranslation: VueI18n.PostTranslationHandler;
  149. t(key: VueI18n.Path, values?: VueI18n.Values): VueI18n.TranslateResult;
  150. t(key: VueI18n.Path, locale: VueI18n.Locale, values?: VueI18n.Values): VueI18n.TranslateResult;
  151. tc(key: VueI18n.Path, choice?: VueI18n.Choice, values?: VueI18n.Values): string;
  152. tc(
  153. key: VueI18n.Path,
  154. choice: VueI18n.Choice,
  155. locale: VueI18n.Locale,
  156. values?: VueI18n.Values,
  157. ): string;
  158. te(key: VueI18n.Path, locale?: VueI18n.Locale): boolean;
  159. d(
  160. value: number | Date,
  161. key?: VueI18n.Path,
  162. locale?: VueI18n.Locale,
  163. ): VueI18n.DateTimeFormatResult;
  164. d(value: number | Date, args?: { [key: string]: string }): VueI18n.DateTimeFormatResult;
  165. n(value: number, key?: VueI18n.Path, locale?: VueI18n.Locale): VueI18n.NumberFormatResult;
  166. n(value: number, args?: { [key: string]: string }): VueI18n.NumberFormatResult;
  167. getLocaleMessage(locale: VueI18n.Locale): VueI18n.LocaleMessageObject;
  168. setLocaleMessage(locale: VueI18n.Locale, message: VueI18n.LocaleMessageObject): void;
  169. mergeLocaleMessage(locale: VueI18n.Locale, message: VueI18n.LocaleMessageObject): void;
  170. getDateTimeFormat(locale: VueI18n.Locale): VueI18n.DateTimeFormat;
  171. setDateTimeFormat(locale: VueI18n.Locale, format: VueI18n.DateTimeFormat): void;
  172. mergeDateTimeFormat(locale: VueI18n.Locale, format: VueI18n.DateTimeFormat): void;
  173. getNumberFormat(locale: VueI18n.Locale): VueI18n.NumberFormat;
  174. setNumberFormat(locale: VueI18n.Locale, format: VueI18n.NumberFormat): void;
  175. mergeNumberFormat(locale: VueI18n.Locale, format: VueI18n.NumberFormat): void;
  176. getChoiceIndex: (choice: number, choicesLength: number) => number;
  177. }
  178. declare class VueI18n {
  179. constructor(options?: VueI18n.I18nOptions)
  180. readonly messages: VueI18n.LocaleMessages;
  181. readonly dateTimeFormats: VueI18n.DateTimeFormats;
  182. readonly numberFormats: VueI18n.NumberFormats;
  183. readonly availableLocales: VueI18n.Locale[];
  184. locale: VueI18n.Locale;
  185. fallbackLocale: VueI18n.FallbackLocale;
  186. missing: VueI18n.MissingHandler;
  187. formatter: VueI18n.Formatter;
  188. formatFallbackMessages: boolean;
  189. silentTranslationWarn: boolean | RegExp;
  190. silentFallbackWarn: boolean | RegExp;
  191. preserveDirectiveContent: boolean;
  192. pluralizationRules: VueI18n.PluralizationRulesMap;
  193. warnHtmlInMessage: VueI18n.WarnHtmlInMessageLevel;
  194. postTranslation: VueI18n.PostTranslationHandler;
  195. t(key: VueI18n.Path, values?: VueI18n.Values): VueI18n.TranslateResult;
  196. t(key: VueI18n.Path, locale: VueI18n.Locale, values?: VueI18n.Values): VueI18n.TranslateResult;
  197. tc(key: VueI18n.Path, choice?: VueI18n.Choice, values?: VueI18n.Values): string;
  198. tc(key: VueI18n.Path, choice: VueI18n.Choice, locale: VueI18n.Locale, values?: VueI18n.Values): string;
  199. te(key: VueI18n.Path, locale?: VueI18n.Locale): boolean;
  200. d(value: number | Date, key?: VueI18n.Path, locale?: VueI18n.Locale): VueI18n.DateTimeFormatResult;
  201. d(value: number | Date, args?: { [key: string]: string }): VueI18n.DateTimeFormatResult;
  202. n(value: number, key?: VueI18n.Path, locale?: VueI18n.Locale): VueI18n.NumberFormatResult;
  203. n(value: number, args?: { [key: string]: string }): VueI18n.NumberFormatResult;
  204. getLocaleMessage(locale: VueI18n.Locale): VueI18n.LocaleMessageObject;
  205. setLocaleMessage(locale: VueI18n.Locale, message: VueI18n.LocaleMessageObject): void;
  206. mergeLocaleMessage(locale: VueI18n.Locale, message: VueI18n.LocaleMessageObject): void;
  207. getDateTimeFormat(locale: VueI18n.Locale): VueI18n.DateTimeFormat;
  208. setDateTimeFormat(locale: VueI18n.Locale, format: VueI18n.DateTimeFormat): void;
  209. mergeDateTimeFormat(locale: VueI18n.Locale, format: VueI18n.DateTimeFormat): void;
  210. getNumberFormat(locale: VueI18n.Locale): VueI18n.NumberFormat;
  211. setNumberFormat(locale: VueI18n.Locale, format: VueI18n.NumberFormat): void;
  212. mergeNumberFormat(locale: VueI18n.Locale, format: VueI18n.NumberFormat): void;
  213. /**
  214. * @param choice {number} a choice index given by the input to $tc: `$tc('path.to.rule', choiceIndex)`
  215. * @param choicesLength {number} an overall amount of available choices
  216. * @returns a final choice index
  217. */
  218. getChoiceIndex: (choice: number, choicesLength: number) => number;
  219. static install: PluginFunction<never>;
  220. static version: string;
  221. static availabilities: VueI18n.IntlAvailability;
  222. }
  223. declare module 'vue/types/vue' {
  224. interface Vue {
  225. readonly $i18n: VueI18n & IVueI18n;
  226. $t: typeof VueI18n.prototype.t;
  227. $tc: typeof VueI18n.prototype.tc;
  228. $te: typeof VueI18n.prototype.te;
  229. $d: typeof VueI18n.prototype.d;
  230. $n: typeof VueI18n.prototype.n;
  231. }
  232. }
  233. declare module 'vue/types/options' {
  234. interface ComponentOptions<V extends Vue> {
  235. i18n?: {
  236. messages?: VueI18n.LocaleMessages;
  237. dateTimeFormats?: VueI18n.DateTimeFormats;
  238. numberFormats?: VueI18n.NumberFormats;
  239. sharedMessages?: VueI18n.LocaleMessages;
  240. };
  241. }
  242. }
  243. export default VueI18n;