UNPKG

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