@xndrjs/i18n 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -8
- package/bin/audit.mjs +14 -0
- package/dist/index.d.ts +1 -1
- package/dist/{types-CFWVT2PP.d.ts → types-C1CpXVOJ.d.ts} +7 -1
- package/dist/validation/index.d.ts +3 -2
- package/dist/validation/index.js +125 -28
- package/dist/validation/index.js.map +1 -1
- package/package.json +5 -3
- package/src/IcuTranslationProviderSingle.test.ts +54 -0
- package/src/audit/audit-dictionaries.test.ts +134 -0
- package/src/audit/audit-dictionaries.ts +172 -0
- package/src/audit/run-audit.test.ts +136 -0
- package/src/audit/run-audit.ts +81 -0
- package/src/codegen/emit/dictionary-file.ts +7 -3
- package/src/codegen/emit/dictionary-schema-file.ts +6 -4
- package/src/codegen/emit/instance-file.ts +10 -5
- package/src/codegen/emit/namespace-loaders-file.ts +10 -6
- package/src/codegen/generate-i18n-types.test.ts +496 -0
- package/src/codegen/generate-i18n-types.ts +33 -5
- package/src/codegen/icu-analysis.ts +22 -7
- package/src/codegen/locale-policy.test.ts +35 -0
- package/src/codegen/locale-policy.ts +26 -0
- package/src/codegen/paths.ts +27 -1
- package/src/codegen/read-dictionary.test.ts +104 -0
- package/src/codegen/read-dictionary.ts +133 -0
- package/src/codegen/types.ts +5 -0
- package/src/ensure-namespace.test.ts +1 -1
- package/src/icu/extract-variables.test.ts +199 -0
- package/src/icu/extract-variables.ts +160 -20
- package/src/icu/parse-template.ts +9 -2
- package/src/setup/setup-i18n.ts +7 -4
- package/src/validation/normalize.ts +17 -10
- package/src/validation/validation.test.ts +104 -0
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { parse } from "@formatjs/icu-messageformat-parser";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import {
|
|
4
|
+
extractVariableMeta,
|
|
5
|
+
inferMergedVariableType,
|
|
6
|
+
mergeVariableMetaAcrossLocales,
|
|
7
|
+
variableMetaToSpec,
|
|
8
|
+
type VariableRole,
|
|
9
|
+
} from "./extract-variables.js";
|
|
10
|
+
|
|
11
|
+
function metaFor(template: string) {
|
|
12
|
+
return extractVariableMeta(parse(template));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function mergeTemplates(...templates: string[]) {
|
|
16
|
+
return mergeVariableMetaAcrossLocales(templates.map((template) => metaFor(template)));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function roles(...items: VariableRole[]) {
|
|
20
|
+
return new Set(items);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
describe("inferMergedVariableType", () => {
|
|
24
|
+
describe("string family", () => {
|
|
25
|
+
it.each<[VariableRole[], "string"]>([
|
|
26
|
+
[["simple"], "string"],
|
|
27
|
+
[["select"], "string"],
|
|
28
|
+
[["simple", "select"], "string"],
|
|
29
|
+
])("accepts %j → %s", (items, expected) => {
|
|
30
|
+
expect(inferMergedVariableType(roles(...items))).toBe(expected);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe("number family", () => {
|
|
35
|
+
it.each<[VariableRole[], "number"]>([
|
|
36
|
+
[["plural"], "number"],
|
|
37
|
+
[["selectordinal"], "number"],
|
|
38
|
+
[["number"], "number"],
|
|
39
|
+
[["simple", "plural"], "number"],
|
|
40
|
+
[["simple", "selectordinal"], "number"],
|
|
41
|
+
[["simple", "number"], "number"],
|
|
42
|
+
[["plural", "number"], "number"],
|
|
43
|
+
[["selectordinal", "number"], "number"],
|
|
44
|
+
])("accepts %j → %s", (items, expected) => {
|
|
45
|
+
expect(inferMergedVariableType(roles(...items))).toBe(expected);
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
describe("date family", () => {
|
|
50
|
+
it.each<[VariableRole[], "date"]>([
|
|
51
|
+
[["date"], "date"],
|
|
52
|
+
[["time"], "date"],
|
|
53
|
+
[["date", "time"], "date"],
|
|
54
|
+
])("accepts %j → %s", (items, expected) => {
|
|
55
|
+
expect(inferMergedVariableType(roles(...items))).toBe(expected);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("conflicts", () => {
|
|
60
|
+
it.each<{ label: string; roles: VariableRole[] }>([
|
|
61
|
+
{ label: "plural + select", roles: ["plural", "select"] },
|
|
62
|
+
{ label: "selectordinal + select", roles: ["selectordinal", "select"] },
|
|
63
|
+
{ label: "plural + selectordinal", roles: ["plural", "selectordinal"] },
|
|
64
|
+
{ label: "select + number", roles: ["select", "number"] },
|
|
65
|
+
{ label: "select + simple + number", roles: ["select", "simple", "number"] },
|
|
66
|
+
{ label: "plural + simple + select", roles: ["plural", "simple", "select"] },
|
|
67
|
+
{ label: "date + simple", roles: ["date", "simple"] },
|
|
68
|
+
{ label: "date + plural", roles: ["date", "plural"] },
|
|
69
|
+
{ label: "time + select", roles: ["time", "select"] },
|
|
70
|
+
{ label: "date + number", roles: ["date", "number"] },
|
|
71
|
+
])("rejects $label", ({ roles: roleList }) => {
|
|
72
|
+
expect(inferMergedVariableType(roles(...roleList))).toBe("CONFLICT");
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
describe("mergeVariableMetaAcrossLocales", () => {
|
|
78
|
+
describe("compatible cross-locale merges", () => {
|
|
79
|
+
it("infers string for simple interpolation in every locale", () => {
|
|
80
|
+
const result = mergeTemplates("Welcome {name}!", "Benvenuto {name}!");
|
|
81
|
+
|
|
82
|
+
expect(result).toEqual({ ok: true, merged: { name: "string" } });
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("infers string when one locale uses select and another uses simple interpolation", () => {
|
|
86
|
+
const result = mergeTemplates(
|
|
87
|
+
"{gender, select, female {she} other {they}}",
|
|
88
|
+
"Pronome: {gender}"
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
expect(result).toEqual({ ok: true, merged: { gender: "string" } });
|
|
92
|
+
expect(variableMetaToSpec(metaFor("Pronome: {gender}"))).toEqual({ gender: "string" });
|
|
93
|
+
expect(variableMetaToSpec(metaFor("{gender, select, other {x}}"))).toEqual({
|
|
94
|
+
gender: "string",
|
|
95
|
+
});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("infers number when English uses plural and Italian uses simple interpolation", () => {
|
|
99
|
+
const result = mergeTemplates(
|
|
100
|
+
"You have {count, plural, one {1 invoice} other {# invoices}}",
|
|
101
|
+
"Hai {count} fatture"
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
expect(result).toEqual({ ok: true, merged: { count: "number" } });
|
|
105
|
+
expect(variableMetaToSpec(metaFor("Hai {count} fatture"))).toEqual({ count: "string" });
|
|
106
|
+
expect(
|
|
107
|
+
variableMetaToSpec(metaFor("You have {count, plural, one {1 invoice} other {# invoices}}"))
|
|
108
|
+
).toEqual({ count: "number" });
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("infers number when one locale uses selectordinal and another uses simple interpolation", () => {
|
|
112
|
+
const result = mergeTemplates(
|
|
113
|
+
"You finished {position, selectordinal, one {#st} other {#th}}",
|
|
114
|
+
"Sei al {position} posto"
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
expect(result).toEqual({ ok: true, merged: { position: "number" } });
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("infers number when one locale uses plural and another uses number format", () => {
|
|
121
|
+
const result = mergeTemplates(
|
|
122
|
+
"{amount, plural, one {1} other {#}}",
|
|
123
|
+
"Totale: {amount, number}"
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
expect(result).toEqual({ ok: true, merged: { amount: "number" } });
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("infers date when locales use date and time on the same variable", () => {
|
|
130
|
+
const result = mergeTemplates("{due, date, short}", "{due, time, short}");
|
|
131
|
+
|
|
132
|
+
expect(result).toEqual({ ok: true, merged: { due: "date" } });
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe("incompatible cross-locale merges", () => {
|
|
137
|
+
it("rejects plural and select on the same variable", () => {
|
|
138
|
+
const result = mergeTemplates(
|
|
139
|
+
"{myVar, select, other {x}}",
|
|
140
|
+
"{myVar, plural, one {1} other {#}}"
|
|
141
|
+
);
|
|
142
|
+
|
|
143
|
+
expect(result.ok).toBe(false);
|
|
144
|
+
if (!result.ok) {
|
|
145
|
+
expect(result.message).toContain('Incompatible ICU variable "myVar"');
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it("rejects plural and selectordinal on the same variable", () => {
|
|
150
|
+
const result = mergeTemplates(
|
|
151
|
+
"{rank, plural, one {1} other {#}}",
|
|
152
|
+
"{rank, selectordinal, one {#st} other {#th}}"
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
expect(result.ok).toBe(false);
|
|
156
|
+
if (!result.ok) {
|
|
157
|
+
expect(result.message).toContain('Incompatible ICU variable "rank"');
|
|
158
|
+
expect(result.message).toContain("plural");
|
|
159
|
+
expect(result.message).toContain("selectordinal");
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("rejects select and number format on the same variable", () => {
|
|
164
|
+
const result = mergeTemplates("{value, select, other {x}}", "Valore: {value, number}");
|
|
165
|
+
|
|
166
|
+
expect(result.ok).toBe(false);
|
|
167
|
+
if (!result.ok) {
|
|
168
|
+
expect(result.message).toContain('Incompatible ICU variable "value"');
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("rejects date and simple interpolation on the same variable", () => {
|
|
173
|
+
const result = mergeTemplates("{due, date, short}", "Scadenza {due}");
|
|
174
|
+
|
|
175
|
+
expect(result.ok).toBe(false);
|
|
176
|
+
if (!result.ok) {
|
|
177
|
+
expect(result.message).toContain('Incompatible ICU variable "due"');
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it("rejects different variable names across locales", () => {
|
|
182
|
+
const result = mergeTemplates("Welcome {name}!", "Benvenuto {nome}!");
|
|
183
|
+
|
|
184
|
+
expect(result.ok).toBe(false);
|
|
185
|
+
if (!result.ok) {
|
|
186
|
+
expect(result.message).toContain("Inconsistent ICU variable names across locales");
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
it("rejects when one locale omits a variable present in another", () => {
|
|
191
|
+
const result = mergeTemplates("Welcome {name}!", "{name} and {count}");
|
|
192
|
+
|
|
193
|
+
expect(result.ok).toBe(false);
|
|
194
|
+
if (!result.ok) {
|
|
195
|
+
expect(result.message).toContain("Inconsistent ICU variable names across locales");
|
|
196
|
+
}
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -3,40 +3,79 @@ import type { parse } from "@formatjs/icu-messageformat-parser";
|
|
|
3
3
|
export type VariableType = "string" | "number" | "date";
|
|
4
4
|
export type VariableSpec = Record<string, VariableType>;
|
|
5
5
|
|
|
6
|
+
export type VariableRole =
|
|
7
|
+
| "simple"
|
|
8
|
+
| "plural"
|
|
9
|
+
| "selectordinal"
|
|
10
|
+
| "select"
|
|
11
|
+
| "number"
|
|
12
|
+
| "date"
|
|
13
|
+
| "time";
|
|
14
|
+
|
|
15
|
+
export type VariableMeta = {
|
|
16
|
+
type: VariableType;
|
|
17
|
+
roles: Set<VariableRole>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type VariableMetaSpec = Record<string, VariableMeta>;
|
|
21
|
+
|
|
6
22
|
type IcuAst = ReturnType<typeof parse>;
|
|
7
23
|
type IcuNode = IcuAst[number];
|
|
8
24
|
|
|
9
|
-
function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
25
|
+
function isPluralOrOrdinalNode(node: IcuNode): node is Extract<IcuNode, { type: 6 }> {
|
|
26
|
+
return node.type === 6 && "pluralType" in node && Boolean(node.pluralType);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function hasVariableName(node: IcuNode): node is Extract<IcuNode, { value: string }> {
|
|
30
|
+
return "value" in node && typeof node.value === "string";
|
|
31
|
+
}
|
|
13
32
|
|
|
14
|
-
|
|
15
|
-
|
|
33
|
+
function addVariableMeta(
|
|
34
|
+
variables: VariableMetaSpec,
|
|
35
|
+
name: string,
|
|
36
|
+
type: VariableType,
|
|
37
|
+
role: VariableRole
|
|
38
|
+
): void {
|
|
39
|
+
const existing = variables[name];
|
|
40
|
+
if (!existing) {
|
|
41
|
+
variables[name] = { type, roles: new Set([role]) };
|
|
42
|
+
return;
|
|
16
43
|
}
|
|
17
44
|
|
|
18
|
-
|
|
45
|
+
existing.roles.add(role);
|
|
46
|
+
existing.type = mergeVariableTypes(existing.type, type);
|
|
19
47
|
}
|
|
20
48
|
|
|
21
|
-
function
|
|
22
|
-
|
|
49
|
+
function mergeVariableTypes(left: VariableType, right: VariableType): VariableType {
|
|
50
|
+
if (left === right) {
|
|
51
|
+
return left;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if ((left === "string" && right === "number") || (left === "number" && right === "string")) {
|
|
55
|
+
return "number";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return right;
|
|
23
59
|
}
|
|
24
60
|
|
|
25
|
-
export function
|
|
26
|
-
const variables:
|
|
61
|
+
export function extractVariableMeta(nodes: IcuAst): VariableMetaSpec {
|
|
62
|
+
const variables: VariableMetaSpec = {};
|
|
27
63
|
|
|
28
64
|
const walk = (walkNodes: IcuAst) => {
|
|
29
65
|
for (const node of walkNodes) {
|
|
30
|
-
if (node.type === 1) {
|
|
31
|
-
variables
|
|
32
|
-
} else if (
|
|
33
|
-
variables
|
|
34
|
-
} else if ((node
|
|
35
|
-
|
|
66
|
+
if (node.type === 1 && hasVariableName(node)) {
|
|
67
|
+
addVariableMeta(variables, node.value, "string", "simple");
|
|
68
|
+
} else if (node.type === 2 && hasVariableName(node)) {
|
|
69
|
+
addVariableMeta(variables, node.value, "number", "number");
|
|
70
|
+
} else if (isPluralOrOrdinalNode(node) && hasVariableName(node)) {
|
|
71
|
+
const role = node.pluralType === "ordinal" ? "selectordinal" : "plural";
|
|
72
|
+
addVariableMeta(variables, node.value, "number", role);
|
|
73
|
+
} else if (node.type === 3 && hasVariableName(node)) {
|
|
74
|
+
addVariableMeta(variables, node.value, "date", "date");
|
|
75
|
+
} else if (node.type === 4 && hasVariableName(node)) {
|
|
76
|
+
addVariableMeta(variables, node.value, "date", "time");
|
|
36
77
|
} else if (node.type === 5 && hasVariableName(node)) {
|
|
37
|
-
variables
|
|
38
|
-
} else if (node.type === 6 && hasVariableName(node)) {
|
|
39
|
-
variables[node.value] = variables[node.value] ?? "string";
|
|
78
|
+
addVariableMeta(variables, node.value, "string", "select");
|
|
40
79
|
}
|
|
41
80
|
|
|
42
81
|
if ("options" in node && node.options) {
|
|
@@ -55,6 +94,107 @@ export function extractVariables(nodes: IcuAst): VariableSpec {
|
|
|
55
94
|
return variables;
|
|
56
95
|
}
|
|
57
96
|
|
|
97
|
+
export function variableMetaToSpec(meta: VariableMetaSpec): VariableSpec {
|
|
98
|
+
return Object.fromEntries(Object.entries(meta).map(([name, entry]) => [name, entry.type]));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export function extractVariables(nodes: IcuAst): VariableSpec {
|
|
102
|
+
return variableMetaToSpec(extractVariableMeta(nodes));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function inferMergedVariableType(roles: Set<VariableRole>): VariableType | "CONFLICT" {
|
|
106
|
+
const hasPlural = roles.has("plural");
|
|
107
|
+
const hasSelectordinal = roles.has("selectordinal");
|
|
108
|
+
const hasSelect = roles.has("select");
|
|
109
|
+
const hasSimple = roles.has("simple");
|
|
110
|
+
const hasNumber = roles.has("number");
|
|
111
|
+
const hasDate = roles.has("date");
|
|
112
|
+
const hasTime = roles.has("time");
|
|
113
|
+
|
|
114
|
+
// Incompatible numeric ICU constructs on the same variable.
|
|
115
|
+
if (hasPlural && hasSelectordinal) {
|
|
116
|
+
return "CONFLICT";
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Select (string-family) vs plural / ordinal / number-format (number-family).
|
|
120
|
+
if (hasSelect && (hasPlural || hasSelectordinal || hasNumber)) {
|
|
121
|
+
return "CONFLICT";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Legacy explicit pairs — covered by the rule above, kept for clarity.
|
|
125
|
+
if (hasPlural && hasSelect) {
|
|
126
|
+
return "CONFLICT";
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (hasSelectordinal && hasSelect) {
|
|
130
|
+
return "CONFLICT";
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (hasDate || hasTime) {
|
|
134
|
+
if (hasPlural || hasSelect || hasSelectordinal || hasSimple || hasNumber) {
|
|
135
|
+
return "CONFLICT";
|
|
136
|
+
}
|
|
137
|
+
return "date";
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (hasPlural || hasSelectordinal || hasNumber) {
|
|
141
|
+
return "number";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (hasSelect || hasSimple) {
|
|
145
|
+
return "string";
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return "string";
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function mergeVariableMetaAcrossLocales(
|
|
152
|
+
localeMetas: readonly VariableMetaSpec[]
|
|
153
|
+
): { ok: true; merged: VariableSpec } | { ok: false; message: string } {
|
|
154
|
+
const keySignatures = localeMetas.map((meta) => Object.keys(meta).sort().join(","));
|
|
155
|
+
if (new Set(keySignatures).size > 1) {
|
|
156
|
+
return {
|
|
157
|
+
ok: false,
|
|
158
|
+
message: `Inconsistent ICU variable names across locales (keys: ${keySignatures.join(" vs ")})`,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const merged: VariableSpec = {};
|
|
163
|
+
const variableNames = new Set(localeMetas.flatMap((meta) => Object.keys(meta)));
|
|
164
|
+
|
|
165
|
+
for (const variableName of variableNames) {
|
|
166
|
+
const combinedRoles = new Set<VariableRole>();
|
|
167
|
+
|
|
168
|
+
for (const meta of localeMetas) {
|
|
169
|
+
for (const role of meta[variableName]?.roles ?? []) {
|
|
170
|
+
combinedRoles.add(role);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const mergedType = inferMergedVariableType(combinedRoles);
|
|
175
|
+
if (mergedType === "CONFLICT") {
|
|
176
|
+
const rolesByLocale = localeMetas
|
|
177
|
+
.map((meta) => {
|
|
178
|
+
const entry = meta[variableName];
|
|
179
|
+
if (!entry) {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
return [...entry.roles].sort().join("|");
|
|
183
|
+
})
|
|
184
|
+
.filter((value): value is string => value !== null);
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
ok: false,
|
|
188
|
+
message: `Incompatible ICU variable "${variableName}" across locales (roles: ${rolesByLocale.join(" vs ")})`,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
merged[variableName] = mergedType;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return { ok: true, merged };
|
|
196
|
+
}
|
|
197
|
+
|
|
58
198
|
export function mergeVariableSpecs(target: VariableSpec, source: VariableSpec): VariableSpec {
|
|
59
199
|
const merged = { ...target };
|
|
60
200
|
|
|
@@ -1,9 +1,15 @@
|
|
|
1
1
|
import { parse } from "@formatjs/icu-messageformat-parser";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
extractVariableMeta,
|
|
4
|
+
variableMetaToSpec,
|
|
5
|
+
type VariableMetaSpec,
|
|
6
|
+
type VariableSpec,
|
|
7
|
+
} from "./extract-variables.js";
|
|
3
8
|
|
|
4
9
|
export type ParseTemplateSuccess = {
|
|
5
10
|
readonly ok: true;
|
|
6
11
|
readonly args: VariableSpec;
|
|
12
|
+
readonly meta: VariableMetaSpec;
|
|
7
13
|
};
|
|
8
14
|
|
|
9
15
|
export type ParseTemplateFailure = {
|
|
@@ -16,7 +22,8 @@ export type ParseTemplateResult = ParseTemplateSuccess | ParseTemplateFailure;
|
|
|
16
22
|
export function parseTemplate(template: string): ParseTemplateResult {
|
|
17
23
|
try {
|
|
18
24
|
const ast = parse(template);
|
|
19
|
-
|
|
25
|
+
const meta = extractVariableMeta(ast);
|
|
26
|
+
return { ok: true, args: variableMetaToSpec(meta), meta };
|
|
20
27
|
} catch (error) {
|
|
21
28
|
const message = error instanceof Error ? error.message : String(error);
|
|
22
29
|
return { ok: false, message };
|
package/src/setup/setup-i18n.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import fs from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
|
+
import { DEFAULT_IMPORT_EXTENSION, importExtensionSuffix } from "../codegen/paths.js";
|
|
4
5
|
import { inferProjectName, typeNamesForProject } from "./type-names.js";
|
|
5
6
|
|
|
6
7
|
export type SetupMode = "single" | "multi";
|
|
@@ -28,11 +29,13 @@ const DEFAULT_STARTER = {
|
|
|
28
29
|
welcome: { en: "Welcome {name}!" },
|
|
29
30
|
};
|
|
30
31
|
|
|
32
|
+
const DEFAULT_IMPORT_SUFFIX = importExtensionSuffix(DEFAULT_IMPORT_EXTENSION);
|
|
33
|
+
|
|
31
34
|
const INDEX_TS =
|
|
32
|
-
`import { createI18n } from "./generated/instance.generated
|
|
33
|
-
`export * from "./generated/instance.generated
|
|
34
|
-
`export * from "./generated/dictionary.generated
|
|
35
|
-
`export * from "./generated/i18n-types.generated
|
|
35
|
+
`import { createI18n } from "./generated/instance.generated${DEFAULT_IMPORT_SUFFIX}";\n\n` +
|
|
36
|
+
`export * from "./generated/instance.generated${DEFAULT_IMPORT_SUFFIX}";\n` +
|
|
37
|
+
`export * from "./generated/dictionary.generated${DEFAULT_IMPORT_SUFFIX}";\n` +
|
|
38
|
+
`export * from "./generated/i18n-types.generated${DEFAULT_IMPORT_SUFFIX}";\n\n` +
|
|
36
39
|
`export const i18n = createI18n();\n`;
|
|
37
40
|
|
|
38
41
|
function writeJson(filePath: string, value: unknown): void {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mergeVariableMetaAcrossLocales, type VariableMetaSpec } from "../icu/extract-variables.js";
|
|
2
2
|
import { parseTemplate } from "../icu/parse-template.js";
|
|
3
3
|
import type {
|
|
4
4
|
DictionarySpec,
|
|
@@ -45,8 +45,9 @@ function normalizeKeyDictionary(
|
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
const locales: Record<string, ParsedKeyEntry["locales"][string]> = {};
|
|
48
|
+
const localeMetas: VariableMetaSpec[] = [];
|
|
48
49
|
let mergedArgs: ParsedKeyEntry["mergedArgs"] = {};
|
|
49
|
-
let
|
|
50
|
+
let localeArgsMismatch: ValidationIssue | undefined;
|
|
50
51
|
|
|
51
52
|
for (const [locale, template] of Object.entries(keyValue)) {
|
|
52
53
|
const localePath = [...keyPathPrefix, key, locale];
|
|
@@ -71,25 +72,31 @@ function normalizeKeyDictionary(
|
|
|
71
72
|
}
|
|
72
73
|
|
|
73
74
|
locales[locale] = { template, args: parsed.args };
|
|
75
|
+
localeMetas.push(parsed.meta);
|
|
76
|
+
}
|
|
74
77
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
} else if (!variableSpecsEqual(mergedArgs, parsed.args)) {
|
|
78
|
+
if (localeMetas.length > 0) {
|
|
79
|
+
const merged = mergeVariableMetaAcrossLocales(localeMetas);
|
|
80
|
+
if (!merged.ok) {
|
|
79
81
|
const localeArgs: Record<string, ParsedKeyEntry["mergedArgs"]> = {};
|
|
80
82
|
for (const [loc, entry] of Object.entries(locales)) {
|
|
81
83
|
localeArgs[loc] = entry.args;
|
|
82
84
|
}
|
|
83
|
-
|
|
85
|
+
localeArgsMismatch = {
|
|
84
86
|
kind: "locale_args_mismatch",
|
|
85
87
|
path: [...keyPathPrefix, key],
|
|
86
88
|
locales: localeArgs,
|
|
87
|
-
message:
|
|
88
|
-
}
|
|
89
|
-
|
|
89
|
+
message: merged.message,
|
|
90
|
+
};
|
|
91
|
+
} else {
|
|
92
|
+
mergedArgs = merged.merged;
|
|
90
93
|
}
|
|
91
94
|
}
|
|
92
95
|
|
|
96
|
+
if (localeArgsMismatch) {
|
|
97
|
+
issues.push(localeArgsMismatch);
|
|
98
|
+
}
|
|
99
|
+
|
|
93
100
|
keys[key] = { locales, mergedArgs };
|
|
94
101
|
}
|
|
95
102
|
|
|
@@ -87,6 +87,110 @@ describe("normalizeDictionary", () => {
|
|
|
87
87
|
expect(result.issues[0]?.kind).toBe("locale_args_mismatch");
|
|
88
88
|
}
|
|
89
89
|
});
|
|
90
|
+
|
|
91
|
+
it("accepts plural in one locale and simple interpolation in another for the same variable", () => {
|
|
92
|
+
const result = normalizeDictionary(
|
|
93
|
+
{
|
|
94
|
+
login_button: { en: "Login" },
|
|
95
|
+
welcome: { en: "Welcome {name}!" },
|
|
96
|
+
invoice_count: {
|
|
97
|
+
en: "You have {count, plural, one {1 invoice} other {# invoices}}",
|
|
98
|
+
it: "Hai {count} fatture",
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
spec
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
expect(result.ok).toBe(true);
|
|
105
|
+
if (result.ok && result.data.mode === "single") {
|
|
106
|
+
expect(result.data.keys.invoice_count?.mergedArgs).toEqual({ count: "number" });
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("rejects plural and select on the same variable across locales", () => {
|
|
111
|
+
const result = normalizeDictionary(
|
|
112
|
+
{
|
|
113
|
+
login_button: { en: "Login" },
|
|
114
|
+
welcome: { en: "Welcome {name}!" },
|
|
115
|
+
invoice_count: {
|
|
116
|
+
en: "{count, select, other {{count}}}",
|
|
117
|
+
it: "{count, plural, one {1} other {#}}",
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
spec
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
expect(result.ok).toBe(false);
|
|
124
|
+
if (!result.ok) {
|
|
125
|
+
expect(result.issues.some((issue) => issue.kind === "locale_args_mismatch")).toBe(true);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it("rejects plural and selectordinal on the same variable across locales", () => {
|
|
130
|
+
const result = normalizeDictionary(
|
|
131
|
+
{
|
|
132
|
+
login_button: { en: "Login" },
|
|
133
|
+
welcome: { en: "Welcome {name}!" },
|
|
134
|
+
invoice_count: {
|
|
135
|
+
en: "{count, plural, one {1} other {#}}",
|
|
136
|
+
it: "{count, selectordinal, one {#°} other {#°}}",
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
spec
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(result.ok).toBe(false);
|
|
143
|
+
if (!result.ok) {
|
|
144
|
+
expect(result.issues.some((issue) => issue.kind === "locale_args_mismatch")).toBe(true);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it("rejects select and number format on the same variable across locales", () => {
|
|
149
|
+
const result = normalizeDictionary(
|
|
150
|
+
{
|
|
151
|
+
login_button: { en: "Login" },
|
|
152
|
+
welcome: { en: "Welcome {name}!" },
|
|
153
|
+
invoice_count: {
|
|
154
|
+
en: "{count, select, other {x}}",
|
|
155
|
+
it: "{count, number}",
|
|
156
|
+
},
|
|
157
|
+
},
|
|
158
|
+
spec
|
|
159
|
+
);
|
|
160
|
+
|
|
161
|
+
expect(result.ok).toBe(false);
|
|
162
|
+
if (!result.ok) {
|
|
163
|
+
expect(result.issues.some((issue) => issue.kind === "locale_args_mismatch")).toBe(true);
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("accepts select in one locale and simple interpolation in another", () => {
|
|
168
|
+
const result = normalizeDictionary(
|
|
169
|
+
{
|
|
170
|
+
login_button: { en: "Login" },
|
|
171
|
+
welcome: {
|
|
172
|
+
en: "{gender, select, female {she} other {they}}",
|
|
173
|
+
it: "Pronome: {gender}",
|
|
174
|
+
},
|
|
175
|
+
invoice_count: {
|
|
176
|
+
en: "You have {count, plural, one {1 invoice} other {# invoices}}",
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
...spec,
|
|
181
|
+
requiredKeys: ["login_button", "welcome", "invoice_count"],
|
|
182
|
+
argsByKey: {
|
|
183
|
+
...spec.argsByKey,
|
|
184
|
+
welcome: { gender: "string" },
|
|
185
|
+
},
|
|
186
|
+
}
|
|
187
|
+
);
|
|
188
|
+
|
|
189
|
+
expect(result.ok).toBe(true);
|
|
190
|
+
if (result.ok && result.data.mode === "single") {
|
|
191
|
+
expect(result.data.keys.welcome?.mergedArgs).toEqual({ gender: "string" });
|
|
192
|
+
}
|
|
193
|
+
});
|
|
90
194
|
});
|
|
91
195
|
|
|
92
196
|
describe("validateNormalizedDictionary", () => {
|