@lingui/core 6.0.0-next.1 → 6.0.0-next.3
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.d.mts +1 -29
- package/dist/index.mjs +0 -29
- package/macro/{browser.cjs → browser.mjs} +2 -1
- package/macro/{index.d.cts → index.d.mts} +2 -2
- package/macro/index.mjs +25 -0
- package/package.json +10 -10
- package/dist/index.cjs +0 -419
- package/dist/index.d.cts +0 -175
- package/macro/index.cjs +0 -6
package/dist/index.d.mts
CHANGED
|
@@ -57,16 +57,6 @@ type Locale = string;
|
|
|
57
57
|
type Locales = Locale | Locale[];
|
|
58
58
|
type Formats = Record<string, Intl.DateTimeFormatOptions | Intl.NumberFormatOptions>;
|
|
59
59
|
type Values = Record<string, unknown>;
|
|
60
|
-
/**
|
|
61
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
62
|
-
*/
|
|
63
|
-
type LocaleData = {
|
|
64
|
-
plurals?: (n: number, ordinal?: boolean) => ReturnType<Intl.PluralRules["select"]>;
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
68
|
-
*/
|
|
69
|
-
type AllLocaleData = Record<Locale, LocaleData>;
|
|
70
60
|
type UncompiledMessage = string;
|
|
71
61
|
type Messages = Record<string, UncompiledMessage | CompiledMessage>;
|
|
72
62
|
type AllMessages = Record<Locale, Messages>;
|
|
@@ -85,10 +75,6 @@ type I18nProps = {
|
|
|
85
75
|
locale?: Locale;
|
|
86
76
|
locales?: Locales;
|
|
87
77
|
messages?: AllMessages;
|
|
88
|
-
/**
|
|
89
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
90
|
-
*/
|
|
91
|
-
localeData?: AllLocaleData;
|
|
92
78
|
missing?: MissingHandler;
|
|
93
79
|
};
|
|
94
80
|
type Events = {
|
|
@@ -107,7 +93,6 @@ type MessageCompiler = (message: string) => CompiledMessage;
|
|
|
107
93
|
declare class I18n extends EventEmitter<Events> {
|
|
108
94
|
private _locale;
|
|
109
95
|
private _locales?;
|
|
110
|
-
private _localeData;
|
|
111
96
|
private _messages;
|
|
112
97
|
private _missing?;
|
|
113
98
|
private _messageCompiler?;
|
|
@@ -115,11 +100,6 @@ declare class I18n extends EventEmitter<Events> {
|
|
|
115
100
|
get locale(): string;
|
|
116
101
|
get locales(): Locales | undefined;
|
|
117
102
|
get messages(): Messages;
|
|
118
|
-
/**
|
|
119
|
-
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
120
|
-
*/
|
|
121
|
-
get localeData(): LocaleData;
|
|
122
|
-
private _loadLocaleData;
|
|
123
103
|
/**
|
|
124
104
|
* Registers a `MessageCompiler` to enable the use of uncompiled catalogs at runtime.
|
|
125
105
|
*
|
|
@@ -136,14 +116,6 @@ declare class I18n extends EventEmitter<Events> {
|
|
|
136
116
|
* ```
|
|
137
117
|
*/
|
|
138
118
|
setMessagesCompiler(compiler: MessageCompiler): this;
|
|
139
|
-
/**
|
|
140
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
141
|
-
*/
|
|
142
|
-
loadLocaleData(allLocaleData: AllLocaleData): void;
|
|
143
|
-
/**
|
|
144
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
145
|
-
*/
|
|
146
|
-
loadLocaleData(locale: Locale, localeData: LocaleData): void;
|
|
147
119
|
private _load;
|
|
148
120
|
load(allMessages: AllMessages): void;
|
|
149
121
|
load(locale: Locale, messages: Messages): void;
|
|
@@ -172,4 +144,4 @@ declare function setupI18n(params?: I18nProps): I18n;
|
|
|
172
144
|
declare const i18n: I18n;
|
|
173
145
|
|
|
174
146
|
export { I18n, formats, i18n, setupI18n };
|
|
175
|
-
export type {
|
|
147
|
+
export type { AllMessages, Locale, Locales, MessageDescriptor, MessageOptions, Messages };
|
package/dist/index.mjs
CHANGED
|
@@ -231,7 +231,6 @@ class EventEmitter {
|
|
|
231
231
|
class I18n extends EventEmitter {
|
|
232
232
|
_locale = "";
|
|
233
233
|
_locales;
|
|
234
|
-
_localeData = {};
|
|
235
234
|
_messages = {};
|
|
236
235
|
_missing;
|
|
237
236
|
_messageCompiler;
|
|
@@ -242,7 +241,6 @@ class I18n extends EventEmitter {
|
|
|
242
241
|
}
|
|
243
242
|
if (params.missing != null) this._missing = params.missing;
|
|
244
243
|
if (params.messages != null) this.load(params.messages);
|
|
245
|
-
if (params.localeData != null) this.loadLocaleData(params.localeData);
|
|
246
244
|
if (typeof params.locale === "string" || params.locales) {
|
|
247
245
|
this.activate(params.locale ?? defaultLocale, params.locales);
|
|
248
246
|
}
|
|
@@ -256,20 +254,6 @@ class I18n extends EventEmitter {
|
|
|
256
254
|
get messages() {
|
|
257
255
|
return this._messages[this._locale] ?? {};
|
|
258
256
|
}
|
|
259
|
-
/**
|
|
260
|
-
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
261
|
-
*/
|
|
262
|
-
get localeData() {
|
|
263
|
-
return this._localeData[this._locale] ?? {};
|
|
264
|
-
}
|
|
265
|
-
_loadLocaleData(locale, localeData) {
|
|
266
|
-
const maybeLocaleData = this._localeData[locale];
|
|
267
|
-
if (!maybeLocaleData) {
|
|
268
|
-
this._localeData[locale] = localeData;
|
|
269
|
-
} else {
|
|
270
|
-
Object.assign(maybeLocaleData, localeData);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
257
|
/**
|
|
274
258
|
* Registers a `MessageCompiler` to enable the use of uncompiled catalogs at runtime.
|
|
275
259
|
*
|
|
@@ -289,19 +273,6 @@ class I18n extends EventEmitter {
|
|
|
289
273
|
this._messageCompiler = compiler;
|
|
290
274
|
return this;
|
|
291
275
|
}
|
|
292
|
-
/**
|
|
293
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
294
|
-
*/
|
|
295
|
-
loadLocaleData(localeOrAllData, localeData) {
|
|
296
|
-
if (typeof localeOrAllData === "string") {
|
|
297
|
-
this._loadLocaleData(localeOrAllData, localeData);
|
|
298
|
-
} else {
|
|
299
|
-
Object.keys(localeOrAllData).forEach(
|
|
300
|
-
(locale) => this._loadLocaleData(locale, localeOrAllData[locale])
|
|
301
|
-
);
|
|
302
|
-
}
|
|
303
|
-
this.emit("change");
|
|
304
|
-
}
|
|
305
276
|
_load(locale, messages) {
|
|
306
277
|
const maybeMessages = this._messages[locale];
|
|
307
278
|
if (!maybeMessages) {
|
|
@@ -8,5 +8,6 @@ export const selectOrdinal = function () {}
|
|
|
8
8
|
|
|
9
9
|
throw new Error(
|
|
10
10
|
`The macro you imported from "@lingui/core/macro" is being executed outside the context of compilation. \n` +
|
|
11
|
-
`This indicates that you don't configured correctly one of the "babel-plugin-macros" / "@lingui/swc-plugin" / "babel-plugin-lingui-macro"
|
|
11
|
+
`This indicates that you don't configured correctly one of the "babel-plugin-macros" / "@lingui/swc-plugin" / "babel-plugin-lingui-macro" \n` +
|
|
12
|
+
"Additionally, dynamic imports — e.g., `await import('@lingui/core/macro')` — are not supported.",
|
|
12
13
|
)
|
|
@@ -15,7 +15,7 @@ export type ChoiceOptions = {
|
|
|
15
15
|
[digit: `${number}`]: string
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
type MacroMessageDescriptor = (
|
|
18
|
+
export type MacroMessageDescriptor = (
|
|
19
19
|
| {
|
|
20
20
|
id: string
|
|
21
21
|
message?: string
|
|
@@ -232,4 +232,4 @@ export const msg: typeof defineMessage
|
|
|
232
232
|
/**
|
|
233
233
|
* Helps to define a name for a variable in the message
|
|
234
234
|
*/
|
|
235
|
-
export function ph(def: LabeledExpression<string | number>): string
|
|
235
|
+
export function ph(def: LabeledExpression<string | number>): string
|
package/macro/index.mjs
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import babelPluginMacros from "babel-plugin-macros"
|
|
2
|
+
import { macro } from "@lingui/babel-plugin-lingui-macro/macro"
|
|
3
|
+
|
|
4
|
+
const { createMacro } = babelPluginMacros
|
|
5
|
+
|
|
6
|
+
export default createMacro(macro, {
|
|
7
|
+
configName: "lingui",
|
|
8
|
+
})
|
|
9
|
+
|
|
10
|
+
// the following shims for the case when this import used by nodejs code without transpilation
|
|
11
|
+
function printError() {
|
|
12
|
+
throw new Error(
|
|
13
|
+
`The macro you imported from "@lingui/core/macro" is being executed outside the context of compilation. \n` +
|
|
14
|
+
`This indicates that you don't configured correctly one of the "babel-plugin-macros" / "@lingui/swc-plugin" / "babel-plugin-lingui-macro" \n` +
|
|
15
|
+
"Additionally, dynamic imports — e.g., `await import('@lingui/core/macro')` — are not supported.",
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const t = printError
|
|
20
|
+
export const msg = printError
|
|
21
|
+
export const ph = printError
|
|
22
|
+
export const defineMessage = printError
|
|
23
|
+
export const plural = printError
|
|
24
|
+
export const select = printError
|
|
25
|
+
export const selectOrdinal = printError
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lingui/core",
|
|
3
|
-
"version": "6.0.0-next.
|
|
3
|
+
"version": "6.0.0-next.3",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"description": "I18n tools for javascript",
|
|
6
6
|
"type": "module",
|
|
@@ -37,9 +37,9 @@
|
|
|
37
37
|
"exports": {
|
|
38
38
|
".": "./dist/index.mjs",
|
|
39
39
|
"./macro": {
|
|
40
|
-
"browser": "./macro/browser.
|
|
41
|
-
"types": "./macro/index.d.
|
|
42
|
-
"default": "./macro/index.
|
|
40
|
+
"browser": "./macro/browser.mjs",
|
|
41
|
+
"types": "./macro/index.d.mts",
|
|
42
|
+
"default": "./macro/index.mjs"
|
|
43
43
|
},
|
|
44
44
|
"./package.json": "./package.json"
|
|
45
45
|
},
|
|
@@ -47,13 +47,13 @@
|
|
|
47
47
|
"LICENSE",
|
|
48
48
|
"README.md",
|
|
49
49
|
"dist/",
|
|
50
|
-
"macro/browser.
|
|
51
|
-
"macro/index.d.
|
|
52
|
-
"macro/index.
|
|
50
|
+
"macro/browser.mjs",
|
|
51
|
+
"macro/index.d.mts",
|
|
52
|
+
"macro/index.mjs"
|
|
53
53
|
],
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@lingui/babel-plugin-lingui-macro": "6.0.0-next.
|
|
56
|
-
"@lingui/message-utils": "6.0.0-next.
|
|
55
|
+
"@lingui/babel-plugin-lingui-macro": "6.0.0-next.3",
|
|
56
|
+
"@lingui/message-utils": "6.0.0-next.3"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@lingui/test-utils": "3.0.3",
|
|
@@ -71,5 +71,5 @@
|
|
|
71
71
|
"unbuild": {
|
|
72
72
|
"declaration": "node16"
|
|
73
73
|
},
|
|
74
|
-
"gitHead": "
|
|
74
|
+
"gitHead": "ebcb6dc8e8d327ae5775cadee931942ef309480f"
|
|
75
75
|
}
|
package/dist/index.cjs
DELETED
|
@@ -1,419 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const compileMessage = require('@lingui/message-utils/compileMessage');
|
|
4
|
-
|
|
5
|
-
const isString = (s) => typeof s === "string";
|
|
6
|
-
const isFunction = (f) => typeof f === "function";
|
|
7
|
-
|
|
8
|
-
const cache = /* @__PURE__ */ new Map();
|
|
9
|
-
const defaultLocale = "en";
|
|
10
|
-
function normalizeLocales(locales) {
|
|
11
|
-
const out = Array.isArray(locales) ? locales : [locales];
|
|
12
|
-
return [...out, defaultLocale];
|
|
13
|
-
}
|
|
14
|
-
function date(locales, value, format) {
|
|
15
|
-
const _locales = normalizeLocales(locales);
|
|
16
|
-
if (!format) {
|
|
17
|
-
format = "default";
|
|
18
|
-
}
|
|
19
|
-
let o;
|
|
20
|
-
if (typeof format === "string") {
|
|
21
|
-
o = {
|
|
22
|
-
day: "numeric",
|
|
23
|
-
month: "short",
|
|
24
|
-
year: "numeric"
|
|
25
|
-
};
|
|
26
|
-
switch (format) {
|
|
27
|
-
case "full":
|
|
28
|
-
o.weekday = "long";
|
|
29
|
-
case "long":
|
|
30
|
-
o.month = "long";
|
|
31
|
-
break;
|
|
32
|
-
case "short":
|
|
33
|
-
o.month = "numeric";
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
o = format;
|
|
38
|
-
}
|
|
39
|
-
const formatter = getMemoized(
|
|
40
|
-
() => cacheKey("date", _locales, format),
|
|
41
|
-
() => new Intl.DateTimeFormat(_locales, o)
|
|
42
|
-
);
|
|
43
|
-
return formatter.format(isString(value) ? new Date(value) : value);
|
|
44
|
-
}
|
|
45
|
-
function time(locales, value, format) {
|
|
46
|
-
let o;
|
|
47
|
-
if (!format) {
|
|
48
|
-
format = "default";
|
|
49
|
-
}
|
|
50
|
-
if (typeof format === "string") {
|
|
51
|
-
o = {
|
|
52
|
-
second: "numeric",
|
|
53
|
-
minute: "numeric",
|
|
54
|
-
hour: "numeric"
|
|
55
|
-
};
|
|
56
|
-
switch (format) {
|
|
57
|
-
case "full":
|
|
58
|
-
case "long":
|
|
59
|
-
o.timeZoneName = "short";
|
|
60
|
-
break;
|
|
61
|
-
case "short":
|
|
62
|
-
delete o.second;
|
|
63
|
-
}
|
|
64
|
-
} else {
|
|
65
|
-
o = format;
|
|
66
|
-
}
|
|
67
|
-
return date(locales, value, o);
|
|
68
|
-
}
|
|
69
|
-
function number(locales, value, format) {
|
|
70
|
-
const _locales = normalizeLocales(locales);
|
|
71
|
-
const formatter = getMemoized(
|
|
72
|
-
() => cacheKey("number", _locales, format),
|
|
73
|
-
() => new Intl.NumberFormat(_locales, format)
|
|
74
|
-
);
|
|
75
|
-
return formatter.format(value);
|
|
76
|
-
}
|
|
77
|
-
function plural(locales, ordinal, value, { offset = 0, ...rules }) {
|
|
78
|
-
const _locales = normalizeLocales(locales);
|
|
79
|
-
const plurals = ordinal ? getMemoized(
|
|
80
|
-
() => cacheKey("plural-ordinal", _locales),
|
|
81
|
-
() => new Intl.PluralRules(_locales, { type: "ordinal" })
|
|
82
|
-
) : getMemoized(
|
|
83
|
-
() => cacheKey("plural-cardinal", _locales),
|
|
84
|
-
() => new Intl.PluralRules(_locales, { type: "cardinal" })
|
|
85
|
-
);
|
|
86
|
-
return rules[value] ?? rules[plurals.select(value - offset)] ?? rules.other;
|
|
87
|
-
}
|
|
88
|
-
function getMemoized(getKey, construct) {
|
|
89
|
-
const key = getKey();
|
|
90
|
-
let formatter = cache.get(key);
|
|
91
|
-
if (!formatter) {
|
|
92
|
-
formatter = construct();
|
|
93
|
-
cache.set(key, formatter);
|
|
94
|
-
}
|
|
95
|
-
return formatter;
|
|
96
|
-
}
|
|
97
|
-
function cacheKey(type, locales, options) {
|
|
98
|
-
const localeKey = locales.join("-");
|
|
99
|
-
return `${type}-${localeKey}-${JSON.stringify(options)}`;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
const formats = {
|
|
103
|
-
__proto__: null,
|
|
104
|
-
date: date,
|
|
105
|
-
defaultLocale: defaultLocale,
|
|
106
|
-
number: number,
|
|
107
|
-
plural: plural,
|
|
108
|
-
time: time
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
const ESCAPE_SEQUENCE_REGEX = /\\u[a-fA-F0-9]{4}|\\x[a-fA-F0-9]{2}/;
|
|
112
|
-
const decodeEscapeSequences = (str) => {
|
|
113
|
-
return str.replace(
|
|
114
|
-
// Same pattern but with capturing groups for extracting values during replacement
|
|
115
|
-
/\\u([a-fA-F0-9]{4})|\\x([a-fA-F0-9]{2})/g,
|
|
116
|
-
(_, unicode, hex) => {
|
|
117
|
-
if (unicode) {
|
|
118
|
-
const codePoint = parseInt(unicode, 16);
|
|
119
|
-
return String.fromCharCode(codePoint);
|
|
120
|
-
} else {
|
|
121
|
-
const codePoint = parseInt(hex, 16);
|
|
122
|
-
return String.fromCharCode(codePoint);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
);
|
|
126
|
-
};
|
|
127
|
-
|
|
128
|
-
const OCTOTHORPE_PH = "%__lingui_octothorpe__%";
|
|
129
|
-
const getDefaultFormats = (locale, passedLocales, formats = {}) => {
|
|
130
|
-
const locales = passedLocales || locale;
|
|
131
|
-
const style = (format) => {
|
|
132
|
-
if (typeof format === "object") return format;
|
|
133
|
-
return formats[format];
|
|
134
|
-
};
|
|
135
|
-
const replaceOctothorpe = (value, message) => {
|
|
136
|
-
const numberFormat = Object.keys(formats).length ? style("number") : void 0;
|
|
137
|
-
const valueStr = number(locales, value, numberFormat);
|
|
138
|
-
return message.replace(new RegExp(OCTOTHORPE_PH, "g"), valueStr);
|
|
139
|
-
};
|
|
140
|
-
return {
|
|
141
|
-
plural: (value, cases) => {
|
|
142
|
-
const { offset = 0 } = cases;
|
|
143
|
-
const message = plural(locales, false, value, cases);
|
|
144
|
-
return replaceOctothorpe(value - offset, message);
|
|
145
|
-
},
|
|
146
|
-
selectordinal: (value, cases) => {
|
|
147
|
-
const { offset = 0 } = cases;
|
|
148
|
-
const message = plural(locales, true, value, cases);
|
|
149
|
-
return replaceOctothorpe(value - offset, message);
|
|
150
|
-
},
|
|
151
|
-
select: selectFormatter,
|
|
152
|
-
number: (value, format) => number(
|
|
153
|
-
locales,
|
|
154
|
-
value,
|
|
155
|
-
style(format) || { style: format }
|
|
156
|
-
),
|
|
157
|
-
date: (value, format) => date(locales, value, style(format) || format),
|
|
158
|
-
time: (value, format) => time(locales, value, style(format) || format)
|
|
159
|
-
};
|
|
160
|
-
};
|
|
161
|
-
const selectFormatter = (value, rules) => rules[value] ?? rules.other;
|
|
162
|
-
function interpolate(translation, locale, locales) {
|
|
163
|
-
return (values = {}, formats) => {
|
|
164
|
-
const formatters = getDefaultFormats(locale, locales, formats);
|
|
165
|
-
const formatMessage = (tokens, replaceOctothorpe = false) => {
|
|
166
|
-
if (!Array.isArray(tokens)) return tokens;
|
|
167
|
-
return tokens.reduce((message, token) => {
|
|
168
|
-
if (token === "#" && replaceOctothorpe) {
|
|
169
|
-
return message + OCTOTHORPE_PH;
|
|
170
|
-
}
|
|
171
|
-
if (isString(token)) {
|
|
172
|
-
return message + token;
|
|
173
|
-
}
|
|
174
|
-
const [name, type, format] = token;
|
|
175
|
-
let interpolatedFormat = {};
|
|
176
|
-
if (type === "plural" || type === "selectordinal" || type === "select") {
|
|
177
|
-
Object.entries(format).forEach(
|
|
178
|
-
([key, value2]) => {
|
|
179
|
-
interpolatedFormat[key] = formatMessage(
|
|
180
|
-
value2,
|
|
181
|
-
type === "plural" || type === "selectordinal"
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
);
|
|
185
|
-
} else {
|
|
186
|
-
interpolatedFormat = format;
|
|
187
|
-
}
|
|
188
|
-
let value;
|
|
189
|
-
if (type) {
|
|
190
|
-
const formatter = formatters[type];
|
|
191
|
-
value = formatter(values[name], interpolatedFormat);
|
|
192
|
-
} else {
|
|
193
|
-
value = values[name];
|
|
194
|
-
}
|
|
195
|
-
if (value == null) {
|
|
196
|
-
return message;
|
|
197
|
-
}
|
|
198
|
-
return message + value;
|
|
199
|
-
}, "");
|
|
200
|
-
};
|
|
201
|
-
const result = formatMessage(translation);
|
|
202
|
-
if (isString(result) && ESCAPE_SEQUENCE_REGEX.test(result)) {
|
|
203
|
-
return decodeEscapeSequences(result);
|
|
204
|
-
}
|
|
205
|
-
if (isString(result)) return result;
|
|
206
|
-
return result ? String(result) : "";
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
class EventEmitter {
|
|
211
|
-
_events = {};
|
|
212
|
-
on(event, listener) {
|
|
213
|
-
this._events[event] ??= /* @__PURE__ */ new Set();
|
|
214
|
-
this._events[event].add(listener);
|
|
215
|
-
return () => this.removeListener(event, listener);
|
|
216
|
-
}
|
|
217
|
-
removeListener(event, listener) {
|
|
218
|
-
const listeners = this._events[event];
|
|
219
|
-
listeners?.delete(listener);
|
|
220
|
-
if (listeners?.size === 0) {
|
|
221
|
-
delete this._events[event];
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
emit(event, ...args) {
|
|
225
|
-
const listeners = this._events[event];
|
|
226
|
-
if (!listeners) return;
|
|
227
|
-
for (const listener of [...listeners]) {
|
|
228
|
-
listener.apply(this, args);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
class I18n extends EventEmitter {
|
|
234
|
-
_locale = "";
|
|
235
|
-
_locales;
|
|
236
|
-
_localeData = {};
|
|
237
|
-
_messages = {};
|
|
238
|
-
_missing;
|
|
239
|
-
_messageCompiler;
|
|
240
|
-
constructor(params) {
|
|
241
|
-
super();
|
|
242
|
-
if (process.env.NODE_ENV !== "production") {
|
|
243
|
-
this.setMessagesCompiler(compileMessage.compileMessage);
|
|
244
|
-
}
|
|
245
|
-
if (params.missing != null) this._missing = params.missing;
|
|
246
|
-
if (params.messages != null) this.load(params.messages);
|
|
247
|
-
if (params.localeData != null) this.loadLocaleData(params.localeData);
|
|
248
|
-
if (typeof params.locale === "string" || params.locales) {
|
|
249
|
-
this.activate(params.locale ?? defaultLocale, params.locales);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
get locale() {
|
|
253
|
-
return this._locale;
|
|
254
|
-
}
|
|
255
|
-
get locales() {
|
|
256
|
-
return this._locales;
|
|
257
|
-
}
|
|
258
|
-
get messages() {
|
|
259
|
-
return this._messages[this._locale] ?? {};
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
263
|
-
*/
|
|
264
|
-
get localeData() {
|
|
265
|
-
return this._localeData[this._locale] ?? {};
|
|
266
|
-
}
|
|
267
|
-
_loadLocaleData(locale, localeData) {
|
|
268
|
-
const maybeLocaleData = this._localeData[locale];
|
|
269
|
-
if (!maybeLocaleData) {
|
|
270
|
-
this._localeData[locale] = localeData;
|
|
271
|
-
} else {
|
|
272
|
-
Object.assign(maybeLocaleData, localeData);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* Registers a `MessageCompiler` to enable the use of uncompiled catalogs at runtime.
|
|
277
|
-
*
|
|
278
|
-
* In production builds, the `MessageCompiler` is typically excluded to reduce bundle size.
|
|
279
|
-
* By default, message catalogs should be precompiled during the build process. However,
|
|
280
|
-
* if you need to compile catalogs at runtime, you can use this method to set a message compiler.
|
|
281
|
-
*
|
|
282
|
-
* Example usage:
|
|
283
|
-
*
|
|
284
|
-
* ```ts
|
|
285
|
-
* import { compileMessage } from "@lingui/message-utils/compileMessage";
|
|
286
|
-
*
|
|
287
|
-
* i18n.setMessagesCompiler(compileMessage);
|
|
288
|
-
* ```
|
|
289
|
-
*/
|
|
290
|
-
setMessagesCompiler(compiler) {
|
|
291
|
-
this._messageCompiler = compiler;
|
|
292
|
-
return this;
|
|
293
|
-
}
|
|
294
|
-
/**
|
|
295
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
296
|
-
*/
|
|
297
|
-
loadLocaleData(localeOrAllData, localeData) {
|
|
298
|
-
if (typeof localeOrAllData === "string") {
|
|
299
|
-
this._loadLocaleData(localeOrAllData, localeData);
|
|
300
|
-
} else {
|
|
301
|
-
Object.keys(localeOrAllData).forEach(
|
|
302
|
-
(locale) => this._loadLocaleData(locale, localeOrAllData[locale])
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
this.emit("change");
|
|
306
|
-
}
|
|
307
|
-
_load(locale, messages) {
|
|
308
|
-
const maybeMessages = this._messages[locale];
|
|
309
|
-
if (!maybeMessages) {
|
|
310
|
-
this._messages[locale] = messages;
|
|
311
|
-
} else {
|
|
312
|
-
Object.assign(maybeMessages, messages);
|
|
313
|
-
}
|
|
314
|
-
}
|
|
315
|
-
load(localeOrMessages, messages) {
|
|
316
|
-
if (typeof localeOrMessages == "string" && typeof messages === "object") {
|
|
317
|
-
this._load(localeOrMessages, messages);
|
|
318
|
-
} else {
|
|
319
|
-
Object.entries(localeOrMessages).forEach(
|
|
320
|
-
([locale, messages2]) => this._load(locale, messages2)
|
|
321
|
-
);
|
|
322
|
-
}
|
|
323
|
-
this.emit("change");
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* @param options {@link LoadAndActivateOptions}
|
|
327
|
-
*/
|
|
328
|
-
loadAndActivate({ locale, locales, messages }) {
|
|
329
|
-
this._locale = locale;
|
|
330
|
-
this._locales = locales || void 0;
|
|
331
|
-
this._messages[this._locale] = messages;
|
|
332
|
-
this.emit("change");
|
|
333
|
-
}
|
|
334
|
-
activate(locale, locales) {
|
|
335
|
-
if (process.env.NODE_ENV !== "production") {
|
|
336
|
-
if (!this._messages[locale]) {
|
|
337
|
-
console.warn(`Messages for locale "${locale}" not loaded.`);
|
|
338
|
-
}
|
|
339
|
-
}
|
|
340
|
-
this._locale = locale;
|
|
341
|
-
this._locales = locales;
|
|
342
|
-
this.emit("change");
|
|
343
|
-
}
|
|
344
|
-
_(id, values, options) {
|
|
345
|
-
if (!this.locale) {
|
|
346
|
-
throw new Error(
|
|
347
|
-
"Lingui: Attempted to call a translation function without setting a locale.\nMake sure to call `i18n.activate(locale)` before using Lingui functions.\nThis issue may also occur due to a race condition in your initialization logic."
|
|
348
|
-
);
|
|
349
|
-
}
|
|
350
|
-
let message = options?.message;
|
|
351
|
-
if (!id) {
|
|
352
|
-
id = "";
|
|
353
|
-
}
|
|
354
|
-
if (!isString(id)) {
|
|
355
|
-
values = id.values || values;
|
|
356
|
-
message = id.message;
|
|
357
|
-
id = id.id;
|
|
358
|
-
}
|
|
359
|
-
const messageForId = this.messages[id];
|
|
360
|
-
const messageMissing = messageForId === void 0;
|
|
361
|
-
const missing = this._missing;
|
|
362
|
-
if (missing && messageMissing) {
|
|
363
|
-
return isFunction(missing) ? missing(this._locale, id) : missing;
|
|
364
|
-
}
|
|
365
|
-
if (messageMissing) {
|
|
366
|
-
this.emit("missing", { id, locale: this._locale });
|
|
367
|
-
}
|
|
368
|
-
let translation = messageForId || message || id;
|
|
369
|
-
if (isString(translation)) {
|
|
370
|
-
if (this._messageCompiler) {
|
|
371
|
-
translation = this._messageCompiler(translation);
|
|
372
|
-
} else {
|
|
373
|
-
console.warn(`Uncompiled message detected! Message:
|
|
374
|
-
|
|
375
|
-
> ${translation}
|
|
376
|
-
|
|
377
|
-
That means you use raw catalog or your catalog doesn't have a translation for the message and fallback was used.
|
|
378
|
-
ICU features such as interpolation and plurals will not work properly for that message.
|
|
379
|
-
|
|
380
|
-
Please compile your catalog first.
|
|
381
|
-
`);
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
if (isString(translation) && ESCAPE_SEQUENCE_REGEX.test(translation))
|
|
385
|
-
return decodeEscapeSequences(translation);
|
|
386
|
-
if (isString(translation)) return translation;
|
|
387
|
-
return interpolate(
|
|
388
|
-
translation,
|
|
389
|
-
this._locale,
|
|
390
|
-
this._locales
|
|
391
|
-
)(values, options?.formats);
|
|
392
|
-
}
|
|
393
|
-
/**
|
|
394
|
-
* Alias for {@see I18n._}
|
|
395
|
-
*/
|
|
396
|
-
t = this._.bind(this);
|
|
397
|
-
/**
|
|
398
|
-
* @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed.
|
|
399
|
-
*/
|
|
400
|
-
date(value, format) {
|
|
401
|
-
return date(this._locales || this._locale, value, format);
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* @deprecated Use `Intl.NumberFormat` directly. This helper will be removed.
|
|
405
|
-
*/
|
|
406
|
-
number(value, format) {
|
|
407
|
-
return number(this._locales || this._locale, value, format);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
function setupI18n(params = {}) {
|
|
411
|
-
return new I18n(params);
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
const i18n = setupI18n();
|
|
415
|
-
|
|
416
|
-
exports.I18n = I18n;
|
|
417
|
-
exports.formats = formats;
|
|
418
|
-
exports.i18n = i18n;
|
|
419
|
-
exports.setupI18n = setupI18n;
|
package/dist/index.d.cts
DELETED
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
import { CompiledMessage } from '@lingui/message-utils/compileMessage';
|
|
2
|
-
|
|
3
|
-
declare const defaultLocale = "en";
|
|
4
|
-
type DateTimeFormatSize = "short" | "default" | "long" | "full";
|
|
5
|
-
type DateTimeFormatValue = Parameters<Intl.DateTimeFormat["format"]>[0];
|
|
6
|
-
type NumberFormatValue = Parameters<Intl.NumberFormat["format"]>[0];
|
|
7
|
-
/**
|
|
8
|
-
* @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed.
|
|
9
|
-
*/
|
|
10
|
-
declare function date(locales: Locales, value: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions | DateTimeFormatSize): string;
|
|
11
|
-
/**
|
|
12
|
-
* @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed.
|
|
13
|
-
*/
|
|
14
|
-
declare function time(locales: Locales, value: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions | DateTimeFormatSize): string;
|
|
15
|
-
/**
|
|
16
|
-
* @deprecated Use `Intl.NumberFormat` directly. This helper will be removed.
|
|
17
|
-
*/
|
|
18
|
-
declare function number(locales: Locales, value: NumberFormatValue, format?: Intl.NumberFormatOptions): string;
|
|
19
|
-
type PluralOptions = {
|
|
20
|
-
[key: string]: Intl.LDMLPluralRule;
|
|
21
|
-
} & {
|
|
22
|
-
offset: number;
|
|
23
|
-
other: string;
|
|
24
|
-
};
|
|
25
|
-
declare function plural(locales: Locales, ordinal: boolean, value: number, { offset, ...rules }: PluralOptions): string;
|
|
26
|
-
|
|
27
|
-
type formats_DateTimeFormatSize = DateTimeFormatSize;
|
|
28
|
-
type formats_DateTimeFormatValue = DateTimeFormatValue;
|
|
29
|
-
type formats_NumberFormatValue = NumberFormatValue;
|
|
30
|
-
type formats_PluralOptions = PluralOptions;
|
|
31
|
-
declare const formats_date: typeof date;
|
|
32
|
-
declare const formats_defaultLocale: typeof defaultLocale;
|
|
33
|
-
declare const formats_number: typeof number;
|
|
34
|
-
declare const formats_plural: typeof plural;
|
|
35
|
-
declare const formats_time: typeof time;
|
|
36
|
-
declare namespace formats {
|
|
37
|
-
export { formats_date as date, formats_defaultLocale as defaultLocale, formats_number as number, formats_plural as plural, formats_time as time };
|
|
38
|
-
export type { formats_DateTimeFormatSize as DateTimeFormatSize, formats_DateTimeFormatValue as DateTimeFormatValue, formats_NumberFormatValue as NumberFormatValue, formats_PluralOptions as PluralOptions };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
declare class EventEmitter<Events extends {
|
|
42
|
-
[name: string]: (...args: any[]) => void;
|
|
43
|
-
}> {
|
|
44
|
-
private readonly _events;
|
|
45
|
-
on<E extends keyof Events>(event: E, listener: Events[E]): () => void;
|
|
46
|
-
removeListener<E extends keyof Events>(event: E, listener: Events[E]): void;
|
|
47
|
-
emit<E extends keyof Events>(event: E, ...args: Parameters<Events[E]>): void;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
type MessageOptions = {
|
|
51
|
-
message?: string;
|
|
52
|
-
formats?: Formats;
|
|
53
|
-
comment?: string;
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
type Locale = string;
|
|
57
|
-
type Locales = Locale | Locale[];
|
|
58
|
-
type Formats = Record<string, Intl.DateTimeFormatOptions | Intl.NumberFormatOptions>;
|
|
59
|
-
type Values = Record<string, unknown>;
|
|
60
|
-
/**
|
|
61
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
62
|
-
*/
|
|
63
|
-
type LocaleData = {
|
|
64
|
-
plurals?: (n: number, ordinal?: boolean) => ReturnType<Intl.PluralRules["select"]>;
|
|
65
|
-
};
|
|
66
|
-
/**
|
|
67
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
68
|
-
*/
|
|
69
|
-
type AllLocaleData = Record<Locale, LocaleData>;
|
|
70
|
-
type UncompiledMessage = string;
|
|
71
|
-
type Messages = Record<string, UncompiledMessage | CompiledMessage>;
|
|
72
|
-
type AllMessages = Record<Locale, Messages>;
|
|
73
|
-
type MessageDescriptor = {
|
|
74
|
-
id: string;
|
|
75
|
-
comment?: string;
|
|
76
|
-
message?: string;
|
|
77
|
-
values?: Record<string, unknown>;
|
|
78
|
-
};
|
|
79
|
-
type MissingMessageEvent = {
|
|
80
|
-
locale: Locale;
|
|
81
|
-
id: string;
|
|
82
|
-
};
|
|
83
|
-
type MissingHandler = string | ((locale: string, id: string) => string);
|
|
84
|
-
type I18nProps = {
|
|
85
|
-
locale?: Locale;
|
|
86
|
-
locales?: Locales;
|
|
87
|
-
messages?: AllMessages;
|
|
88
|
-
/**
|
|
89
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
90
|
-
*/
|
|
91
|
-
localeData?: AllLocaleData;
|
|
92
|
-
missing?: MissingHandler;
|
|
93
|
-
};
|
|
94
|
-
type Events = {
|
|
95
|
-
change: () => void;
|
|
96
|
-
missing: (event: MissingMessageEvent) => void;
|
|
97
|
-
};
|
|
98
|
-
type LoadAndActivateOptions = {
|
|
99
|
-
/** initial active locale */
|
|
100
|
-
locale: Locale;
|
|
101
|
-
/** list of alternative locales (BCP 47 language tags) which are used for number and date formatting */
|
|
102
|
-
locales?: Locales;
|
|
103
|
-
/** compiled message catalog */
|
|
104
|
-
messages: Messages;
|
|
105
|
-
};
|
|
106
|
-
type MessageCompiler = (message: string) => CompiledMessage;
|
|
107
|
-
declare class I18n extends EventEmitter<Events> {
|
|
108
|
-
private _locale;
|
|
109
|
-
private _locales?;
|
|
110
|
-
private _localeData;
|
|
111
|
-
private _messages;
|
|
112
|
-
private _missing?;
|
|
113
|
-
private _messageCompiler?;
|
|
114
|
-
constructor(params: I18nProps);
|
|
115
|
-
get locale(): string;
|
|
116
|
-
get locales(): Locales | undefined;
|
|
117
|
-
get messages(): Messages;
|
|
118
|
-
/**
|
|
119
|
-
* @deprecated this has no effect. Please remove this from the code. Deprecated in v4
|
|
120
|
-
*/
|
|
121
|
-
get localeData(): LocaleData;
|
|
122
|
-
private _loadLocaleData;
|
|
123
|
-
/**
|
|
124
|
-
* Registers a `MessageCompiler` to enable the use of uncompiled catalogs at runtime.
|
|
125
|
-
*
|
|
126
|
-
* In production builds, the `MessageCompiler` is typically excluded to reduce bundle size.
|
|
127
|
-
* By default, message catalogs should be precompiled during the build process. However,
|
|
128
|
-
* if you need to compile catalogs at runtime, you can use this method to set a message compiler.
|
|
129
|
-
*
|
|
130
|
-
* Example usage:
|
|
131
|
-
*
|
|
132
|
-
* ```ts
|
|
133
|
-
* import { compileMessage } from "@lingui/message-utils/compileMessage";
|
|
134
|
-
*
|
|
135
|
-
* i18n.setMessagesCompiler(compileMessage);
|
|
136
|
-
* ```
|
|
137
|
-
*/
|
|
138
|
-
setMessagesCompiler(compiler: MessageCompiler): this;
|
|
139
|
-
/**
|
|
140
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
141
|
-
*/
|
|
142
|
-
loadLocaleData(allLocaleData: AllLocaleData): void;
|
|
143
|
-
/**
|
|
144
|
-
* @deprecated Plurals automatically used from Intl.PluralRules you can safely remove this call. Deprecated in v4
|
|
145
|
-
*/
|
|
146
|
-
loadLocaleData(locale: Locale, localeData: LocaleData): void;
|
|
147
|
-
private _load;
|
|
148
|
-
load(allMessages: AllMessages): void;
|
|
149
|
-
load(locale: Locale, messages: Messages): void;
|
|
150
|
-
/**
|
|
151
|
-
* @param options {@link LoadAndActivateOptions}
|
|
152
|
-
*/
|
|
153
|
-
loadAndActivate({ locale, locales, messages }: LoadAndActivateOptions): void;
|
|
154
|
-
activate(locale: Locale, locales?: Locales): void;
|
|
155
|
-
_(descriptor: MessageDescriptor): string;
|
|
156
|
-
_(id: string, values?: Values, options?: MessageOptions): string;
|
|
157
|
-
/**
|
|
158
|
-
* Alias for {@see I18n._}
|
|
159
|
-
*/
|
|
160
|
-
t: I18n["_"];
|
|
161
|
-
/**
|
|
162
|
-
* @deprecated Use `Intl.DateTimeFormat` directly. This helper will be removed.
|
|
163
|
-
*/
|
|
164
|
-
date(value?: string | DateTimeFormatValue, format?: Intl.DateTimeFormatOptions): string;
|
|
165
|
-
/**
|
|
166
|
-
* @deprecated Use `Intl.NumberFormat` directly. This helper will be removed.
|
|
167
|
-
*/
|
|
168
|
-
number(value: NumberFormatValue, format?: Intl.NumberFormatOptions): string;
|
|
169
|
-
}
|
|
170
|
-
declare function setupI18n(params?: I18nProps): I18n;
|
|
171
|
-
|
|
172
|
-
declare const i18n: I18n;
|
|
173
|
-
|
|
174
|
-
export { I18n, formats, i18n, setupI18n };
|
|
175
|
-
export type { AllLocaleData, AllMessages, Locale, LocaleData, Locales, MessageDescriptor, MessageOptions, Messages };
|