@lingui/core 4.5.0 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +53 -40
- package/dist/index.d.cts +15 -9
- package/dist/index.d.mts +15 -9
- package/dist/index.d.ts +15 -9
- package/dist/index.mjs +53 -40
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -11,9 +11,10 @@ const isString = (s) => typeof s === "string";
|
|
|
11
11
|
const isFunction = (f) => typeof f === "function";
|
|
12
12
|
|
|
13
13
|
const cache = /* @__PURE__ */ new Map();
|
|
14
|
+
const defaultLocale = "en";
|
|
14
15
|
function normalizeLocales(locales) {
|
|
15
16
|
const out = Array.isArray(locales) ? locales : [locales];
|
|
16
|
-
return [...out,
|
|
17
|
+
return [...out, defaultLocale];
|
|
17
18
|
}
|
|
18
19
|
function date(locales, value, format) {
|
|
19
20
|
const _locales = normalizeLocales(locales);
|
|
@@ -59,16 +60,19 @@ function cacheKey(type, locales, options) {
|
|
|
59
60
|
const formats = {
|
|
60
61
|
__proto__: null,
|
|
61
62
|
date: date,
|
|
63
|
+
defaultLocale: defaultLocale,
|
|
62
64
|
number: number,
|
|
63
65
|
plural: plural
|
|
64
66
|
};
|
|
65
67
|
|
|
66
68
|
const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
|
|
67
|
-
const getDefaultFormats = (locale,
|
|
68
|
-
locales =
|
|
69
|
-
const style = (format) =>
|
|
69
|
+
const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
70
|
+
const locales = passedLocales || locale;
|
|
71
|
+
const style = (format) => {
|
|
72
|
+
return typeof format === "object" ? format : formats[format] || { style: format };
|
|
73
|
+
};
|
|
70
74
|
const replaceOctothorpe = (value, message) => {
|
|
71
|
-
const numberFormat = Object.keys(formats).length ? style("number") :
|
|
75
|
+
const numberFormat = Object.keys(formats).length ? style("number") : void 0;
|
|
72
76
|
const valueStr = number(locales, value, numberFormat);
|
|
73
77
|
return message.replace("#", valueStr);
|
|
74
78
|
};
|
|
@@ -83,14 +87,16 @@ const getDefaultFormats = (locale, locales, formats = {}) => {
|
|
|
83
87
|
const message = plural(locales, true, value, cases);
|
|
84
88
|
return replaceOctothorpe(value - offset, message);
|
|
85
89
|
},
|
|
86
|
-
select:
|
|
90
|
+
select: selectFormatter,
|
|
87
91
|
number: (value, format) => number(locales, value, style(format)),
|
|
88
92
|
date: (value, format) => date(locales, value, style(format)),
|
|
89
|
-
undefined:
|
|
93
|
+
undefined: undefinedFormatter
|
|
90
94
|
};
|
|
91
95
|
};
|
|
96
|
+
const selectFormatter = (value, rules) => rules[value] ?? rules.other;
|
|
97
|
+
const undefinedFormatter = (value) => value;
|
|
92
98
|
function interpolate(translation, locale, locales) {
|
|
93
|
-
return (values
|
|
99
|
+
return (values = {}, formats) => {
|
|
94
100
|
const formatters = getDefaultFormats(locale, locales, formats);
|
|
95
101
|
const formatMessage = (message) => {
|
|
96
102
|
if (!Array.isArray(message))
|
|
@@ -100,14 +106,15 @@ function interpolate(translation, locale, locales) {
|
|
|
100
106
|
return message2 + token;
|
|
101
107
|
const [name, type, format] = token;
|
|
102
108
|
let interpolatedFormat = {};
|
|
103
|
-
if (format != null &&
|
|
104
|
-
Object.
|
|
105
|
-
interpolatedFormat[key] = formatMessage(
|
|
109
|
+
if (format != null && typeof format === "object") {
|
|
110
|
+
Object.entries(format).forEach(([key, value2]) => {
|
|
111
|
+
interpolatedFormat[key] = formatMessage(value2);
|
|
106
112
|
});
|
|
107
113
|
} else {
|
|
108
114
|
interpolatedFormat = format;
|
|
109
115
|
}
|
|
110
|
-
const
|
|
116
|
+
const formatter = formatters[type];
|
|
117
|
+
const value = formatter(values[name], interpolatedFormat);
|
|
111
118
|
if (value == null)
|
|
112
119
|
return message2;
|
|
113
120
|
return message2 + value;
|
|
@@ -119,7 +126,7 @@ function interpolate(translation, locale, locales) {
|
|
|
119
126
|
}
|
|
120
127
|
if (isString(result))
|
|
121
128
|
return result.trim();
|
|
122
|
-
return result;
|
|
129
|
+
return result ? String(result) : "";
|
|
123
130
|
};
|
|
124
131
|
}
|
|
125
132
|
|
|
@@ -134,25 +141,28 @@ class EventEmitter {
|
|
|
134
141
|
__publicField$1(this, "_events", {});
|
|
135
142
|
}
|
|
136
143
|
on(event, listener) {
|
|
137
|
-
|
|
138
|
-
|
|
144
|
+
var _a;
|
|
145
|
+
(_a = this._events)[event] ?? (_a[event] = []);
|
|
139
146
|
this._events[event].push(listener);
|
|
140
147
|
return () => this.removeListener(event, listener);
|
|
141
148
|
}
|
|
142
149
|
removeListener(event, listener) {
|
|
143
|
-
|
|
150
|
+
const maybeListeners = this._getListeners(event);
|
|
151
|
+
if (!maybeListeners)
|
|
144
152
|
return;
|
|
145
|
-
const index =
|
|
153
|
+
const index = maybeListeners.indexOf(listener);
|
|
146
154
|
if (~index)
|
|
147
|
-
|
|
155
|
+
maybeListeners.splice(index, 1);
|
|
148
156
|
}
|
|
149
157
|
emit(event, ...args) {
|
|
150
|
-
|
|
158
|
+
const maybeListeners = this._getListeners(event);
|
|
159
|
+
if (!maybeListeners)
|
|
151
160
|
return;
|
|
152
|
-
|
|
161
|
+
maybeListeners.map((listener) => listener.apply(this, args));
|
|
153
162
|
}
|
|
154
|
-
|
|
155
|
-
|
|
163
|
+
_getListeners(event) {
|
|
164
|
+
const maybeListeners = this._events[event];
|
|
165
|
+
return Array.isArray(maybeListeners) ? maybeListeners : false;
|
|
156
166
|
}
|
|
157
167
|
}
|
|
158
168
|
|
|
@@ -165,25 +175,23 @@ var __publicField = (obj, key, value) => {
|
|
|
165
175
|
class I18n extends EventEmitter {
|
|
166
176
|
constructor(params) {
|
|
167
177
|
super();
|
|
168
|
-
__publicField(this, "_locale");
|
|
178
|
+
__publicField(this, "_locale", "");
|
|
169
179
|
__publicField(this, "_locales");
|
|
170
|
-
__publicField(this, "_localeData");
|
|
171
|
-
__publicField(this, "_messages");
|
|
180
|
+
__publicField(this, "_localeData", {});
|
|
181
|
+
__publicField(this, "_messages", {});
|
|
172
182
|
__publicField(this, "_missing");
|
|
173
183
|
/**
|
|
174
184
|
* Alias for {@see I18n._}
|
|
175
185
|
*/
|
|
176
186
|
__publicField(this, "t", this._.bind(this));
|
|
177
|
-
this._messages = {};
|
|
178
|
-
this._localeData = {};
|
|
179
187
|
if (params.missing != null)
|
|
180
188
|
this._missing = params.missing;
|
|
181
189
|
if (params.messages != null)
|
|
182
190
|
this.load(params.messages);
|
|
183
191
|
if (params.localeData != null)
|
|
184
192
|
this.loadLocaleData(params.localeData);
|
|
185
|
-
if (params.locale
|
|
186
|
-
this.activate(params.locale, params.locales);
|
|
193
|
+
if (typeof params.locale === "string" || params.locales) {
|
|
194
|
+
this.activate(params.locale ?? defaultLocale, params.locales);
|
|
187
195
|
}
|
|
188
196
|
}
|
|
189
197
|
get locale() {
|
|
@@ -202,15 +210,17 @@ class I18n extends EventEmitter {
|
|
|
202
210
|
return this._localeData[this._locale] ?? {};
|
|
203
211
|
}
|
|
204
212
|
_loadLocaleData(locale, localeData) {
|
|
205
|
-
|
|
213
|
+
const maybeLocaleData = this._localeData[locale];
|
|
214
|
+
if (!maybeLocaleData) {
|
|
206
215
|
this._localeData[locale] = localeData;
|
|
207
216
|
} else {
|
|
208
|
-
Object.assign(
|
|
217
|
+
Object.assign(maybeLocaleData, localeData);
|
|
209
218
|
}
|
|
210
219
|
}
|
|
211
220
|
/**
|
|
212
221
|
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
213
222
|
*/
|
|
223
|
+
// @ts-ignore deprecated, so ignore the reported error
|
|
214
224
|
loadLocaleData(localeOrAllData, localeData) {
|
|
215
225
|
if (localeData != null) {
|
|
216
226
|
this._loadLocaleData(localeOrAllData, localeData);
|
|
@@ -222,18 +232,19 @@ class I18n extends EventEmitter {
|
|
|
222
232
|
this.emit("change");
|
|
223
233
|
}
|
|
224
234
|
_load(locale, messages) {
|
|
225
|
-
|
|
235
|
+
const maybeMessages = this._messages[locale];
|
|
236
|
+
if (!maybeMessages) {
|
|
226
237
|
this._messages[locale] = messages;
|
|
227
238
|
} else {
|
|
228
|
-
Object.assign(
|
|
239
|
+
Object.assign(maybeMessages, messages);
|
|
229
240
|
}
|
|
230
241
|
}
|
|
231
242
|
load(localeOrMessages, messages) {
|
|
232
|
-
if (messages
|
|
243
|
+
if (typeof localeOrMessages == "string" && typeof messages === "object") {
|
|
233
244
|
this._load(localeOrMessages, messages);
|
|
234
245
|
} else {
|
|
235
|
-
Object.
|
|
236
|
-
(locale) => this._load(locale,
|
|
246
|
+
Object.entries(localeOrMessages).forEach(
|
|
247
|
+
([locale, messages2]) => this._load(locale, messages2)
|
|
237
248
|
);
|
|
238
249
|
}
|
|
239
250
|
this.emit("change");
|
|
@@ -257,13 +268,15 @@ class I18n extends EventEmitter {
|
|
|
257
268
|
this._locales = locales;
|
|
258
269
|
this.emit("change");
|
|
259
270
|
}
|
|
260
|
-
_(id, values
|
|
271
|
+
_(id, values, options) {
|
|
272
|
+
let message = options?.message;
|
|
261
273
|
if (!isString(id)) {
|
|
262
274
|
values = id.values || values;
|
|
263
275
|
message = id.message;
|
|
264
276
|
id = id.id;
|
|
265
277
|
}
|
|
266
|
-
const
|
|
278
|
+
const messageForId = this.messages[id];
|
|
279
|
+
const messageMissing = messageForId === void 0;
|
|
267
280
|
const missing = this._missing;
|
|
268
281
|
if (missing && messageMissing) {
|
|
269
282
|
return isFunction(missing) ? missing(this._locale, id) : missing;
|
|
@@ -271,7 +284,7 @@ class I18n extends EventEmitter {
|
|
|
271
284
|
if (messageMissing) {
|
|
272
285
|
this.emit("missing", { id, locale: this._locale });
|
|
273
286
|
}
|
|
274
|
-
let translation =
|
|
287
|
+
let translation = messageForId || message || id;
|
|
275
288
|
if (process.env.NODE_ENV !== "production") {
|
|
276
289
|
translation = isString(translation) ? compileMessage.compileMessage(translation) : translation;
|
|
277
290
|
}
|
|
@@ -283,7 +296,7 @@ class I18n extends EventEmitter {
|
|
|
283
296
|
translation,
|
|
284
297
|
this._locale,
|
|
285
298
|
this._locales
|
|
286
|
-
)(values, formats);
|
|
299
|
+
)(values, options?.formats);
|
|
287
300
|
}
|
|
288
301
|
date(value, format) {
|
|
289
302
|
return date(this._locales || this._locale, value, format);
|
package/dist/index.d.cts
CHANGED
|
@@ -7,7 +7,7 @@ declare class EventEmitter<Events extends {
|
|
|
7
7
|
on(event: keyof Events, listener: Events[typeof event]): () => void;
|
|
8
8
|
removeListener(event: keyof Events, listener: Events[typeof event]): void;
|
|
9
9
|
emit(event: keyof Events, ...args: Parameters<Events[typeof event]>): void;
|
|
10
|
-
private
|
|
10
|
+
private _getListeners;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
type MessageOptions = {
|
|
@@ -67,13 +67,13 @@ type LoadAndActivateOptions = {
|
|
|
67
67
|
};
|
|
68
68
|
declare class I18n extends EventEmitter<Events> {
|
|
69
69
|
private _locale;
|
|
70
|
-
private _locales
|
|
70
|
+
private _locales?;
|
|
71
71
|
private _localeData;
|
|
72
72
|
private _messages;
|
|
73
|
-
private _missing
|
|
73
|
+
private _missing?;
|
|
74
74
|
constructor(params: setupI18nProps);
|
|
75
75
|
get locale(): string;
|
|
76
|
-
get locales(): Locales;
|
|
76
|
+
get locales(): Locales | undefined;
|
|
77
77
|
get messages(): Messages;
|
|
78
78
|
/**
|
|
79
79
|
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
@@ -107,18 +107,24 @@ declare class I18n extends EventEmitter<Events> {
|
|
|
107
107
|
}
|
|
108
108
|
declare function setupI18n(params?: setupI18nProps): I18n;
|
|
109
109
|
|
|
110
|
+
declare const defaultLocale = "en";
|
|
110
111
|
declare function date(locales: Locales, value: string | Date, format?: Intl.DateTimeFormatOptions): string;
|
|
111
112
|
declare function number(locales: Locales, value: number, format?: Intl.NumberFormatOptions): string;
|
|
112
|
-
|
|
113
|
-
[
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
type PluralOptions = {
|
|
114
|
+
[key: string]: Intl.LDMLPluralRule;
|
|
115
|
+
} & {
|
|
116
|
+
offset: number;
|
|
117
|
+
other: string;
|
|
118
|
+
};
|
|
119
|
+
declare function plural(locales: Locales, ordinal: boolean, value: number, { offset, ...rules }: PluralOptions): string;
|
|
116
120
|
|
|
121
|
+
type formats_PluralOptions = PluralOptions;
|
|
117
122
|
declare const formats_date: typeof date;
|
|
123
|
+
declare const formats_defaultLocale: typeof defaultLocale;
|
|
118
124
|
declare const formats_number: typeof number;
|
|
119
125
|
declare const formats_plural: typeof plural;
|
|
120
126
|
declare namespace formats {
|
|
121
|
-
export { formats_date as date, formats_number as number, formats_plural as plural };
|
|
127
|
+
export { type formats_PluralOptions as PluralOptions, formats_date as date, formats_defaultLocale as defaultLocale, formats_number as number, formats_plural as plural };
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
declare const i18n: I18n;
|
package/dist/index.d.mts
CHANGED
|
@@ -7,7 +7,7 @@ declare class EventEmitter<Events extends {
|
|
|
7
7
|
on(event: keyof Events, listener: Events[typeof event]): () => void;
|
|
8
8
|
removeListener(event: keyof Events, listener: Events[typeof event]): void;
|
|
9
9
|
emit(event: keyof Events, ...args: Parameters<Events[typeof event]>): void;
|
|
10
|
-
private
|
|
10
|
+
private _getListeners;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
type MessageOptions = {
|
|
@@ -67,13 +67,13 @@ type LoadAndActivateOptions = {
|
|
|
67
67
|
};
|
|
68
68
|
declare class I18n extends EventEmitter<Events> {
|
|
69
69
|
private _locale;
|
|
70
|
-
private _locales
|
|
70
|
+
private _locales?;
|
|
71
71
|
private _localeData;
|
|
72
72
|
private _messages;
|
|
73
|
-
private _missing
|
|
73
|
+
private _missing?;
|
|
74
74
|
constructor(params: setupI18nProps);
|
|
75
75
|
get locale(): string;
|
|
76
|
-
get locales(): Locales;
|
|
76
|
+
get locales(): Locales | undefined;
|
|
77
77
|
get messages(): Messages;
|
|
78
78
|
/**
|
|
79
79
|
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
@@ -107,18 +107,24 @@ declare class I18n extends EventEmitter<Events> {
|
|
|
107
107
|
}
|
|
108
108
|
declare function setupI18n(params?: setupI18nProps): I18n;
|
|
109
109
|
|
|
110
|
+
declare const defaultLocale = "en";
|
|
110
111
|
declare function date(locales: Locales, value: string | Date, format?: Intl.DateTimeFormatOptions): string;
|
|
111
112
|
declare function number(locales: Locales, value: number, format?: Intl.NumberFormatOptions): string;
|
|
112
|
-
|
|
113
|
-
[
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
type PluralOptions = {
|
|
114
|
+
[key: string]: Intl.LDMLPluralRule;
|
|
115
|
+
} & {
|
|
116
|
+
offset: number;
|
|
117
|
+
other: string;
|
|
118
|
+
};
|
|
119
|
+
declare function plural(locales: Locales, ordinal: boolean, value: number, { offset, ...rules }: PluralOptions): string;
|
|
116
120
|
|
|
121
|
+
type formats_PluralOptions = PluralOptions;
|
|
117
122
|
declare const formats_date: typeof date;
|
|
123
|
+
declare const formats_defaultLocale: typeof defaultLocale;
|
|
118
124
|
declare const formats_number: typeof number;
|
|
119
125
|
declare const formats_plural: typeof plural;
|
|
120
126
|
declare namespace formats {
|
|
121
|
-
export { formats_date as date, formats_number as number, formats_plural as plural };
|
|
127
|
+
export { type formats_PluralOptions as PluralOptions, formats_date as date, formats_defaultLocale as defaultLocale, formats_number as number, formats_plural as plural };
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
declare const i18n: I18n;
|
package/dist/index.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ declare class EventEmitter<Events extends {
|
|
|
7
7
|
on(event: keyof Events, listener: Events[typeof event]): () => void;
|
|
8
8
|
removeListener(event: keyof Events, listener: Events[typeof event]): void;
|
|
9
9
|
emit(event: keyof Events, ...args: Parameters<Events[typeof event]>): void;
|
|
10
|
-
private
|
|
10
|
+
private _getListeners;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
type MessageOptions = {
|
|
@@ -67,13 +67,13 @@ type LoadAndActivateOptions = {
|
|
|
67
67
|
};
|
|
68
68
|
declare class I18n extends EventEmitter<Events> {
|
|
69
69
|
private _locale;
|
|
70
|
-
private _locales
|
|
70
|
+
private _locales?;
|
|
71
71
|
private _localeData;
|
|
72
72
|
private _messages;
|
|
73
|
-
private _missing
|
|
73
|
+
private _missing?;
|
|
74
74
|
constructor(params: setupI18nProps);
|
|
75
75
|
get locale(): string;
|
|
76
|
-
get locales(): Locales;
|
|
76
|
+
get locales(): Locales | undefined;
|
|
77
77
|
get messages(): Messages;
|
|
78
78
|
/**
|
|
79
79
|
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
@@ -107,18 +107,24 @@ declare class I18n extends EventEmitter<Events> {
|
|
|
107
107
|
}
|
|
108
108
|
declare function setupI18n(params?: setupI18nProps): I18n;
|
|
109
109
|
|
|
110
|
+
declare const defaultLocale = "en";
|
|
110
111
|
declare function date(locales: Locales, value: string | Date, format?: Intl.DateTimeFormatOptions): string;
|
|
111
112
|
declare function number(locales: Locales, value: number, format?: Intl.NumberFormatOptions): string;
|
|
112
|
-
|
|
113
|
-
[
|
|
114
|
-
|
|
115
|
-
|
|
113
|
+
type PluralOptions = {
|
|
114
|
+
[key: string]: Intl.LDMLPluralRule;
|
|
115
|
+
} & {
|
|
116
|
+
offset: number;
|
|
117
|
+
other: string;
|
|
118
|
+
};
|
|
119
|
+
declare function plural(locales: Locales, ordinal: boolean, value: number, { offset, ...rules }: PluralOptions): string;
|
|
116
120
|
|
|
121
|
+
type formats_PluralOptions = PluralOptions;
|
|
117
122
|
declare const formats_date: typeof date;
|
|
123
|
+
declare const formats_defaultLocale: typeof defaultLocale;
|
|
118
124
|
declare const formats_number: typeof number;
|
|
119
125
|
declare const formats_plural: typeof plural;
|
|
120
126
|
declare namespace formats {
|
|
121
|
-
export { formats_date as date, formats_number as number, formats_plural as plural };
|
|
127
|
+
export { type formats_PluralOptions as PluralOptions, formats_date as date, formats_defaultLocale as defaultLocale, formats_number as number, formats_plural as plural };
|
|
122
128
|
}
|
|
123
129
|
|
|
124
130
|
declare const i18n: I18n;
|
package/dist/index.mjs
CHANGED
|
@@ -5,9 +5,10 @@ const isString = (s) => typeof s === "string";
|
|
|
5
5
|
const isFunction = (f) => typeof f === "function";
|
|
6
6
|
|
|
7
7
|
const cache = /* @__PURE__ */ new Map();
|
|
8
|
+
const defaultLocale = "en";
|
|
8
9
|
function normalizeLocales(locales) {
|
|
9
10
|
const out = Array.isArray(locales) ? locales : [locales];
|
|
10
|
-
return [...out,
|
|
11
|
+
return [...out, defaultLocale];
|
|
11
12
|
}
|
|
12
13
|
function date(locales, value, format) {
|
|
13
14
|
const _locales = normalizeLocales(locales);
|
|
@@ -53,16 +54,19 @@ function cacheKey(type, locales, options) {
|
|
|
53
54
|
const formats = {
|
|
54
55
|
__proto__: null,
|
|
55
56
|
date: date,
|
|
57
|
+
defaultLocale: defaultLocale,
|
|
56
58
|
number: number,
|
|
57
59
|
plural: plural
|
|
58
60
|
};
|
|
59
61
|
|
|
60
62
|
const UNICODE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/g;
|
|
61
|
-
const getDefaultFormats = (locale,
|
|
62
|
-
locales =
|
|
63
|
-
const style = (format) =>
|
|
63
|
+
const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
64
|
+
const locales = passedLocales || locale;
|
|
65
|
+
const style = (format) => {
|
|
66
|
+
return typeof format === "object" ? format : formats[format] || { style: format };
|
|
67
|
+
};
|
|
64
68
|
const replaceOctothorpe = (value, message) => {
|
|
65
|
-
const numberFormat = Object.keys(formats).length ? style("number") :
|
|
69
|
+
const numberFormat = Object.keys(formats).length ? style("number") : void 0;
|
|
66
70
|
const valueStr = number(locales, value, numberFormat);
|
|
67
71
|
return message.replace("#", valueStr);
|
|
68
72
|
};
|
|
@@ -77,14 +81,16 @@ const getDefaultFormats = (locale, locales, formats = {}) => {
|
|
|
77
81
|
const message = plural(locales, true, value, cases);
|
|
78
82
|
return replaceOctothorpe(value - offset, message);
|
|
79
83
|
},
|
|
80
|
-
select:
|
|
84
|
+
select: selectFormatter,
|
|
81
85
|
number: (value, format) => number(locales, value, style(format)),
|
|
82
86
|
date: (value, format) => date(locales, value, style(format)),
|
|
83
|
-
undefined:
|
|
87
|
+
undefined: undefinedFormatter
|
|
84
88
|
};
|
|
85
89
|
};
|
|
90
|
+
const selectFormatter = (value, rules) => rules[value] ?? rules.other;
|
|
91
|
+
const undefinedFormatter = (value) => value;
|
|
86
92
|
function interpolate(translation, locale, locales) {
|
|
87
|
-
return (values
|
|
93
|
+
return (values = {}, formats) => {
|
|
88
94
|
const formatters = getDefaultFormats(locale, locales, formats);
|
|
89
95
|
const formatMessage = (message) => {
|
|
90
96
|
if (!Array.isArray(message))
|
|
@@ -94,14 +100,15 @@ function interpolate(translation, locale, locales) {
|
|
|
94
100
|
return message2 + token;
|
|
95
101
|
const [name, type, format] = token;
|
|
96
102
|
let interpolatedFormat = {};
|
|
97
|
-
if (format != null &&
|
|
98
|
-
Object.
|
|
99
|
-
interpolatedFormat[key] = formatMessage(
|
|
103
|
+
if (format != null && typeof format === "object") {
|
|
104
|
+
Object.entries(format).forEach(([key, value2]) => {
|
|
105
|
+
interpolatedFormat[key] = formatMessage(value2);
|
|
100
106
|
});
|
|
101
107
|
} else {
|
|
102
108
|
interpolatedFormat = format;
|
|
103
109
|
}
|
|
104
|
-
const
|
|
110
|
+
const formatter = formatters[type];
|
|
111
|
+
const value = formatter(values[name], interpolatedFormat);
|
|
105
112
|
if (value == null)
|
|
106
113
|
return message2;
|
|
107
114
|
return message2 + value;
|
|
@@ -113,7 +120,7 @@ function interpolate(translation, locale, locales) {
|
|
|
113
120
|
}
|
|
114
121
|
if (isString(result))
|
|
115
122
|
return result.trim();
|
|
116
|
-
return result;
|
|
123
|
+
return result ? String(result) : "";
|
|
117
124
|
};
|
|
118
125
|
}
|
|
119
126
|
|
|
@@ -128,25 +135,28 @@ class EventEmitter {
|
|
|
128
135
|
__publicField$1(this, "_events", {});
|
|
129
136
|
}
|
|
130
137
|
on(event, listener) {
|
|
131
|
-
|
|
132
|
-
|
|
138
|
+
var _a;
|
|
139
|
+
(_a = this._events)[event] ?? (_a[event] = []);
|
|
133
140
|
this._events[event].push(listener);
|
|
134
141
|
return () => this.removeListener(event, listener);
|
|
135
142
|
}
|
|
136
143
|
removeListener(event, listener) {
|
|
137
|
-
|
|
144
|
+
const maybeListeners = this._getListeners(event);
|
|
145
|
+
if (!maybeListeners)
|
|
138
146
|
return;
|
|
139
|
-
const index =
|
|
147
|
+
const index = maybeListeners.indexOf(listener);
|
|
140
148
|
if (~index)
|
|
141
|
-
|
|
149
|
+
maybeListeners.splice(index, 1);
|
|
142
150
|
}
|
|
143
151
|
emit(event, ...args) {
|
|
144
|
-
|
|
152
|
+
const maybeListeners = this._getListeners(event);
|
|
153
|
+
if (!maybeListeners)
|
|
145
154
|
return;
|
|
146
|
-
|
|
155
|
+
maybeListeners.map((listener) => listener.apply(this, args));
|
|
147
156
|
}
|
|
148
|
-
|
|
149
|
-
|
|
157
|
+
_getListeners(event) {
|
|
158
|
+
const maybeListeners = this._events[event];
|
|
159
|
+
return Array.isArray(maybeListeners) ? maybeListeners : false;
|
|
150
160
|
}
|
|
151
161
|
}
|
|
152
162
|
|
|
@@ -159,25 +169,23 @@ var __publicField = (obj, key, value) => {
|
|
|
159
169
|
class I18n extends EventEmitter {
|
|
160
170
|
constructor(params) {
|
|
161
171
|
super();
|
|
162
|
-
__publicField(this, "_locale");
|
|
172
|
+
__publicField(this, "_locale", "");
|
|
163
173
|
__publicField(this, "_locales");
|
|
164
|
-
__publicField(this, "_localeData");
|
|
165
|
-
__publicField(this, "_messages");
|
|
174
|
+
__publicField(this, "_localeData", {});
|
|
175
|
+
__publicField(this, "_messages", {});
|
|
166
176
|
__publicField(this, "_missing");
|
|
167
177
|
/**
|
|
168
178
|
* Alias for {@see I18n._}
|
|
169
179
|
*/
|
|
170
180
|
__publicField(this, "t", this._.bind(this));
|
|
171
|
-
this._messages = {};
|
|
172
|
-
this._localeData = {};
|
|
173
181
|
if (params.missing != null)
|
|
174
182
|
this._missing = params.missing;
|
|
175
183
|
if (params.messages != null)
|
|
176
184
|
this.load(params.messages);
|
|
177
185
|
if (params.localeData != null)
|
|
178
186
|
this.loadLocaleData(params.localeData);
|
|
179
|
-
if (params.locale
|
|
180
|
-
this.activate(params.locale, params.locales);
|
|
187
|
+
if (typeof params.locale === "string" || params.locales) {
|
|
188
|
+
this.activate(params.locale ?? defaultLocale, params.locales);
|
|
181
189
|
}
|
|
182
190
|
}
|
|
183
191
|
get locale() {
|
|
@@ -196,15 +204,17 @@ class I18n extends EventEmitter {
|
|
|
196
204
|
return this._localeData[this._locale] ?? {};
|
|
197
205
|
}
|
|
198
206
|
_loadLocaleData(locale, localeData) {
|
|
199
|
-
|
|
207
|
+
const maybeLocaleData = this._localeData[locale];
|
|
208
|
+
if (!maybeLocaleData) {
|
|
200
209
|
this._localeData[locale] = localeData;
|
|
201
210
|
} else {
|
|
202
|
-
Object.assign(
|
|
211
|
+
Object.assign(maybeLocaleData, localeData);
|
|
203
212
|
}
|
|
204
213
|
}
|
|
205
214
|
/**
|
|
206
215
|
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
207
216
|
*/
|
|
217
|
+
// @ts-ignore deprecated, so ignore the reported error
|
|
208
218
|
loadLocaleData(localeOrAllData, localeData) {
|
|
209
219
|
if (localeData != null) {
|
|
210
220
|
this._loadLocaleData(localeOrAllData, localeData);
|
|
@@ -216,18 +226,19 @@ class I18n extends EventEmitter {
|
|
|
216
226
|
this.emit("change");
|
|
217
227
|
}
|
|
218
228
|
_load(locale, messages) {
|
|
219
|
-
|
|
229
|
+
const maybeMessages = this._messages[locale];
|
|
230
|
+
if (!maybeMessages) {
|
|
220
231
|
this._messages[locale] = messages;
|
|
221
232
|
} else {
|
|
222
|
-
Object.assign(
|
|
233
|
+
Object.assign(maybeMessages, messages);
|
|
223
234
|
}
|
|
224
235
|
}
|
|
225
236
|
load(localeOrMessages, messages) {
|
|
226
|
-
if (messages
|
|
237
|
+
if (typeof localeOrMessages == "string" && typeof messages === "object") {
|
|
227
238
|
this._load(localeOrMessages, messages);
|
|
228
239
|
} else {
|
|
229
|
-
Object.
|
|
230
|
-
(locale) => this._load(locale,
|
|
240
|
+
Object.entries(localeOrMessages).forEach(
|
|
241
|
+
([locale, messages2]) => this._load(locale, messages2)
|
|
231
242
|
);
|
|
232
243
|
}
|
|
233
244
|
this.emit("change");
|
|
@@ -251,13 +262,15 @@ class I18n extends EventEmitter {
|
|
|
251
262
|
this._locales = locales;
|
|
252
263
|
this.emit("change");
|
|
253
264
|
}
|
|
254
|
-
_(id, values
|
|
265
|
+
_(id, values, options) {
|
|
266
|
+
let message = options?.message;
|
|
255
267
|
if (!isString(id)) {
|
|
256
268
|
values = id.values || values;
|
|
257
269
|
message = id.message;
|
|
258
270
|
id = id.id;
|
|
259
271
|
}
|
|
260
|
-
const
|
|
272
|
+
const messageForId = this.messages[id];
|
|
273
|
+
const messageMissing = messageForId === void 0;
|
|
261
274
|
const missing = this._missing;
|
|
262
275
|
if (missing && messageMissing) {
|
|
263
276
|
return isFunction(missing) ? missing(this._locale, id) : missing;
|
|
@@ -265,7 +278,7 @@ class I18n extends EventEmitter {
|
|
|
265
278
|
if (messageMissing) {
|
|
266
279
|
this.emit("missing", { id, locale: this._locale });
|
|
267
280
|
}
|
|
268
|
-
let translation =
|
|
281
|
+
let translation = messageForId || message || id;
|
|
269
282
|
if (process.env.NODE_ENV !== "production") {
|
|
270
283
|
translation = isString(translation) ? compileMessage(translation) : translation;
|
|
271
284
|
}
|
|
@@ -277,7 +290,7 @@ class I18n extends EventEmitter {
|
|
|
277
290
|
translation,
|
|
278
291
|
this._locale,
|
|
279
292
|
this._locales
|
|
280
|
-
)(values, formats);
|
|
293
|
+
)(values, options?.formats);
|
|
281
294
|
}
|
|
282
295
|
date(value, format) {
|
|
283
296
|
return date(this._locales || this._locale, value, format);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/core",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.6.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "I18n tools for javascript",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -54,12 +54,12 @@
|
|
|
54
54
|
],
|
|
55
55
|
"dependencies": {
|
|
56
56
|
"@babel/runtime": "^7.20.13",
|
|
57
|
-
"@lingui/message-utils": "4.
|
|
57
|
+
"@lingui/message-utils": "4.6.0",
|
|
58
58
|
"unraw": "^3.0.0"
|
|
59
59
|
},
|
|
60
60
|
"devDependencies": {
|
|
61
61
|
"@lingui/jest-mocks": "*",
|
|
62
62
|
"unbuild": "2.0.0"
|
|
63
63
|
},
|
|
64
|
-
"gitHead": "
|
|
64
|
+
"gitHead": "2afa0efb2d0cd1d47adc76e1eec9f5e57e34ae18"
|
|
65
65
|
}
|