UNPKG

6.11 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 };
47declare type Modifiers = { [key: string]: (str : string) => string };
48
49declare type TranslateResult = string | LocaleMessages;
50declare type DateTimeFormatResult = string;
51declare type NumberFormatResult = string;
52declare type MissingHandler = (locale: Locale, key: Path, vm?: any) => string | void;
53
54declare type FormattedNumberPartType = 'currency' | 'decimal' | 'fraction' | 'group' | 'infinity' | 'integer' | 'literal' | 'minusSign' | 'nan' | 'plusSign' | 'percentSign';
55declare type FormattedNumberPart = {
56 type: FormattedNumberPartType,
57 value: string,
58};
59// This array is the same as Intl.NumberFormat.formatToParts() return value:
60// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat/formatToParts#Return_value
61declare type NumberFormatToPartsResult = Array<FormattedNumberPart>;
62
63declare type WarnHtmlInMessageLevel = 'off' | 'warn' | 'error';
64
65declare type I18nOptions = {
66 locale?: Locale,
67 fallbackLocale?: Locale,
68 messages?: LocaleMessages,
69 dateTimeFormats?: DateTimeFormats,
70 numberFormats?: NumberFormats,
71 formatter?: Formatter,
72 missing?: MissingHandler,
73 modifiers?: Modifiers,
74 root?: I18n, // for internal
75 fallbackRoot?: boolean,
76 formatFallbackMessages?: boolean,
77 sync?: boolean,
78 silentTranslationWarn?: boolean | RegExp,
79 silentFallbackWarn?: boolean | RegExp,
80 pluralizationRules?: PluralizationRules,
81 preserveDirectiveContent?: boolean,
82 warnHtmlInMessage?: WarnHtmlInMessageLevel,
83 sharedMessages?: LocaleMessage,
84};
85
86declare type IntlAvailability = {
87 dateTimeFormat: boolean,
88 numberFormat: boolean
89};
90
91declare type PluralizationRules = {
92 [lang: string]: (choice: number, choicesLength: number) => number,
93}
94
95declare interface I18n {
96 static install: () => void, // for Vue plugin interface
97 static version: string,
98 static availabilities: IntlAvailability,
99 get vm (): any, // for internal
100 get locale (): Locale,
101 set locale (locale: Locale): void,
102 get fallbackLocale (): Locale,
103 set fallbackLocale (locale: Locale): void,
104 get messages (): LocaleMessages,
105 get dateTimeFormats (): DateTimeFormats,
106 get numberFormats (): NumberFormats,
107 get availableLocales (): Locale[],
108 get missing (): ?MissingHandler,
109 set missing (handler: MissingHandler): void,
110 get formatter (): Formatter,
111 set formatter (formatter: Formatter): void,
112 get formatFallbackMessages (): boolean,
113 set formatFallbackMessages (fallback: boolean): void,
114 get silentTranslationWarn (): boolean | RegExp,
115 set silentTranslationWarn (silent: boolean | RegExp): void,
116 get silentFallbackWarn (): boolean | RegExp,
117 set silentFallbackWarn (slient: boolean | RegExp): void,
118 get pluralizationRules (): PluralizationRules,
119 set pluralizationRules (rules: PluralizationRules): void,
120 get preserveDirectiveContent (): boolean,
121 set preserveDirectiveContent (preserve: boolean): void,
122 get warnHtmlInMessage (): WarnHtmlInMessageLevel,
123 set warnHtmlInMessage (level: WarnHtmlInMessageLevel): void,
124
125 getLocaleMessage (locale: Locale): LocaleMessageObject,
126 setLocaleMessage (locale: Locale, message: LocaleMessageObject): void,
127 mergeLocaleMessage (locale: Locale, message: LocaleMessageObject): void,
128 t (key: Path, ...values: any): TranslateResult,
129 i (key: Path, locale: Locale, values: Object): TranslateResult,
130 tc (key: Path, choice?: number, ...values: any): TranslateResult,
131 te (key: Path, locale?: Locale): boolean,
132 getDateTimeFormat (locale: Locale): DateTimeFormat,
133 setDateTimeFormat (locale: Locale, format: DateTimeFormat): void,
134 mergeDateTimeFormat (locale: Locale, format: DateTimeFormat): void,
135 d (value: number | Date, ...args: any): DateTimeFormatResult,
136 getNumberFormat (locale: Locale): NumberFormat,
137 setNumberFormat (locale: Locale, format: NumberFormat): void,
138 mergeNumberFormat (locale: Locale, format: NumberFormat): void,
139 n (value: number, ...args: any): NumberFormatResult,
140 getChoiceIndex: (choice: number, choicesLength: number) => number,
141 pluralizationRules: PluralizationRules,
142 preserveDirectiveContent: boolean
143};
144
145declare interface Formatter {
146 interpolate (message: string, values: any, path: string): (Array<any> | null)
147};