UNPKG

6.81 kBPlain TextView Raw
1import BigNumber from "bignumber.js";
2
3import { I18n } from "./I18n";
4
5export type MakePlural = (count: number, ordinal?: boolean) => string;
6
7export interface Dict {
8 [key: string]: any;
9}
10
11export type DateTime = string | number | Date;
12
13export interface TimeAgoInWordsOptions {
14 includeSeconds?: boolean;
15 scope?: Scope;
16}
17
18export type Numeric = BigNumber | string | number;
19
20/**
21 * Controls handling of arithmetic exceptions and rounding.
22 *
23 * - "up": round away from zero
24 * - "down" or "truncate": round towards zero (truncate)
25 * - "halfUp" or "default": round towards the nearest neighbor, unless both
26 * neighbors are equidistant, in which case round away from zero.
27 * - "halfDown": round towards the nearest neighbor, unless both neighbors are
28 * equidistant, in which case round towards zero.
29 * - "halfEven" or "banker": round towards the nearest neighbor, unless both
30 * neighbors are equidistant, in which case round towards the even neighbor
31 * (Banker’s rounding)
32 * - "ceiling" or "ceil": round towards positive infinity
33 * - "floor": round towards negative infinity
34 *
35 * @type {string}
36 */
37export type RoundingMode =
38 | "up"
39 | "down"
40 | "truncate"
41 | "halfUp"
42 | "default"
43 | "halfDown"
44 | "halfEven"
45 | "banker"
46 | "ceiling"
47 | "ceil"
48 | "floor";
49
50export interface FormatNumberOptions {
51 format: string;
52 negativeFormat: string;
53 precision: number | null;
54 roundMode: RoundingMode;
55 significant: boolean;
56 separator: string;
57 delimiter: string;
58 stripInsignificantZeros: boolean;
59 raise: boolean;
60 unit: string;
61}
62
63// I18n#numberToHumanSize options.
64export type NumberToHumanSizeOptions = Omit<
65 FormatNumberOptions,
66 "format" | "negativeFormat" | "raise"
67>;
68
69export type NumberToHumanUnits = {
70 [key: string]: string;
71};
72
73export type NumberToHumanOptions = Omit<
74 FormatNumberOptions,
75 "negativeFormat" | "unit" | "raise"
76> & {
77 units: NumberToHumanUnits | string;
78};
79
80export type NumberToDelimitedOptions = {
81 delimiterPattern: RegExp;
82 delimiter: string;
83 separator: string;
84};
85
86export type NumberToPercentageOptions = Omit<FormatNumberOptions, "raise">;
87
88export type NumberToRoundedOptions = Omit<
89 FormatNumberOptions,
90 "format" | "negativeFormat" | "raise"
91> & { precision: number };
92
93export type NumberToCurrencyOptions = FormatNumberOptions;
94
95export interface ToSentenceOptions {
96 wordsConnector: string;
97 twoWordsConnector: string;
98 lastWordConnector: string;
99}
100
101// Default primitive types.
102export type PrimitiveType = number | string | null | undefined | boolean;
103export type ArrayType = AnyObject[];
104export type AnyObject = PrimitiveType | ArrayType | ObjectType;
105
106export interface ObjectType {
107 [key: string]: PrimitiveType | ArrayType | ObjectType;
108}
109
110/**
111 * Possible missing translation behavior.
112 * @type {String}
113 */
114export type MissingBehavior = "message" | "guess" | "error";
115
116// The I18n class initializer options.
117export interface I18nOptions {
118 /**
119 * Set default locale. This locale will be used when fallback is enabled and
120 * the translation doesn't exist in a particular locale. Defaults to `en`.
121 *
122 * @type {string}
123 */
124 defaultLocale: string;
125
126 /**
127 * Set available locales. This will be used to load pluralizers automatically.
128 *
129 * @type {string[]}
130 */
131 availableLocales: string[];
132
133 /**
134 * Set the default string separator. Defaults to `.`, as in
135 * `scope.translation`.
136 *
137 * @type {string}
138 */
139 defaultSeparator: string;
140
141 /**
142 * Set if engine should fallback to the default locale when a translation is
143 * missing. Defaults to `false`.
144 *
145 * When enabled, missing translations will first be looked for in less
146 * specific versions of the requested locale and if that fails by taking them
147 * from your `I18n#defaultLocale`.
148 *
149 * @type {boolean}
150 */
151 enableFallback: boolean;
152
153 /**
154 * Set the current locale. Defaults to `en`.
155 *
156 * @type {string}
157 */
158 locale: string;
159
160 /**
161 * Set missing translation behavior.
162 *
163 * - `message` will display a message that the translation is missing.
164 * - `guess` will try to guess the string.
165 * - `error` will raise an exception whenever a translation is not defined.
166 *
167 * See {@link MissingTranslation.register} for instructions on how to define
168 * your own behavior.
169 *
170 * @type {MissingBehavior}
171 */
172 missingBehavior: MissingBehavior;
173
174 /**
175 * Return a missing placeholder message for given parameters.
176 *
177 * @type {MissingPlaceholderHandler}
178 */
179 missingPlaceholder: MissingPlaceholderHandler;
180
181 /**
182 * Return a placeholder message for null values. Defaults to the same behavior
183 * as `I18n.missingPlaceholder`.
184 *
185 * @type {NullPlaceholderHandler}
186 */
187 nullPlaceholder: NullPlaceholderHandler;
188
189 /**
190 * If you use missingBehavior with 'message', but want to know that the string
191 * is actually missing for testing purposes, you can prefix the guessed string
192 * by setting the value here. By default, no prefix is used.
193 *
194 * @type {string}
195 */
196 missingTranslationPrefix: string;
197
198 /**
199 * Set the placeholder format. Accepts `{{placeholder}}` and `%{placeholder}`.
200 *
201 * @type {RegExp}
202 */
203 placeholder: RegExp;
204
205 /**
206 * Transform keys. By default, it returns the key as it is, but allows for
207 * overriding. For instance, you can set a function to receive the camelcase
208 * key, and convert it to snake case.
209 *
210 * @type {function}
211 */
212 transformKey: (key: string) => string;
213}
214
215// The translation scope.
216export type Scope = Readonly<string | string[]>;
217
218// The locale resolver.
219export type LocaleResolver = (i18n: I18n, locale: string) => string[];
220
221// The pluralizer function.
222export type Pluralizer = (i18n: I18n, count: number) => string[];
223
224// The missing translation strategy.
225export type MissingTranslationStrategy = (
226 i18n: I18n,
227 scope: Scope,
228 options: Dict,
229) => string;
230
231export interface TranslateOptions {
232 defaultValue?: any;
233 count?: number;
234 scope?: Scope;
235 defaults?: Dict[];
236 missingBehavior?: MissingBehavior | string;
237 [key: string]: any;
238}
239
240export type MissingPlaceholderHandler = (
241 i18n: I18n,
242 placeholder: string,
243 message: string,
244 options: Dict,
245) => string;
246
247export type NullPlaceholderHandler = (
248 i18n: I18n,
249 placeholder: string,
250 message: string,
251 options: Dict,
252) => string;
253
254export type DayNames = [string, string, string, string, string, string, string];
255export type MonthNames = [
256 null,
257 string,
258 string,
259 string,
260 string,
261 string,
262 string,
263 string,
264 string,
265 string,
266 string,
267 string,
268 string,
269];
270
271export interface StrftimeOptions {
272 meridian: {
273 am: string;
274 pm: string;
275 };
276
277 dayNames: DayNames;
278 abbrDayNames: DayNames;
279 monthNames: MonthNames;
280 abbrMonthNames: MonthNames;
281}
282
283export type OnChangeHandler = (i18n: I18n) => void;