UNPKG

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