@verbaly/compiler 0.29.0 → 0.31.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 +40 -7
- package/dist/cli.js +350 -82
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +32 -3
- package/dist/index.js +358 -90
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/cli.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { parseArgs } from "node:util";
|
|
3
3
|
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
4
4
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
5
|
-
import { RICH_TAGS, createVerbaly, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
5
|
+
import { RICH_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import { glob } from "tinyglobby";
|
|
8
8
|
import { parse as parse$1 } from "@babel/parser";
|
|
@@ -51,12 +51,175 @@ function writeCatalog(cfg, locale, catalog) {
|
|
|
51
51
|
return serialized;
|
|
52
52
|
}
|
|
53
53
|
//#endregion
|
|
54
|
+
//#region src/validate.ts
|
|
55
|
+
const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
|
|
56
|
+
"zero",
|
|
57
|
+
"one",
|
|
58
|
+
"two",
|
|
59
|
+
"few",
|
|
60
|
+
"many"
|
|
61
|
+
]);
|
|
62
|
+
const EXACT = /^=\d+$/;
|
|
63
|
+
function shapes(message) {
|
|
64
|
+
const out = /* @__PURE__ */ new Map();
|
|
65
|
+
walk$1(parse(message), out);
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function walk$1(nodes, out) {
|
|
69
|
+
for (const node of nodes) {
|
|
70
|
+
if (node.kind !== "param" || !node.variants) continue;
|
|
71
|
+
const selectors = node.variants.map(([key]) => key);
|
|
72
|
+
if (!out.has(node.name)) out.set(node.name, {
|
|
73
|
+
selectors,
|
|
74
|
+
plural: selectors.some((key) => PLURAL_CATEGORIES.has(key)),
|
|
75
|
+
ordinal: node.ordinal === true
|
|
76
|
+
});
|
|
77
|
+
for (const [, body] of node.variants) walk$1(body, out);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
function paramNames(message) {
|
|
81
|
+
const names = /* @__PURE__ */ new Set();
|
|
82
|
+
collect(parse(message), names);
|
|
83
|
+
return [...names].sort();
|
|
84
|
+
}
|
|
85
|
+
function collect(nodes, out) {
|
|
86
|
+
for (const node of nodes) {
|
|
87
|
+
if (node.kind !== "param") continue;
|
|
88
|
+
out.add(node.name);
|
|
89
|
+
if (node.variants) for (const [, body] of node.variants) collect(body, out);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
93
|
+
function markupNames(message) {
|
|
94
|
+
const out = /* @__PURE__ */ new Set();
|
|
95
|
+
for (const match of message.matchAll(TAG)) if (match[1] || match[3]) out.add(match[2]);
|
|
96
|
+
return out;
|
|
97
|
+
}
|
|
98
|
+
function tagTokens(message, markup) {
|
|
99
|
+
const out = [];
|
|
100
|
+
for (const match of message.matchAll(TAG)) if (markup.has(match[2])) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
101
|
+
return out.sort();
|
|
102
|
+
}
|
|
103
|
+
function surplus(a, b) {
|
|
104
|
+
const rest = [...b];
|
|
105
|
+
const out = [];
|
|
106
|
+
for (const item of a) {
|
|
107
|
+
const at = rest.indexOf(item);
|
|
108
|
+
if (at < 0) out.push(item);
|
|
109
|
+
else rest.splice(at, 1);
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
function list(items) {
|
|
114
|
+
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
115
|
+
}
|
|
116
|
+
function validateMessage(message, locale) {
|
|
117
|
+
const issues = [];
|
|
118
|
+
for (const [name, shape] of shapes(message)) {
|
|
119
|
+
if (!shape.plural) continue;
|
|
120
|
+
if (!shape.selectors.includes("other")) {
|
|
121
|
+
issues.push({
|
|
122
|
+
severity: "error",
|
|
123
|
+
message: `the plural block for {${name}} has no "other" case, so any count it does not list renders empty`
|
|
124
|
+
});
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
if (!locale) continue;
|
|
128
|
+
const missing = reachableCategories(locale, shape.ordinal).filter((category) => category !== "other" && !shape.selectors.includes(category));
|
|
129
|
+
if (missing.length > 0) issues.push({
|
|
130
|
+
severity: "warning",
|
|
131
|
+
message: `${locale} also needs the ${list(missing)} plural ${missing.length > 1 ? "forms" : "form"} for {${name}}`
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
return issues;
|
|
135
|
+
}
|
|
136
|
+
function validatePair(source, translated) {
|
|
137
|
+
const issues = [];
|
|
138
|
+
const lostParams = surplus(paramNames(source), paramNames(translated));
|
|
139
|
+
const newParams = surplus(paramNames(translated), paramNames(source));
|
|
140
|
+
if (lostParams.length > 0) issues.push({
|
|
141
|
+
severity: "error",
|
|
142
|
+
message: `${list(lostParams.map((name) => `{${name}}`))} ${lostParams.length > 1 ? "are" : "is"} missing, so ${lostParams.length > 1 ? "their values" : "its value"} never reaches the text`
|
|
143
|
+
});
|
|
144
|
+
if (newParams.length > 0) issues.push({
|
|
145
|
+
severity: "error",
|
|
146
|
+
message: `${list(newParams.map((name) => `{${name}}`))} ${newParams.length > 1 ? "are" : "is"} not in the source message, so ${newParams.length > 1 ? "they render" : "it renders"} as literal text`
|
|
147
|
+
});
|
|
148
|
+
const markup = /* @__PURE__ */ new Set([...markupNames(source), ...markupNames(translated)]);
|
|
149
|
+
const lostTags = tagReport(surplus(tagTokens(source, markup), tagTokens(translated, markup)));
|
|
150
|
+
const newTags = tagReport(surplus(tagTokens(translated, markup), tagTokens(source, markup)));
|
|
151
|
+
if (lostTags.length > 0) issues.push({
|
|
152
|
+
severity: "error",
|
|
153
|
+
message: `${list(lostTags)} ${lostTags.length > 1 ? "are" : "is"} missing from the translation`
|
|
154
|
+
});
|
|
155
|
+
if (newTags.length > 0) issues.push({
|
|
156
|
+
severity: "error",
|
|
157
|
+
message: `${list(newTags)} ${newTags.length > 1 ? "are" : "is"} not in the source message`
|
|
158
|
+
});
|
|
159
|
+
const target = shapes(translated);
|
|
160
|
+
for (const [name, shape] of shapes(source)) {
|
|
161
|
+
const other = target.get(name);
|
|
162
|
+
if (!other) {
|
|
163
|
+
issues.push({
|
|
164
|
+
severity: "error",
|
|
165
|
+
message: `the ${shape.plural ? "plural" : "select"} block for {${name}} was flattened into plain text`
|
|
166
|
+
});
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
if (shape.selectors.includes("other") && !other.selectors.includes("other") && !other.plural) issues.push({
|
|
170
|
+
severity: "error",
|
|
171
|
+
message: `the "other" case for {${name}} is missing, so unlisted values render empty`
|
|
172
|
+
});
|
|
173
|
+
const lostExact = shape.selectors.filter((key) => EXACT.test(key)).filter((key) => !other.selectors.includes(key));
|
|
174
|
+
if (lostExact.length > 0) issues.push({
|
|
175
|
+
severity: "warning",
|
|
176
|
+
message: `the ${list(lostExact)} ${lostExact.length > 1 ? "cases" : "case"} for {${name}} ${lostExact.length > 1 ? "are" : "is"} missing, so ${lostExact.length > 1 ? "those counts" : "that count"} falls back to "other"`
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
return issues;
|
|
180
|
+
}
|
|
181
|
+
function tagReport(tokens) {
|
|
182
|
+
const out = [];
|
|
183
|
+
const seen = /* @__PURE__ */ new Set();
|
|
184
|
+
for (const token of tokens) {
|
|
185
|
+
const name = token.replaceAll("/", "");
|
|
186
|
+
if (seen.has(name)) continue;
|
|
187
|
+
seen.add(name);
|
|
188
|
+
out.push(token.endsWith("/") ? `<${name}/>` : `<${name}>`);
|
|
189
|
+
}
|
|
190
|
+
return out;
|
|
191
|
+
}
|
|
192
|
+
const COUNT_PROBE = 200;
|
|
193
|
+
const reachable = /* @__PURE__ */ new Map();
|
|
194
|
+
function reachableCategories(locale, ordinal) {
|
|
195
|
+
const type = ordinal ? "ordinal" : "cardinal";
|
|
196
|
+
const cacheKey = `${locale}\u0000${type}`;
|
|
197
|
+
const hit = reachable.get(cacheKey);
|
|
198
|
+
if (hit) return hit;
|
|
199
|
+
let out;
|
|
200
|
+
try {
|
|
201
|
+
const rules = new Intl.PluralRules(locale, { type });
|
|
202
|
+
const seen = /* @__PURE__ */ new Set();
|
|
203
|
+
for (let n = 0; n <= COUNT_PROBE; n++) seen.add(rules.select(n));
|
|
204
|
+
out = [...seen];
|
|
205
|
+
} catch {
|
|
206
|
+
out = [];
|
|
207
|
+
}
|
|
208
|
+
reachable.set(cacheKey, out);
|
|
209
|
+
return out;
|
|
210
|
+
}
|
|
211
|
+
//#endregion
|
|
54
212
|
//#region src/check.ts
|
|
213
|
+
function gatePasses(result) {
|
|
214
|
+
return result.missing.length === 0 && result.unknown.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
215
|
+
}
|
|
55
216
|
function check(cfg, catalogs, registry) {
|
|
56
|
-
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
57
217
|
const extracted = registry.messages();
|
|
218
|
+
const flat = {};
|
|
219
|
+
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
220
|
+
const source = flat[cfg.sourceLocale] ?? {};
|
|
58
221
|
const unknown = [];
|
|
59
|
-
for (const [key, files] of registry.usedKeys()) if (!(extracted.has(key) || cfg.locales.some((locale) =>
|
|
222
|
+
for (const [key, files] of registry.usedKeys()) if (!(extracted.has(key) || cfg.locales.some((locale) => flat[locale]?.[key] !== void 0))) unknown.push({
|
|
60
223
|
key,
|
|
61
224
|
files
|
|
62
225
|
});
|
|
@@ -69,16 +232,42 @@ function check(cfg, catalogs, registry) {
|
|
|
69
232
|
});
|
|
70
233
|
for (const locale of cfg.locales) {
|
|
71
234
|
if (locale === cfg.sourceLocale) continue;
|
|
72
|
-
for (const key of needed) if (!
|
|
235
|
+
for (const key of needed) if (!flat[locale]?.[key]) missing.push({
|
|
73
236
|
locale,
|
|
74
237
|
key,
|
|
75
238
|
source: source[key] ?? extracted.get(key)?.message
|
|
76
239
|
});
|
|
77
240
|
}
|
|
241
|
+
const broken = [];
|
|
242
|
+
const add = (locale, key, issues) => {
|
|
243
|
+
for (const issue of issues) broken.push({
|
|
244
|
+
locale,
|
|
245
|
+
key,
|
|
246
|
+
severity: issue.severity,
|
|
247
|
+
issue: issue.message
|
|
248
|
+
});
|
|
249
|
+
};
|
|
250
|
+
const sourceText = { ...source };
|
|
251
|
+
for (const [key, entry] of extracted) if (!sourceText[key]) sourceText[key] = entry.message;
|
|
252
|
+
for (const [key, text] of Object.entries(sourceText)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
253
|
+
for (const locale of cfg.locales) {
|
|
254
|
+
if (locale === cfg.sourceLocale) continue;
|
|
255
|
+
for (const [key, translated] of Object.entries(flat[locale] ?? {})) {
|
|
256
|
+
if (!translated) continue;
|
|
257
|
+
add(locale, key, validateMessage(translated, locale));
|
|
258
|
+
const text = sourceText[key];
|
|
259
|
+
if (text) add(locale, key, validatePair(text, translated));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
78
262
|
return {
|
|
79
|
-
ok:
|
|
263
|
+
ok: gatePasses({
|
|
264
|
+
missing,
|
|
265
|
+
unknown,
|
|
266
|
+
broken
|
|
267
|
+
}),
|
|
80
268
|
missing,
|
|
81
|
-
unknown
|
|
269
|
+
unknown,
|
|
270
|
+
broken
|
|
82
271
|
};
|
|
83
272
|
}
|
|
84
273
|
function formatCheckResult(result) {
|
|
@@ -94,8 +283,23 @@ function formatCheckResult(result) {
|
|
|
94
283
|
lines.push("unknown keys (not in any catalog):");
|
|
95
284
|
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.join(", ")})`);
|
|
96
285
|
}
|
|
286
|
+
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
287
|
+
if (errors.length > 0) {
|
|
288
|
+
lines.push("broken translations:");
|
|
289
|
+
for (const entry of errors) lines.push(` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
290
|
+
}
|
|
97
291
|
return lines.join("\n");
|
|
98
292
|
}
|
|
293
|
+
function checkNextSteps(result) {
|
|
294
|
+
const steps = [];
|
|
295
|
+
if (result.missing.length > 0) steps.push("missing: run `npx verbaly extract` to scaffold the keys, then fill them (or run `npx verbaly translate`)");
|
|
296
|
+
if (result.unknown.length > 0) steps.push("unknown keys: the key used in the code is in no catalog, fix the key or run `npx verbaly extract`");
|
|
297
|
+
if (result.broken.some((entry) => entry.severity === "error")) steps.push("broken: a translation has to keep the params, tags and plural cases of its source message");
|
|
298
|
+
return steps.join("\n");
|
|
299
|
+
}
|
|
300
|
+
function formatCheckWarnings(result) {
|
|
301
|
+
return result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`).join("\n");
|
|
302
|
+
}
|
|
99
303
|
function truncate(text, max) {
|
|
100
304
|
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
101
305
|
}
|
|
@@ -136,6 +340,19 @@ function githubCheckAnnotations(result, registry, root) {
|
|
|
136
340
|
const file = entry.files[0];
|
|
137
341
|
lines.push(file ? `::error file=${escapeProperty(relative(root, file).replaceAll("\\", "/"))}::${text}` : `::error::${text}`);
|
|
138
342
|
}
|
|
343
|
+
for (const entry of result.broken) {
|
|
344
|
+
const origin = messages.get(entry.key);
|
|
345
|
+
const text = escapeData(`[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
346
|
+
const command = entry.severity === "error" ? "error" : "warning";
|
|
347
|
+
if (!origin) {
|
|
348
|
+
lines.push(`::${command}::${text}`);
|
|
349
|
+
continue;
|
|
350
|
+
}
|
|
351
|
+
const file = relative(root, origin.file).replaceAll("\\", "/");
|
|
352
|
+
const content = readSource(origin.file);
|
|
353
|
+
const line = content === void 0 ? void 0 : lineAt(content, origin.start);
|
|
354
|
+
lines.push(`::${command} file=${escapeProperty(file)}${line ? `,line=${line}` : ""}::${text}`);
|
|
355
|
+
}
|
|
139
356
|
return lines;
|
|
140
357
|
}
|
|
141
358
|
function lineAt(content, offset) {
|
|
@@ -152,13 +369,6 @@ function escapeProperty(text) {
|
|
|
152
369
|
}
|
|
153
370
|
//#endregion
|
|
154
371
|
//#region src/params.ts
|
|
155
|
-
const PLURAL_KEYS = /* @__PURE__ */ new Set([
|
|
156
|
-
"zero",
|
|
157
|
-
"one",
|
|
158
|
-
"two",
|
|
159
|
-
"few",
|
|
160
|
-
"many"
|
|
161
|
-
]);
|
|
162
372
|
const NUMBER_FORMATS = /* @__PURE__ */ new Set([
|
|
163
373
|
"number",
|
|
164
374
|
"integer",
|
|
@@ -178,7 +388,7 @@ function visit(nodes, out) {
|
|
|
178
388
|
out.set(node.name, types);
|
|
179
389
|
if (node.variants) {
|
|
180
390
|
for (const [key, body] of node.variants) {
|
|
181
|
-
if (
|
|
391
|
+
if (PLURAL_CATEGORIES.has(key) || /^=\d+$/.test(key)) types.add("number");
|
|
182
392
|
else if (key !== "other") types.add("string");
|
|
183
393
|
visit(body, out);
|
|
184
394
|
}
|
|
@@ -947,28 +1157,80 @@ function pruneCatalogs(cfg, catalogs, registry) {
|
|
|
947
1157
|
}
|
|
948
1158
|
//#endregion
|
|
949
1159
|
//#region src/init.ts
|
|
950
|
-
const
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
1160
|
+
const HOSTS = [
|
|
1161
|
+
{
|
|
1162
|
+
name: "nuxt",
|
|
1163
|
+
dep: "nuxt",
|
|
1164
|
+
pkg: "@verbaly/nuxt",
|
|
1165
|
+
wire: "add '@verbaly/nuxt' to the modules in nuxt.config"
|
|
1166
|
+
},
|
|
1167
|
+
{
|
|
1168
|
+
name: "next",
|
|
1169
|
+
dep: "next",
|
|
1170
|
+
pkg: "@verbaly/next",
|
|
1171
|
+
wire: "wrap the export of next.config with withVerbaly"
|
|
1172
|
+
},
|
|
1173
|
+
{
|
|
1174
|
+
name: "sveltekit",
|
|
1175
|
+
dep: "@sveltejs/kit",
|
|
1176
|
+
pkg: "@verbaly/vite",
|
|
1177
|
+
wire: "add verbaly() to the plugins in vite.config (per-request locale: @verbaly/sveltekit)"
|
|
1178
|
+
},
|
|
1179
|
+
{
|
|
1180
|
+
name: "astro",
|
|
1181
|
+
dep: "astro",
|
|
1182
|
+
pkg: "@verbaly/astro",
|
|
1183
|
+
wire: "add verbaly() to the integrations in astro.config"
|
|
1184
|
+
},
|
|
1185
|
+
{
|
|
1186
|
+
name: "vite",
|
|
1187
|
+
dep: "vite",
|
|
1188
|
+
pkg: "@verbaly/vite",
|
|
1189
|
+
wire: "add verbaly() to the plugins in vite.config"
|
|
1190
|
+
},
|
|
1191
|
+
{
|
|
1192
|
+
name: "webpack",
|
|
1193
|
+
dep: "webpack",
|
|
1194
|
+
pkg: "@verbaly/unplugin",
|
|
1195
|
+
wire: "add the verbaly webpack plugin to your build config"
|
|
1196
|
+
},
|
|
1197
|
+
{
|
|
1198
|
+
name: "rspack",
|
|
1199
|
+
dep: "@rspack/core",
|
|
1200
|
+
pkg: "@verbaly/unplugin",
|
|
1201
|
+
wire: "add the verbaly rspack plugin to your build config"
|
|
1202
|
+
},
|
|
1203
|
+
{
|
|
1204
|
+
name: "rollup",
|
|
1205
|
+
dep: "rollup",
|
|
1206
|
+
pkg: "@verbaly/unplugin",
|
|
1207
|
+
wire: "add the verbaly rollup plugin to your build config"
|
|
1208
|
+
},
|
|
1209
|
+
{
|
|
1210
|
+
name: "esbuild",
|
|
1211
|
+
dep: "esbuild",
|
|
1212
|
+
pkg: "@verbaly/unplugin",
|
|
1213
|
+
wire: "add the verbaly esbuild plugin to your build config"
|
|
1214
|
+
}
|
|
957
1215
|
];
|
|
958
|
-
|
|
1216
|
+
const WIRING_PACKAGES = [...new Set(HOSTS.map((host) => host.pkg))];
|
|
1217
|
+
function readDependencies(root) {
|
|
959
1218
|
const path = join(root, "package.json");
|
|
960
|
-
if (!existsSync(path)) return
|
|
1219
|
+
if (!existsSync(path)) return {};
|
|
961
1220
|
try {
|
|
962
1221
|
const pkg = JSON.parse(readFileSync(path, "utf8"));
|
|
963
|
-
|
|
1222
|
+
return {
|
|
964
1223
|
...pkg.dependencies,
|
|
965
1224
|
...pkg.devDependencies
|
|
966
1225
|
};
|
|
967
|
-
return BUNDLERS.find((name) => deps[name]);
|
|
968
1226
|
} catch {
|
|
969
|
-
return;
|
|
1227
|
+
return {};
|
|
970
1228
|
}
|
|
971
1229
|
}
|
|
1230
|
+
function detectHost(root) {
|
|
1231
|
+
const deps = readDependencies(root);
|
|
1232
|
+
return HOSTS.find((host) => deps[host.dep]);
|
|
1233
|
+
}
|
|
972
1234
|
function configSource(options, typescript) {
|
|
973
1235
|
const fields = [` sourceLocale: '${options.sourceLocale ?? "en"}',`];
|
|
974
1236
|
if (options.locales?.length) fields.push(` locales: [${options.locales.map((l) => `'${l}'`).join(", ")}],`);
|
|
@@ -1000,16 +1262,12 @@ function init(options = {}) {
|
|
|
1000
1262
|
created.push(label);
|
|
1001
1263
|
}
|
|
1002
1264
|
}
|
|
1003
|
-
const
|
|
1004
|
-
const next = [];
|
|
1005
|
-
if (bundler === "astro") next.push("install the integration: pnpm add -D @verbaly/astro", "add verbaly() to the integrations in astro.config");
|
|
1006
|
-
else if (bundler === "vite") next.push("install the plugin: pnpm add -D @verbaly/vite", "add verbaly() to the plugins in vite.config");
|
|
1007
|
-
else if (bundler) next.push("install the plugin: pnpm add -D @verbaly/unplugin", `add the verbaly ${bundler} plugin to your build config`);
|
|
1008
|
-
else next.push("run \"verbaly extract\" after writing your first t`…` message");
|
|
1265
|
+
const host = detectHost(root);
|
|
1266
|
+
const next = host ? [`pnpm add -D ${host.pkg}`, host.wire] : ["run \"verbaly extract\" after writing your first t`…` message"];
|
|
1009
1267
|
return {
|
|
1010
1268
|
created,
|
|
1011
1269
|
skipped,
|
|
1012
|
-
|
|
1270
|
+
host: host?.name,
|
|
1013
1271
|
configFile,
|
|
1014
1272
|
next
|
|
1015
1273
|
};
|
|
@@ -1055,11 +1313,11 @@ async function doctor(cfg) {
|
|
|
1055
1313
|
}
|
|
1056
1314
|
try {
|
|
1057
1315
|
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
1058
|
-
const bad =
|
|
1316
|
+
const bad = badLeaf(parsed);
|
|
1059
1317
|
if (bad) {
|
|
1060
1318
|
catalogsHealthy = false;
|
|
1061
|
-
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad
|
|
1062
|
-
} else catalogs[locale] = parsed;
|
|
1319
|
+
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
1320
|
+
} else catalogs[locale] = flatten(parsed);
|
|
1063
1321
|
} catch {
|
|
1064
1322
|
catalogsHealthy = false;
|
|
1065
1323
|
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
@@ -1069,21 +1327,22 @@ async function doctor(cfg) {
|
|
|
1069
1327
|
}
|
|
1070
1328
|
const source = catalogs[cfg.sourceLocale];
|
|
1071
1329
|
if (source && Object.keys(source).length === 0) warn("source", `source catalog ${cfg.sourceLocale}.json is empty`, "write your first t`…` message and run `npx verbaly extract`");
|
|
1072
|
-
const deps =
|
|
1073
|
-
const
|
|
1074
|
-
const
|
|
1075
|
-
if (!
|
|
1076
|
-
else if (
|
|
1077
|
-
else
|
|
1078
|
-
|
|
1079
|
-
if (source
|
|
1330
|
+
const deps = readDependencies(cfg.root);
|
|
1331
|
+
const host = detectHost(cfg.root);
|
|
1332
|
+
const installed = WIRING_PACKAGES.find((pkg) => deps[pkg]);
|
|
1333
|
+
if (!host) ok("plugin", "no bundler detected, the CLI flow (extract/check) applies");
|
|
1334
|
+
else if (installed) ok("plugin", `${installed} installed for ${host.name}`);
|
|
1335
|
+
else warn("plugin", `${host.name} detected but ${host.pkg} is not installed`, `pnpm add -D ${host.pkg} and ${host.wire}`);
|
|
1336
|
+
const scanning = cfg.include.length > 0;
|
|
1337
|
+
if (!scanning) ok("sources", "source scanning is off (include: []), the catalogs are the source of truth");
|
|
1338
|
+
if (source && scanning && cfg.dts !== false) {
|
|
1080
1339
|
const dtsPath = cfg.dts ?? join(cfg.root, "verbaly.d.ts");
|
|
1081
1340
|
if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
|
|
1082
1341
|
else if (readFileSync(dtsPath, "utf8") !== generateDts(source)) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
|
|
1083
1342
|
else ok("types", "verbaly.d.ts is up to date");
|
|
1084
1343
|
}
|
|
1085
1344
|
const registry = await extractProject(cfg);
|
|
1086
|
-
if (source) {
|
|
1345
|
+
if (source && scanning) {
|
|
1087
1346
|
const extracted = registry.messages();
|
|
1088
1347
|
const used = registry.usedKeys();
|
|
1089
1348
|
const orphans = Object.keys(source).filter((key) => !extracted.has(key) && !used.has(key));
|
|
@@ -1097,6 +1356,13 @@ async function doctor(cfg) {
|
|
|
1097
1356
|
const locales = [...new Set(result.missing.map((m) => m.locale))];
|
|
1098
1357
|
warn("translations", `${result.missing.length} missing ${result.missing.length === 1 ? "translation" : "translations"} (${locales.join(", ")})`, "run `npx verbaly translate` or fill the catalogs (`npx verbaly check` for details)");
|
|
1099
1358
|
}
|
|
1359
|
+
const broken = result.broken.filter((entry) => entry.severity === "error");
|
|
1360
|
+
if (broken.length > 0) {
|
|
1361
|
+
const locales = [...new Set(broken.map((b) => b.locale))];
|
|
1362
|
+
error("translations", `${broken.length} broken ${broken.length === 1 ? "translation" : "translations"} (${locales.join(", ")}): present but not rendering what the source renders`, "run `npx verbaly check` to read what each one lost");
|
|
1363
|
+
}
|
|
1364
|
+
const warnings = result.broken.filter((entry) => entry.severity === "warning");
|
|
1365
|
+
if (warnings.length > 0) warn("translations", `${warnings.length} structural ${warnings.length === 1 ? "warning" : "warnings"} (plural forms a language asks for)`, "run `npx verbaly check` to read them, they never fail the build");
|
|
1100
1366
|
if (result.ok) ok("translations", "all translations complete");
|
|
1101
1367
|
}
|
|
1102
1368
|
return {
|
|
@@ -1108,15 +1374,13 @@ function preview(keys) {
|
|
|
1108
1374
|
const head = keys.slice(0, PREVIEW).join(", ");
|
|
1109
1375
|
return keys.length > PREVIEW ? `${head}, …` : head;
|
|
1110
1376
|
}
|
|
1111
|
-
function
|
|
1112
|
-
|
|
1113
|
-
const
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
} catch {
|
|
1119
|
-
return {};
|
|
1377
|
+
function badLeaf(tree, prefix = "") {
|
|
1378
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
1379
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
1380
|
+
if (typeof value === "string") continue;
|
|
1381
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return path;
|
|
1382
|
+
const bad = badLeaf(value, path);
|
|
1383
|
+
if (bad) return bad;
|
|
1120
1384
|
}
|
|
1121
1385
|
}
|
|
1122
1386
|
//#endregion
|
|
@@ -1572,24 +1836,12 @@ async function resolveProvider(cfg, model) {
|
|
|
1572
1836
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1573
1837
|
}
|
|
1574
1838
|
function structureMatches(source, translated) {
|
|
1575
|
-
return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
|
|
1576
|
-
}
|
|
1577
|
-
function paramNames(message) {
|
|
1578
1839
|
try {
|
|
1579
|
-
return [...
|
|
1840
|
+
return ![...validateMessage(translated), ...validatePair(source, translated)].some((issue) => issue.severity === "error");
|
|
1580
1841
|
} catch {
|
|
1581
|
-
return
|
|
1842
|
+
return false;
|
|
1582
1843
|
}
|
|
1583
1844
|
}
|
|
1584
|
-
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
1585
|
-
function tagTokens(message) {
|
|
1586
|
-
const out = [];
|
|
1587
|
-
for (const match of message.matchAll(TAG)) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
1588
|
-
return out.sort();
|
|
1589
|
-
}
|
|
1590
|
-
function sameMembers(a, b) {
|
|
1591
|
-
return a.length === b.length && a.every((value, i) => value === b[i]);
|
|
1592
|
-
}
|
|
1593
1845
|
//#endregion
|
|
1594
1846
|
//#region src/exchange.ts
|
|
1595
1847
|
function isMobileFormat(format) {
|
|
@@ -2077,19 +2329,30 @@ function decodeEntities(text) {
|
|
|
2077
2329
|
//#endregion
|
|
2078
2330
|
//#region src/status.ts
|
|
2079
2331
|
function status(cfg, catalogs, registry, drafts = {}) {
|
|
2080
|
-
const
|
|
2081
|
-
const
|
|
2082
|
-
const
|
|
2332
|
+
const extracted = registry.messages();
|
|
2333
|
+
const flat = {};
|
|
2334
|
+
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
2335
|
+
const source = flat[cfg.sourceLocale] ?? {};
|
|
2336
|
+
const needed = /* @__PURE__ */ new Set([...extracted.keys(), ...Object.keys(source)]);
|
|
2337
|
+
const live = effectiveDrafts(drafts, flat);
|
|
2083
2338
|
const locales = [];
|
|
2084
2339
|
for (const locale of cfg.locales) {
|
|
2085
2340
|
if (locale === cfg.sourceLocale) continue;
|
|
2341
|
+
const catalog = flat[locale] ?? {};
|
|
2086
2342
|
let translated = 0;
|
|
2087
|
-
for (const key of needed) if (
|
|
2343
|
+
for (const key of needed) if (catalog[key]) translated += 1;
|
|
2344
|
+
let broken = 0;
|
|
2345
|
+
for (const [key, text] of Object.entries(catalog)) {
|
|
2346
|
+
if (!text) continue;
|
|
2347
|
+
const from = source[key] ?? extracted.get(key)?.message;
|
|
2348
|
+
if ([...validateMessage(text, locale), ...from ? validatePair(from, text) : []].some((issue) => issue.severity === "error")) broken += 1;
|
|
2349
|
+
}
|
|
2088
2350
|
locales.push({
|
|
2089
2351
|
locale,
|
|
2090
2352
|
translated,
|
|
2091
2353
|
total: needed.size,
|
|
2092
|
-
drafts: live[locale]?.length ?? 0
|
|
2354
|
+
drafts: live[locale]?.length ?? 0,
|
|
2355
|
+
broken
|
|
2093
2356
|
});
|
|
2094
2357
|
}
|
|
2095
2358
|
return {
|
|
@@ -2104,11 +2367,12 @@ function formatStatusResult(result) {
|
|
|
2104
2367
|
lines.push(" no target locales (add locales to your config)");
|
|
2105
2368
|
return lines.join("\n");
|
|
2106
2369
|
}
|
|
2107
|
-
for (const { locale, translated, total, drafts } of result.locales) {
|
|
2370
|
+
for (const { locale, translated, total, drafts, broken } of result.locales) {
|
|
2108
2371
|
const pct = total === 0 ? 100 : Math.floor(translated / total * 100);
|
|
2109
|
-
const mark = translated === total ? " ✓" : "";
|
|
2110
|
-
const
|
|
2111
|
-
|
|
2372
|
+
const mark = translated === total && broken === 0 ? " ✓" : "";
|
|
2373
|
+
const notes = [drafts > 0 ? `${drafts} unreviewed` : "", broken > 0 ? `${broken} broken` : ""].filter(Boolean);
|
|
2374
|
+
const note = notes.length > 0 ? `, ${notes.join(", ")}` : "";
|
|
2375
|
+
lines.push(` ${locale}: ${translated}/${total} translated (${pct}%${note})${mark}`);
|
|
2112
2376
|
}
|
|
2113
2377
|
return lines.join("\n");
|
|
2114
2378
|
}
|
|
@@ -2462,7 +2726,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
2462
2726
|
});
|
|
2463
2727
|
if (result.created.length) console.log(`[verbaly] created: ${result.created.join(", ")}`);
|
|
2464
2728
|
if (result.skipped.length) console.log(` kept (already there): ${result.skipped.join(", ")}`);
|
|
2465
|
-
if (result.
|
|
2729
|
+
if (result.host) console.log(` detected: ${result.host}`);
|
|
2466
2730
|
console.log([" next steps:", ...result.next.map((step, i) => ` ${i + 1}. ${step}`)].join("\n"));
|
|
2467
2731
|
return;
|
|
2468
2732
|
}
|
|
@@ -2561,6 +2825,11 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
2561
2825
|
const result = check(cfg, catalogs, registry);
|
|
2562
2826
|
const unreviewed = values.drafts ? effectiveDrafts(loadDrafts(cfg), catalogs) : {};
|
|
2563
2827
|
const draftKeys = Object.entries(unreviewed);
|
|
2828
|
+
if (reporter === "github") for (const line of githubCheckAnnotations(result, registry, cfg.root)) console.error(line);
|
|
2829
|
+
else {
|
|
2830
|
+
const warnings = formatCheckWarnings(result);
|
|
2831
|
+
if (warnings) console.warn(`[verbaly] warnings (the gate still passes)\n${warnings}`);
|
|
2832
|
+
}
|
|
2564
2833
|
if (result.ok && draftKeys.length === 0) {
|
|
2565
2834
|
console.log("[verbaly] all translations complete ✓");
|
|
2566
2835
|
return;
|
|
@@ -2571,10 +2840,9 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
2571
2840
|
process.exitCode = 1;
|
|
2572
2841
|
return;
|
|
2573
2842
|
}
|
|
2574
|
-
|
|
2575
|
-
|
|
2576
|
-
|
|
2577
|
-
} else console.error(`[verbaly] check failed\n${formatCheckResult(result)}`);
|
|
2843
|
+
const brokenCount = result.broken.filter((entry) => entry.severity === "error").length;
|
|
2844
|
+
const report = reporter === "github" ? `[verbaly] check failed: ${result.missing.length} missing, ${result.unknown.length} unknown, ${brokenCount} broken` : `[verbaly] check failed\n${formatCheckResult(result)}`;
|
|
2845
|
+
console.error(`${report}\n${checkNextSteps(result)}`);
|
|
2578
2846
|
process.exitCode = 1;
|
|
2579
2847
|
return;
|
|
2580
2848
|
}
|