@intlayer/core 9.9.3-canary.1 → 9.9.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/cjs/interpreter/getInsertion.cjs +2 -1
- package/dist/cjs/interpreter/getInsertion.cjs.map +1 -1
- package/dist/cjs/messageFormat/i18next.cjs +59 -251
- package/dist/cjs/messageFormat/i18next.cjs.map +1 -1
- package/dist/cjs/messageFormat/resolveMessage.cjs +40 -27
- package/dist/cjs/messageFormat/resolveMessage.cjs.map +1 -1
- package/dist/esm/interpreter/getInsertion.mjs +2 -1
- package/dist/esm/interpreter/getInsertion.mjs.map +1 -1
- package/dist/esm/messageFormat/i18next.mjs +59 -251
- package/dist/esm/messageFormat/i18next.mjs.map +1 -1
- package/dist/esm/messageFormat/resolveMessage.mjs +40 -27
- package/dist/esm/messageFormat/resolveMessage.mjs.map +1 -1
- package/dist/types/index.d.ts +2 -2
- package/dist/types/messageFormat/i18next.d.ts.map +1 -1
- package/dist/types/messageFormat/index.d.ts +2 -2
- package/dist/types/messageFormat/resolveMessage.d.ts +14 -5
- package/dist/types/messageFormat/resolveMessage.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -1,253 +1,66 @@
|
|
|
1
1
|
import { deepTransformNode } from "../interpreter/getContent/deepTransform.mjs";
|
|
2
|
-
import { enu as enumeration } from "../transpiler/enumeration/enumeration.mjs";
|
|
3
|
-
import { gender } from "../transpiler/gender/gender.mjs";
|
|
4
2
|
import { html } from "../transpiler/html/html.mjs";
|
|
5
3
|
import { insert as insertion } from "../transpiler/insertion/insertion.mjs";
|
|
6
|
-
import { plural } from "../transpiler/plural/plural.mjs";
|
|
7
|
-
import { select } from "../transpiler/select/select.mjs";
|
|
8
4
|
import * as NodeTypes from "@intlayer/types/nodeType";
|
|
9
5
|
|
|
10
6
|
// src/messageFormat/i18next.ts
|
|
11
|
-
/**
|
|
12
|
-
const
|
|
13
|
-
/** `{
|
|
14
|
-
const
|
|
7
|
+
/** An HTML-like tag (`<strong>`, `<1>`), rendered through an `html` node. */
|
|
8
|
+
const HTML_TAG_PATTERN = /<[a-zA-Z0-9-]+[^>]*>/;
|
|
9
|
+
/** A closed `{{…}}` interpolation. */
|
|
10
|
+
const INTERPOLATION_PATTERN = /\{\{[\s\S]*?\}\}/;
|
|
15
11
|
/**
|
|
16
|
-
*
|
|
12
|
+
* Converts one i18next message into intlayer content.
|
|
17
13
|
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* formatter writes them.
|
|
14
|
+
* As in i18next, only `{{…}}` is syntax: single braces stay text
|
|
15
|
+
* (`Press {Ctrl+K}`, `$t(key, {"count": 1})`, ICU blocks), and each
|
|
16
|
+
* interpolation keeps its spelling so write-back leaves the source unchanged.
|
|
17
|
+
* Plurals and contexts are key suffixes (`key_one`, `key_male`), kept as keys.
|
|
23
18
|
*/
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return FORMATTED_ARGUMENT_PATTERN.test(remainingText) || isInsideOption && SIMPLE_ARGUMENT_PATTERN.test(remainingText);
|
|
29
|
-
};
|
|
30
|
-
const parseNodes = (isInsideOption) => {
|
|
31
|
-
const nodes = [];
|
|
32
|
-
let currentText = "";
|
|
33
|
-
while (index < text.length) {
|
|
34
|
-
const char = text[index];
|
|
35
|
-
if (char === "{" && text[index + 1] === "{") {
|
|
36
|
-
if (currentText) {
|
|
37
|
-
nodes.push(currentText);
|
|
38
|
-
currentText = "";
|
|
39
|
-
}
|
|
40
|
-
index += 2;
|
|
41
|
-
nodes.push(parseStandardArgument());
|
|
42
|
-
} else if (char === "{" && isICUArgumentStart(isInsideOption)) {
|
|
43
|
-
if (currentText) {
|
|
44
|
-
nodes.push(currentText);
|
|
45
|
-
currentText = "";
|
|
46
|
-
}
|
|
47
|
-
index++;
|
|
48
|
-
nodes.push(parseICUArgument());
|
|
49
|
-
} else if (char === "}" && isInsideOption) break;
|
|
50
|
-
else {
|
|
51
|
-
currentText += char;
|
|
52
|
-
index++;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
if (currentText) nodes.push(currentText);
|
|
56
|
-
return nodes;
|
|
57
|
-
};
|
|
58
|
-
const parseStandardArgument = () => {
|
|
59
|
-
let name = "";
|
|
60
|
-
while (index < text.length) {
|
|
61
|
-
if (text[index] === "}" && text[index + 1] === "}") {
|
|
62
|
-
index += 2;
|
|
63
|
-
return {
|
|
64
|
-
type: "argument",
|
|
65
|
-
name: name.trim().replace(/^-\s+/, "-")
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
name += text[index];
|
|
69
|
-
index++;
|
|
70
|
-
}
|
|
71
|
-
throw new Error("Unclosed i18next variable");
|
|
72
|
-
};
|
|
73
|
-
const parseICUArgument = () => {
|
|
74
|
-
let name = "";
|
|
75
|
-
while (index < text.length && /[^,}]/.test(text[index])) {
|
|
76
|
-
name += text[index];
|
|
77
|
-
index++;
|
|
78
|
-
}
|
|
79
|
-
name = name.trim();
|
|
80
|
-
if (index >= text.length) throw new Error("Unclosed argument");
|
|
81
|
-
if (text[index] === "}") {
|
|
82
|
-
index++;
|
|
83
|
-
return {
|
|
84
|
-
type: "argument",
|
|
85
|
-
name
|
|
86
|
-
};
|
|
87
|
-
}
|
|
88
|
-
if (text[index] === ",") {
|
|
89
|
-
index++;
|
|
90
|
-
let type = "";
|
|
91
|
-
while (index < text.length && /[^,}]/.test(text[index])) {
|
|
92
|
-
type += text[index];
|
|
93
|
-
index++;
|
|
94
|
-
}
|
|
95
|
-
type = type.trim();
|
|
96
|
-
if (index >= text.length) throw new Error("Unclosed argument");
|
|
97
|
-
if (text[index] === "}") {
|
|
98
|
-
index++;
|
|
99
|
-
return {
|
|
100
|
-
type: "argument",
|
|
101
|
-
name,
|
|
102
|
-
format: { type }
|
|
103
|
-
};
|
|
104
|
-
}
|
|
105
|
-
if (text[index] === ",") {
|
|
106
|
-
index++;
|
|
107
|
-
if (type === "plural" || type === "select") {
|
|
108
|
-
const options = {};
|
|
109
|
-
while (index < text.length && text[index] !== "}") {
|
|
110
|
-
while (index < text.length && /\s/.test(text[index])) index++;
|
|
111
|
-
let key = "";
|
|
112
|
-
while (index < text.length && /[^{\s]/.test(text[index])) {
|
|
113
|
-
key += text[index];
|
|
114
|
-
index++;
|
|
115
|
-
}
|
|
116
|
-
while (index < text.length && /\s/.test(text[index])) index++;
|
|
117
|
-
if (text[index] !== "{") throw new Error("Expected { after option key");
|
|
118
|
-
index++;
|
|
119
|
-
const value = parseNodes(true);
|
|
120
|
-
if (text[index] !== "}") throw new Error("Expected } after option value");
|
|
121
|
-
index++;
|
|
122
|
-
options[key] = value;
|
|
123
|
-
while (index < text.length && /\s/.test(text[index])) index++;
|
|
124
|
-
}
|
|
125
|
-
index++;
|
|
126
|
-
if (type === "plural") return {
|
|
127
|
-
type: "plural",
|
|
128
|
-
name,
|
|
129
|
-
options
|
|
130
|
-
};
|
|
131
|
-
else if (type === "select") return {
|
|
132
|
-
type: "select",
|
|
133
|
-
name,
|
|
134
|
-
options
|
|
135
|
-
};
|
|
136
|
-
} else {
|
|
137
|
-
let style = "";
|
|
138
|
-
while (index < text.length && text[index] !== "}") {
|
|
139
|
-
style += text[index];
|
|
140
|
-
index++;
|
|
141
|
-
}
|
|
142
|
-
if (index >= text.length) throw new Error("Unclosed argument");
|
|
143
|
-
style = style.trim();
|
|
144
|
-
index++;
|
|
145
|
-
return {
|
|
146
|
-
type: "argument",
|
|
147
|
-
name,
|
|
148
|
-
format: {
|
|
149
|
-
type,
|
|
150
|
-
style
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
throw new Error("Malformed argument");
|
|
157
|
-
};
|
|
158
|
-
return parseNodes(false);
|
|
159
|
-
};
|
|
160
|
-
const i18nextNodesToIntlayer = (nodes) => {
|
|
161
|
-
if (nodes.length === 0) return "";
|
|
162
|
-
if (nodes.length === 1 && typeof nodes[0] === "string") {
|
|
163
|
-
const node = nodes[0];
|
|
164
|
-
if (/<[a-zA-Z0-9-]+[^>]*>/.test(node)) return html(node);
|
|
165
|
-
return node;
|
|
166
|
-
}
|
|
167
|
-
if (nodes.every((node) => typeof node === "string" || node.type === "argument")) {
|
|
168
|
-
let str = "";
|
|
169
|
-
for (const node of nodes) if (typeof node === "string") str += node;
|
|
170
|
-
else if (typeof node !== "string" && node.type === "argument") {
|
|
171
|
-
if (node.format) str += `{${node.name}, ${node.format.type}${node.format.style ? `, ${node.format.style}` : ""}}`;
|
|
172
|
-
else str += `{{${node.name}}}`;
|
|
173
|
-
}
|
|
174
|
-
if (/<[a-zA-Z0-9-]+[^>]*>/.test(str)) return html(str);
|
|
175
|
-
return insertion(str);
|
|
176
|
-
}
|
|
177
|
-
if (nodes.length === 1) {
|
|
178
|
-
const node = nodes[0];
|
|
179
|
-
if (typeof node === "string") {
|
|
180
|
-
if (/<[a-zA-Z0-9-]+[^>]*>/.test(node)) return html(node);
|
|
181
|
-
return node;
|
|
182
|
-
}
|
|
183
|
-
if (node?.type === "argument") {
|
|
184
|
-
if (node.format) return insertion(`{${node.name}, ${node.format.type}${node.format.style ? `, ${node.format.style}` : ""}}`);
|
|
185
|
-
return insertion(`{{${node.name}}}`);
|
|
186
|
-
}
|
|
187
|
-
if (node?.type === "plural") {
|
|
188
|
-
const options = {};
|
|
189
|
-
let hasExactMatch = false;
|
|
190
|
-
for (const key of Object.keys(node.options)) if (key.startsWith("=")) {
|
|
191
|
-
hasExactMatch = true;
|
|
192
|
-
break;
|
|
193
|
-
}
|
|
194
|
-
if (hasExactMatch) {
|
|
195
|
-
for (const [key, val] of Object.entries(node.options)) {
|
|
196
|
-
let newKey = key;
|
|
197
|
-
if (key.startsWith("=")) newKey = key.substring(1);
|
|
198
|
-
else if (key === "one") newKey = "1";
|
|
199
|
-
else if (key === "two") newKey = "2";
|
|
200
|
-
else if (key === "few") newKey = "<=3";
|
|
201
|
-
else if (key === "many") newKey = ">=4";
|
|
202
|
-
else if (key === "other") newKey = "fallback";
|
|
203
|
-
const replacedVal = val.map((v) => {
|
|
204
|
-
if (typeof v === "string") return v.replace(/#/g, `{{${node.name}}}`);
|
|
205
|
-
return v;
|
|
206
|
-
});
|
|
207
|
-
options[newKey] = i18nextNodesToIntlayer(replacedVal);
|
|
208
|
-
}
|
|
209
|
-
options.__intlayer_icu_var = node.name;
|
|
210
|
-
return enumeration(options);
|
|
211
|
-
} else {
|
|
212
|
-
for (const [key, val] of Object.entries(node.options)) {
|
|
213
|
-
const replacedVal = val.map((v) => {
|
|
214
|
-
if (typeof v === "string") return v.replace(/#/g, `{{${node.name}}}`);
|
|
215
|
-
return v;
|
|
216
|
-
});
|
|
217
|
-
options[key] = i18nextNodesToIntlayer(replacedVal);
|
|
218
|
-
}
|
|
219
|
-
return plural(options);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
if (node?.type === "select") {
|
|
223
|
-
const options = {};
|
|
224
|
-
for (const [key, val] of Object.entries(node.options)) options[key === "other" ? "fallback" : key] = i18nextNodesToIntlayer(val);
|
|
225
|
-
const optionKeys = Object.keys(options);
|
|
226
|
-
if ((options.male || options.female) && optionKeys.every((k) => [
|
|
227
|
-
"male",
|
|
228
|
-
"female",
|
|
229
|
-
"other",
|
|
230
|
-
"fallback"
|
|
231
|
-
].includes(k))) return gender({
|
|
232
|
-
fallback: options.fallback,
|
|
233
|
-
male: options.male,
|
|
234
|
-
female: options.female
|
|
235
|
-
});
|
|
236
|
-
return select(options, node.name);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
return nodes.map((node) => i18nextNodesToIntlayer([node]));
|
|
19
|
+
const i18nextMessageToIntlayer = (message) => {
|
|
20
|
+
if (HTML_TAG_PATTERN.test(message)) return html(message);
|
|
21
|
+
if (INTERPOLATION_PATTERN.test(message)) return insertion(message);
|
|
22
|
+
return message;
|
|
240
23
|
};
|
|
241
24
|
const i18nextToIntlayerPlugin = {
|
|
242
|
-
canHandle: (node) => typeof node === "string" && (node.includes("{") ||
|
|
243
|
-
transform: (node) =>
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
25
|
+
canHandle: (node) => typeof node === "string" && (node.includes("{{") || HTML_TAG_PATTERN.test(node)),
|
|
26
|
+
transform: (node) => i18nextMessageToIntlayer(node)
|
|
27
|
+
};
|
|
28
|
+
const isContentRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value) && !("nodeType" in value);
|
|
29
|
+
/** Plural variable i18next reads `key_<category>` suffixes on. */
|
|
30
|
+
const I18NEXT_PLURAL_VARIABLE = "count";
|
|
31
|
+
/**
|
|
32
|
+
* i18next entries for a plural, gender or select node stored under `key`:
|
|
33
|
+
* `key_one`/`key_other` for a plural on `count`; `key_male`… with `key` as the
|
|
34
|
+
* fallback for a context; `key_male_one` for a plural inside a context.
|
|
35
|
+
* `undefined` when the node has no suffix form (it is written as ICU).
|
|
36
|
+
*/
|
|
37
|
+
const getSuffixedEntries = (key, value, isContextAllowed = true) => {
|
|
38
|
+
if (typeof value !== "object" || value === null) return void 0;
|
|
39
|
+
const node = value;
|
|
40
|
+
if (node.nodeType === NodeTypes.PLURAL) {
|
|
41
|
+
if (node.variable && node.variable !== I18NEXT_PLURAL_VARIABLE) return;
|
|
42
|
+
return Object.entries(node[NodeTypes.PLURAL]).filter(([, content]) => content !== void 0).map(([category, content]) => [`${key}_${category}`, content]);
|
|
250
43
|
}
|
|
44
|
+
if (isContextAllowed && (node.nodeType === NodeTypes.GENDER || node.nodeType === NodeTypes.SELECT)) return Object.entries(node[node.nodeType]).filter(([, content]) => content !== void 0).flatMap(([context, content]) => {
|
|
45
|
+
const contextKey = context === "fallback" ? key : `${key}_${context}`;
|
|
46
|
+
return getSuffixedEntries(contextKey, content, false) ?? [[contextKey, content]];
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* Rewrites plural, gender and select nodes held by object keys as i18next
|
|
51
|
+
* suffixed keys, which plain i18next resolves (it prints ICU blocks as text).
|
|
52
|
+
* A node keeps its key, and is written as ICU, when a suffixed key would
|
|
53
|
+
* overwrite an existing sibling.
|
|
54
|
+
*/
|
|
55
|
+
const expandSuffixedKeys = (content) => {
|
|
56
|
+
if (Array.isArray(content)) return content.map(expandSuffixedKeys);
|
|
57
|
+
if (!isContentRecord(content)) return content;
|
|
58
|
+
const expandedEntries = Object.entries(content).flatMap(([key, value]) => {
|
|
59
|
+
const suffixedEntries = getSuffixedEntries(key, value);
|
|
60
|
+
const isCollisionFree = suffixedEntries?.every(([suffixedKey]) => suffixedKey === key || !(suffixedKey in content));
|
|
61
|
+
return suffixedEntries && isCollisionFree ? suffixedEntries : [[key, value]];
|
|
62
|
+
});
|
|
63
|
+
return Object.fromEntries(expandedEntries.map(([key, value]) => [key, expandSuffixedKeys(value)]));
|
|
251
64
|
};
|
|
252
65
|
const intlayerToI18nextPlugin = {
|
|
253
66
|
canHandle: (node) => {
|
|
@@ -279,17 +92,12 @@ const intlayerToI18nextPlugin = {
|
|
|
279
92
|
const childVal = next(val, props);
|
|
280
93
|
transformedOptions[key] = typeof childVal === "string" ? childVal : JSON.stringify(childVal);
|
|
281
94
|
}
|
|
282
|
-
|
|
283
|
-
const fallbackVal = transformedOptions.other || Object.values(transformedOptions)[0];
|
|
284
|
-
if (fallbackVal) {
|
|
285
|
-
const match = fallbackVal.match(/\{\{([a-zA-Z0-9_]+)\}\}/) || fallbackVal.match(/\{([a-zA-Z0-9_]+)\}(?!,)/);
|
|
286
|
-
if (match?.[1]) varName = match[1];
|
|
287
|
-
}
|
|
95
|
+
const varName = node.variable ?? I18NEXT_PLURAL_VARIABLE;
|
|
288
96
|
const parts = [];
|
|
289
97
|
for (const [key, val] of Object.entries(transformedOptions)) {
|
|
290
98
|
const icuKey = key;
|
|
291
99
|
let strVal = val;
|
|
292
|
-
strVal = strVal.replace(/\{\{([^}]
|
|
100
|
+
strVal = strVal.replace(/\{\{\s*([^}]+?)\s*\}\}/g, "{$1}");
|
|
293
101
|
strVal = strVal.replace(new RegExp(`\\{${varName}\\}`, "g"), "#");
|
|
294
102
|
parts.push(`${icuKey} {${strVal}}`);
|
|
295
103
|
}
|
|
@@ -306,7 +114,7 @@ const intlayerToI18nextPlugin = {
|
|
|
306
114
|
let varName = options.__intlayer_icu_var || "count";
|
|
307
115
|
if (!options.__intlayer_icu_var) {
|
|
308
116
|
const fallbackVal = transformedOptions.fallback || transformedOptions.other || Object.values(transformedOptions)[0];
|
|
309
|
-
const match = fallbackVal?.match(/\{\{([a-zA-Z0-9_]+)\}\}/) || fallbackVal?.match(/\{([a-zA-Z0-9_]+)\}(?!,)/);
|
|
117
|
+
const match = fallbackVal?.match(/\{\{\s*([a-zA-Z0-9_]+)\s*\}\}/) || fallbackVal?.match(/\{([a-zA-Z0-9_]+)\}(?!,)/);
|
|
310
118
|
if (match?.[1]) varName = match[1];
|
|
311
119
|
}
|
|
312
120
|
const keys = Object.keys(transformedOptions);
|
|
@@ -333,7 +141,7 @@ const intlayerToI18nextPlugin = {
|
|
|
333
141
|
else if (key === ">=4") icuKey = "many";
|
|
334
142
|
else if (/^\d+$/.test(key)) icuKey = `=${key}`;
|
|
335
143
|
let strVal = val;
|
|
336
|
-
strVal = strVal.replace(/\{\{([^}]
|
|
144
|
+
strVal = strVal.replace(/\{\{\s*([^}]+?)\s*\}\}/g, "{$1}");
|
|
337
145
|
strVal = strVal.replace(new RegExp(`\\{${varName}\\}`, "g"), "#");
|
|
338
146
|
parts.push(`${icuKey} {${strVal}}`);
|
|
339
147
|
}
|
|
@@ -348,7 +156,7 @@ const intlayerToI18nextPlugin = {
|
|
|
348
156
|
let icuKey = key;
|
|
349
157
|
if (key === "fallback") icuKey = "other";
|
|
350
158
|
let strVal = val;
|
|
351
|
-
strVal = strVal.replace(/\{\{([^}]
|
|
159
|
+
strVal = strVal.replace(/\{\{\s*([^}]+?)\s*\}\}/g, "{$1}");
|
|
352
160
|
parts.push(`${icuKey} {${strVal}}`);
|
|
353
161
|
}
|
|
354
162
|
return `{${varName}, select, ${parts.join(" ")}}`;
|
|
@@ -368,7 +176,7 @@ const intlayerToI18nextPlugin = {
|
|
|
368
176
|
if (key === "fallback") icuKey = "other";
|
|
369
177
|
const childVal = next(val, props);
|
|
370
178
|
let strVal = typeof childVal === "string" ? childVal : JSON.stringify(childVal);
|
|
371
|
-
strVal = strVal.replace(/\{\{([^}]
|
|
179
|
+
strVal = strVal.replace(/\{\{\s*([^}]+?)\s*\}\}/g, "{$1}");
|
|
372
180
|
parts.push(`${icuKey} {${strVal}}`);
|
|
373
181
|
}
|
|
374
182
|
return `{${varName}, select, ${parts.join(" ")}}`;
|
|
@@ -386,7 +194,7 @@ const intlayerToI18nextPlugin = {
|
|
|
386
194
|
const icuKey = key === "fallback" ? "other" : key;
|
|
387
195
|
const childVal = next(val, props);
|
|
388
196
|
let strVal = typeof childVal === "string" ? childVal : JSON.stringify(childVal);
|
|
389
|
-
strVal = strVal.replace(/\{\{([^}]
|
|
197
|
+
strVal = strVal.replace(/\{\{\s*([^}]+?)\s*\}\}/g, "{$1}");
|
|
390
198
|
parts.push(`${icuKey} {${strVal}}`);
|
|
391
199
|
}
|
|
392
200
|
return `{${varName}, select, ${parts.join(" ")}}`;
|
|
@@ -396,7 +204,7 @@ const intlayerToI18nextPlugin = {
|
|
|
396
204
|
}
|
|
397
205
|
};
|
|
398
206
|
const intlayerToI18nextFormatter = (message) => {
|
|
399
|
-
return deepTransformNode(message, {
|
|
207
|
+
return deepTransformNode(expandSuffixedKeys(message), {
|
|
400
208
|
dictionaryKey: "i18next",
|
|
401
209
|
keyPath: [],
|
|
402
210
|
plugins: [{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"i18next.mjs","names":["insert","enu"],"sources":["../../../src/messageFormat/i18next.ts"],"sourcesContent":["import type { Dictionary } from '@intlayer/types/dictionary';\nimport * as NodeTypes from '@intlayer/types/nodeType';\nimport { deepTransformNode } from '../interpreter';\nimport { enu, gender, html, insert, plural, select } from '../transpiler';\nimport type { JsonValue } from './ICU';\n\n// Types for our AST\ntype I18NextNode =\n | string\n | {\n type: 'argument';\n name: string;\n format?: { type: string; style?: string };\n }\n | { type: 'plural'; name: string; options: Record<string, I18NextNode[]> }\n | { type: 'select'; name: string; options: Record<string, I18NextNode[]> };\n\n/** `{name, plural|select|number|date|time …`: an ICU argument with a format. */\nconst FORMATTED_ARGUMENT_PATTERN =\n /^\\{\\s*[\\p{L}\\p{N}_$][\\p{L}\\p{N}_$.-]*\\s*,\\s*(?:plural|select|number|date|time)\\s*[,}]/u;\n\n/** `{name}`: a bare ICU argument. */\nconst SIMPLE_ARGUMENT_PATTERN = /^\\{\\s*[\\p{L}\\p{N}_$][\\p{L}\\p{N}_$.-]*\\s*\\}/u;\n\n/**\n * Parses an i18next message into nodes.\n *\n * i18next treats single braces as literal text, so a lone `{…}` or `}` at the\n * top level stays text (`Press {Ctrl+K}`, `$t(key, {\"count\": 1})`). Only ICU\n * blocks with a known format (`{count, plural, …}`) are parsed there; bare\n * `{name}` arguments are read inside plural/select options, where the reverse\n * formatter writes them.\n */\nconst parseI18Next = (text: string): I18NextNode[] => {\n let index = 0;\n\n const isICUArgumentStart = (isInsideOption: boolean): boolean => {\n const remainingText = text.slice(index);\n\n return (\n FORMATTED_ARGUMENT_PATTERN.test(remainingText) ||\n (isInsideOption && SIMPLE_ARGUMENT_PATTERN.test(remainingText))\n );\n };\n\n const parseNodes = (isInsideOption: boolean): I18NextNode[] => {\n const nodes: I18NextNode[] = [];\n let currentText = '';\n\n while (index < text.length) {\n const char = text[index];\n\n // Standard i18next variable: {{var}}\n if (char === '{' && text[index + 1] === '{') {\n if (currentText) {\n nodes.push(currentText);\n currentText = '';\n }\n index += 2; // skip {{\n nodes.push(parseStandardArgument());\n }\n // ICU syntax: {var, plural, ...}, or {var} inside an option\n else if (char === '{' && isICUArgumentStart(isInsideOption)) {\n if (currentText) {\n nodes.push(currentText);\n currentText = '';\n }\n index++; // skip {\n nodes.push(parseICUArgument());\n } else if (char === '}' && isInsideOption) {\n // End of the current plural/select option\n break;\n } else {\n currentText += char;\n index++;\n }\n }\n\n if (currentText) {\n nodes.push(currentText);\n }\n return nodes;\n };\n\n const parseStandardArgument = (): I18NextNode => {\n // We are past '{{'\n let name = '';\n while (index < text.length) {\n // Check for closing }}\n if (text[index] === '}' && text[index + 1] === '}') {\n index += 2; // skip }}\n // Unescaped `{{- name}}` → `{{-name}}`: the compat runtime exposes\n // every value, unescaped, under `-name`.\n return { type: 'argument', name: name.trim().replace(/^-\\s+/, '-') };\n }\n name += text[index];\n index++;\n }\n throw new Error('Unclosed i18next variable');\n };\n\n const parseICUArgument = (): I18NextNode => {\n // We are past '{'\n let name = '';\n while (index < text.length && /[^,}]/.test(text[index]!)) {\n name += text[index];\n index++;\n }\n name = name.trim();\n\n if (index >= text.length) throw new Error('Unclosed argument');\n\n if (text[index] === '}') {\n index++;\n return { type: 'argument', name };\n }\n\n // Must be comma\n if (text[index] === ',') {\n index++;\n // Parse type\n let type = '';\n while (index < text.length && /[^,}]/.test(text[index]!)) {\n type += text[index];\n index++;\n }\n type = type.trim();\n\n if (index >= text.length) throw new Error('Unclosed argument');\n\n if (text[index] === '}') {\n index++;\n return { type: 'argument', name, format: { type } };\n }\n\n if (text[index] === ',') {\n index++;\n\n // If plural or select, parse options\n if (type === 'plural' || type === 'select') {\n const options: Record<string, I18NextNode[]> = {};\n\n while (index < text.length && text[index] !== '}') {\n while (index < text.length && /\\s/.test(text[index]!)) index++;\n\n let key = '';\n while (index < text.length && /[^{\\s]/.test(text[index]!)) {\n key += text[index];\n index++;\n }\n\n while (index < text.length && /\\s/.test(text[index]!)) index++;\n\n if (text[index] !== '{')\n throw new Error('Expected { after option key');\n index++; // skip {\n\n const value = parseNodes(true);\n\n if (text[index] !== '}')\n throw new Error('Expected } after option value');\n index++; // skip }\n\n options[key] = value;\n\n while (index < text.length && /\\s/.test(text[index]!)) index++;\n }\n\n index++; // skip closing argument }\n\n if (type === 'plural') {\n return { type: 'plural', name, options };\n } else if (type === 'select') {\n return { type: 'select', name, options };\n }\n } else {\n // Parse style for number/date/time\n let style = '';\n while (index < text.length && text[index] !== '}') {\n style += text[index];\n index++;\n }\n if (index >= text.length) throw new Error('Unclosed argument');\n\n style = style.trim();\n index++; // skip }\n\n return { type: 'argument', name, format: { type, style } };\n }\n }\n }\n\n throw new Error('Malformed argument');\n };\n\n return parseNodes(false);\n};\n\nconst i18nextNodesToIntlayer = (nodes: I18NextNode[]): any => {\n if (nodes.length === 0) return '';\n if (nodes.length === 1 && typeof nodes[0] === 'string') {\n const node = nodes[0];\n if (/<[a-zA-Z0-9-]+[^>]*>/.test(node)) {\n return html(node);\n }\n return node;\n }\n\n const canFlatten = nodes.every(\n (node) => typeof node === 'string' || node.type === 'argument'\n );\n\n if (canFlatten) {\n let str = '';\n for (const node of nodes) {\n if (typeof node === 'string') {\n str += node;\n } else if (typeof node !== 'string' && node.type === 'argument') {\n if (node.format) {\n // For formatted arguments, use ICU syntax: {name, type, style}\n str += `{${node.name}, ${node.format.type}${\n node.format.style ? `, ${node.format.style}` : ''\n }}`;\n } else {\n // For simple arguments, use standard i18next: {{name}}\n str += `{{${node.name}}}`;\n }\n }\n }\n if (/<[a-zA-Z0-9-]+[^>]*>/.test(str)) {\n return html(str);\n }\n return insert(str);\n }\n\n if (nodes.length === 1) {\n const node = nodes[0];\n if (typeof node === 'string') {\n if (/<[a-zA-Z0-9-]+[^>]*>/.test(node)) {\n return html(node);\n }\n return node;\n }\n\n if (node?.type === 'argument') {\n if (node.format) {\n return insert(\n `{${node.name}, ${node.format.type}${\n node.format.style ? `, ${node.format.style}` : ''\n }}`\n );\n }\n return insert(`{{${node.name}}}`);\n }\n\n if (node?.type === 'plural') {\n const options: Record<string, any> = {};\n let hasExactMatch = false;\n\n for (const key of Object.keys(node.options)) {\n if (key.startsWith('=')) {\n hasExactMatch = true;\n break;\n }\n }\n\n if (hasExactMatch) {\n for (const [key, val] of Object.entries(node.options)) {\n let newKey = key;\n if (key.startsWith('=')) {\n newKey = key.substring(1);\n } else if (key === 'one') {\n newKey = '1';\n } else if (key === 'two') {\n newKey = '2';\n } else if (key === 'few') {\n newKey = '<=3';\n } else if (key === 'many') {\n newKey = '>=4';\n } else if (key === 'other') {\n newKey = 'fallback';\n }\n // Handle # replacement\n const replacedVal = val.map((v) => {\n if (typeof v === 'string') {\n // In ICU plural, # is replaced by the number\n // In i18next, if using ICU plugin, it behaves same.\n // We map it to {{varName}} in Intlayer\n return v.replace(/#/g, `{{${node.name}}}`);\n }\n return v;\n });\n\n options[newKey] = i18nextNodesToIntlayer(replacedVal);\n }\n\n // Preserve variable name\n options.__intlayer_icu_var = node.name;\n\n return enu(options);\n } else {\n for (const [key, val] of Object.entries(node.options)) {\n // Handle # replacement\n const replacedVal = val.map((v) => {\n if (typeof v === 'string') {\n return v.replace(/#/g, `{{${node.name}}}`);\n }\n return v;\n });\n\n options[key] = i18nextNodesToIntlayer(replacedVal);\n }\n\n return plural(options as any);\n }\n }\n\n if (node?.type === 'select') {\n const options: Record<string, any> = {};\n for (const [key, val] of Object.entries(node.options)) {\n // ICU names its catch-all case `other`; intlayer names it `fallback`.\n options[key === 'other' ? 'fallback' : key] =\n i18nextNodesToIntlayer(val);\n }\n\n // Check for gender\n const optionKeys = Object.keys(options);\n const isGender =\n (options.male || options.female) &&\n optionKeys.every((k) =>\n ['male', 'female', 'other', 'fallback'].includes(k)\n );\n\n if (isGender) {\n return gender({\n fallback: options.fallback,\n male: options.male,\n female: options.female,\n });\n }\n\n // Any other discriminant maps to a `select` node, which resolves on\n // arbitrary string values at runtime.\n return select(options, node.name);\n }\n }\n\n return nodes.map((node) => i18nextNodesToIntlayer([node]));\n};\n\nconst i18nextToIntlayerPlugin = {\n canHandle: (node: any) =>\n typeof node === 'string' &&\n (node.includes('{') ||\n node.includes('}') ||\n /<[a-zA-Z0-9-]+[^>]*>/.test(node)),\n transform: (node: any) => {\n try {\n const ast = parseI18Next(node);\n return i18nextNodesToIntlayer(ast);\n } catch {\n return node;\n }\n },\n};\n\nconst intlayerToI18nextPlugin = {\n canHandle: (node: any) => {\n if (typeof node === 'string') return true;\n\n if (\n node &&\n typeof node === 'object' &&\n (node.nodeType === NodeTypes.INSERTION ||\n node.nodeType === NodeTypes.HTML ||\n node.nodeType === NodeTypes.ENUMERATION ||\n node.nodeType === NodeTypes.PLURAL ||\n node.nodeType === NodeTypes.GENDER ||\n node.nodeType === NodeTypes.SELECT ||\n node.nodeType === 'composite')\n ) {\n return true;\n }\n\n if (Array.isArray(node)) {\n if (node.length === 0) return false;\n\n let hasNode = false;\n let hasPlainObjectOrArray = false;\n\n for (const item of node) {\n if (typeof item === 'string') {\n } else if (\n item &&\n typeof item === 'object' &&\n (item.nodeType === NodeTypes.INSERTION ||\n item.nodeType === NodeTypes.HTML ||\n item.nodeType === NodeTypes.ENUMERATION ||\n item.nodeType === NodeTypes.PLURAL ||\n item.nodeType === NodeTypes.GENDER ||\n item.nodeType === NodeTypes.SELECT ||\n item.nodeType === 'composite')\n ) {\n hasNode = true;\n } else {\n hasPlainObjectOrArray = true;\n }\n }\n\n // If it contains plain objects or nested arrays, it's a structural array\n if (hasPlainObjectOrArray) return false;\n // If it contains ONLY strings, it's a structural array, not a composite string\n if (!hasNode) return false;\n\n return true;\n }\n\n return false;\n },\n transform: (node: any, props: any, next: any) => {\n if (typeof node === 'string') {\n return node;\n }\n\n if (node.nodeType === NodeTypes.INSERTION) {\n if (node[NodeTypes.INSERTION].match(/\\{[^}]*,[^}]*\\}/)) {\n return node[NodeTypes.INSERTION];\n }\n return node[NodeTypes.INSERTION];\n }\n\n if (node.nodeType === NodeTypes.HTML) {\n return node[NodeTypes.HTML];\n }\n\n if (node.nodeType === NodeTypes.PLURAL) {\n const options = node[NodeTypes.PLURAL];\n\n const transformedOptions: Record<string, string> = {};\n for (const [key, val] of Object.entries(options)) {\n const childVal = next(val, props);\n transformedOptions[key] =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n }\n\n let varName = 'count';\n\n const fallbackVal =\n transformedOptions.other || Object.values(transformedOptions)[0];\n\n if (fallbackVal) {\n const match =\n fallbackVal.match(/\\{\\{([a-zA-Z0-9_]+)\\}\\}/) ||\n fallbackVal.match(/\\{([a-zA-Z0-9_]+)\\}(?!,)/);\n if (match?.[1]) {\n varName = match[1];\n }\n }\n\n const parts = [];\n\n for (const [key, val] of Object.entries(transformedOptions)) {\n const icuKey = key;\n let strVal = val;\n\n strVal = strVal.replace(/\\{\\{([^}]+)\\}\\}/g, '{$1}');\n strVal = strVal.replace(new RegExp(`\\\\{${varName}\\\\}`, 'g'), '#');\n\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, plural, ${parts.join(' ')}}`;\n }\n\n if (node.nodeType === NodeTypes.ENUMERATION) {\n const options = node[NodeTypes.ENUMERATION];\n\n const transformedOptions: Record<string, string> = {};\n for (const [key, val] of Object.entries(options)) {\n if (key === '__intlayer_icu_var' || key === '__intlayer_icu_ordinal')\n continue;\n const childVal = next(val, props);\n transformedOptions[key] =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n }\n\n let varName = options.__intlayer_icu_var || 'count';\n\n if (!options.__intlayer_icu_var) {\n const fallbackVal =\n transformedOptions.fallback ||\n transformedOptions.other ||\n Object.values(transformedOptions)[0];\n\n const match =\n fallbackVal?.match(/\\{\\{([a-zA-Z0-9_]+)\\}\\}/) ||\n fallbackVal?.match(/\\{([a-zA-Z0-9_]+)\\}(?!,)/);\n if (match?.[1]) {\n varName = match[1];\n }\n }\n\n const keys = Object.keys(transformedOptions);\n const pluralKeys = [\n '1',\n '2',\n '<=3',\n '>=4',\n 'fallback',\n 'other',\n 'zero',\n 'one',\n 'two',\n 'few',\n 'many',\n ];\n const isPlural = keys.every(\n (k) => pluralKeys.includes(k) || /^[<>=]?\\d+(\\.\\d+)?$/.test(k)\n );\n\n const parts = [];\n\n if (isPlural) {\n for (const [key, val] of Object.entries(transformedOptions)) {\n let icuKey = key;\n if (key === 'fallback') icuKey = 'other';\n else if (key === '<=3') icuKey = 'few';\n else if (key === '>=4') icuKey = 'many';\n else if (/^\\d+$/.test(key)) icuKey = `=${key}`;\n\n let strVal = val;\n\n strVal = strVal.replace(/\\{\\{([^}]+)\\}\\}/g, '{$1}');\n strVal = strVal.replace(new RegExp(`\\\\{${varName}\\\\}`, 'g'), '#');\n\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, plural, ${parts.join(' ')}}`;\n } else {\n const entries = Object.entries(transformedOptions).sort(\n ([keyA], [keyB]) => {\n if (keyA === 'fallback' || keyA === 'other') return 1;\n if (keyB === 'fallback' || keyB === 'other') return -1;\n return 0;\n }\n );\n\n for (const [key, val] of entries) {\n let icuKey = key;\n if (key === 'fallback') icuKey = 'other';\n\n let strVal = val;\n strVal = strVal.replace(/\\{\\{([^}]+)\\}\\}/g, '{$1}');\n\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, select, ${parts.join(' ')}}`;\n }\n }\n\n if (node.nodeType === NodeTypes.GENDER) {\n const options = node[NodeTypes.GENDER];\n const varName = 'gender';\n const parts = [];\n\n const entries = Object.entries(options).sort(([keyA], [keyB]) => {\n if (keyA === 'fallback') return 1;\n if (keyB === 'fallback') return -1;\n return 0;\n });\n\n for (const [key, val] of entries) {\n let icuKey = key;\n if (key === 'fallback') icuKey = 'other';\n\n const childVal = next(val, props);\n let strVal =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n\n strVal = strVal.replace(/\\{\\{([^}]+)\\}\\}/g, '{$1}');\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, select, ${parts.join(' ')}}`;\n }\n\n if (node.nodeType === NodeTypes.SELECT) {\n const options = node[NodeTypes.SELECT];\n const varName = node.variable || 'value';\n const parts = [];\n\n // ICU requires the `other` case to come last.\n const entries = Object.entries(options).sort(([keyA], [keyB]) => {\n if (keyA === 'fallback') return 1;\n if (keyB === 'fallback') return -1;\n return 0;\n });\n\n for (const [key, val] of entries) {\n const icuKey = key === 'fallback' ? 'other' : key;\n\n const childVal = next(val, props);\n let strVal =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n\n strVal = strVal.replace(/\\{\\{([^}]+)\\}\\}/g, '{$1}');\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, select, ${parts.join(' ')}}`;\n }\n\n if (\n Array.isArray(node) ||\n (node.nodeType === 'composite' && Array.isArray(node.composite))\n ) {\n const arr = Array.isArray(node) ? node : node.composite;\n const items = arr.map((item: any) => next(item, props));\n return items.join('');\n }\n\n return next(node, props);\n },\n};\n\nexport const intlayerToI18nextFormatter = (\n message: Dictionary['content']\n): JsonValue => {\n return deepTransformNode(message, {\n dictionaryKey: 'i18next',\n keyPath: [],\n plugins: [{ id: 'i18next', ...intlayerToI18nextPlugin }],\n });\n};\n\nexport const i18nextToIntlayerFormatter = (\n message: JsonValue\n): Dictionary['content'] => {\n return deepTransformNode(message, {\n dictionaryKey: 'i18next',\n keyPath: [],\n plugins: [{ id: 'i18next', ...i18nextToIntlayerPlugin }],\n });\n};\n"],"mappings":";;;;;;;;;;;AAkBA,MAAM,6BACJ;;AAGF,MAAM,0BAA0B;;;;;;;;;;AAWhC,MAAM,gBAAgB,SAAgC;CACpD,IAAI,QAAQ;CAEZ,MAAM,sBAAsB,mBAAqC;EAC/D,MAAM,gBAAgB,KAAK,MAAM,KAAK;EAEtC,OACE,2BAA2B,KAAK,aAAa,KAC5C,kBAAkB,wBAAwB,KAAK,aAAa;CAEjE;CAEA,MAAM,cAAc,mBAA2C;EAC7D,MAAM,QAAuB,CAAC;EAC9B,IAAI,cAAc;EAElB,OAAO,QAAQ,KAAK,QAAQ;GAC1B,MAAM,OAAO,KAAK;GAGlB,IAAI,SAAS,OAAO,KAAK,QAAQ,OAAO,KAAK;IAC3C,IAAI,aAAa;KACf,MAAM,KAAK,WAAW;KACtB,cAAc;IAChB;IACA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AAW,WAAW,KAAK,UAAU,QAAQ;IAEnE,SAAS,OAAO,QAAQ,oBAAoB,MAAM;IAClD,MAAM,KAAK,GAAG,OAAO,IAAI,OAAO,EAAE;GACpC;GACA,OAAO,IAAI,QAAQ,YAAY,MAAM,KAAK,GAAG,EAAE;EACjD;EAEA,IACE,MAAM,QAAQ,IAAI,KACjB,KAAK,aAAa,eAAe,MAAM,QAAQ,KAAK,SAAS,GAI9D,QAFY,MAAM,QAAQ,IAAI,IAAI,OAAO,KAAK,UAC7B,CAAC,KAAK,SAAc,KAAK,MAAM,KAAK,CAC1C,CAAC,CAAC,KAAK,EAAE;EAGtB,OAAO,KAAK,MAAM,KAAK;CACzB;AACF;AAEA,MAAa,8BACX,YACc;CACd,OAAO,kBAAkB,SAAS;EAChC,eAAe;EACf,SAAS,CAAC;EACV,SAAS,CAAC;GAAE,IAAI;GAAW,GAAG;EAAwB,CAAC;CACzD,CAAC;AACH;AAEA,MAAa,8BACX,YAC0B;CAC1B,OAAO,kBAAkB,SAAS;EAChC,eAAe;EACf,SAAS,CAAC;EACV,SAAS,CAAC;GAAE,IAAI;GAAW,GAAG;EAAwB,CAAC;CACzD,CAAC;AACH"}
|
|
1
|
+
{"version":3,"file":"i18next.mjs","names":["insert"],"sources":["../../../src/messageFormat/i18next.ts"],"sourcesContent":["import type { Dictionary } from '@intlayer/types/dictionary';\nimport * as NodeTypes from '@intlayer/types/nodeType';\nimport { deepTransformNode } from '../interpreter';\nimport { html, insert } from '../transpiler';\nimport type { JsonValue } from './ICU';\n\n/** An HTML-like tag (`<strong>`, `<1>`), rendered through an `html` node. */\nconst HTML_TAG_PATTERN = /<[a-zA-Z0-9-]+[^>]*>/;\n\n/** A closed `{{…}}` interpolation. */\nconst INTERPOLATION_PATTERN = /\\{\\{[\\s\\S]*?\\}\\}/;\n\n/**\n * Converts one i18next message into intlayer content.\n *\n * As in i18next, only `{{…}}` is syntax: single braces stay text\n * (`Press {Ctrl+K}`, `$t(key, {\"count\": 1})`, ICU blocks), and each\n * interpolation keeps its spelling so write-back leaves the source unchanged.\n * Plurals and contexts are key suffixes (`key_one`, `key_male`), kept as keys.\n */\nconst i18nextMessageToIntlayer = (message: string) => {\n if (HTML_TAG_PATTERN.test(message)) return html(message);\n if (INTERPOLATION_PATTERN.test(message)) return insert(message);\n\n return message;\n};\n\nconst i18nextToIntlayerPlugin = {\n canHandle: (node: unknown): node is string =>\n typeof node === 'string' &&\n (node.includes('{{') || HTML_TAG_PATTERN.test(node)),\n transform: (node: string) => i18nextMessageToIntlayer(node),\n};\n\n/** Content keyed by name: a plain object, not an intlayer node. */\ntype ContentRecord = Record<string, unknown>;\n\nconst isContentRecord = (value: unknown): value is ContentRecord =>\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value) &&\n !('nodeType' in value);\n\n/** Plural variable i18next reads `key_<category>` suffixes on. */\nconst I18NEXT_PLURAL_VARIABLE = 'count';\n\n/**\n * i18next entries for a plural, gender or select node stored under `key`:\n * `key_one`/`key_other` for a plural on `count`; `key_male`… with `key` as the\n * fallback for a context; `key_male_one` for a plural inside a context.\n * `undefined` when the node has no suffix form (it is written as ICU).\n */\nconst getSuffixedEntries = (\n key: string,\n value: unknown,\n isContextAllowed = true\n): [string, unknown][] | undefined => {\n if (typeof value !== 'object' || value === null) return undefined;\n\n const node = value as ContentRecord & {\n nodeType?: string;\n variable?: string;\n };\n\n if (node.nodeType === NodeTypes.PLURAL) {\n if (node.variable && node.variable !== I18NEXT_PLURAL_VARIABLE) {\n return undefined;\n }\n\n return Object.entries(node[NodeTypes.PLURAL] as ContentRecord)\n .filter(([, content]) => content !== undefined)\n .map(([category, content]) => [`${key}_${category}`, content]);\n }\n\n if (\n isContextAllowed &&\n (node.nodeType === NodeTypes.GENDER || node.nodeType === NodeTypes.SELECT)\n ) {\n return Object.entries(node[node.nodeType] as ContentRecord)\n .filter(([, content]) => content !== undefined)\n .flatMap(([context, content]): [string, unknown][] => {\n const contextKey = context === 'fallback' ? key : `${key}_${context}`;\n\n return (\n getSuffixedEntries(contextKey, content, false) ?? [\n [contextKey, content],\n ]\n );\n });\n }\n\n return undefined;\n};\n\n/**\n * Rewrites plural, gender and select nodes held by object keys as i18next\n * suffixed keys, which plain i18next resolves (it prints ICU blocks as text).\n * A node keeps its key, and is written as ICU, when a suffixed key would\n * overwrite an existing sibling.\n */\nconst expandSuffixedKeys = (content: unknown): unknown => {\n if (Array.isArray(content)) return content.map(expandSuffixedKeys);\n if (!isContentRecord(content)) return content;\n\n const expandedEntries = Object.entries(content).flatMap(\n ([key, value]): [string, unknown][] => {\n const suffixedEntries = getSuffixedEntries(key, value);\n const isCollisionFree = suffixedEntries?.every(\n ([suffixedKey]) => suffixedKey === key || !(suffixedKey in content)\n );\n\n return suffixedEntries && isCollisionFree\n ? suffixedEntries\n : [[key, value]];\n }\n );\n\n return Object.fromEntries(\n expandedEntries.map(([key, value]) => [key, expandSuffixedKeys(value)])\n );\n};\n\nconst intlayerToI18nextPlugin = {\n canHandle: (node: any) => {\n if (typeof node === 'string') return true;\n\n if (\n node &&\n typeof node === 'object' &&\n (node.nodeType === NodeTypes.INSERTION ||\n node.nodeType === NodeTypes.HTML ||\n node.nodeType === NodeTypes.ENUMERATION ||\n node.nodeType === NodeTypes.PLURAL ||\n node.nodeType === NodeTypes.GENDER ||\n node.nodeType === NodeTypes.SELECT ||\n node.nodeType === 'composite')\n ) {\n return true;\n }\n\n if (Array.isArray(node)) {\n if (node.length === 0) return false;\n\n let hasNode = false;\n let hasPlainObjectOrArray = false;\n\n for (const item of node) {\n if (typeof item === 'string') {\n } else if (\n item &&\n typeof item === 'object' &&\n (item.nodeType === NodeTypes.INSERTION ||\n item.nodeType === NodeTypes.HTML ||\n item.nodeType === NodeTypes.ENUMERATION ||\n item.nodeType === NodeTypes.PLURAL ||\n item.nodeType === NodeTypes.GENDER ||\n item.nodeType === NodeTypes.SELECT ||\n item.nodeType === 'composite')\n ) {\n hasNode = true;\n } else {\n hasPlainObjectOrArray = true;\n }\n }\n\n // If it contains plain objects or nested arrays, it's a structural array\n if (hasPlainObjectOrArray) return false;\n // If it contains ONLY strings, it's a structural array, not a composite string\n if (!hasNode) return false;\n\n return true;\n }\n\n return false;\n },\n transform: (node: any, props: any, next: any) => {\n if (typeof node === 'string') {\n return node;\n }\n\n if (node.nodeType === NodeTypes.INSERTION) {\n if (node[NodeTypes.INSERTION].match(/\\{[^}]*,[^}]*\\}/)) {\n return node[NodeTypes.INSERTION];\n }\n return node[NodeTypes.INSERTION];\n }\n\n if (node.nodeType === NodeTypes.HTML) {\n return node[NodeTypes.HTML];\n }\n\n if (node.nodeType === NodeTypes.PLURAL) {\n const options = node[NodeTypes.PLURAL];\n\n const transformedOptions: Record<string, string> = {};\n for (const [key, val] of Object.entries(options)) {\n const childVal = next(val, props);\n transformedOptions[key] =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n }\n\n // A plural selects on its `variable`, `count` by default\n const varName: string = node.variable ?? I18NEXT_PLURAL_VARIABLE;\n\n const parts = [];\n\n for (const [key, val] of Object.entries(transformedOptions)) {\n const icuKey = key;\n let strVal = val;\n\n strVal = strVal.replace(/\\{\\{\\s*([^}]+?)\\s*\\}\\}/g, '{$1}');\n strVal = strVal.replace(new RegExp(`\\\\{${varName}\\\\}`, 'g'), '#');\n\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, plural, ${parts.join(' ')}}`;\n }\n\n if (node.nodeType === NodeTypes.ENUMERATION) {\n const options = node[NodeTypes.ENUMERATION];\n\n const transformedOptions: Record<string, string> = {};\n for (const [key, val] of Object.entries(options)) {\n if (key === '__intlayer_icu_var' || key === '__intlayer_icu_ordinal')\n continue;\n const childVal = next(val, props);\n transformedOptions[key] =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n }\n\n let varName = options.__intlayer_icu_var || 'count';\n\n if (!options.__intlayer_icu_var) {\n const fallbackVal =\n transformedOptions.fallback ||\n transformedOptions.other ||\n Object.values(transformedOptions)[0];\n\n const match =\n fallbackVal?.match(/\\{\\{\\s*([a-zA-Z0-9_]+)\\s*\\}\\}/) ||\n fallbackVal?.match(/\\{([a-zA-Z0-9_]+)\\}(?!,)/);\n if (match?.[1]) {\n varName = match[1];\n }\n }\n\n const keys = Object.keys(transformedOptions);\n const pluralKeys = [\n '1',\n '2',\n '<=3',\n '>=4',\n 'fallback',\n 'other',\n 'zero',\n 'one',\n 'two',\n 'few',\n 'many',\n ];\n const isPlural = keys.every(\n (k) => pluralKeys.includes(k) || /^[<>=]?\\d+(\\.\\d+)?$/.test(k)\n );\n\n const parts = [];\n\n if (isPlural) {\n for (const [key, val] of Object.entries(transformedOptions)) {\n let icuKey = key;\n if (key === 'fallback') icuKey = 'other';\n else if (key === '<=3') icuKey = 'few';\n else if (key === '>=4') icuKey = 'many';\n else if (/^\\d+$/.test(key)) icuKey = `=${key}`;\n\n let strVal = val;\n\n strVal = strVal.replace(/\\{\\{\\s*([^}]+?)\\s*\\}\\}/g, '{$1}');\n strVal = strVal.replace(new RegExp(`\\\\{${varName}\\\\}`, 'g'), '#');\n\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, plural, ${parts.join(' ')}}`;\n } else {\n const entries = Object.entries(transformedOptions).sort(\n ([keyA], [keyB]) => {\n if (keyA === 'fallback' || keyA === 'other') return 1;\n if (keyB === 'fallback' || keyB === 'other') return -1;\n return 0;\n }\n );\n\n for (const [key, val] of entries) {\n let icuKey = key;\n if (key === 'fallback') icuKey = 'other';\n\n let strVal = val;\n strVal = strVal.replace(/\\{\\{\\s*([^}]+?)\\s*\\}\\}/g, '{$1}');\n\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, select, ${parts.join(' ')}}`;\n }\n }\n\n if (node.nodeType === NodeTypes.GENDER) {\n const options = node[NodeTypes.GENDER];\n const varName = 'gender';\n const parts = [];\n\n const entries = Object.entries(options).sort(([keyA], [keyB]) => {\n if (keyA === 'fallback') return 1;\n if (keyB === 'fallback') return -1;\n return 0;\n });\n\n for (const [key, val] of entries) {\n let icuKey = key;\n if (key === 'fallback') icuKey = 'other';\n\n const childVal = next(val, props);\n let strVal =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n\n strVal = strVal.replace(/\\{\\{\\s*([^}]+?)\\s*\\}\\}/g, '{$1}');\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, select, ${parts.join(' ')}}`;\n }\n\n if (node.nodeType === NodeTypes.SELECT) {\n const options = node[NodeTypes.SELECT];\n const varName = node.variable || 'value';\n const parts = [];\n\n // ICU requires the `other` case to come last.\n const entries = Object.entries(options).sort(([keyA], [keyB]) => {\n if (keyA === 'fallback') return 1;\n if (keyB === 'fallback') return -1;\n return 0;\n });\n\n for (const [key, val] of entries) {\n const icuKey = key === 'fallback' ? 'other' : key;\n\n const childVal = next(val, props);\n let strVal =\n typeof childVal === 'string' ? childVal : JSON.stringify(childVal);\n\n strVal = strVal.replace(/\\{\\{\\s*([^}]+?)\\s*\\}\\}/g, '{$1}');\n parts.push(`${icuKey} {${strVal}}`);\n }\n return `{${varName}, select, ${parts.join(' ')}}`;\n }\n\n if (\n Array.isArray(node) ||\n (node.nodeType === 'composite' && Array.isArray(node.composite))\n ) {\n const arr = Array.isArray(node) ? node : node.composite;\n const items = arr.map((item: any) => next(item, props));\n return items.join('');\n }\n\n return next(node, props);\n },\n};\n\nexport const intlayerToI18nextFormatter = (\n message: Dictionary['content']\n): JsonValue => {\n return deepTransformNode(expandSuffixedKeys(message), {\n dictionaryKey: 'i18next',\n keyPath: [],\n plugins: [{ id: 'i18next', ...intlayerToI18nextPlugin }],\n });\n};\n\nexport const i18nextToIntlayerFormatter = (\n message: JsonValue\n): Dictionary['content'] => {\n return deepTransformNode(message, {\n dictionaryKey: 'i18next',\n keyPath: [],\n plugins: [{ id: 'i18next', ...i18nextToIntlayerPlugin }],\n });\n};\n"],"mappings":";;;;;;;AAOA,MAAM,mBAAmB;;AAGzB,MAAM,wBAAwB;;;;;;;;;AAU9B,MAAM,4BA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U,QAAQ;IAEnE,SAAS,OAAO,QAAQ,2BAA2B,MAAM;IACzD,MAAM,KAAK,GAAG,OAAO,IAAI,OAAO,EAAE;GACpC;GACA,OAAO,IAAI,QAAQ,YAAY,MAAM,KAAK,GAAG,EAAE;EACjD;EAEA,IACE,MAAM,QAAQ,IAAI,KACjB,KAAK,aAAa,eAAe,MAAM,QAAQ,KAAK,SAAS,GAI9D,QAFY,MAAM,QAAQ,IAAI,IAAI,OAAO,KAAK,UAC7B,CAAC,KAAK,SAAc,KAAK,MAAM,KAAK,CAC1C,CAAC,CAAC,KAAK,EAAE;EAGtB,OAAO,KAAK,MAAM,KAAK;CACzB;AACF;AAEA,MAAa,8BACX,YACc;CACd,OAAO,kBAAkB,mBAAmB,OAAO,GAAG;EACpD,eAAe;EACf,SAAS,CAAC;EACV,SAAS,CAAC;GAAE,IAAI;GAAW,GAAG;EAAwB,CAAC;CACzD,CAAC;AACH;AAEA,MAAa,8BACX,YAC0B;CAC1B,OAAO,kBAAkB,SAAS;EAChC,eAAe;EACf,SAAS,CAAC;EACV,SAAS,CAAC;GAAE,IAAI;GAAW,GAAG;EAAwB,CAAC;CACzD,CAAC;AACH"}
|
|
@@ -74,19 +74,25 @@ const formatArgument = (value, type, style, locale, formatters) => {
|
|
|
74
74
|
* optionally formatted the i18next way (`{{price, number}}`)
|
|
75
75
|
* 2. ICU formatted arguments `{value, number}` / `{ts, date, long}`
|
|
76
76
|
* 3. Bare single-brace arguments `{name}` (ICU / vue-i18n simple args)
|
|
77
|
+
*
|
|
78
|
+
* With `'double-brace'` interpolation, only step 1 runs.
|
|
77
79
|
*/
|
|
78
|
-
const interpolateMessage = (template, values = {}, locale = "en", formatters
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
})
|
|
80
|
+
const interpolateMessage = (template, values = {}, locale = "en", formatters, interpolation = "default") => {
|
|
81
|
+
const interpolated = template.replace(/\{\{\s*([^{},]+?)\s*(?:,\s*(\w+)\s*(?:,\s*([^{}]+?)\s*)?)?\}\}/g, (match, path, type, style) => {
|
|
82
|
+
const value = resolveValuePath(values, path) ?? resolveValuePath(values, path.replace(/^-\s+/, "-"));
|
|
83
|
+
if (value === void 0) return match;
|
|
84
|
+
return type ? formatArgument(value, type, style, locale, formatters) : String(value);
|
|
85
|
+
});
|
|
86
|
+
if (interpolation === "double-brace") return interpolated;
|
|
87
|
+
return interpolated.replace(/\{\s*([\w.]+)\s*,\s*(\w+)\s*(?:,\s*([^}]+?)\s*)?\}/g, (match, path, type, style) => {
|
|
88
|
+
const value = resolveValuePath(values, path);
|
|
89
|
+
if (value === void 0) return match;
|
|
90
|
+
return formatArgument(value, type, style, locale, formatters);
|
|
91
|
+
}).replace(/\{\s*([\w.]+)\s*\}/g, (match, path) => {
|
|
92
|
+
const value = resolveValuePath(values, path);
|
|
93
|
+
return value === void 0 ? match : String(value);
|
|
94
|
+
});
|
|
95
|
+
};
|
|
90
96
|
/** Reads the selector value of an enumeration/plural node from the values. */
|
|
91
97
|
const getSelectorValue = (values, variableName) => values[variableName] ?? values.count ?? values.n;
|
|
92
98
|
/**
|
|
@@ -97,25 +103,25 @@ const getSelectorValue = (values, variableName) => values[variableName] ?? value
|
|
|
97
103
|
* Function nodes (produced by the interpreter plugins for `insertion`,
|
|
98
104
|
* `enumeration` and `plural` content) are invoked with the values.
|
|
99
105
|
*/
|
|
100
|
-
const resolveMessageNode = (node, values = {}, locale = "en", formatters) => {
|
|
106
|
+
const resolveMessageNode = (node, values = {}, locale = "en", formatters, interpolation = "default") => {
|
|
101
107
|
if (node === null || node === void 0) return node;
|
|
102
|
-
if (typeof node === "string") return interpolateMessage(node, values, locale, formatters);
|
|
108
|
+
if (typeof node === "string") return interpolateMessage(node, values, locale, formatters, interpolation);
|
|
103
109
|
if (typeof node === "number" || typeof node === "boolean") return String(node);
|
|
104
110
|
if (typeof node === "function") try {
|
|
105
|
-
return resolveMessageNode(node(values), values, locale, formatters);
|
|
111
|
+
return resolveMessageNode(node(values), values, locale, formatters, interpolation);
|
|
106
112
|
} catch {
|
|
107
113
|
return;
|
|
108
114
|
}
|
|
109
|
-
if (Array.isArray(node)) return node.map((item) => String(resolveMessageNode(item, values, locale, formatters) ?? "")).join("");
|
|
115
|
+
if (Array.isArray(node)) return node.map((item) => String(resolveMessageNode(item, values, locale, formatters, interpolation) ?? "")).join("");
|
|
110
116
|
const typedNode = node;
|
|
111
|
-
if (typedNode.nodeType === NodeTypes.INSERTION) return resolveMessageNode(typedNode[NodeTypes.INSERTION], values, locale, formatters);
|
|
112
|
-
if (typedNode.nodeType === NodeTypes.HTML) return resolveMessageNode(typedNode[NodeTypes.HTML], values, locale, formatters);
|
|
117
|
+
if (typedNode.nodeType === NodeTypes.INSERTION) return resolveMessageNode(typedNode[NodeTypes.INSERTION], values, locale, formatters, interpolation);
|
|
118
|
+
if (typedNode.nodeType === NodeTypes.HTML) return resolveMessageNode(typedNode[NodeTypes.HTML], values, locale, formatters, interpolation);
|
|
113
119
|
if (typedNode.nodeType === NodeTypes.PLURAL) {
|
|
114
120
|
const pluralState = typedNode[NodeTypes.PLURAL];
|
|
115
121
|
const variableName = typeof typedNode.variable === "string" ? typedNode.variable : DEFAULT_PLURAL_VARIABLE;
|
|
116
122
|
const count = Number(getSelectorValue(values, variableName) ?? 1);
|
|
117
123
|
const selected = getPlural(pluralState, count, locale);
|
|
118
|
-
return resolveMessageNode(selected, values, locale, formatters);
|
|
124
|
+
return resolveMessageNode(selected, values, locale, formatters, interpolation);
|
|
119
125
|
}
|
|
120
126
|
if (typedNode.nodeType === NodeTypes.ENUMERATION) {
|
|
121
127
|
const enumerationState = typedNode[NodeTypes.ENUMERATION];
|
|
@@ -134,19 +140,19 @@ const resolveMessageNode = (node, values = {}, locale = "en", formatters) => {
|
|
|
134
140
|
const matchedKey = findMatchingCondition(options, count);
|
|
135
141
|
selected = matchedKey ? options[matchedKey] : options[getCachedIntl("PluralRules", locale).select(count)] ?? getEnumeration(options, count);
|
|
136
142
|
} else selected = options[String(selector)] ?? options.fallback ?? options.other;
|
|
137
|
-
return resolveMessageNode(selected, values, locale, formatters);
|
|
143
|
+
return resolveMessageNode(selected, values, locale, formatters, interpolation);
|
|
138
144
|
}
|
|
139
145
|
if (typedNode.nodeType === NodeTypes.SELECT) {
|
|
140
146
|
const selectState = typedNode[NodeTypes.SELECT];
|
|
141
147
|
const variableName = typeof typedNode.variable === "string" ? typedNode.variable : "value";
|
|
142
148
|
const selector = getSelectorValue(values, variableName);
|
|
143
149
|
const selected = getSelect(selectState, String(selector ?? ""));
|
|
144
|
-
return resolveMessageNode(selected, values, locale, formatters);
|
|
150
|
+
return resolveMessageNode(selected, values, locale, formatters, interpolation);
|
|
145
151
|
}
|
|
146
152
|
if (typedNode.nodeType === NodeTypes.GENDER) {
|
|
147
153
|
const genderState = typedNode[NodeTypes.GENDER];
|
|
148
154
|
const selected = genderState[String(values.gender ?? "")] ?? genderState.fallback ?? genderState.other;
|
|
149
|
-
return resolveMessageNode(selected, values, locale, formatters);
|
|
155
|
+
return resolveMessageNode(selected, values, locale, formatters, interpolation);
|
|
150
156
|
}
|
|
151
157
|
return node;
|
|
152
158
|
};
|
|
@@ -157,10 +163,11 @@ const resolveMessageNode = (node, values = {}, locale = "en", formatters) => {
|
|
|
157
163
|
* the bundle through a built dictionary: the build already converted every
|
|
158
164
|
* dialect construct into intlayer nodes, and the interpreter turned those into
|
|
159
165
|
* callables, so no message parser is involved. Plain strings still get
|
|
160
|
-
* `{{name}}` / `{name}` / `{value, number}` interpolation
|
|
166
|
+
* `{{name}}` / `{name}` / `{value, number}` interpolation (only `{{…}}` with
|
|
167
|
+
* `'double-brace'` interpolation).
|
|
161
168
|
*/
|
|
162
|
-
const resolveMessageNodeToString = (node, values = {}, locale = "en", formatters) => {
|
|
163
|
-
const resolved = resolveMessageNode(node, values, locale, formatters);
|
|
169
|
+
const resolveMessageNodeToString = (node, values = {}, locale = "en", formatters, interpolation = "default") => {
|
|
170
|
+
const resolved = resolveMessageNode(node, values, locale, formatters, interpolation);
|
|
164
171
|
return typeof resolved === "string" ? resolved : String(resolved ?? "");
|
|
165
172
|
};
|
|
166
173
|
/**
|
|
@@ -176,12 +183,18 @@ const resolveMessageNodeToString = (node, values = {}, locale = "en", formatters
|
|
|
176
183
|
* resolveIcuMessage('Hello {name}', { name: 'John' }, 'en'); // 'Hello John'
|
|
177
184
|
* ```
|
|
178
185
|
*/
|
|
179
|
-
const createMessageResolver = (formatter) => (message, values = {}, locale = "en", formatters) => resolveMessageNodeToString(typeof message === "string" ? formatter(message) : message, values, locale, formatters);
|
|
186
|
+
const createMessageResolver = (formatter, interpolation = "default") => (message, values = {}, locale = "en", formatters) => resolveMessageNodeToString(typeof message === "string" ? formatter(message) : message, values, locale, formatters, interpolation);
|
|
180
187
|
const DIALECT_FORMATTERS = {
|
|
181
188
|
icu: icuToIntlayerFormatter,
|
|
182
189
|
i18next: i18nextToIntlayerFormatter,
|
|
183
190
|
"vue-i18n": vueI18nToIntlayerFormatter
|
|
184
191
|
};
|
|
192
|
+
/** i18next reads only `{{…}}`; the other dialects use single braces too. */
|
|
193
|
+
const DIALECT_INTERPOLATION = {
|
|
194
|
+
icu: "default",
|
|
195
|
+
i18next: "double-brace",
|
|
196
|
+
"vue-i18n": "default"
|
|
197
|
+
};
|
|
185
198
|
/**
|
|
186
199
|
* Resolves a raw message — string in a library dialect, or an intlayer node
|
|
187
200
|
* tree — into a final string using interpolation values and locale.
|
|
@@ -203,7 +216,7 @@ const DIALECT_FORMATTERS = {
|
|
|
203
216
|
* // '3 items'
|
|
204
217
|
* ```
|
|
205
218
|
*/
|
|
206
|
-
const resolveMessage = (message, values = {}, locale = "en", dialect = "icu") => createMessageResolver(DIALECT_FORMATTERS[dialect])(message, values, locale);
|
|
219
|
+
const resolveMessage = (message, values = {}, locale = "en", dialect = "icu") => createMessageResolver(DIALECT_FORMATTERS[dialect], DIALECT_INTERPOLATION[dialect])(message, values, locale);
|
|
207
220
|
/**
|
|
208
221
|
* Tokenizes a message containing XML-like tags (`'Visit <link>the docs</link>'`,
|
|
209
222
|
* `'hello <1>{{name}}</1>'`, `'line<br/>break'`) into a token tree.
|