@verbaly/compiler 0.77.0 → 0.79.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 +9 -9
- package/dist/{claude-CHpftMVn.js → claude-BtdaNZEw.js} +2 -2
- package/dist/cli.js +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/index.js +3 -3
- package/dist/{run-D9RNsrMs.js → run-DobJrwby.js} +1465 -1414
- package/package.json +2 -2
|
@@ -10,7 +10,7 @@ import { applyEdits, modify, parse as parse$2 } from "jsonc-parser";
|
|
|
10
10
|
import picomatch from "picomatch";
|
|
11
11
|
import MagicString from "magic-string";
|
|
12
12
|
import { parseArgs } from "node:util";
|
|
13
|
-
// src/host.ts
|
|
13
|
+
// src/config/host.ts
|
|
14
14
|
const HOSTS = [
|
|
15
15
|
{
|
|
16
16
|
name: "nuxt",
|
|
@@ -130,6 +130,11 @@ function installCommand(root, packages, dev = false) {
|
|
|
130
130
|
const manager = packageManager(root);
|
|
131
131
|
return `${manager} ${manager === "npm" ? "install" : "add"}${dev ? manager === "bun" ? " -d" : " -D" : ""} ${packages.join(" ")}`;
|
|
132
132
|
}
|
|
133
|
+
function cliReachable(root) {
|
|
134
|
+
const bin = join(root, "node_modules", ".bin");
|
|
135
|
+
return existsSync(join(bin, "verbaly")) || existsSync(join(bin, "verbaly.CMD"));
|
|
136
|
+
}
|
|
137
|
+
const CLI_INSTALL_FIX = "add `@verbaly/compiler` to your dev dependencies, then `npx verbaly` resolves";
|
|
133
138
|
const TYPE_SLOTS = {
|
|
134
139
|
next: [".verbaly", "types.d.ts"],
|
|
135
140
|
astro: [
|
|
@@ -145,12 +150,140 @@ function defaultTypesPath(root, host = detectHost(root)?.name) {
|
|
|
145
150
|
if (slot) return join(root, ...slot);
|
|
146
151
|
return existsSync(join(root, "src")) ? join(root, "src", "verbaly.d.ts") : join(root, "verbaly.d.ts");
|
|
147
152
|
}
|
|
148
|
-
// src/
|
|
153
|
+
// src/util/warn.ts
|
|
154
|
+
const warned = /* @__PURE__ */ new Set();
|
|
155
|
+
function warnOnce(message, dedupeKey = message) {
|
|
156
|
+
if (warned.has(dedupeKey)) return;
|
|
157
|
+
warned.add(dedupeKey);
|
|
158
|
+
console.warn(`[verbaly] ${message}`);
|
|
159
|
+
}
|
|
160
|
+
function warnParseError(file, message) {
|
|
161
|
+
warnOnce(`${file}: could not be parsed (${message}), its messages were not extracted`);
|
|
162
|
+
}
|
|
163
|
+
// src/config/config.ts
|
|
164
|
+
const SOURCE_EXTENSIONS = "{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}";
|
|
165
|
+
const PSEUDO_LOCALE = "en-XA";
|
|
166
|
+
function resolveConfig(config = {}) {
|
|
167
|
+
const root = resolve(config.root ?? process.cwd());
|
|
168
|
+
const dir = resolve(root, config.dir ?? "locales");
|
|
169
|
+
const sourceLocale = config.sourceLocale ?? "en";
|
|
170
|
+
const host = detectHost(root)?.name;
|
|
171
|
+
const localesDeclared = config.locales !== void 0;
|
|
172
|
+
const locales = /* @__PURE__ */ new Set([sourceLocale, ...config.locales ?? []]);
|
|
173
|
+
if (!localesDeclared && existsSync(dir)) {
|
|
174
|
+
for (const file of readdirSync(dir)) if (file.endsWith(".json") && !file.startsWith(".") && file !== `en-XA.json`) locales.add(file.slice(0, -5));
|
|
175
|
+
}
|
|
176
|
+
return {
|
|
177
|
+
root,
|
|
178
|
+
dir,
|
|
179
|
+
sourceLocale,
|
|
180
|
+
localesDeclared,
|
|
181
|
+
routing: config.routing ?? (config.render ? "prefix-except-source" : "no-prefix"),
|
|
182
|
+
icu: config.icu,
|
|
183
|
+
relative: config.relative,
|
|
184
|
+
locales: [...locales],
|
|
185
|
+
include: config.include ?? [`{src,app}/**/*.{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}`],
|
|
186
|
+
exclude: config.exclude ?? ["**/node_modules/**", "**/dist/**"],
|
|
187
|
+
dts: typeof config.dts === "string" ? resolve(root, config.dts) : config.dts === false ? false : defaultTypesPath(root, host),
|
|
188
|
+
translate: config.translate ?? {},
|
|
189
|
+
render: config.render ?? {},
|
|
190
|
+
bundle: config.bundle ?? {},
|
|
191
|
+
host,
|
|
192
|
+
trailingSlash: config.trailingSlash
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
const CONFIG_FILES = [
|
|
196
|
+
"verbaly.config.js",
|
|
197
|
+
"verbaly.config.mjs",
|
|
198
|
+
"verbaly.config.ts",
|
|
199
|
+
"verbaly.config.mts",
|
|
200
|
+
"verbaly.config.json"
|
|
201
|
+
];
|
|
202
|
+
function findConfigFile(root) {
|
|
203
|
+
return CONFIG_FILES.find((name) => existsSync(join(root, name)));
|
|
204
|
+
}
|
|
205
|
+
async function loadConfigFile(root) {
|
|
206
|
+
for (const name of ["verbaly.config.js", "verbaly.config.mjs"]) {
|
|
207
|
+
const path = join(root, name);
|
|
208
|
+
if (existsSync(path)) {
|
|
209
|
+
const url = `${pathToFileURL(path).href}?mtime=${statSync(path).mtimeMs}`;
|
|
210
|
+
delete createRequire(import.meta.url).cache[resolve(path)];
|
|
211
|
+
return (await import(url)).default ?? {};
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
for (const name of ["verbaly.config.ts", "verbaly.config.mts"]) {
|
|
215
|
+
const path = join(root, name);
|
|
216
|
+
if (existsSync(path)) return loadTsConfig(path);
|
|
217
|
+
}
|
|
218
|
+
const jsonPath = join(root, "verbaly.config.json");
|
|
219
|
+
if (existsSync(jsonPath)) return readJsonConfig(jsonPath);
|
|
220
|
+
return {};
|
|
221
|
+
}
|
|
222
|
+
function readJsonConfig(path) {
|
|
223
|
+
const content = readFileSync(path, "utf8");
|
|
224
|
+
const body = content.charCodeAt(0) === 65279 ? content.slice(1) : content;
|
|
225
|
+
try {
|
|
226
|
+
return JSON.parse(body);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
throw new Error(`[verbaly] ${path} is not valid JSON: ${error.message}`, { cause: error });
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
async function loadTsConfig(path) {
|
|
232
|
+
const { createJiti } = await import("jiti");
|
|
233
|
+
const mod = await createJiti(import.meta.url, {
|
|
234
|
+
fsCache: false,
|
|
235
|
+
moduleCache: false,
|
|
236
|
+
interopDefault: false
|
|
237
|
+
}).import(path);
|
|
238
|
+
return mod.__esModule ? mod.default ?? {} : mod;
|
|
239
|
+
}
|
|
240
|
+
function isModuleNotFound(error, name) {
|
|
241
|
+
return error instanceof Error && error.code === "ERR_MODULE_NOT_FOUND" && error.message.includes(name);
|
|
242
|
+
}
|
|
243
|
+
function targetLocales(cfg, override) {
|
|
244
|
+
return (override ?? cfg.locales).filter((locale) => locale !== cfg.sourceLocale && cfg.locales.includes(locale));
|
|
245
|
+
}
|
|
246
|
+
async function loadConfig(root, overrides = {}, from = {}) {
|
|
247
|
+
const fileConfig = await loadConfigFile(root);
|
|
248
|
+
if (from.plugin) warnPluginOnly(root, fileConfig, overrides);
|
|
249
|
+
return resolveConfig({
|
|
250
|
+
...fileConfig,
|
|
251
|
+
...definedOnly(overrides),
|
|
252
|
+
root
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
const READ_BY_CLI = [
|
|
256
|
+
"sourceLocale",
|
|
257
|
+
"locales",
|
|
258
|
+
"dir",
|
|
259
|
+
"include",
|
|
260
|
+
"exclude"
|
|
261
|
+
];
|
|
262
|
+
function warnPluginOnly(root, file, overrides) {
|
|
263
|
+
const cli = resolveConfig({
|
|
264
|
+
...file,
|
|
265
|
+
root
|
|
266
|
+
});
|
|
267
|
+
const build = resolveConfig({
|
|
268
|
+
...file,
|
|
269
|
+
...definedOnly(overrides),
|
|
270
|
+
root
|
|
271
|
+
});
|
|
272
|
+
const keys = READ_BY_CLI.filter((key) => overrides[key] !== void 0 && JSON.stringify(cli[key]) !== JSON.stringify(build[key]));
|
|
273
|
+
if (keys.length === 0) return;
|
|
274
|
+
const names = keys.length > 1 ? `${keys.slice(0, -1).join(", ")} and ${keys.at(-1)}` : keys[0];
|
|
275
|
+
const [verb, pronoun] = keys.length > 1 ? ["are", "them"] : ["is", "it"];
|
|
276
|
+
warnOnce(`${names} ${verb} set in the plugin options, which \`verbaly check\` and \`verbaly extract\` never read: move ${pronoun} to verbaly.config so the command line checks what the build ships`, `plugin-only:${root}`);
|
|
277
|
+
}
|
|
278
|
+
function definedOnly(config) {
|
|
279
|
+
return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== void 0));
|
|
280
|
+
}
|
|
281
|
+
// src/util/eol.ts
|
|
149
282
|
function withLineEndings(text, existing) {
|
|
150
283
|
const first = existing?.indexOf("\n") ?? -1;
|
|
151
284
|
return first > 0 && existing[first - 1] === "\r" ? text.replace(/\r?\n/g, "\r\n") : text;
|
|
152
285
|
}
|
|
153
|
-
// src/catalog.ts
|
|
286
|
+
// src/catalog/catalog.ts
|
|
154
287
|
function emptyCatalog() {
|
|
155
288
|
return Object.create(null);
|
|
156
289
|
}
|
|
@@ -305,264 +438,19 @@ function someLeaf(tree, pattern) {
|
|
|
305
438
|
} else if (isTree(value) && someLeaf(value, pattern)) return true;
|
|
306
439
|
return false;
|
|
307
440
|
}
|
|
308
|
-
// src/
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
a: "á",
|
|
312
|
-
b: "ƀ",
|
|
313
|
-
c: "ç",
|
|
314
|
-
d: "đ",
|
|
315
|
-
e: "é",
|
|
316
|
-
f: "ƒ",
|
|
317
|
-
g: "ğ",
|
|
318
|
-
h: "ĥ",
|
|
319
|
-
i: "í",
|
|
320
|
-
j: "ĵ",
|
|
321
|
-
k: "ķ",
|
|
322
|
-
l: "ĺ",
|
|
323
|
-
m: "ɱ",
|
|
324
|
-
n: "ñ",
|
|
325
|
-
o: "ó",
|
|
326
|
-
p: "þ",
|
|
327
|
-
q: "ǫ",
|
|
328
|
-
r: "ŕ",
|
|
329
|
-
s: "š",
|
|
330
|
-
t: "ţ",
|
|
331
|
-
u: "ú",
|
|
332
|
-
v: "ṽ",
|
|
333
|
-
w: "ŵ",
|
|
334
|
-
x: "ẋ",
|
|
335
|
-
y: "ý",
|
|
336
|
-
z: "ž",
|
|
337
|
-
A: "Á",
|
|
338
|
-
B: "Ɓ",
|
|
339
|
-
C: "Ç",
|
|
340
|
-
D: "Đ",
|
|
341
|
-
E: "É",
|
|
342
|
-
F: "Ƒ",
|
|
343
|
-
G: "Ğ",
|
|
344
|
-
H: "Ĥ",
|
|
345
|
-
I: "Í",
|
|
346
|
-
J: "Ĵ",
|
|
347
|
-
K: "Ķ",
|
|
348
|
-
L: "Ĺ",
|
|
349
|
-
M: "Ḿ",
|
|
350
|
-
N: "Ñ",
|
|
351
|
-
O: "Ó",
|
|
352
|
-
P: "Þ",
|
|
353
|
-
Q: "Ǫ",
|
|
354
|
-
R: "Ŕ",
|
|
355
|
-
S: "Š",
|
|
356
|
-
T: "Ţ",
|
|
357
|
-
U: "Ú",
|
|
358
|
-
V: "Ṽ",
|
|
359
|
-
W: "Ŵ",
|
|
360
|
-
X: "Ẍ",
|
|
361
|
-
Y: "Ý",
|
|
362
|
-
Z: "Ž"
|
|
363
|
-
};
|
|
364
|
-
const TAG_AT = /<\/?[a-zA-Z][\w-]*\/?>/y;
|
|
365
|
-
function pseudoLocalize(message) {
|
|
366
|
-
let out = "";
|
|
367
|
-
let letters = 0;
|
|
368
|
-
let i = 0;
|
|
369
|
-
while (i < message.length) {
|
|
370
|
-
const two = message.slice(i, i + 2);
|
|
371
|
-
if (two === "{{" || two === "}}" || two === "||" || two === "##") {
|
|
372
|
-
out += two;
|
|
373
|
-
i += 2;
|
|
374
|
-
continue;
|
|
375
|
-
}
|
|
376
|
-
const ch = message[i];
|
|
377
|
-
if (ch === "{") {
|
|
378
|
-
const end = matchBrace$1(message, i);
|
|
379
|
-
out += message.slice(i, end);
|
|
380
|
-
i = end;
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
if (ch === "<") {
|
|
384
|
-
TAG_AT.lastIndex = i;
|
|
385
|
-
const m = TAG_AT.exec(message);
|
|
386
|
-
if (m) {
|
|
387
|
-
out += m[0];
|
|
388
|
-
i += m[0].length;
|
|
389
|
-
continue;
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
const mapped = ACCENTS[ch];
|
|
393
|
-
if (mapped) {
|
|
394
|
-
out += mapped;
|
|
395
|
-
letters += 1;
|
|
396
|
-
} else out += ch;
|
|
397
|
-
i += 1;
|
|
398
|
-
}
|
|
399
|
-
const pad = "~".repeat(Math.ceil(letters / 3));
|
|
400
|
-
return `⟦${out}${pad ? " " + pad : ""}⟧`;
|
|
441
|
+
// src/util/text.ts
|
|
442
|
+
function counted(count, noun, many = `${noun}s`) {
|
|
443
|
+
return `${count} ${count === 1 ? noun : many}`;
|
|
401
444
|
}
|
|
402
|
-
function
|
|
403
|
-
|
|
404
|
-
for (let i = start; i < message.length; i++) {
|
|
405
|
-
const two = message.slice(i, i + 2);
|
|
406
|
-
if (two === "{{" || two === "}}") {
|
|
407
|
-
i += 1;
|
|
408
|
-
continue;
|
|
409
|
-
}
|
|
410
|
-
const ch = message[i];
|
|
411
|
-
if (ch === "{") depth += 1;
|
|
412
|
-
else if (ch === "}") {
|
|
413
|
-
depth -= 1;
|
|
414
|
-
if (depth === 0) return i + 1;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
return message.length;
|
|
445
|
+
function truncate(text, max) {
|
|
446
|
+
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
418
447
|
}
|
|
419
|
-
function
|
|
420
|
-
|
|
421
|
-
const target = emptyCatalog();
|
|
422
|
-
for (const [key, msg] of Object.entries(source)) target[key] = msg ? pseudoLocalize(msg) : "";
|
|
423
|
-
catalogs[locale] = target;
|
|
424
|
-
return Object.keys(target).filter((key) => target[key]);
|
|
448
|
+
function byCodeUnit(a, b) {
|
|
449
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
425
450
|
}
|
|
426
|
-
// src/
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
if (warned.has(dedupeKey)) return;
|
|
430
|
-
warned.add(dedupeKey);
|
|
431
|
-
console.warn(`[verbaly] ${message}`);
|
|
432
|
-
}
|
|
433
|
-
function warnParseError(file, message) {
|
|
434
|
-
warnOnce(`${file}: could not be parsed (${message}), its messages were not extracted`);
|
|
435
|
-
}
|
|
436
|
-
// src/config.ts
|
|
437
|
-
const SOURCE_EXTENSIONS = "{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}";
|
|
438
|
-
function resolveConfig(config = {}) {
|
|
439
|
-
const root = resolve(config.root ?? process.cwd());
|
|
440
|
-
const dir = resolve(root, config.dir ?? "locales");
|
|
441
|
-
const sourceLocale = config.sourceLocale ?? "en";
|
|
442
|
-
const host = detectHost(root)?.name;
|
|
443
|
-
const localesDeclared = config.locales !== void 0;
|
|
444
|
-
const locales = /* @__PURE__ */ new Set([sourceLocale, ...config.locales ?? []]);
|
|
445
|
-
if (!localesDeclared && existsSync(dir)) {
|
|
446
|
-
for (const file of readdirSync(dir)) if (file.endsWith(".json") && !file.startsWith(".") && file !== `en-XA.json`) locales.add(file.slice(0, -5));
|
|
447
|
-
}
|
|
448
|
-
return {
|
|
449
|
-
root,
|
|
450
|
-
dir,
|
|
451
|
-
sourceLocale,
|
|
452
|
-
localesDeclared,
|
|
453
|
-
routing: config.routing ?? (config.render ? "prefix-except-source" : "no-prefix"),
|
|
454
|
-
icu: config.icu,
|
|
455
|
-
relative: config.relative,
|
|
456
|
-
locales: [...locales],
|
|
457
|
-
include: config.include ?? [`{src,app}/**/*.{js,jsx,ts,tsx,mjs,mts,svelte,vue,astro}`],
|
|
458
|
-
exclude: config.exclude ?? ["**/node_modules/**", "**/dist/**"],
|
|
459
|
-
dts: typeof config.dts === "string" ? resolve(root, config.dts) : config.dts === false ? false : defaultTypesPath(root, host),
|
|
460
|
-
translate: config.translate ?? {},
|
|
461
|
-
render: config.render ?? {},
|
|
462
|
-
bundle: config.bundle ?? {},
|
|
463
|
-
host,
|
|
464
|
-
trailingSlash: config.trailingSlash
|
|
465
|
-
};
|
|
466
|
-
}
|
|
467
|
-
const CONFIG_FILES = [
|
|
468
|
-
"verbaly.config.js",
|
|
469
|
-
"verbaly.config.mjs",
|
|
470
|
-
"verbaly.config.ts",
|
|
471
|
-
"verbaly.config.mts",
|
|
472
|
-
"verbaly.config.json"
|
|
473
|
-
];
|
|
474
|
-
function findConfigFile(root) {
|
|
475
|
-
return CONFIG_FILES.find((name) => existsSync(join(root, name)));
|
|
476
|
-
}
|
|
477
|
-
async function loadConfigFile(root) {
|
|
478
|
-
for (const name of ["verbaly.config.js", "verbaly.config.mjs"]) {
|
|
479
|
-
const path = join(root, name);
|
|
480
|
-
if (existsSync(path)) {
|
|
481
|
-
const url = `${pathToFileURL(path).href}?mtime=${statSync(path).mtimeMs}`;
|
|
482
|
-
delete createRequire(import.meta.url).cache[resolve(path)];
|
|
483
|
-
return (await import(url)).default ?? {};
|
|
484
|
-
}
|
|
485
|
-
}
|
|
486
|
-
for (const name of ["verbaly.config.ts", "verbaly.config.mts"]) {
|
|
487
|
-
const path = join(root, name);
|
|
488
|
-
if (existsSync(path)) return loadTsConfig(path);
|
|
489
|
-
}
|
|
490
|
-
const jsonPath = join(root, "verbaly.config.json");
|
|
491
|
-
if (existsSync(jsonPath)) return readJsonConfig(jsonPath);
|
|
492
|
-
return {};
|
|
493
|
-
}
|
|
494
|
-
function readJsonConfig(path) {
|
|
495
|
-
const content = readFileSync(path, "utf8");
|
|
496
|
-
const body = content.charCodeAt(0) === 65279 ? content.slice(1) : content;
|
|
497
|
-
try {
|
|
498
|
-
return JSON.parse(body);
|
|
499
|
-
} catch (error) {
|
|
500
|
-
throw new Error(`[verbaly] ${path} is not valid JSON: ${error.message}`, { cause: error });
|
|
501
|
-
}
|
|
502
|
-
}
|
|
503
|
-
async function loadTsConfig(path) {
|
|
504
|
-
const { createJiti } = await import("jiti");
|
|
505
|
-
const mod = await createJiti(import.meta.url, {
|
|
506
|
-
fsCache: false,
|
|
507
|
-
moduleCache: false,
|
|
508
|
-
interopDefault: false
|
|
509
|
-
}).import(path);
|
|
510
|
-
return mod.__esModule ? mod.default ?? {} : mod;
|
|
511
|
-
}
|
|
512
|
-
function isModuleNotFound(error, name) {
|
|
513
|
-
return error instanceof Error && error.code === "ERR_MODULE_NOT_FOUND" && error.message.includes(name);
|
|
514
|
-
}
|
|
515
|
-
function targetLocales(cfg, override) {
|
|
516
|
-
return (override ?? cfg.locales).filter((locale) => locale !== cfg.sourceLocale && cfg.locales.includes(locale));
|
|
517
|
-
}
|
|
518
|
-
async function loadConfig(root, overrides = {}, from = {}) {
|
|
519
|
-
const fileConfig = await loadConfigFile(root);
|
|
520
|
-
if (from.plugin) warnPluginOnly(root, fileConfig, overrides);
|
|
521
|
-
return resolveConfig({
|
|
522
|
-
...fileConfig,
|
|
523
|
-
...definedOnly(overrides),
|
|
524
|
-
root
|
|
525
|
-
});
|
|
526
|
-
}
|
|
527
|
-
const READ_BY_CLI = [
|
|
528
|
-
"sourceLocale",
|
|
529
|
-
"locales",
|
|
530
|
-
"dir",
|
|
531
|
-
"include",
|
|
532
|
-
"exclude"
|
|
533
|
-
];
|
|
534
|
-
function warnPluginOnly(root, file, overrides) {
|
|
535
|
-
const cli = resolveConfig({
|
|
536
|
-
...file,
|
|
537
|
-
root
|
|
538
|
-
});
|
|
539
|
-
const build = resolveConfig({
|
|
540
|
-
...file,
|
|
541
|
-
...definedOnly(overrides),
|
|
542
|
-
root
|
|
543
|
-
});
|
|
544
|
-
const keys = READ_BY_CLI.filter((key) => overrides[key] !== void 0 && JSON.stringify(cli[key]) !== JSON.stringify(build[key]));
|
|
545
|
-
if (keys.length === 0) return;
|
|
546
|
-
const names = keys.length > 1 ? `${keys.slice(0, -1).join(", ")} and ${keys.at(-1)}` : keys[0];
|
|
547
|
-
const [verb, pronoun] = keys.length > 1 ? ["are", "them"] : ["is", "it"];
|
|
548
|
-
warnOnce(`${names} ${verb} set in the plugin options, which \`verbaly check\` and \`verbaly extract\` never read: move ${pronoun} to verbaly.config so the command line checks what the build ships`, `plugin-only:${root}`);
|
|
549
|
-
}
|
|
550
|
-
function definedOnly(config) {
|
|
551
|
-
return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== void 0));
|
|
552
|
-
}
|
|
553
|
-
// src/text.ts
|
|
554
|
-
function counted(count, noun, many = `${noun}s`) {
|
|
555
|
-
return `${count} ${count === 1 ? noun : many}`;
|
|
556
|
-
}
|
|
557
|
-
function truncate(text, max) {
|
|
558
|
-
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
559
|
-
}
|
|
560
|
-
function byCodeUnit(a, b) {
|
|
561
|
-
return a < b ? -1 : a > b ? 1 : 0;
|
|
562
|
-
}
|
|
563
|
-
// src/bundle.ts
|
|
564
|
-
function isExcluded(key, prefixes) {
|
|
565
|
-
return prefixes.some((prefix) => key === prefix || key.startsWith(`${prefix}.`));
|
|
451
|
+
// src/emit/bundle.ts
|
|
452
|
+
function isExcluded(key, prefixes) {
|
|
453
|
+
return prefixes.some((prefix) => key === prefix || key.startsWith(`${prefix}.`));
|
|
566
454
|
}
|
|
567
455
|
function clientCatalogs(cfg, catalogs) {
|
|
568
456
|
const prefixes = cfg.bundle.exclude ?? [];
|
|
@@ -603,7 +491,7 @@ function auditBundle(cfg, catalogs, registry) {
|
|
|
603
491
|
function formatBundleIssue(issue) {
|
|
604
492
|
return `bundle.exclude "${issue.prefix}" ${issue.problem}`;
|
|
605
493
|
}
|
|
606
|
-
// src/registry.ts
|
|
494
|
+
// src/scan/registry.ts
|
|
607
495
|
var MessageRegistry = class {
|
|
608
496
|
files = /* @__PURE__ */ new Map();
|
|
609
497
|
update(file, analysis) {
|
|
@@ -676,11 +564,18 @@ var MessageRegistry = class {
|
|
|
676
564
|
function earlier(a, b) {
|
|
677
565
|
return a.file === b.file ? a.start < b.start : a.file < b.file;
|
|
678
566
|
}
|
|
679
|
-
// src/
|
|
567
|
+
// src/util/ids.ts
|
|
568
|
+
const VIRTUAL_ID = "virtual:verbaly";
|
|
569
|
+
const APP_ID = "verbaly/app";
|
|
570
|
+
const LOCALE_IMPORT = "@verbaly/app/locale/";
|
|
571
|
+
function isAppModule(source) {
|
|
572
|
+
return source === "verbaly/app" || source === "virtual:verbaly";
|
|
573
|
+
}
|
|
574
|
+
// src/util/key.ts
|
|
680
575
|
function stableKey(message) {
|
|
681
576
|
return createHash("sha256").update(message).digest("base64url").slice(0, 8);
|
|
682
577
|
}
|
|
683
|
-
// src/analyze.ts
|
|
578
|
+
// src/scan/analyze.ts
|
|
684
579
|
function parseErrorMessage(error) {
|
|
685
580
|
return error instanceof Error ? error.message : String(error);
|
|
686
581
|
}
|
|
@@ -800,7 +695,7 @@ function analyzeScript(code, file, options = {}) {
|
|
|
800
695
|
function lineOf(node) {
|
|
801
696
|
return node.loc?.start.line ?? 1;
|
|
802
697
|
}
|
|
803
|
-
const VERBALY_SOURCE = /^(?:verbaly
|
|
698
|
+
const VERBALY_SOURCE = /^(?:verbaly|@verbaly\/[\w-]+(?:\/[\w-]+)*)$/;
|
|
804
699
|
const T_FACTORIES = /* @__PURE__ */ new Map([["useT", "call"], ["getT", "await"]]);
|
|
805
700
|
const INSTANCE_FACTORIES = /* @__PURE__ */ new Set(["useVerbaly", "getVerbaly"]);
|
|
806
701
|
var Bindings = class {
|
|
@@ -823,7 +718,7 @@ var Bindings = class {
|
|
|
823
718
|
source,
|
|
824
719
|
imported
|
|
825
720
|
});
|
|
826
|
-
if (source
|
|
721
|
+
if (isAppModule(source) && imported === "t") this.direct.add(local);
|
|
827
722
|
}
|
|
828
723
|
}
|
|
829
724
|
addDeclarator(node) {
|
|
@@ -883,7 +778,7 @@ var Bindings = class {
|
|
|
883
778
|
imported: local
|
|
884
779
|
};
|
|
885
780
|
return {
|
|
886
|
-
verbaly: VERBALY_SOURCE.test(found.source),
|
|
781
|
+
verbaly: VERBALY_SOURCE.test(found.source) || isAppModule(found.source),
|
|
887
782
|
imported: found.imported
|
|
888
783
|
};
|
|
889
784
|
}
|
|
@@ -1206,7 +1101,7 @@ function walk$1(node, visit) {
|
|
|
1206
1101
|
function isNode(value) {
|
|
1207
1102
|
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
1208
1103
|
}
|
|
1209
|
-
// src/sfc.ts
|
|
1104
|
+
// src/scan/sfc.ts
|
|
1210
1105
|
const SFC_FILE_RE = /\.(?:svelte|vue|astro)$/;
|
|
1211
1106
|
function analyzeFile(code, file) {
|
|
1212
1107
|
if (SFC_FILE_RE.test(file)) return analyzeSfc(code, file);
|
|
@@ -1396,7 +1291,7 @@ function stringEnd(code, start) {
|
|
|
1396
1291
|
else i += 1;
|
|
1397
1292
|
return code.length;
|
|
1398
1293
|
}
|
|
1399
|
-
// src/extract.ts
|
|
1294
|
+
// src/scan/extract.ts
|
|
1400
1295
|
const kept = /* @__PURE__ */ new WeakMap();
|
|
1401
1296
|
async function extractProject(cfg, options = {}) {
|
|
1402
1297
|
const files = await glob(cfg.include, {
|
|
@@ -1481,7 +1376,7 @@ function pruneCatalogs(cfg, catalogs, registry) {
|
|
|
1481
1376
|
}
|
|
1482
1377
|
return removed;
|
|
1483
1378
|
}
|
|
1484
|
-
// src/validate.ts
|
|
1379
|
+
// src/gate/validate.ts
|
|
1485
1380
|
let remembered = /* @__PURE__ */ new Map();
|
|
1486
1381
|
let reading;
|
|
1487
1382
|
function read(message) {
|
|
@@ -1692,7 +1587,7 @@ function reachableCategories(locale, ordinal) {
|
|
|
1692
1587
|
reachable.set(cacheKey, out);
|
|
1693
1588
|
return out;
|
|
1694
1589
|
}
|
|
1695
|
-
// src/params.ts
|
|
1590
|
+
// src/emit/params.ts
|
|
1696
1591
|
const NUMBER_FORMATS = /* @__PURE__ */ new Set([
|
|
1697
1592
|
"number",
|
|
1698
1593
|
"integer",
|
|
@@ -1734,7 +1629,7 @@ function renderParamType(types) {
|
|
|
1734
1629
|
]) members.add(m);
|
|
1735
1630
|
return [...members].join(" | ");
|
|
1736
1631
|
}
|
|
1737
|
-
// src/tsconfig.ts
|
|
1632
|
+
// src/config/tsconfig.ts
|
|
1738
1633
|
function readTsconfig(root) {
|
|
1739
1634
|
const path = join(root, "tsconfig.json");
|
|
1740
1635
|
let text;
|
|
@@ -1793,6 +1688,7 @@ function includeTypes(root, file) {
|
|
|
1793
1688
|
writeFileSync(read.path, applyEdits(read.text, edits));
|
|
1794
1689
|
return "added";
|
|
1795
1690
|
}
|
|
1691
|
+
// src/emit/codegen.ts
|
|
1796
1692
|
const DTS_HEADER = "// generated by verbaly: do not edit";
|
|
1797
1693
|
const SLICE_HELPERS = `function readSlice(root, path) {
|
|
1798
1694
|
const locale = readPath({ supported: locales, path });
|
|
@@ -1809,7 +1705,7 @@ export function adoptPage(options) {
|
|
|
1809
1705
|
|
|
1810
1706
|
`;
|
|
1811
1707
|
function generateRuntimeModule(cfg, options = {}) {
|
|
1812
|
-
const importPath = options.localeImport ?? ((locale) =>
|
|
1708
|
+
const importPath = options.localeImport ?? ((locale) => "@verbaly/app/locale/" + locale);
|
|
1813
1709
|
const others = cfg.locales.filter((locale) => locale !== cfg.sourceLocale);
|
|
1814
1710
|
const src = JSON.stringify(cfg.sourceLocale);
|
|
1815
1711
|
const loaders = others.map((locale) => ` ${JSON.stringify(locale)}: () => import('${importPath(locale)}'),`).join("\n");
|
|
@@ -1845,8 +1741,10 @@ export function defineKeys(keys) {
|
|
|
1845
1741
|
}
|
|
1846
1742
|
|
|
1847
1743
|
// the whole switch a language control needs, in either mode: catalog or navigation, then persistence
|
|
1744
|
+
// instance: the one the page renders with, where a framework builds one per request
|
|
1848
1745
|
export function switchLocale(locale, options) {
|
|
1849
|
-
|
|
1746
|
+
const { instance, ...rest } = options ?? {};
|
|
1747
|
+
return runSwitch(instance ?? v, locale, { supported: locales, sourceLocale, routing, trailingSlash, ...rest });
|
|
1850
1748
|
}
|
|
1851
1749
|
|
|
1852
1750
|
const localeLoaders = {
|
|
@@ -1935,7 +1833,7 @@ function generateDts(catalog, options = {}) {
|
|
|
1935
1833
|
// biome-ignore-all lint: generated by verbaly
|
|
1936
1834
|
// biome-ignore-all format: generated by verbaly
|
|
1937
1835
|
// prettier-ignore
|
|
1938
|
-
declare module '
|
|
1836
|
+
declare module '${APP_ID}' {
|
|
1939
1837
|
export interface VerbalyMessages {
|
|
1940
1838
|
${lines.join("\n")}
|
|
1941
1839
|
}
|
|
@@ -1994,7 +1892,10 @@ ${lines.join("\n")}
|
|
|
1994
1892
|
options?: Omit<
|
|
1995
1893
|
import('verbaly').SwitchLocaleOptions,
|
|
1996
1894
|
'routing' | 'supported' | 'sourceLocale' | 'trailingSlash'
|
|
1997
|
-
|
|
1895
|
+
> & {
|
|
1896
|
+
// the instance the page renders with, where the framework builds one per request
|
|
1897
|
+
instance?: Pick<import('verbaly').Verbaly, 'loadLocale' | 'setLocale'>;
|
|
1898
|
+
},
|
|
1998
1899
|
): Promise<void>;
|
|
1999
1900
|
export function getLocale(): string;
|
|
2000
1901
|
export function subscribe(listener: () => void): () => void;
|
|
@@ -2009,7 +1910,12 @@ ${lines.join("\n")}
|
|
|
2009
1910
|
}
|
|
2010
1911
|
|
|
2011
1912
|
// prettier-ignore
|
|
2012
|
-
declare module '
|
|
1913
|
+
declare module '${VIRTUAL_ID}' {
|
|
1914
|
+
export * from '${APP_ID}';
|
|
1915
|
+
}
|
|
1916
|
+
|
|
1917
|
+
// prettier-ignore
|
|
1918
|
+
declare module '${LOCALE_IMPORT}*' {
|
|
2013
1919
|
const messages: Record<string, string>;
|
|
2014
1920
|
export default messages;
|
|
2015
1921
|
}
|
|
@@ -2061,7 +1967,7 @@ function removeRootDts(cfg, target) {
|
|
|
2061
1967
|
rmSync(old);
|
|
2062
1968
|
warnOnce(`the types live in ${relative(cfg.root, target).replaceAll("\\", "/")} now, so the verbaly.d.ts Verbaly wrote at the root was removed`);
|
|
2063
1969
|
}
|
|
2064
|
-
// src/findings.ts
|
|
1970
|
+
// src/gate/findings.ts
|
|
2065
1971
|
function siteOf(msg) {
|
|
2066
1972
|
return {
|
|
2067
1973
|
file: msg.file,
|
|
@@ -2182,7 +2088,7 @@ function createDevReporter(root, print = (line) => console.warn(`[verbaly] ${lin
|
|
|
2182
2088
|
if (fresh.length > cap) print(`and ${fresh.length - cap} more: \`npx verbaly doctor\` names them`);
|
|
2183
2089
|
} };
|
|
2184
2090
|
}
|
|
2185
|
-
// src/state.ts
|
|
2091
|
+
// src/catalog/state.ts
|
|
2186
2092
|
const STATE_FILE = "verbaly.lock";
|
|
2187
2093
|
const LOCK_VERSION = 1;
|
|
2188
2094
|
const README = "Written by Verbaly: the source text each translation was made for, which ones are drafts, and which languages migrate converted. Commit it with your catalogs and never edit it: deleting it marks every draft as reviewed.";
|
|
@@ -2515,7 +2421,7 @@ function readFingerprints(value) {
|
|
|
2515
2421
|
}
|
|
2516
2422
|
return out;
|
|
2517
2423
|
}
|
|
2518
|
-
// src/project.ts
|
|
2424
|
+
// src/project/project.ts
|
|
2519
2425
|
async function syncProject(cfg, options = {}) {
|
|
2520
2426
|
const registry = options.registry ?? await extractProject(cfg);
|
|
2521
2427
|
const catalogs = options.catalogs ?? loadCatalogs(cfg);
|
|
@@ -2571,624 +2477,276 @@ function confirmFromDisk(cfg, catalogs, registry) {
|
|
|
2571
2477
|
registry.update(file, analyzeFile(code, file));
|
|
2572
2478
|
}
|
|
2573
2479
|
}
|
|
2574
|
-
// src/
|
|
2575
|
-
|
|
2576
|
-
"
|
|
2577
|
-
"react-i18next",
|
|
2578
|
-
"vue-i18n",
|
|
2579
|
-
"react-intl",
|
|
2580
|
-
"next-intl",
|
|
2581
|
-
"svelte-i18n"
|
|
2582
|
-
];
|
|
2583
|
-
function detectLibraries(root) {
|
|
2584
|
-
const path = join(root, "package.json");
|
|
2585
|
-
if (!existsSync(path)) return [];
|
|
2586
|
-
let manifest;
|
|
2587
|
-
try {
|
|
2588
|
-
manifest = JSON.parse(readFileSync(path, "utf8"));
|
|
2589
|
-
} catch {
|
|
2590
|
-
return [];
|
|
2591
|
-
}
|
|
2592
|
-
const deps = {
|
|
2593
|
-
...manifest.dependencies,
|
|
2594
|
-
...manifest.devDependencies
|
|
2595
|
-
};
|
|
2596
|
-
return KNOWN.filter((name) => deps[name] !== void 0);
|
|
2597
|
-
}
|
|
2598
|
-
const PLAIN = /\{\{\s*([A-Za-z_$][\w$]*)\s*\}\}/g;
|
|
2599
|
-
const FORMATTED = /\{\{\s*-?\s*[A-Za-z_$][\w$]*\s*,[^}]*\}\}/;
|
|
2600
|
-
const UNESCAPED = /\{\{\s*-\s*[A-Za-z_$][\w$]*\s*\}\}/;
|
|
2601
|
-
const NESTED = /\$t\(/;
|
|
2602
|
-
const DOUBLE = /\{\{[^{}]*\}\}/;
|
|
2603
|
-
const DOUBLES = new RegExp(DOUBLE.source, "g");
|
|
2604
|
-
const ONE_PLAIN = new RegExp(`^${PLAIN.source}$`);
|
|
2605
|
-
const NAME = /^\s*([A-Za-z_$][\w$]*)\s*$/;
|
|
2606
|
-
const CATEGORIES = [
|
|
2607
|
-
"zero",
|
|
2608
|
-
"one",
|
|
2609
|
-
"two",
|
|
2610
|
-
"few",
|
|
2611
|
-
"many",
|
|
2612
|
-
"other"
|
|
2613
|
-
];
|
|
2614
|
-
const SUFFIX = new RegExp(`^(.*)_(${[...CATEGORIES, "plural"].join("|")})$`);
|
|
2615
|
-
function i18nextProblem(text) {
|
|
2616
|
-
if (NESTED.test(text)) return "uses $t() nesting, which has no direct equivalent";
|
|
2617
|
-
if (UNESCAPED.test(text)) return "uses {{- name}}, so decide whether the message is rich";
|
|
2618
|
-
if (FORMATTED.test(text)) return "uses an i18next format, which needs the matching Verbaly one";
|
|
2619
|
-
for (const [double] of text.matchAll(DOUBLES)) if (!ONE_PLAIN.test(double)) return "uses {{…}} with more than a name, which needs a Verbaly param";
|
|
2620
|
-
}
|
|
2621
|
-
function convertBraces(text) {
|
|
2622
|
-
let out = "";
|
|
2623
|
-
let last = 0;
|
|
2624
|
-
for (const match of text.matchAll(PLAIN)) {
|
|
2625
|
-
out += escapeBraces(text.slice(last, match.index)) + `{${match[1]}}`;
|
|
2626
|
-
last = match.index + match[0].length;
|
|
2627
|
-
}
|
|
2628
|
-
return out + escapeBraces(text.slice(last));
|
|
2629
|
-
}
|
|
2630
|
-
function escapeBraces(text) {
|
|
2631
|
-
return text.replace(/[{}]/g, (brace) => brace + brace);
|
|
2480
|
+
// src/gate/check.ts
|
|
2481
|
+
function gatePasses(result) {
|
|
2482
|
+
return result.missing.length === 0 && result.unknown.length === 0 && result.collisions.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
2632
2483
|
}
|
|
2633
|
-
function
|
|
2634
|
-
|
|
2635
|
-
let i = 0;
|
|
2636
|
-
while (i < text.length) {
|
|
2637
|
-
const ch = text[i];
|
|
2638
|
-
if ((ch === "{" || ch === "}") && text[i + 1] === ch) {
|
|
2639
|
-
out += ch + ch;
|
|
2640
|
-
i += 2;
|
|
2641
|
-
} else if (ch === "{") {
|
|
2642
|
-
const close = text.indexOf("}", i);
|
|
2643
|
-
const name = close < 0 ? void 0 : NAME.exec(text.slice(i + 1, close))?.[1];
|
|
2644
|
-
if (name === void 0) return void 0;
|
|
2645
|
-
out += name === "count" ? "#" : `{${name}}`;
|
|
2646
|
-
i = close + 1;
|
|
2647
|
-
} else {
|
|
2648
|
-
out += ch === "#" || ch === "|" || ch === "}" ? ch + ch : ch;
|
|
2649
|
-
i += 1;
|
|
2650
|
-
}
|
|
2651
|
-
}
|
|
2652
|
-
return out;
|
|
2484
|
+
function warningCount(result) {
|
|
2485
|
+
return result.broken.filter((entry) => entry.severity === "warning").length + result.extra.length + result.divergent.length + result.outdated.length;
|
|
2653
2486
|
}
|
|
2654
|
-
function
|
|
2655
|
-
return
|
|
2487
|
+
function check(cfg, catalogs, registry, fingerprints = {}) {
|
|
2488
|
+
return gatePass(() => checkPass(cfg, catalogs, registry, fingerprints));
|
|
2656
2489
|
}
|
|
2657
|
-
function
|
|
2658
|
-
const
|
|
2659
|
-
const
|
|
2660
|
-
for (const
|
|
2661
|
-
|
|
2662
|
-
|
|
2663
|
-
|
|
2664
|
-
|
|
2665
|
-
|
|
2666
|
-
|
|
2667
|
-
|
|
2490
|
+
function checkPass(cfg, catalogs, registry, fingerprints) {
|
|
2491
|
+
const extracted = registry.messages();
|
|
2492
|
+
const flat = {};
|
|
2493
|
+
for (const locale of cfg.locales) flat[locale] = flatView(catalogs[locale] ?? {});
|
|
2494
|
+
const source = flat[cfg.sourceLocale] ?? {};
|
|
2495
|
+
const shipped = shippedCatalogs(cfg, flat, registry);
|
|
2496
|
+
const sourceText = shipped[cfg.sourceLocale];
|
|
2497
|
+
const used = registry.usedKeys();
|
|
2498
|
+
const unknown = [];
|
|
2499
|
+
for (const [key, files] of registry.usedKeys(true)) if (!(extracted.has(key) || cfg.locales.some((locale) => flat[locale]?.[key] !== void 0))) unknown.push({
|
|
2500
|
+
key,
|
|
2501
|
+
files
|
|
2502
|
+
});
|
|
2503
|
+
const needed = /* @__PURE__ */ new Set([...extracted.keys(), ...Object.keys(source)]);
|
|
2504
|
+
const missing = [];
|
|
2505
|
+
for (const key of extracted.keys()) if (!source[key]) missing.push({
|
|
2506
|
+
locale: cfg.sourceLocale,
|
|
2507
|
+
key,
|
|
2508
|
+
source: extracted.get(key)?.message
|
|
2509
|
+
});
|
|
2510
|
+
for (const locale of cfg.locales) {
|
|
2511
|
+
if (locale === cfg.sourceLocale) continue;
|
|
2512
|
+
for (const key of needed) if (!flat[locale]?.[key]) missing.push({
|
|
2513
|
+
locale,
|
|
2668
2514
|
key,
|
|
2669
|
-
|
|
2515
|
+
source: source[key] ?? extracted.get(key)?.message
|
|
2670
2516
|
});
|
|
2671
2517
|
}
|
|
2672
|
-
|
|
2673
|
-
|
|
2674
|
-
|
|
2675
|
-
|
|
2676
|
-
|
|
2518
|
+
const broken = [];
|
|
2519
|
+
const add = (locale, key, issues) => {
|
|
2520
|
+
for (const issue of issues) broken.push({
|
|
2521
|
+
locale,
|
|
2522
|
+
key,
|
|
2523
|
+
severity: issue.severity,
|
|
2524
|
+
issue: issue.message
|
|
2677
2525
|
});
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
|
|
2692
|
-
continue;
|
|
2693
|
-
|
|
2694
|
-
|
|
2695
|
-
|
|
2696
|
-
locale,
|
|
2697
|
-
key: base,
|
|
2698
|
-
reason: "only a plural form, so add the singular before merging"
|
|
2699
|
-
});
|
|
2700
|
-
continue;
|
|
2701
|
-
}
|
|
2702
|
-
const sorted = [...forms.entries()].sort((a, b) => order(a[0], b[0]));
|
|
2703
|
-
const cases = [];
|
|
2704
|
-
let problem;
|
|
2705
|
-
for (const [category, form] of sorted) {
|
|
2706
|
-
problem = fromI18next ? i18nextProblem(form.value) : void 0;
|
|
2707
|
-
const body = problem ? void 0 : toVariant(fromI18next ? convertBraces(form.value) : form.value);
|
|
2708
|
-
if (body === void 0) {
|
|
2709
|
-
problem ??= "a form holds more than a name in braces, so merge it by hand";
|
|
2710
|
-
break;
|
|
2711
|
-
}
|
|
2712
|
-
cases.push(`${category}: ${body}`);
|
|
2526
|
+
};
|
|
2527
|
+
const divergent = [];
|
|
2528
|
+
for (const [key, entry] of extracted) {
|
|
2529
|
+
const written = source[key];
|
|
2530
|
+
if (written && written !== entry.message) divergent.push({
|
|
2531
|
+
key,
|
|
2532
|
+
catalog: written,
|
|
2533
|
+
code: siteOf(entry)
|
|
2534
|
+
});
|
|
2535
|
+
}
|
|
2536
|
+
for (const [key, text] of Object.entries(sourceText)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
2537
|
+
for (const locale of cfg.locales) {
|
|
2538
|
+
if (locale === cfg.sourceLocale) continue;
|
|
2539
|
+
for (const [key, translated] of Object.entries(flat[locale] ?? {})) {
|
|
2540
|
+
if (!translated) continue;
|
|
2541
|
+
add(locale, key, validateMessage(translated, locale));
|
|
2542
|
+
const text = sourceText[key];
|
|
2543
|
+
if (text) add(locale, key, validatePair(text, translated));
|
|
2713
2544
|
}
|
|
2714
|
-
|
|
2715
|
-
|
|
2545
|
+
}
|
|
2546
|
+
const extra = [];
|
|
2547
|
+
for (const locale of cfg.locales) {
|
|
2548
|
+
if (locale === cfg.sourceLocale) continue;
|
|
2549
|
+
for (const key of Object.keys(flat[locale] ?? {})) {
|
|
2550
|
+
if (source[key] !== void 0 || extracted.has(key)) continue;
|
|
2551
|
+
extra.push({
|
|
2716
2552
|
locale,
|
|
2717
|
-
key
|
|
2718
|
-
|
|
2553
|
+
key,
|
|
2554
|
+
files: used.get(key) ?? []
|
|
2719
2555
|
});
|
|
2720
|
-
continue;
|
|
2721
2556
|
}
|
|
2722
|
-
const merged = `{count | ${cases.join(" | ")}}`;
|
|
2723
|
-
for (const [, form] of sorted) delete out[form.key];
|
|
2724
|
-
out[base] = merged;
|
|
2725
|
-
plurals.push({
|
|
2726
|
-
locale,
|
|
2727
|
-
key: base,
|
|
2728
|
-
from: sorted.map(([, form]) => form.key),
|
|
2729
|
-
after: merged
|
|
2730
|
-
});
|
|
2731
2557
|
}
|
|
2558
|
+
const collisions = collisionEntries(registry);
|
|
2559
|
+
return {
|
|
2560
|
+
ok: gatePasses({
|
|
2561
|
+
missing,
|
|
2562
|
+
unknown,
|
|
2563
|
+
broken,
|
|
2564
|
+
collisions
|
|
2565
|
+
}),
|
|
2566
|
+
missing,
|
|
2567
|
+
unknown,
|
|
2568
|
+
broken,
|
|
2569
|
+
extra,
|
|
2570
|
+
collisions,
|
|
2571
|
+
divergent,
|
|
2572
|
+
outdated: outdatedTranslations(cfg, shipped, fingerprints)
|
|
2573
|
+
};
|
|
2732
2574
|
}
|
|
2733
|
-
function
|
|
2734
|
-
const
|
|
2735
|
-
const
|
|
2736
|
-
|
|
2737
|
-
|
|
2738
|
-
|
|
2739
|
-
|
|
2740
|
-
|
|
2741
|
-
const pending = cfg.locales.filter((locale) => !done.has(locale));
|
|
2742
|
-
const fromI18next = pending.some((locale) => Object.values(catalogs.get(locale)).some((value) => typeof value === "string" && (DOUBLE.test(value) || NESTED.test(value))));
|
|
2743
|
-
for (const locale of cfg.locales) {
|
|
2744
|
-
const catalog = catalogs.get(locale);
|
|
2745
|
-
const out = Object.assign(emptyCatalog(), catalog);
|
|
2746
|
-
const convert = fromI18next && !done.has(locale);
|
|
2747
|
-
for (const [key, value] of Object.entries(catalog)) {
|
|
2748
|
-
if (!convert || typeof value !== "string") continue;
|
|
2749
|
-
const problem = i18nextProblem(value);
|
|
2750
|
-
if (problem) {
|
|
2751
|
-
skipped.push({
|
|
2752
|
-
locale,
|
|
2753
|
-
key,
|
|
2754
|
-
reason: problem
|
|
2755
|
-
});
|
|
2756
|
-
continue;
|
|
2757
|
-
}
|
|
2758
|
-
const after = convertBraces(value);
|
|
2759
|
-
if (after !== value) {
|
|
2760
|
-
braces.push({
|
|
2761
|
-
locale,
|
|
2762
|
-
key,
|
|
2763
|
-
before: value,
|
|
2764
|
-
after
|
|
2765
|
-
});
|
|
2766
|
-
out[key] = after;
|
|
2767
|
-
}
|
|
2768
|
-
}
|
|
2769
|
-
if (options.plurals) mergePlurals(locale, catalog, out, plurals, skipped, convert);
|
|
2770
|
-
if ((braces.some((b) => b.locale === locale) || plurals.some((p) => p.locale === locale)) && options.write) {
|
|
2771
|
-
writeCatalog(cfg, locale, out);
|
|
2772
|
-
written.push(locale);
|
|
2575
|
+
function formatCheckResult(result, root) {
|
|
2576
|
+
const show = (file) => root ? relative(root, file).replaceAll("\\", "/") : file;
|
|
2577
|
+
const lines = [];
|
|
2578
|
+
if (result.missing.length > 0) {
|
|
2579
|
+
lines.push("missing translations:");
|
|
2580
|
+
for (const entry of result.missing) {
|
|
2581
|
+
const hint = entry.source ? `: "${truncate(entry.source, 40)}"` : "";
|
|
2582
|
+
lines.push(` [${entry.locale}] ${entry.key}${hint}`);
|
|
2773
2583
|
}
|
|
2774
2584
|
}
|
|
2775
|
-
if (
|
|
2776
|
-
|
|
2777
|
-
|
|
2778
|
-
...state,
|
|
2779
|
-
migrated: [...done, ...converted]
|
|
2780
|
-
});
|
|
2585
|
+
if (result.unknown.length > 0) {
|
|
2586
|
+
lines.push("unknown keys (not in any catalog):");
|
|
2587
|
+
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.map(show).join(", ")})`);
|
|
2781
2588
|
}
|
|
2782
|
-
const
|
|
2783
|
-
|
|
2784
|
-
|
|
2785
|
-
|
|
2786
|
-
|
|
2787
|
-
|
|
2788
|
-
written
|
|
2789
|
-
|
|
2790
|
-
}
|
|
2589
|
+
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
2590
|
+
if (errors.length > 0) {
|
|
2591
|
+
lines.push("broken translations:");
|
|
2592
|
+
for (const entry of errors) lines.push(` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
2593
|
+
}
|
|
2594
|
+
if (result.collisions.length > 0) {
|
|
2595
|
+
lines.push("keys written with more than one text:");
|
|
2596
|
+
for (const entry of result.collisions) lines.push(` ${formatCollision(entry, root)}`);
|
|
2597
|
+
}
|
|
2598
|
+
return lines.join("\n");
|
|
2791
2599
|
}
|
|
2792
|
-
|
|
2793
|
-
|
|
2794
|
-
|
|
2795
|
-
|
|
2796
|
-
|
|
2797
|
-
|
|
2798
|
-
|
|
2600
|
+
function formatBuildReport(result, sourceLocale, root, cap = 5) {
|
|
2601
|
+
const show = (file) => root ? relative(root, file).replaceAll("\\", "/") : file;
|
|
2602
|
+
const lines = [];
|
|
2603
|
+
const list = (entries, line) => {
|
|
2604
|
+
for (const entry of entries.slice(0, cap)) lines.push(` ${line(entry)}`);
|
|
2605
|
+
if (entries.length > cap) lines.push(` and ${entries.length - cap} more`);
|
|
2606
|
+
};
|
|
2607
|
+
const broken = result.broken.filter((entry) => entry.severity === "error");
|
|
2608
|
+
if (broken.length > 0) {
|
|
2609
|
+
lines.push(`✗ the build goes on with ${counted(broken.length, "broken translation")}, each rendering differently from its source:`);
|
|
2610
|
+
list(broken, (entry) => `[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
2611
|
+
}
|
|
2612
|
+
if (result.collisions.length > 0) {
|
|
2613
|
+
lines.push(`✗ the build goes on with ${counted(result.collisions.length, "key")} written with more than one text:`);
|
|
2614
|
+
list(result.collisions, (entry) => formatCollision(entry, root));
|
|
2615
|
+
}
|
|
2616
|
+
const quoted = (entry) => entry.source ? `: "${truncate(entry.source, 40)}"` : "";
|
|
2617
|
+
const untranslated = result.missing.filter((entry) => entry.locale !== sourceLocale);
|
|
2618
|
+
if (untranslated.length > 0) {
|
|
2619
|
+
lines.push(`⚠ the build goes on without ${counted(untranslated.length, "translation")}, shown in ${sourceLocale} instead:`);
|
|
2620
|
+
list(untranslated, (entry) => `[${entry.locale}] ${entry.key}${quoted(entry)}`);
|
|
2799
2621
|
}
|
|
2622
|
+
if (result.unknown.length > 0) {
|
|
2623
|
+
lines.push(`⚠ the build goes on with ${counted(result.unknown.length, "key")} no catalog has, so the key itself shows:`);
|
|
2624
|
+
list(result.unknown, (entry) => `${entry.key} (used in ${entry.files.map(show).join(", ")})`);
|
|
2625
|
+
}
|
|
2626
|
+
const unsynced = result.missing.filter((entry) => entry.locale === sourceLocale);
|
|
2627
|
+
if (unsynced.length > 0) lines.push(`⚠ the build goes on with ${counted(unsynced.length, "text")} the ${sourceLocale} catalog does not have yet, taken from the code`);
|
|
2628
|
+
return lines;
|
|
2800
2629
|
}
|
|
2801
|
-
function
|
|
2802
|
-
const
|
|
2803
|
-
|
|
2630
|
+
function checkNextSteps(result, cliReachable = true) {
|
|
2631
|
+
const steps = [];
|
|
2632
|
+
if (result.missing.length > 0) steps.push("missing: run `npx verbaly extract` to scaffold the keys, then fill them (or run `npx verbaly translate`)");
|
|
2633
|
+
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`");
|
|
2634
|
+
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");
|
|
2635
|
+
if (result.collisions.length > 0) steps.push("collisions: give each text its own key in t.id(…), or write the same text in every place");
|
|
2636
|
+
if (steps.length > 0 && !cliReachable) steps.push(`the verbaly command is not linked in this project: ${CLI_INSTALL_FIX}`);
|
|
2637
|
+
return steps.join("\n");
|
|
2804
2638
|
}
|
|
2805
|
-
function
|
|
2806
|
-
|
|
2807
|
-
const
|
|
2808
|
-
|
|
2639
|
+
function formatCheckWarnings(result, root, options = {}) {
|
|
2640
|
+
const lines = result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
2641
|
+
for (const entry of result.extra) lines.push(entry.files.length > 0 ? ` [${entry.locale}] ${entry.key}: only this translation has it, so the source language shows the key itself` : ` [${entry.locale}] ${entry.key}: only this translation has it, and no code reads it`);
|
|
2642
|
+
for (const { key, catalog, code } of result.divergent) lines.push(` ${key}: the source catalog says "${truncate(catalog, 40)}" and the code says "${truncate(code.message, 40)}" (${sourcePlace(code, root)}): the code wins, so edit the text there (extract writes it into the catalog)`);
|
|
2643
|
+
if (!options.outdatedFails) for (const entry of result.outdated) lines.push(` ${formatFinding({
|
|
2644
|
+
kind: "outdated",
|
|
2645
|
+
...entry
|
|
2646
|
+
}, root)}`);
|
|
2647
|
+
return lines.join("\n");
|
|
2809
2648
|
}
|
|
2810
|
-
|
|
2811
|
-
const
|
|
2812
|
-
"
|
|
2813
|
-
"
|
|
2814
|
-
|
|
2815
|
-
|
|
2816
|
-
|
|
2817
|
-
|
|
2818
|
-
|
|
2819
|
-
|
|
2820
|
-
|
|
2821
|
-
|
|
2822
|
-
];
|
|
2823
|
-
function existingCatalogs(root) {
|
|
2824
|
-
return CATALOG_DIRS.find((dir) => {
|
|
2825
|
-
try {
|
|
2826
|
-
return readdirSync(join(root, dir)).some((file) => file.endsWith(".json") && isLocaleTag(file.slice(0, -5)));
|
|
2827
|
-
} catch {
|
|
2828
|
-
return false;
|
|
2829
|
-
}
|
|
2830
|
-
});
|
|
2831
|
-
}
|
|
2832
|
-
function cliReachable(root) {
|
|
2833
|
-
const bin = join(root, "node_modules", ".bin");
|
|
2834
|
-
return existsSync(join(bin, "verbaly")) || existsSync(join(bin, "verbaly.CMD"));
|
|
2835
|
-
}
|
|
2836
|
-
const CLI_INSTALL_FIX = "add `@verbaly/compiler` to your dev dependencies, then `npx verbaly` resolves";
|
|
2837
|
-
function configSource(options, typescript) {
|
|
2838
|
-
const fields = [` sourceLocale: '${options.sourceLocale ?? "en"}',`];
|
|
2839
|
-
if (options.locales?.length) fields.push(` locales: [${options.locales.map((l) => `'${l}'`).join(", ")}],`);
|
|
2840
|
-
if (options.dir) fields.push(` dir: '${options.dir}',`);
|
|
2841
|
-
const body = `export default {\n${fields.join("\n")}\n}`;
|
|
2842
|
-
if (typescript) return `import type { VerbalyConfig } from '@verbaly/compiler';\n\n${body} satisfies VerbalyConfig;\n`;
|
|
2843
|
-
return `/** @type {import('@verbaly/compiler').VerbalyConfig} */\n${body};\n`;
|
|
2844
|
-
}
|
|
2845
|
-
async function init(input = {}) {
|
|
2846
|
-
const root = input.root ?? process.cwd();
|
|
2847
|
-
const created = [];
|
|
2848
|
-
const skipped = [];
|
|
2849
|
-
const renamed = [];
|
|
2850
|
-
const refused = [];
|
|
2851
|
-
const typed = input.locales?.flatMap((locale) => {
|
|
2852
|
-
if (isLocaleTag(locale)) return [locale];
|
|
2853
|
-
const near = suggestTag(locale);
|
|
2854
|
-
if (near) renamed.push({
|
|
2855
|
-
from: locale,
|
|
2856
|
-
to: near
|
|
2857
|
-
});
|
|
2858
|
-
else refused.push(locale);
|
|
2859
|
-
return near ? [near] : [];
|
|
2860
|
-
});
|
|
2861
|
-
const locales = typed && [...new Set(typed)];
|
|
2862
|
-
let sourceLocale = input.sourceLocale;
|
|
2863
|
-
if (sourceLocale !== void 0 && !isLocaleTag(sourceLocale)) {
|
|
2864
|
-
const near = suggestTag(sourceLocale);
|
|
2865
|
-
if (!near) throw new Error(`[verbaly] "${sourceLocale}" is not a locale tag: pass --source with one like en or pt-BR`);
|
|
2866
|
-
renamed.push({
|
|
2867
|
-
from: sourceLocale,
|
|
2868
|
-
to: near
|
|
2649
|
+
function githubCheckAnnotations(result, registry, cfg, options = {}) {
|
|
2650
|
+
const messages = registry.messages();
|
|
2651
|
+
const rel = (file) => escapeProperty(relative(cfg.root, file).replaceAll("\\", "/"));
|
|
2652
|
+
const at = (file, line) => `file=${rel(file)}${line ? `,line=${line}` : ""}`;
|
|
2653
|
+
const lines = [];
|
|
2654
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
2655
|
+
for (const entry of result.missing) {
|
|
2656
|
+
const grouped = byKey.get(entry.key);
|
|
2657
|
+
if (grouped) grouped.locales.push(entry.locale);
|
|
2658
|
+
else byKey.set(entry.key, {
|
|
2659
|
+
...entry,
|
|
2660
|
+
locales: [entry.locale]
|
|
2869
2661
|
});
|
|
2870
|
-
sourceLocale = near;
|
|
2871
2662
|
}
|
|
2872
|
-
const
|
|
2873
|
-
|
|
2874
|
-
|
|
2875
|
-
|
|
2876
|
-
|
|
2877
|
-
|
|
2878
|
-
const typescript = existsSync(join(root, "tsconfig.json"));
|
|
2879
|
-
const configFile = existing ?? (typescript ? "verbaly.config.ts" : "verbaly.config.mjs");
|
|
2880
|
-
const found = existing || options.dir ? void 0 : existingCatalogs(root);
|
|
2881
|
-
if (existing) skipped.push(existing);
|
|
2882
|
-
else {
|
|
2883
|
-
const dir = found === "locales" ? void 0 : found;
|
|
2884
|
-
writeFileSync(join(root, configFile), configSource({
|
|
2885
|
-
...options,
|
|
2886
|
-
dir: options.dir ?? dir
|
|
2887
|
-
}, typescript));
|
|
2888
|
-
created.push(configFile);
|
|
2663
|
+
for (const entry of byKey.values()) {
|
|
2664
|
+
const origin = messages.get(entry.key);
|
|
2665
|
+
const hint = entry.source ? `: "${truncate(entry.source, 60)}"` : "";
|
|
2666
|
+
const text = escapeData(`missing [${entry.locales.join(", ")}] ${entry.key}${hint}`);
|
|
2667
|
+
if (origin) lines.push(`::error ${at(origin.file, origin.line)}::${text}`);
|
|
2668
|
+
else lines.push(`::error::${text}`);
|
|
2889
2669
|
}
|
|
2890
|
-
const
|
|
2891
|
-
|
|
2892
|
-
|
|
2893
|
-
|
|
2894
|
-
|
|
2895
|
-
|
|
2896
|
-
|
|
2897
|
-
|
|
2898
|
-
|
|
2899
|
-
|
|
2900
|
-
|
|
2901
|
-
|
|
2902
|
-
else {
|
|
2903
|
-
writeFileSync(file, "{}\n");
|
|
2904
|
-
created.push(label);
|
|
2670
|
+
for (const entry of result.unknown) {
|
|
2671
|
+
const text = escapeData(`unknown key "${entry.key}" (not in any catalog)`);
|
|
2672
|
+
const file = entry.files[0];
|
|
2673
|
+
lines.push(file ? `::error file=${rel(file)}::${text}` : `::error::${text}`);
|
|
2674
|
+
}
|
|
2675
|
+
for (const entry of result.broken) {
|
|
2676
|
+
const origin = messages.get(entry.key);
|
|
2677
|
+
const text = escapeData(`[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
2678
|
+
const command = entry.severity === "error" ? "error" : "warning";
|
|
2679
|
+
if (!origin) {
|
|
2680
|
+
lines.push(`::${command}::${text}`);
|
|
2681
|
+
continue;
|
|
2905
2682
|
}
|
|
2683
|
+
lines.push(`::${command} ${at(origin.file, origin.line)}::${text}`);
|
|
2906
2684
|
}
|
|
2907
|
-
const
|
|
2908
|
-
|
|
2909
|
-
|
|
2910
|
-
|
|
2911
|
-
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
2918
|
-
|
|
2919
|
-
|
|
2920
|
-
|
|
2921
|
-
|
|
2922
|
-
}
|
|
2685
|
+
for (const entry of result.extra) {
|
|
2686
|
+
const text = escapeData(`[${entry.locale}] ${entry.key}: only this translation has it`);
|
|
2687
|
+
const file = entry.files[0];
|
|
2688
|
+
lines.push(file ? `::warning file=${rel(file)}::${text}` : `::warning::${text}`);
|
|
2689
|
+
}
|
|
2690
|
+
for (const { key, sites } of result.collisions) {
|
|
2691
|
+
const texts = new Set(sites.map((place) => place.message)).size;
|
|
2692
|
+
const text = escapeData(`"${key}" has ${counted(texts, "text")}, and every place shows the first one`);
|
|
2693
|
+
for (const place of sites) lines.push(`::error ${at(place.file, place.line)}::${text}`);
|
|
2694
|
+
}
|
|
2695
|
+
for (const { key, code } of result.divergent) {
|
|
2696
|
+
const text = escapeData(`"${key}": the source catalog has another text, the code's one ships`);
|
|
2697
|
+
lines.push(`::warning ${at(code.file, code.line)}::${text}`);
|
|
2698
|
+
}
|
|
2699
|
+
const outdated = options.outdatedFails ? "error" : "warning";
|
|
2700
|
+
for (const { locale, key } of result.outdated) {
|
|
2701
|
+
const text = escapeData(`[${locale}] ${key}: translated from an older source text`);
|
|
2702
|
+
lines.push(`::${outdated} file=${rel(catalogPath(cfg, locale))}::${text}`);
|
|
2703
|
+
}
|
|
2704
|
+
return lines;
|
|
2923
2705
|
}
|
|
2924
|
-
|
|
2925
|
-
|
|
2926
|
-
return result.missing.length === 0 && result.unknown.length === 0 && result.collisions.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
2706
|
+
function escapeData(text) {
|
|
2707
|
+
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
2927
2708
|
}
|
|
2928
|
-
function
|
|
2929
|
-
return
|
|
2709
|
+
function escapeProperty(text) {
|
|
2710
|
+
return escapeData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
|
|
2930
2711
|
}
|
|
2931
|
-
|
|
2932
|
-
|
|
2712
|
+
// src/scan/transform.ts
|
|
2713
|
+
function quote(text, single) {
|
|
2714
|
+
if (!single) return JSON.stringify(text);
|
|
2715
|
+
return `'${text.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
2933
2716
|
}
|
|
2934
|
-
function
|
|
2935
|
-
const
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
2939
|
-
const
|
|
2940
|
-
const
|
|
2941
|
-
|
|
2942
|
-
|
|
2943
|
-
|
|
2944
|
-
|
|
2945
|
-
|
|
2946
|
-
|
|
2947
|
-
|
|
2948
|
-
|
|
2949
|
-
for (const key of extracted.keys()) if (!source[key]) missing.push({
|
|
2950
|
-
locale: cfg.sourceLocale,
|
|
2951
|
-
key,
|
|
2952
|
-
source: extracted.get(key)?.message
|
|
2953
|
-
});
|
|
2954
|
-
for (const locale of cfg.locales) {
|
|
2955
|
-
if (locale === cfg.sourceLocale) continue;
|
|
2956
|
-
for (const key of needed) if (!flat[locale]?.[key]) missing.push({
|
|
2957
|
-
locale,
|
|
2958
|
-
key,
|
|
2959
|
-
source: source[key] ?? extracted.get(key)?.message
|
|
2960
|
-
});
|
|
2961
|
-
}
|
|
2962
|
-
const broken = [];
|
|
2963
|
-
const add = (locale, key, issues) => {
|
|
2964
|
-
for (const issue of issues) broken.push({
|
|
2965
|
-
locale,
|
|
2966
|
-
key,
|
|
2967
|
-
severity: issue.severity,
|
|
2968
|
-
issue: issue.message
|
|
2969
|
-
});
|
|
2970
|
-
};
|
|
2971
|
-
const divergent = [];
|
|
2972
|
-
for (const [key, entry] of extracted) {
|
|
2973
|
-
const written = source[key];
|
|
2974
|
-
if (written && written !== entry.message) divergent.push({
|
|
2975
|
-
key,
|
|
2976
|
-
catalog: written,
|
|
2977
|
-
code: siteOf(entry)
|
|
2978
|
-
});
|
|
2979
|
-
}
|
|
2980
|
-
for (const [key, text] of Object.entries(sourceText)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
2981
|
-
for (const locale of cfg.locales) {
|
|
2982
|
-
if (locale === cfg.sourceLocale) continue;
|
|
2983
|
-
for (const [key, translated] of Object.entries(flat[locale] ?? {})) {
|
|
2984
|
-
if (!translated) continue;
|
|
2985
|
-
add(locale, key, validateMessage(translated, locale));
|
|
2986
|
-
const text = sourceText[key];
|
|
2987
|
-
if (text) add(locale, key, validatePair(text, translated));
|
|
2988
|
-
}
|
|
2989
|
-
}
|
|
2990
|
-
const extra = [];
|
|
2991
|
-
for (const locale of cfg.locales) {
|
|
2992
|
-
if (locale === cfg.sourceLocale) continue;
|
|
2993
|
-
for (const key of Object.keys(flat[locale] ?? {})) {
|
|
2994
|
-
if (source[key] !== void 0 || extracted.has(key)) continue;
|
|
2995
|
-
extra.push({
|
|
2996
|
-
locale,
|
|
2997
|
-
key,
|
|
2998
|
-
files: used.get(key) ?? []
|
|
2999
|
-
});
|
|
2717
|
+
function transformCode(code, file, analysis) {
|
|
2718
|
+
const own = analysis ?? analyzeFile(code, file);
|
|
2719
|
+
if (!analysis && own.parseError) warnParseError(file, own.parseError);
|
|
2720
|
+
const { tagged } = own;
|
|
2721
|
+
if (tagged.length === 0) return null;
|
|
2722
|
+
const s = new MagicString(code);
|
|
2723
|
+
for (const msg of tagged) {
|
|
2724
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2725
|
+
const entries = msg.params.filter((p) => !seen.has(p.name) && seen.add(p.name));
|
|
2726
|
+
const pairs = entries.map((p) => `${quote(p.name, msg.singleQuote)}: ${code.slice(p.start, p.end)}`).join(", ");
|
|
2727
|
+
if (msg.jsx) {
|
|
2728
|
+
const values = entries.length ? ` values={{ ${pairs} }}` : "";
|
|
2729
|
+
const components = msg.jsx.components.length ? ` components={{ ${msg.jsx.components.map((c) => `${JSON.stringify(c.name)}: ${c.source}`).join(", ")} }}` : "";
|
|
2730
|
+
s.overwrite(msg.start, msg.end, `<${msg.jsx.name} id=${JSON.stringify(msg.key)}${values}${components} />`);
|
|
2731
|
+
continue;
|
|
3000
2732
|
}
|
|
2733
|
+
const tagSource = code.slice(msg.tagStart, msg.tagEnd);
|
|
2734
|
+
const args = entries.length ? `, { ${pairs} }` : "";
|
|
2735
|
+
s.overwrite(msg.start, msg.end, `${tagSource}(${quote(msg.key, msg.singleQuote)}${args})`);
|
|
3001
2736
|
}
|
|
3002
|
-
const collisions = collisionEntries(registry);
|
|
3003
2737
|
return {
|
|
3004
|
-
|
|
3005
|
-
|
|
3006
|
-
unknown,
|
|
3007
|
-
broken,
|
|
3008
|
-
collisions
|
|
3009
|
-
}),
|
|
3010
|
-
missing,
|
|
3011
|
-
unknown,
|
|
3012
|
-
broken,
|
|
3013
|
-
extra,
|
|
3014
|
-
collisions,
|
|
3015
|
-
divergent,
|
|
3016
|
-
outdated: outdatedTranslations(cfg, shipped, fingerprints)
|
|
2738
|
+
code: s.toString(),
|
|
2739
|
+
map: s.generateMap({ hires: true })
|
|
3017
2740
|
};
|
|
3018
2741
|
}
|
|
3019
|
-
|
|
3020
|
-
|
|
3021
|
-
const lines = [];
|
|
3022
|
-
if (result.missing.length > 0) {
|
|
3023
|
-
lines.push("missing translations:");
|
|
3024
|
-
for (const entry of result.missing) {
|
|
3025
|
-
const hint = entry.source ? `: "${truncate(entry.source, 40)}"` : "";
|
|
3026
|
-
lines.push(` [${entry.locale}] ${entry.key}${hint}`);
|
|
3027
|
-
}
|
|
3028
|
-
}
|
|
3029
|
-
if (result.unknown.length > 0) {
|
|
3030
|
-
lines.push("unknown keys (not in any catalog):");
|
|
3031
|
-
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.map(show).join(", ")})`);
|
|
3032
|
-
}
|
|
3033
|
-
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
3034
|
-
if (errors.length > 0) {
|
|
3035
|
-
lines.push("broken translations:");
|
|
3036
|
-
for (const entry of errors) lines.push(` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
3037
|
-
}
|
|
3038
|
-
if (result.collisions.length > 0) {
|
|
3039
|
-
lines.push("keys written with more than one text:");
|
|
3040
|
-
for (const entry of result.collisions) lines.push(` ${formatCollision(entry, root)}`);
|
|
3041
|
-
}
|
|
3042
|
-
return lines.join("\n");
|
|
3043
|
-
}
|
|
3044
|
-
function formatBuildReport(result, sourceLocale, root, cap = 5) {
|
|
3045
|
-
const show = (file) => root ? relative(root, file).replaceAll("\\", "/") : file;
|
|
3046
|
-
const lines = [];
|
|
3047
|
-
const list = (entries, line) => {
|
|
3048
|
-
for (const entry of entries.slice(0, cap)) lines.push(` ${line(entry)}`);
|
|
3049
|
-
if (entries.length > cap) lines.push(` and ${entries.length - cap} more`);
|
|
3050
|
-
};
|
|
3051
|
-
const broken = result.broken.filter((entry) => entry.severity === "error");
|
|
3052
|
-
if (broken.length > 0) {
|
|
3053
|
-
lines.push(`✗ the build goes on with ${counted(broken.length, "broken translation")}, each rendering differently from its source:`);
|
|
3054
|
-
list(broken, (entry) => `[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
3055
|
-
}
|
|
3056
|
-
if (result.collisions.length > 0) {
|
|
3057
|
-
lines.push(`✗ the build goes on with ${counted(result.collisions.length, "key")} written with more than one text:`);
|
|
3058
|
-
list(result.collisions, (entry) => formatCollision(entry, root));
|
|
3059
|
-
}
|
|
3060
|
-
const quoted = (entry) => entry.source ? `: "${truncate(entry.source, 40)}"` : "";
|
|
3061
|
-
const untranslated = result.missing.filter((entry) => entry.locale !== sourceLocale);
|
|
3062
|
-
if (untranslated.length > 0) {
|
|
3063
|
-
lines.push(`⚠ the build goes on without ${counted(untranslated.length, "translation")}, shown in ${sourceLocale} instead:`);
|
|
3064
|
-
list(untranslated, (entry) => `[${entry.locale}] ${entry.key}${quoted(entry)}`);
|
|
3065
|
-
}
|
|
3066
|
-
if (result.unknown.length > 0) {
|
|
3067
|
-
lines.push(`⚠ the build goes on with ${counted(result.unknown.length, "key")} no catalog has, so the key itself shows:`);
|
|
3068
|
-
list(result.unknown, (entry) => `${entry.key} (used in ${entry.files.map(show).join(", ")})`);
|
|
3069
|
-
}
|
|
3070
|
-
const unsynced = result.missing.filter((entry) => entry.locale === sourceLocale);
|
|
3071
|
-
if (unsynced.length > 0) lines.push(`⚠ the build goes on with ${counted(unsynced.length, "text")} the ${sourceLocale} catalog does not have yet, taken from the code`);
|
|
3072
|
-
return lines;
|
|
3073
|
-
}
|
|
3074
|
-
function checkNextSteps(result, cliReachable = true) {
|
|
3075
|
-
const steps = [];
|
|
3076
|
-
if (result.missing.length > 0) steps.push("missing: run `npx verbaly extract` to scaffold the keys, then fill them (or run `npx verbaly translate`)");
|
|
3077
|
-
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`");
|
|
3078
|
-
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");
|
|
3079
|
-
if (result.collisions.length > 0) steps.push("collisions: give each text its own key in t.id(…), or write the same text in every place");
|
|
3080
|
-
if (steps.length > 0 && !cliReachable) steps.push(`the verbaly command is not linked in this project: ${CLI_INSTALL_FIX}`);
|
|
3081
|
-
return steps.join("\n");
|
|
3082
|
-
}
|
|
3083
|
-
function formatCheckWarnings(result, root, options = {}) {
|
|
3084
|
-
const lines = result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
3085
|
-
for (const entry of result.extra) lines.push(entry.files.length > 0 ? ` [${entry.locale}] ${entry.key}: only this translation has it, so the source language shows the key itself` : ` [${entry.locale}] ${entry.key}: only this translation has it, and no code reads it`);
|
|
3086
|
-
for (const { key, catalog, code } of result.divergent) lines.push(` ${key}: the source catalog says "${truncate(catalog, 40)}" and the code says "${truncate(code.message, 40)}" (${sourcePlace(code, root)}): the code wins, so edit the text there (extract writes it into the catalog)`);
|
|
3087
|
-
if (!options.outdatedFails) for (const entry of result.outdated) lines.push(` ${formatFinding({
|
|
3088
|
-
kind: "outdated",
|
|
3089
|
-
...entry
|
|
3090
|
-
}, root)}`);
|
|
3091
|
-
return lines.join("\n");
|
|
3092
|
-
}
|
|
3093
|
-
function githubCheckAnnotations(result, registry, cfg, options = {}) {
|
|
3094
|
-
const messages = registry.messages();
|
|
3095
|
-
const rel = (file) => escapeProperty(relative(cfg.root, file).replaceAll("\\", "/"));
|
|
3096
|
-
const at = (file, line) => `file=${rel(file)}${line ? `,line=${line}` : ""}`;
|
|
3097
|
-
const lines = [];
|
|
3098
|
-
const byKey = /* @__PURE__ */ new Map();
|
|
3099
|
-
for (const entry of result.missing) {
|
|
3100
|
-
const grouped = byKey.get(entry.key);
|
|
3101
|
-
if (grouped) grouped.locales.push(entry.locale);
|
|
3102
|
-
else byKey.set(entry.key, {
|
|
3103
|
-
...entry,
|
|
3104
|
-
locales: [entry.locale]
|
|
3105
|
-
});
|
|
3106
|
-
}
|
|
3107
|
-
for (const entry of byKey.values()) {
|
|
3108
|
-
const origin = messages.get(entry.key);
|
|
3109
|
-
const hint = entry.source ? `: "${truncate(entry.source, 60)}"` : "";
|
|
3110
|
-
const text = escapeData(`missing [${entry.locales.join(", ")}] ${entry.key}${hint}`);
|
|
3111
|
-
if (origin) lines.push(`::error ${at(origin.file, origin.line)}::${text}`);
|
|
3112
|
-
else lines.push(`::error::${text}`);
|
|
3113
|
-
}
|
|
3114
|
-
for (const entry of result.unknown) {
|
|
3115
|
-
const text = escapeData(`unknown key "${entry.key}" (not in any catalog)`);
|
|
3116
|
-
const file = entry.files[0];
|
|
3117
|
-
lines.push(file ? `::error file=${rel(file)}::${text}` : `::error::${text}`);
|
|
3118
|
-
}
|
|
3119
|
-
for (const entry of result.broken) {
|
|
3120
|
-
const origin = messages.get(entry.key);
|
|
3121
|
-
const text = escapeData(`[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
3122
|
-
const command = entry.severity === "error" ? "error" : "warning";
|
|
3123
|
-
if (!origin) {
|
|
3124
|
-
lines.push(`::${command}::${text}`);
|
|
3125
|
-
continue;
|
|
3126
|
-
}
|
|
3127
|
-
lines.push(`::${command} ${at(origin.file, origin.line)}::${text}`);
|
|
3128
|
-
}
|
|
3129
|
-
for (const entry of result.extra) {
|
|
3130
|
-
const text = escapeData(`[${entry.locale}] ${entry.key}: only this translation has it`);
|
|
3131
|
-
const file = entry.files[0];
|
|
3132
|
-
lines.push(file ? `::warning file=${rel(file)}::${text}` : `::warning::${text}`);
|
|
3133
|
-
}
|
|
3134
|
-
for (const { key, sites } of result.collisions) {
|
|
3135
|
-
const texts = new Set(sites.map((place) => place.message)).size;
|
|
3136
|
-
const text = escapeData(`"${key}" has ${counted(texts, "text")}, and every place shows the first one`);
|
|
3137
|
-
for (const place of sites) lines.push(`::error ${at(place.file, place.line)}::${text}`);
|
|
3138
|
-
}
|
|
3139
|
-
for (const { key, code } of result.divergent) {
|
|
3140
|
-
const text = escapeData(`"${key}": the source catalog has another text, the code's one ships`);
|
|
3141
|
-
lines.push(`::warning ${at(code.file, code.line)}::${text}`);
|
|
3142
|
-
}
|
|
3143
|
-
const outdated = options.outdatedFails ? "error" : "warning";
|
|
3144
|
-
for (const { locale, key } of result.outdated) {
|
|
3145
|
-
const text = escapeData(`[${locale}] ${key}: translated from an older source text`);
|
|
3146
|
-
lines.push(`::${outdated} file=${rel(catalogPath(cfg, locale))}::${text}`);
|
|
3147
|
-
}
|
|
3148
|
-
return lines;
|
|
3149
|
-
}
|
|
3150
|
-
function escapeData(text) {
|
|
3151
|
-
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
3152
|
-
}
|
|
3153
|
-
function escapeProperty(text) {
|
|
3154
|
-
return escapeData(text).replaceAll(":", "%3A").replaceAll(",", "%2C");
|
|
3155
|
-
}
|
|
3156
|
-
// src/transform.ts
|
|
3157
|
-
function quote(text, single) {
|
|
3158
|
-
if (!single) return JSON.stringify(text);
|
|
3159
|
-
return `'${text.replace(/\\/g, "\\\\").replace(/'/g, "\\'")}'`;
|
|
3160
|
-
}
|
|
3161
|
-
function transformCode(code, file, analysis) {
|
|
3162
|
-
const own = analysis ?? analyzeFile(code, file);
|
|
3163
|
-
if (!analysis && own.parseError) warnParseError(file, own.parseError);
|
|
3164
|
-
const { tagged } = own;
|
|
3165
|
-
if (tagged.length === 0) return null;
|
|
3166
|
-
const s = new MagicString(code);
|
|
3167
|
-
for (const msg of tagged) {
|
|
3168
|
-
const seen = /* @__PURE__ */ new Set();
|
|
3169
|
-
const entries = msg.params.filter((p) => !seen.has(p.name) && seen.add(p.name));
|
|
3170
|
-
const pairs = entries.map((p) => `${quote(p.name, msg.singleQuote)}: ${code.slice(p.start, p.end)}`).join(", ");
|
|
3171
|
-
if (msg.jsx) {
|
|
3172
|
-
const values = entries.length ? ` values={{ ${pairs} }}` : "";
|
|
3173
|
-
const components = msg.jsx.components.length ? ` components={{ ${msg.jsx.components.map((c) => `${JSON.stringify(c.name)}: ${c.source}`).join(", ")} }}` : "";
|
|
3174
|
-
s.overwrite(msg.start, msg.end, `<${msg.jsx.name} id=${JSON.stringify(msg.key)}${values}${components} />`);
|
|
3175
|
-
continue;
|
|
3176
|
-
}
|
|
3177
|
-
const tagSource = code.slice(msg.tagStart, msg.tagEnd);
|
|
3178
|
-
const args = entries.length ? `, { ${pairs} }` : "";
|
|
3179
|
-
s.overwrite(msg.start, msg.end, `${tagSource}(${quote(msg.key, msg.singleQuote)}${args})`);
|
|
3180
|
-
}
|
|
3181
|
-
return {
|
|
3182
|
-
code: s.toString(),
|
|
3183
|
-
map: s.generateMap({ hires: true })
|
|
3184
|
-
};
|
|
3185
|
-
}
|
|
3186
|
-
// src/plugin.ts
|
|
3187
|
-
const RESOLVED_VIRTUAL_ID = "\0virtual:verbaly";
|
|
2742
|
+
// src/emit/plugin.ts
|
|
2743
|
+
const RESOLVED_VIRTUAL_ID = "\0" + VIRTUAL_ID;
|
|
3188
2744
|
const LOCALE_MODULE_PREFIX = `${RESOLVED_VIRTUAL_ID}/locale/`;
|
|
3189
2745
|
const SOURCE_FILE_RE = /\.(?:[cm]?[jt]sx?|svelte|vue|astro)$/;
|
|
3190
2746
|
function resolveVirtualId(id) {
|
|
3191
|
-
if (id
|
|
2747
|
+
if (isAppModule(id)) return RESOLVED_VIRTUAL_ID;
|
|
2748
|
+
if (id.startsWith("@verbaly/app/locale/")) return LOCALE_MODULE_PREFIX + id.slice(20);
|
|
2749
|
+
if (id.startsWith(`virtual:verbaly/`)) return "\0" + id;
|
|
3192
2750
|
}
|
|
3193
2751
|
function loadVirtualModule(id, cfg, catalogs) {
|
|
3194
2752
|
const client = clientCatalogs(cfg, catalogs);
|
|
@@ -3221,6 +2779,15 @@ function transformSource(code, id, registry, scan) {
|
|
|
3221
2779
|
result: transformCode(code, id, analysis) ?? null
|
|
3222
2780
|
};
|
|
3223
2781
|
}
|
|
2782
|
+
function writeBuildTypes(cfg, catalogs) {
|
|
2783
|
+
try {
|
|
2784
|
+
writeDts(cfg, catalogs[cfg.sourceLocale] ?? {});
|
|
2785
|
+
} catch (error) {
|
|
2786
|
+
const code = error?.code;
|
|
2787
|
+
if (typeof code !== "string") throw error;
|
|
2788
|
+
warnOnce(`could not write the types to ${cfg.dts === false ? "" : relative(cfg.root, cfg.dts).replaceAll("\\", "/")} (${code}): run \`npx verbaly typegen\` where the project can be written`, `types:${cfg.root}`);
|
|
2789
|
+
}
|
|
2790
|
+
}
|
|
3224
2791
|
function runBuildGate(cfg, registry, failOnMissing) {
|
|
3225
2792
|
const catalogs = loadCatalogs(cfg);
|
|
3226
2793
|
for (const issue of auditBundle(cfg, catalogs, registry)) warnOnce(`${formatBundleIssue(issue)}\n fix: ${issue.fix}`, `bundle:${issue.prefix}`);
|
|
@@ -3265,7 +2832,7 @@ function buildFingerprints(cfg) {
|
|
|
3265
2832
|
if (problem) warnOnce(`${problem}, so outdated translations are not reported`, "gate:state");
|
|
3266
2833
|
return state.fingerprints;
|
|
3267
2834
|
}
|
|
3268
|
-
// src/scope.ts
|
|
2835
|
+
// src/scan/scope.ts
|
|
3269
2836
|
const TAGGED_HINT = /(?<![\w$.])\$?t(?:\.id\s*\([^)]*\))?\s*`/;
|
|
3270
2837
|
const NOT_SOURCE = [
|
|
3271
2838
|
"**/node_modules/**",
|
|
@@ -3331,7 +2898,7 @@ async function filesOutsideInclude(cfg) {
|
|
|
3331
2898
|
}
|
|
3332
2899
|
return found;
|
|
3333
2900
|
}
|
|
3334
|
-
// src/status.ts
|
|
2901
|
+
// src/gate/status.ts
|
|
3335
2902
|
function status(cfg, catalogs, registry, drafts = {}, fingerprints = {}) {
|
|
3336
2903
|
const extracted = registry.messages();
|
|
3337
2904
|
const flat = {};
|
|
@@ -3388,7 +2955,129 @@ function formatStatusResult(result) {
|
|
|
3388
2955
|
}
|
|
3389
2956
|
return lines.join("\n");
|
|
3390
2957
|
}
|
|
3391
|
-
// src/
|
|
2958
|
+
// src/cli/args.ts
|
|
2959
|
+
const OPTIONS = {
|
|
2960
|
+
root: { type: "string" },
|
|
2961
|
+
dir: { type: "string" },
|
|
2962
|
+
source: { type: "string" },
|
|
2963
|
+
locales: { type: "string" },
|
|
2964
|
+
prune: { type: "boolean" },
|
|
2965
|
+
watch: { type: "boolean" },
|
|
2966
|
+
write: { type: "boolean" },
|
|
2967
|
+
json: { type: "boolean" },
|
|
2968
|
+
drafts: { type: "boolean" },
|
|
2969
|
+
outdated: { type: "boolean" },
|
|
2970
|
+
draft: { type: "boolean" },
|
|
2971
|
+
approve: { type: "boolean" },
|
|
2972
|
+
reporter: { type: "string" },
|
|
2973
|
+
model: { type: "string" },
|
|
2974
|
+
locale: { type: "string" },
|
|
2975
|
+
site: { type: "string" },
|
|
2976
|
+
attribute: { type: "string" },
|
|
2977
|
+
base: { type: "string" },
|
|
2978
|
+
"base-url": { type: "string" },
|
|
2979
|
+
redirect: { type: "boolean" },
|
|
2980
|
+
sitemap: { type: "boolean" },
|
|
2981
|
+
clean: { type: "boolean" },
|
|
2982
|
+
"dry-run": { type: "boolean" },
|
|
2983
|
+
plurals: { type: "boolean" },
|
|
2984
|
+
format: { type: "string" },
|
|
2985
|
+
out: { type: "string" },
|
|
2986
|
+
missing: { type: "boolean" },
|
|
2987
|
+
overwrite: { type: "boolean" },
|
|
2988
|
+
help: {
|
|
2989
|
+
type: "boolean",
|
|
2990
|
+
short: "h"
|
|
2991
|
+
}
|
|
2992
|
+
};
|
|
2993
|
+
function parseCli(args) {
|
|
2994
|
+
return parseArgs({
|
|
2995
|
+
args,
|
|
2996
|
+
allowPositionals: true,
|
|
2997
|
+
options: OPTIONS
|
|
2998
|
+
});
|
|
2999
|
+
}
|
|
3000
|
+
function csv(value) {
|
|
3001
|
+
return value?.split(",").map((item) => item.trim()).filter(Boolean);
|
|
3002
|
+
}
|
|
3003
|
+
// src/cli/output.ts
|
|
3004
|
+
function sayLockNews(cfg, before) {
|
|
3005
|
+
const news = lockNews(cfg, before);
|
|
3006
|
+
if (news) console.log(` ${formatFinding({
|
|
3007
|
+
kind: "lock",
|
|
3008
|
+
...news
|
|
3009
|
+
}, cfg.root)}`);
|
|
3010
|
+
}
|
|
3011
|
+
function byLocale(entries) {
|
|
3012
|
+
const out = /* @__PURE__ */ new Map();
|
|
3013
|
+
for (const { locale, key } of entries) {
|
|
3014
|
+
const keys = out.get(locale);
|
|
3015
|
+
if (keys) keys.push(key);
|
|
3016
|
+
else out.set(locale, [key]);
|
|
3017
|
+
}
|
|
3018
|
+
return [...out];
|
|
3019
|
+
}
|
|
3020
|
+
// src/cli/commands/check.ts
|
|
3021
|
+
async function checkCommand(cfg, values) {
|
|
3022
|
+
const reporter = values.reporter ?? "text";
|
|
3023
|
+
if (reporter !== "text" && reporter !== "github") {
|
|
3024
|
+
console.error(`[verbaly] unknown reporter "${values.reporter}", use text or github`);
|
|
3025
|
+
process.exitCode = 1;
|
|
3026
|
+
return;
|
|
3027
|
+
}
|
|
3028
|
+
const registry = await extractProject(cfg);
|
|
3029
|
+
const catalogs = loadCatalogs(cfg);
|
|
3030
|
+
const state = checkState(cfg, values.drafts === true || values.outdated === true);
|
|
3031
|
+
const result = check(cfg, catalogs, registry, state.fingerprints);
|
|
3032
|
+
const unreviewed = values.drafts ? effectiveDrafts(state.drafts, catalogs) : {};
|
|
3033
|
+
const outdated = values.outdated ? result.outdated : [];
|
|
3034
|
+
const gated = { outdatedFails: values.outdated === true };
|
|
3035
|
+
if (reporter === "github") for (const line of githubCheckAnnotations(result, registry, cfg, gated)) console.error(line);
|
|
3036
|
+
else {
|
|
3037
|
+
const warnings = formatCheckWarnings(result, cfg.root, gated);
|
|
3038
|
+
if (warnings) console.warn(`[verbaly] warnings (the gate still passes)\n${warnings}`);
|
|
3039
|
+
}
|
|
3040
|
+
const awaiting = [...Object.entries(unreviewed).map(([locale, keys]) => ` [${locale}] ${keys.length} unreviewed: ${keys.join(", ")}`), ...byLocale(outdated).map(([locale, keys]) => ` [${locale}] ${keys.length} outdated: ${keys.join(", ")}`)];
|
|
3041
|
+
if (result.ok && awaiting.length === 0) {
|
|
3042
|
+
console.log("[verbaly] all translations complete ✓");
|
|
3043
|
+
return;
|
|
3044
|
+
}
|
|
3045
|
+
if (!result.ok) {
|
|
3046
|
+
const brokenCount = result.broken.filter((entry) => entry.severity === "error").length;
|
|
3047
|
+
const report = reporter === "github" ? `[verbaly] check failed: ${result.missing.length} missing, ${result.unknown.length} unknown, ${brokenCount} broken, ${counted(result.collisions.length, "collision")}` : `[verbaly] check failed\n${formatCheckResult(result, cfg.root)}`;
|
|
3048
|
+
console.error(`${report}\n${checkNextSteps(result)}`);
|
|
3049
|
+
}
|
|
3050
|
+
if (awaiting.length > 0) {
|
|
3051
|
+
for (const line of awaiting) console.error(line);
|
|
3052
|
+
console.error("[verbaly] check failed: translations awaiting review (run verbaly review, then --approve what holds)");
|
|
3053
|
+
}
|
|
3054
|
+
process.exitCode = 1;
|
|
3055
|
+
}
|
|
3056
|
+
function checkState(cfg, gating) {
|
|
3057
|
+
if (gating) return loadState(cfg);
|
|
3058
|
+
const { state, problem } = readState(cfg);
|
|
3059
|
+
if (problem) console.warn(`[verbaly] ${problem}, so outdated translations are not reported`);
|
|
3060
|
+
return state;
|
|
3061
|
+
}
|
|
3062
|
+
// src/util/tag.ts
|
|
3063
|
+
function isLocaleTag(tag) {
|
|
3064
|
+
try {
|
|
3065
|
+
Intl.getCanonicalLocales(tag);
|
|
3066
|
+
return true;
|
|
3067
|
+
} catch {
|
|
3068
|
+
return false;
|
|
3069
|
+
}
|
|
3070
|
+
}
|
|
3071
|
+
function suggestTag(tag) {
|
|
3072
|
+
const near = tag.replace(/_/g, "-");
|
|
3073
|
+
return near !== tag && isLocaleTag(near) ? near : void 0;
|
|
3074
|
+
}
|
|
3075
|
+
function projectLocale(tag, locales) {
|
|
3076
|
+
if (locales.includes(tag)) return tag;
|
|
3077
|
+
const plain = (value) => value.replace(/_/g, "-").toLowerCase();
|
|
3078
|
+
return locales.find((locale) => plain(locale) === plain(tag));
|
|
3079
|
+
}
|
|
3080
|
+
// src/project/doctor.ts
|
|
3392
3081
|
const PREVIEW = 5;
|
|
3393
3082
|
function readRoutingChoice(root, configFile) {
|
|
3394
3083
|
try {
|
|
@@ -3531,7 +3220,7 @@ async function doctor(cfg) {
|
|
|
3531
3220
|
const stray = registry.strayImports();
|
|
3532
3221
|
if (stray.length > 0) {
|
|
3533
3222
|
const files = [...new Set(stray.map((entry) => rel(entry.file)))];
|
|
3534
|
-
error("imports", `t is imported from a verbaly package, which never exports it, in ${counted(files.length, "file")} (${preview(files)})`, "t comes from your instance (React: const t = useT()) or from
|
|
3223
|
+
error("imports", `t is imported from a verbaly package, which never exports it, in ${counted(files.length, "file")} (${preview(files)})`, "t comes from your instance (React: const t = useT()) or from verbaly/app");
|
|
3535
3224
|
}
|
|
3536
3225
|
const missed = registry.missed();
|
|
3537
3226
|
if (missed.length > 0) {
|
|
@@ -3614,7 +3303,23 @@ function preview(keys) {
|
|
|
3614
3303
|
const head = keys.slice(0, PREVIEW).join(", ");
|
|
3615
3304
|
return keys.length > PREVIEW ? `${head}, …` : head;
|
|
3616
3305
|
}
|
|
3617
|
-
// src/
|
|
3306
|
+
// src/cli/commands/doctor.ts
|
|
3307
|
+
async function doctorCommand(cfg) {
|
|
3308
|
+
const result = await doctor(cfg);
|
|
3309
|
+
console.log(`[verbaly] doctor: ${counted(result.entries.length, "check")}`);
|
|
3310
|
+
for (const entry of result.entries) {
|
|
3311
|
+
const line = formatDoctorEntry(entry);
|
|
3312
|
+
if (entry.level === "error") console.error(line);
|
|
3313
|
+
else if (entry.level === "warn") console.warn(line);
|
|
3314
|
+
else console.log(line);
|
|
3315
|
+
}
|
|
3316
|
+
if (result.ok) console.log(`[verbaly] ${formatDoctorHealth(result)} ✓`);
|
|
3317
|
+
else {
|
|
3318
|
+
console.error("[verbaly] doctor found problems");
|
|
3319
|
+
process.exitCode = 1;
|
|
3320
|
+
}
|
|
3321
|
+
}
|
|
3322
|
+
// src/exchange/inline.ts
|
|
3618
3323
|
const OPEN_TAG = /^<([a-zA-Z][a-zA-Z0-9-]*)\s*(\/?)>/;
|
|
3619
3324
|
const BREAK = /^(?:br|wbr)$/;
|
|
3620
3325
|
function messageToInline(text) {
|
|
@@ -3691,7 +3396,7 @@ function parseParts(text, pos, closeTag) {
|
|
|
3691
3396
|
continue;
|
|
3692
3397
|
}
|
|
3693
3398
|
if (text[i] === "{") {
|
|
3694
|
-
const end = matchBrace(text, i);
|
|
3399
|
+
const end = matchBrace$1(text, i);
|
|
3695
3400
|
if (end === -1) {
|
|
3696
3401
|
plain += "{";
|
|
3697
3402
|
i += 1;
|
|
@@ -3760,7 +3465,7 @@ function parseParts(text, pos, closeTag) {
|
|
|
3760
3465
|
closed: false
|
|
3761
3466
|
};
|
|
3762
3467
|
}
|
|
3763
|
-
function matchBrace(text, start) {
|
|
3468
|
+
function matchBrace$1(text, start) {
|
|
3764
3469
|
let depth = 0;
|
|
3765
3470
|
for (let i = start; i < text.length; i++) {
|
|
3766
3471
|
const pair = text[i] + (text[i + 1] ?? "");
|
|
@@ -3830,7 +3535,7 @@ function attr(attrs, name) {
|
|
|
3830
3535
|
const value = new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`).exec(attrs)?.[1];
|
|
3831
3536
|
return value === void 0 ? void 0 : unescapeXml(value);
|
|
3832
3537
|
}
|
|
3833
|
-
// src/mobile.ts
|
|
3538
|
+
// src/exchange/mobile.ts
|
|
3834
3539
|
function androidValuesDir(locale, sourceLocale) {
|
|
3835
3540
|
if (locale === sourceLocale) return "values";
|
|
3836
3541
|
const parts = locale.split("-");
|
|
@@ -3868,7 +3573,7 @@ function androidText(text) {
|
|
|
3868
3573
|
function iosText(text) {
|
|
3869
3574
|
return text.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\t/g, "\\t");
|
|
3870
3575
|
}
|
|
3871
|
-
// src/po.ts
|
|
3576
|
+
// src/exchange/po.ts
|
|
3872
3577
|
function toPo(sourceLocale, locale, entries) {
|
|
3873
3578
|
return [
|
|
3874
3579
|
[
|
|
@@ -3963,7 +3668,7 @@ function poUnquote(text) {
|
|
|
3963
3668
|
return ch;
|
|
3964
3669
|
});
|
|
3965
3670
|
}
|
|
3966
|
-
// src/translate.ts
|
|
3671
|
+
// src/exchange/translate.ts
|
|
3967
3672
|
const RETRY_STATUS = /* @__PURE__ */ new Set([
|
|
3968
3673
|
408,
|
|
3969
3674
|
409,
|
|
@@ -4142,7 +3847,7 @@ function formatTranslateFailures(failed) {
|
|
|
4142
3847
|
async function resolveProvider(cfg, model) {
|
|
4143
3848
|
const configured = cfg.translate.provider;
|
|
4144
3849
|
if (typeof configured === "function") return configured;
|
|
4145
|
-
const { claudeProvider } = await import("./claude-
|
|
3850
|
+
const { claudeProvider } = await import("./claude-BtdaNZEw.js");
|
|
4146
3851
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
4147
3852
|
}
|
|
4148
3853
|
function structureMatches(source, translated) {
|
|
@@ -4172,7 +3877,7 @@ function glossaryFor(cfg, locale, messages) {
|
|
|
4172
3877
|
}
|
|
4173
3878
|
return Object.keys(out).length > 0 ? out : void 0;
|
|
4174
3879
|
}
|
|
4175
|
-
// src/exchange.ts
|
|
3880
|
+
// src/exchange/exchange.ts
|
|
4176
3881
|
function isMobileFormat(format) {
|
|
4177
3882
|
return format === "android-xml" || format === "ios-strings";
|
|
4178
3883
|
}
|
|
@@ -4432,7 +4137,715 @@ function csvRows(content) {
|
|
|
4432
4137
|
if (row.some((cell) => cell !== "")) rows.push(row);
|
|
4433
4138
|
return rows;
|
|
4434
4139
|
}
|
|
4435
|
-
// src/
|
|
4140
|
+
// src/cli/commands/export.ts
|
|
4141
|
+
async function exportCommand(cfg, values) {
|
|
4142
|
+
const format = values.format ?? "xliff";
|
|
4143
|
+
if (![
|
|
4144
|
+
"xliff",
|
|
4145
|
+
"csv",
|
|
4146
|
+
"po",
|
|
4147
|
+
"android-xml",
|
|
4148
|
+
"ios-strings"
|
|
4149
|
+
].includes(format)) {
|
|
4150
|
+
console.error(`[verbaly] unknown format "${values.format}", use xliff, csv, po, android-xml or ios-strings`);
|
|
4151
|
+
process.exitCode = 1;
|
|
4152
|
+
return;
|
|
4153
|
+
}
|
|
4154
|
+
if (values.missing && isMobileFormat(format)) {
|
|
4155
|
+
console.error(`[verbaly] --missing is for translator formats (xliff, csv): ${format} already skips untranslated keys so the app falls back to the source locale`);
|
|
4156
|
+
process.exitCode = 1;
|
|
4157
|
+
return;
|
|
4158
|
+
}
|
|
4159
|
+
const registry = await extractProject(cfg);
|
|
4160
|
+
const result = exportCatalogs(cfg, shippedCatalogs(cfg, loadCatalogs(cfg), registry, { newKeys: false }), {
|
|
4161
|
+
locales: csv(values.locales),
|
|
4162
|
+
format,
|
|
4163
|
+
out: values.out,
|
|
4164
|
+
missing: values.missing,
|
|
4165
|
+
origins: isMobileFormat(format) ? void 0 : await collectOrigins(cfg, registry)
|
|
4166
|
+
});
|
|
4167
|
+
if (result.files.length === 0) {
|
|
4168
|
+
console.log("[verbaly] no target locales to export (add locales to your config)");
|
|
4169
|
+
return;
|
|
4170
|
+
}
|
|
4171
|
+
const note = isMobileFormat(result.format) ? "untranslated skipped" : "untranslated";
|
|
4172
|
+
console.log(`[verbaly] exported ${counted(result.files.length, "locale")} (${result.format}) → ${result.dir}`);
|
|
4173
|
+
for (const file of result.files) console.log(` ${file.locale}: ${counted(file.total, "message")} (${file.untranslated} ${note}) → ${file.path}`);
|
|
4174
|
+
}
|
|
4175
|
+
// src/emit/watch.ts
|
|
4176
|
+
const NATIVE_TREE = process.platform === "darwin" || process.platform === "win32";
|
|
4177
|
+
function watchTree(root, options, onFile) {
|
|
4178
|
+
const watchers = /* @__PURE__ */ new Map();
|
|
4179
|
+
const close = (name) => {
|
|
4180
|
+
watchers.get(name)?.close();
|
|
4181
|
+
watchers.delete(name);
|
|
4182
|
+
};
|
|
4183
|
+
const open = (name, recursive, listener) => {
|
|
4184
|
+
const watcher = watch(join(root, name), {
|
|
4185
|
+
recursive,
|
|
4186
|
+
persistent: options.persistent ?? true
|
|
4187
|
+
}, listener);
|
|
4188
|
+
watcher.on("error", (error) => {
|
|
4189
|
+
close(name);
|
|
4190
|
+
console.warn(`[verbaly] stopped watching ${name || "the project"}:`, error);
|
|
4191
|
+
});
|
|
4192
|
+
watchers.set(name, watcher);
|
|
4193
|
+
};
|
|
4194
|
+
if (options.native ?? NATIVE_TREE) open("", true, (_event, file) => {
|
|
4195
|
+
if (file) onFile(file);
|
|
4196
|
+
});
|
|
4197
|
+
else {
|
|
4198
|
+
const sync = (name) => {
|
|
4199
|
+
close(name);
|
|
4200
|
+
if (name !== options.keep && (name === "node_modules" || name.startsWith("."))) return;
|
|
4201
|
+
try {
|
|
4202
|
+
if (!lstatSync(join(root, name)).isDirectory()) return;
|
|
4203
|
+
open(name, true, (_event, file) => {
|
|
4204
|
+
if (file) onFile(`${name}/${file}`);
|
|
4205
|
+
});
|
|
4206
|
+
} catch (error) {
|
|
4207
|
+
if (error.code !== "ENOENT") console.warn(`[verbaly] could not watch ${name}:`, error);
|
|
4208
|
+
}
|
|
4209
|
+
};
|
|
4210
|
+
open("", false, (event, file) => {
|
|
4211
|
+
if (!file) return;
|
|
4212
|
+
if (event === "rename") sync(file);
|
|
4213
|
+
onFile(file);
|
|
4214
|
+
});
|
|
4215
|
+
for (const entry of readdirSync(root, { withFileTypes: true })) if (entry.isDirectory()) sync(entry.name);
|
|
4216
|
+
}
|
|
4217
|
+
return () => {
|
|
4218
|
+
for (const name of [...watchers.keys()]) close(name);
|
|
4219
|
+
};
|
|
4220
|
+
}
|
|
4221
|
+
function watchProject(cfg, run, options = {}) {
|
|
4222
|
+
const catalogDir = relative(cfg.root, cfg.dir).replaceAll("\\", "/");
|
|
4223
|
+
let timer;
|
|
4224
|
+
let running = false;
|
|
4225
|
+
let queued = false;
|
|
4226
|
+
async function refresh() {
|
|
4227
|
+
if (running) {
|
|
4228
|
+
queued = true;
|
|
4229
|
+
return;
|
|
4230
|
+
}
|
|
4231
|
+
running = true;
|
|
4232
|
+
try {
|
|
4233
|
+
await run();
|
|
4234
|
+
} catch (error) {
|
|
4235
|
+
console.warn("[verbaly] watch run failed:", error);
|
|
4236
|
+
} finally {
|
|
4237
|
+
running = false;
|
|
4238
|
+
if (queued) {
|
|
4239
|
+
queued = false;
|
|
4240
|
+
schedule();
|
|
4241
|
+
}
|
|
4242
|
+
}
|
|
4243
|
+
}
|
|
4244
|
+
function schedule() {
|
|
4245
|
+
clearTimeout(timer);
|
|
4246
|
+
timer = setTimeout(() => void refresh(), options.debounce ?? 150);
|
|
4247
|
+
}
|
|
4248
|
+
const close = watchTree(cfg.root, {}, (filename) => {
|
|
4249
|
+
if (!filename) return;
|
|
4250
|
+
const file = filename.replaceAll("\\", "/");
|
|
4251
|
+
if (file.includes("node_modules/") || file.endsWith(".d.ts")) return;
|
|
4252
|
+
if (catalogDir && file.startsWith(`${catalogDir}/`)) return;
|
|
4253
|
+
if (SOURCE_FILE_RE.test(file)) schedule();
|
|
4254
|
+
});
|
|
4255
|
+
return () => {
|
|
4256
|
+
clearTimeout(timer);
|
|
4257
|
+
close();
|
|
4258
|
+
};
|
|
4259
|
+
}
|
|
4260
|
+
// src/cli/commands/extract.ts
|
|
4261
|
+
async function extractCommand(cfg, values) {
|
|
4262
|
+
const dryRun = values["dry-run"];
|
|
4263
|
+
if (values.watch && (dryRun || values.prune)) {
|
|
4264
|
+
console.error("[verbaly] --watch runs alone: prune is a deliberate one-shot action and dry-run writes nothing");
|
|
4265
|
+
process.exitCode = 1;
|
|
4266
|
+
return;
|
|
4267
|
+
}
|
|
4268
|
+
const reporter = values.watch ? createDevReporter(cfg.root, (line) => console.warn(` ${line}`)) : void 0;
|
|
4269
|
+
async function runExtract() {
|
|
4270
|
+
const result = await syncProject(cfg, {
|
|
4271
|
+
prune: values.prune,
|
|
4272
|
+
dryRun
|
|
4273
|
+
});
|
|
4274
|
+
if (result.pruneBlocked) {
|
|
4275
|
+
const unread = result.registry.parseErrors().length;
|
|
4276
|
+
console.warn(` prune skipped: an unparsed file may read any key (${counted(unread, "file")} below), so it waits until every file parses`);
|
|
4277
|
+
}
|
|
4278
|
+
for (const [locale, keys] of Object.entries(result.pruned)) console.log(dryRun ? ` ${locale}: would prune ${keys.length}: ${keys.join(", ")}` : ` ${locale}: -${keys.length} pruned`);
|
|
4279
|
+
const origin = result.inCode === result.messages ? "" : ` (${result.inCode} in code)`;
|
|
4280
|
+
console.log(`[verbaly] ${counted(result.messages, "message")}${origin} · locales: ${cfg.locales.join(", ")}${dryRun ? " (dry run, nothing written)" : ""}`);
|
|
4281
|
+
for (const [locale, keys] of Object.entries(result.added)) console.log(` ${locale}: ${dryRun ? `would add ${keys.length}` : `+${keys.length}`}`);
|
|
4282
|
+
const warnings = result.findings.filter((finding) => {
|
|
4283
|
+
if (finding.kind !== "replaced" && finding.kind !== "lock") return true;
|
|
4284
|
+
console.log(` ${formatFinding(finding, cfg.root)}`);
|
|
4285
|
+
return false;
|
|
4286
|
+
});
|
|
4287
|
+
if (reporter) reporter.report("extract", warnings);
|
|
4288
|
+
else for (const finding of warnings) console.warn(` ${formatFinding(finding, cfg.root)}`);
|
|
4289
|
+
if (result.stateProblem) console.warn(` ${result.stateProblem}, so drafts and outdated translations were not updated`);
|
|
4290
|
+
}
|
|
4291
|
+
await runExtract();
|
|
4292
|
+
if (values.watch) {
|
|
4293
|
+
watchProject(cfg, runExtract);
|
|
4294
|
+
console.log("[verbaly] watching for source changes (ctrl+c to stop)");
|
|
4295
|
+
}
|
|
4296
|
+
}
|
|
4297
|
+
// src/cli/commands/import.ts
|
|
4298
|
+
async function importCommand(cfg, values, positionals) {
|
|
4299
|
+
const files = positionals.slice(1);
|
|
4300
|
+
if (files.length === 0) {
|
|
4301
|
+
console.error("[verbaly] import needs at least one file: verbaly import verbaly-export/es.xlf");
|
|
4302
|
+
process.exitCode = 1;
|
|
4303
|
+
return;
|
|
4304
|
+
}
|
|
4305
|
+
if (!values["dry-run"]) loadState(cfg);
|
|
4306
|
+
const registry = await extractProject(cfg);
|
|
4307
|
+
const catalogs = shippedCatalogs(cfg, loadCatalogs(cfg), registry, { newKeys: false });
|
|
4308
|
+
const result = importCatalogs(cfg, catalogs, files, {
|
|
4309
|
+
locale: values.locale,
|
|
4310
|
+
overwrite: values.overwrite,
|
|
4311
|
+
dryRun: values["dry-run"]
|
|
4312
|
+
});
|
|
4313
|
+
for (const { file, from, to } of result.mapped) console.log(` ${file}: "${from}" read as ${to}`);
|
|
4314
|
+
for (const { file, locale, reason } of result.unmatched) {
|
|
4315
|
+
if (reason === "undeclared") {
|
|
4316
|
+
console.warn(` ${file}: "${locale}" is not one of this project's locales (${cfg.locales.join(", ")}), so nothing was imported from it`);
|
|
4317
|
+
console.warn(` fix: add ${locale} to locales in your verbaly config, or pass --locale with one of them`);
|
|
4318
|
+
} else {
|
|
4319
|
+
console.warn(` ${file}: "${locale}" is not a locale tag, so nothing was imported from it`);
|
|
4320
|
+
console.warn(" fix: pass --locale with the tag it stands for, like pt-BR");
|
|
4321
|
+
}
|
|
4322
|
+
process.exitCode = 1;
|
|
4323
|
+
}
|
|
4324
|
+
const writes = [];
|
|
4325
|
+
for (const [locale, keys] of Object.entries(result.imported)) {
|
|
4326
|
+
if (!values["dry-run"]) {
|
|
4327
|
+
const catalog = catalogs[locale] ?? {};
|
|
4328
|
+
writeCatalog(cfg, locale, catalog);
|
|
4329
|
+
const seen = result.sources[locale] ?? {};
|
|
4330
|
+
const entries = keys.map((key) => ({
|
|
4331
|
+
key,
|
|
4332
|
+
text: catalog[key],
|
|
4333
|
+
source: seen[key]
|
|
4334
|
+
}));
|
|
4335
|
+
writes.push({
|
|
4336
|
+
locale,
|
|
4337
|
+
entries,
|
|
4338
|
+
draft: values.draft === true
|
|
4339
|
+
});
|
|
4340
|
+
}
|
|
4341
|
+
const verb = values["dry-run"] ? "would import" : "imported";
|
|
4342
|
+
console.log(` ${locale}: +${keys.length} ${verb}${values.draft ? " (draft)" : ""}`);
|
|
4343
|
+
}
|
|
4344
|
+
const lockBefore = stateFile(cfg);
|
|
4345
|
+
if (writes.length > 0) recordTranslations(cfg, catalogs[cfg.sourceLocale] ?? {}, writes);
|
|
4346
|
+
sayLockNews(cfg, lockBefore);
|
|
4347
|
+
for (const [locale, keys] of Object.entries(result.skipped)) console.log(` ${locale}: ${keys.length} already translated, kept (use --overwrite to replace)`);
|
|
4348
|
+
for (const [locale, keys] of Object.entries(result.rejected)) console.warn(` ${locale}: ${keys.length} rejected (params/tags not preserved): ${keys.join(", ")}`);
|
|
4349
|
+
for (const [locale, keys] of Object.entries(result.unknown)) console.warn(` ${locale}: ${counted(keys.length, "unknown key")} ignored (not in the source catalog): ${keys.join(", ")}`);
|
|
4350
|
+
if (Object.keys(result.imported).length === 0 && result.unmatched.length === 0) console.log("[verbaly] nothing to import ✓");
|
|
4351
|
+
}
|
|
4352
|
+
// src/catalog/migrate.ts
|
|
4353
|
+
const KNOWN = [
|
|
4354
|
+
"i18next",
|
|
4355
|
+
"react-i18next",
|
|
4356
|
+
"vue-i18n",
|
|
4357
|
+
"react-intl",
|
|
4358
|
+
"next-intl",
|
|
4359
|
+
"svelte-i18n"
|
|
4360
|
+
];
|
|
4361
|
+
function detectLibraries(root) {
|
|
4362
|
+
const path = join(root, "package.json");
|
|
4363
|
+
if (!existsSync(path)) return [];
|
|
4364
|
+
let manifest;
|
|
4365
|
+
try {
|
|
4366
|
+
manifest = JSON.parse(readFileSync(path, "utf8"));
|
|
4367
|
+
} catch {
|
|
4368
|
+
return [];
|
|
4369
|
+
}
|
|
4370
|
+
const deps = {
|
|
4371
|
+
...manifest.dependencies,
|
|
4372
|
+
...manifest.devDependencies
|
|
4373
|
+
};
|
|
4374
|
+
return KNOWN.filter((name) => deps[name] !== void 0);
|
|
4375
|
+
}
|
|
4376
|
+
const PLAIN = /\{\{\s*([A-Za-z_$][\w$]*)\s*\}\}/g;
|
|
4377
|
+
const FORMATTED = /\{\{\s*-?\s*[A-Za-z_$][\w$]*\s*,[^}]*\}\}/;
|
|
4378
|
+
const UNESCAPED = /\{\{\s*-\s*[A-Za-z_$][\w$]*\s*\}\}/;
|
|
4379
|
+
const NESTED = /\$t\(/;
|
|
4380
|
+
const DOUBLE = /\{\{[^{}]*\}\}/;
|
|
4381
|
+
const DOUBLES = new RegExp(DOUBLE.source, "g");
|
|
4382
|
+
const ONE_PLAIN = new RegExp(`^${PLAIN.source}$`);
|
|
4383
|
+
const NAME = /^\s*([A-Za-z_$][\w$]*)\s*$/;
|
|
4384
|
+
const CATEGORIES = [
|
|
4385
|
+
"zero",
|
|
4386
|
+
"one",
|
|
4387
|
+
"two",
|
|
4388
|
+
"few",
|
|
4389
|
+
"many",
|
|
4390
|
+
"other"
|
|
4391
|
+
];
|
|
4392
|
+
const SUFFIX = new RegExp(`^(.*)_(${[...CATEGORIES, "plural"].join("|")})$`);
|
|
4393
|
+
function i18nextProblem(text) {
|
|
4394
|
+
if (NESTED.test(text)) return "uses $t() nesting, which has no direct equivalent";
|
|
4395
|
+
if (UNESCAPED.test(text)) return "uses {{- name}}, so decide whether the message is rich";
|
|
4396
|
+
if (FORMATTED.test(text)) return "uses an i18next format, which needs the matching Verbaly one";
|
|
4397
|
+
for (const [double] of text.matchAll(DOUBLES)) if (!ONE_PLAIN.test(double)) return "uses {{…}} with more than a name, which needs a Verbaly param";
|
|
4398
|
+
}
|
|
4399
|
+
function convertBraces(text) {
|
|
4400
|
+
let out = "";
|
|
4401
|
+
let last = 0;
|
|
4402
|
+
for (const match of text.matchAll(PLAIN)) {
|
|
4403
|
+
out += escapeBraces(text.slice(last, match.index)) + `{${match[1]}}`;
|
|
4404
|
+
last = match.index + match[0].length;
|
|
4405
|
+
}
|
|
4406
|
+
return out + escapeBraces(text.slice(last));
|
|
4407
|
+
}
|
|
4408
|
+
function escapeBraces(text) {
|
|
4409
|
+
return text.replace(/[{}]/g, (brace) => brace + brace);
|
|
4410
|
+
}
|
|
4411
|
+
function toVariant(text) {
|
|
4412
|
+
let out = "";
|
|
4413
|
+
let i = 0;
|
|
4414
|
+
while (i < text.length) {
|
|
4415
|
+
const ch = text[i];
|
|
4416
|
+
if ((ch === "{" || ch === "}") && text[i + 1] === ch) {
|
|
4417
|
+
out += ch + ch;
|
|
4418
|
+
i += 2;
|
|
4419
|
+
} else if (ch === "{") {
|
|
4420
|
+
const close = text.indexOf("}", i);
|
|
4421
|
+
const name = close < 0 ? void 0 : NAME.exec(text.slice(i + 1, close))?.[1];
|
|
4422
|
+
if (name === void 0) return void 0;
|
|
4423
|
+
out += name === "count" ? "#" : `{${name}}`;
|
|
4424
|
+
i = close + 1;
|
|
4425
|
+
} else {
|
|
4426
|
+
out += ch === "#" || ch === "|" || ch === "}" ? ch + ch : ch;
|
|
4427
|
+
i += 1;
|
|
4428
|
+
}
|
|
4429
|
+
}
|
|
4430
|
+
return out;
|
|
4431
|
+
}
|
|
4432
|
+
function order(a, b) {
|
|
4433
|
+
return CATEGORIES.indexOf(a) - CATEGORIES.indexOf(b);
|
|
4434
|
+
}
|
|
4435
|
+
function mergePlurals(locale, catalog, out, plurals, skipped, fromI18next) {
|
|
4436
|
+
const groups = /* @__PURE__ */ new Map();
|
|
4437
|
+
const legacy = /* @__PURE__ */ new Set();
|
|
4438
|
+
for (const [key, value] of Object.entries(catalog)) {
|
|
4439
|
+
const match = SUFFIX.exec(key);
|
|
4440
|
+
if (!match || typeof value !== "string") continue;
|
|
4441
|
+
const [, base, suffix] = match;
|
|
4442
|
+
if (suffix === "plural") legacy.add(base);
|
|
4443
|
+
const category = suffix === "plural" ? "other" : suffix;
|
|
4444
|
+
if (!groups.has(base)) groups.set(base, /* @__PURE__ */ new Map());
|
|
4445
|
+
groups.get(base).set(category, {
|
|
4446
|
+
key,
|
|
4447
|
+
value
|
|
4448
|
+
});
|
|
4449
|
+
}
|
|
4450
|
+
for (const [base, forms] of groups) {
|
|
4451
|
+
const bare = catalog[base];
|
|
4452
|
+
if (legacy.has(base) && typeof bare === "string" && !forms.has("one")) forms.set("one", {
|
|
4453
|
+
key: base,
|
|
4454
|
+
value: bare
|
|
4455
|
+
});
|
|
4456
|
+
else if (bare !== void 0) {
|
|
4457
|
+
skipped.push({
|
|
4458
|
+
locale,
|
|
4459
|
+
key: base,
|
|
4460
|
+
reason: `"${base}" already exists, so merging would overwrite it`
|
|
4461
|
+
});
|
|
4462
|
+
continue;
|
|
4463
|
+
}
|
|
4464
|
+
if (!forms.has("other")) {
|
|
4465
|
+
skipped.push({
|
|
4466
|
+
locale,
|
|
4467
|
+
key: base,
|
|
4468
|
+
reason: "no _other form, so the block would have no catch-all"
|
|
4469
|
+
});
|
|
4470
|
+
continue;
|
|
4471
|
+
}
|
|
4472
|
+
if (forms.size === 1) {
|
|
4473
|
+
skipped.push({
|
|
4474
|
+
locale,
|
|
4475
|
+
key: base,
|
|
4476
|
+
reason: "only a plural form, so add the singular before merging"
|
|
4477
|
+
});
|
|
4478
|
+
continue;
|
|
4479
|
+
}
|
|
4480
|
+
const sorted = [...forms.entries()].sort((a, b) => order(a[0], b[0]));
|
|
4481
|
+
const cases = [];
|
|
4482
|
+
let problem;
|
|
4483
|
+
for (const [category, form] of sorted) {
|
|
4484
|
+
problem = fromI18next ? i18nextProblem(form.value) : void 0;
|
|
4485
|
+
const body = problem ? void 0 : toVariant(fromI18next ? convertBraces(form.value) : form.value);
|
|
4486
|
+
if (body === void 0) {
|
|
4487
|
+
problem ??= "a form holds more than a name in braces, so merge it by hand";
|
|
4488
|
+
break;
|
|
4489
|
+
}
|
|
4490
|
+
cases.push(`${category}: ${body}`);
|
|
4491
|
+
}
|
|
4492
|
+
if (problem) {
|
|
4493
|
+
skipped.push({
|
|
4494
|
+
locale,
|
|
4495
|
+
key: base,
|
|
4496
|
+
reason: problem
|
|
4497
|
+
});
|
|
4498
|
+
continue;
|
|
4499
|
+
}
|
|
4500
|
+
const merged = `{count | ${cases.join(" | ")}}`;
|
|
4501
|
+
for (const [, form] of sorted) delete out[form.key];
|
|
4502
|
+
out[base] = merged;
|
|
4503
|
+
plurals.push({
|
|
4504
|
+
locale,
|
|
4505
|
+
key: base,
|
|
4506
|
+
from: sorted.map(([, form]) => form.key),
|
|
4507
|
+
after: merged
|
|
4508
|
+
});
|
|
4509
|
+
}
|
|
4510
|
+
}
|
|
4511
|
+
function migrateCatalogs(cfg, options = {}) {
|
|
4512
|
+
const braces = [];
|
|
4513
|
+
const plurals = [];
|
|
4514
|
+
const skipped = [];
|
|
4515
|
+
const written = [];
|
|
4516
|
+
const state = loadState(cfg);
|
|
4517
|
+
const done = new Set(state.migrated ?? []);
|
|
4518
|
+
const catalogs = new Map(cfg.locales.map((locale) => [locale, readCatalog(cfg, locale)]));
|
|
4519
|
+
const pending = cfg.locales.filter((locale) => !done.has(locale));
|
|
4520
|
+
const fromI18next = pending.some((locale) => Object.values(catalogs.get(locale)).some((value) => typeof value === "string" && (DOUBLE.test(value) || NESTED.test(value))));
|
|
4521
|
+
for (const locale of cfg.locales) {
|
|
4522
|
+
const catalog = catalogs.get(locale);
|
|
4523
|
+
const out = Object.assign(emptyCatalog(), catalog);
|
|
4524
|
+
const convert = fromI18next && !done.has(locale);
|
|
4525
|
+
for (const [key, value] of Object.entries(catalog)) {
|
|
4526
|
+
if (!convert || typeof value !== "string") continue;
|
|
4527
|
+
const problem = i18nextProblem(value);
|
|
4528
|
+
if (problem) {
|
|
4529
|
+
skipped.push({
|
|
4530
|
+
locale,
|
|
4531
|
+
key,
|
|
4532
|
+
reason: problem
|
|
4533
|
+
});
|
|
4534
|
+
continue;
|
|
4535
|
+
}
|
|
4536
|
+
const after = convertBraces(value);
|
|
4537
|
+
if (after !== value) {
|
|
4538
|
+
braces.push({
|
|
4539
|
+
locale,
|
|
4540
|
+
key,
|
|
4541
|
+
before: value,
|
|
4542
|
+
after
|
|
4543
|
+
});
|
|
4544
|
+
out[key] = after;
|
|
4545
|
+
}
|
|
4546
|
+
}
|
|
4547
|
+
if (options.plurals) mergePlurals(locale, catalog, out, plurals, skipped, convert);
|
|
4548
|
+
if ((braces.some((b) => b.locale === locale) || plurals.some((p) => p.locale === locale)) && options.write) {
|
|
4549
|
+
writeCatalog(cfg, locale, out);
|
|
4550
|
+
written.push(locale);
|
|
4551
|
+
}
|
|
4552
|
+
}
|
|
4553
|
+
if (options.write && fromI18next) {
|
|
4554
|
+
const converted = pending.filter((locale) => existsSync(catalogPath(cfg, locale)));
|
|
4555
|
+
if (converted.length) saveState(cfg, {
|
|
4556
|
+
...state,
|
|
4557
|
+
migrated: [...done, ...converted]
|
|
4558
|
+
});
|
|
4559
|
+
}
|
|
4560
|
+
const remembered = cfg.locales.filter((locale) => done.has(locale));
|
|
4561
|
+
return {
|
|
4562
|
+
detected: detectLibraries(cfg.root),
|
|
4563
|
+
braces,
|
|
4564
|
+
plurals,
|
|
4565
|
+
skipped,
|
|
4566
|
+
written,
|
|
4567
|
+
remembered
|
|
4568
|
+
};
|
|
4569
|
+
}
|
|
4570
|
+
// src/project/init.ts
|
|
4571
|
+
const CATALOG_DIRS = [
|
|
4572
|
+
"locales",
|
|
4573
|
+
"src/locales",
|
|
4574
|
+
"src/i18n/locales",
|
|
4575
|
+
"src/i18n",
|
|
4576
|
+
"i18n",
|
|
4577
|
+
"lang",
|
|
4578
|
+
"src/lang",
|
|
4579
|
+
"translations",
|
|
4580
|
+
"src/translations",
|
|
4581
|
+
"messages"
|
|
4582
|
+
];
|
|
4583
|
+
function existingCatalogs(root) {
|
|
4584
|
+
return CATALOG_DIRS.find((dir) => {
|
|
4585
|
+
try {
|
|
4586
|
+
return readdirSync(join(root, dir)).some((file) => file.endsWith(".json") && isLocaleTag(file.slice(0, -5)));
|
|
4587
|
+
} catch {
|
|
4588
|
+
return false;
|
|
4589
|
+
}
|
|
4590
|
+
});
|
|
4591
|
+
}
|
|
4592
|
+
function configSource(options, typescript) {
|
|
4593
|
+
const fields = [` sourceLocale: '${options.sourceLocale ?? "en"}',`];
|
|
4594
|
+
if (options.locales?.length) fields.push(` locales: [${options.locales.map((l) => `'${l}'`).join(", ")}],`);
|
|
4595
|
+
if (options.dir) fields.push(` dir: '${options.dir}',`);
|
|
4596
|
+
const body = `export default {\n${fields.join("\n")}\n}`;
|
|
4597
|
+
if (typescript) return `import type { VerbalyConfig } from '@verbaly/compiler';\n\n${body} satisfies VerbalyConfig;\n`;
|
|
4598
|
+
return `/** @type {import('@verbaly/compiler').VerbalyConfig} */\n${body};\n`;
|
|
4599
|
+
}
|
|
4600
|
+
async function init(input = {}) {
|
|
4601
|
+
const root = input.root ?? process.cwd();
|
|
4602
|
+
const created = [];
|
|
4603
|
+
const skipped = [];
|
|
4604
|
+
const renamed = [];
|
|
4605
|
+
const refused = [];
|
|
4606
|
+
const typed = input.locales?.flatMap((locale) => {
|
|
4607
|
+
if (isLocaleTag(locale)) return [locale];
|
|
4608
|
+
const near = suggestTag(locale);
|
|
4609
|
+
if (near) renamed.push({
|
|
4610
|
+
from: locale,
|
|
4611
|
+
to: near
|
|
4612
|
+
});
|
|
4613
|
+
else refused.push(locale);
|
|
4614
|
+
return near ? [near] : [];
|
|
4615
|
+
});
|
|
4616
|
+
const locales = typed && [...new Set(typed)];
|
|
4617
|
+
let sourceLocale = input.sourceLocale;
|
|
4618
|
+
if (sourceLocale !== void 0 && !isLocaleTag(sourceLocale)) {
|
|
4619
|
+
const near = suggestTag(sourceLocale);
|
|
4620
|
+
if (!near) throw new Error(`[verbaly] "${sourceLocale}" is not a locale tag: pass --source with one like en or pt-BR`);
|
|
4621
|
+
renamed.push({
|
|
4622
|
+
from: sourceLocale,
|
|
4623
|
+
to: near
|
|
4624
|
+
});
|
|
4625
|
+
sourceLocale = near;
|
|
4626
|
+
}
|
|
4627
|
+
const options = {
|
|
4628
|
+
...input,
|
|
4629
|
+
locales,
|
|
4630
|
+
sourceLocale
|
|
4631
|
+
};
|
|
4632
|
+
const existing = findConfigFile(root);
|
|
4633
|
+
const typescript = existsSync(join(root, "tsconfig.json"));
|
|
4634
|
+
const configFile = existing ?? (typescript ? "verbaly.config.ts" : "verbaly.config.mjs");
|
|
4635
|
+
const found = existing || options.dir ? void 0 : existingCatalogs(root);
|
|
4636
|
+
if (existing) skipped.push(existing);
|
|
4637
|
+
else {
|
|
4638
|
+
const dir = found === "locales" ? void 0 : found;
|
|
4639
|
+
writeFileSync(join(root, configFile), configSource({
|
|
4640
|
+
...options,
|
|
4641
|
+
dir: options.dir ?? dir
|
|
4642
|
+
}, typescript));
|
|
4643
|
+
created.push(configFile);
|
|
4644
|
+
}
|
|
4645
|
+
const fromFile = existing ? await loadConfigFile(root) : {};
|
|
4646
|
+
const scaffold = {
|
|
4647
|
+
dir: options.dir ?? fromFile.dir ?? found,
|
|
4648
|
+
sourceLocale: options.sourceLocale ?? fromFile.sourceLocale,
|
|
4649
|
+
locales: options.locales ?? fromFile.locales
|
|
4650
|
+
};
|
|
4651
|
+
const dir = join(root, scaffold.dir ?? "locales");
|
|
4652
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
4653
|
+
for (const locale of /* @__PURE__ */ new Set([scaffold.sourceLocale ?? "en", ...scaffold.locales ?? []])) {
|
|
4654
|
+
const file = join(dir, `${locale}.json`);
|
|
4655
|
+
const label = relative(root, file).replaceAll("\\", "/");
|
|
4656
|
+
if (existsSync(file)) skipped.push(label);
|
|
4657
|
+
else {
|
|
4658
|
+
writeFileSync(file, "{}\n");
|
|
4659
|
+
created.push(label);
|
|
4660
|
+
}
|
|
4661
|
+
}
|
|
4662
|
+
const host = detectHost(root);
|
|
4663
|
+
const deps = readDependencies(root);
|
|
4664
|
+
const missing = host?.install.filter((pkg) => !deps[pkg]) ?? [];
|
|
4665
|
+
const next = host ? [...missing.length ? [installCommand(root, missing)] : [], host.wire] : ["run \"verbaly extract\" after writing your first t`…` message"];
|
|
4666
|
+
const others = detectLibraries(root);
|
|
4667
|
+
if (found && others.length) next.push(`port the ${others.join(" and ")} catalogs: npx verbaly migrate reports, --write applies`);
|
|
4668
|
+
return {
|
|
4669
|
+
created,
|
|
4670
|
+
skipped,
|
|
4671
|
+
host: host?.name,
|
|
4672
|
+
configFile,
|
|
4673
|
+
next,
|
|
4674
|
+
renamed,
|
|
4675
|
+
refused,
|
|
4676
|
+
found
|
|
4677
|
+
};
|
|
4678
|
+
}
|
|
4679
|
+
// src/cli/commands/init.ts
|
|
4680
|
+
async function initCommand(values) {
|
|
4681
|
+
const result = await init({
|
|
4682
|
+
root: values.root,
|
|
4683
|
+
dir: values.dir,
|
|
4684
|
+
sourceLocale: values.source,
|
|
4685
|
+
locales: csv(values.locales)
|
|
4686
|
+
});
|
|
4687
|
+
if (result.created.length) console.log(`[verbaly] created: ${result.created.join(", ")}`);
|
|
4688
|
+
if (result.skipped.length) console.log(` kept (already there): ${result.skipped.join(", ")}`);
|
|
4689
|
+
for (const { from, to } of result.renamed) console.log(` wrote ${to} for ${from}, since a locale tag takes a hyphen`);
|
|
4690
|
+
for (const locale of result.refused) console.warn(` left out "${locale}": it is not a locale tag, use one like es or pt-BR`);
|
|
4691
|
+
if (result.found) console.log(` catalogs: ${result.found}, kept where they already are`);
|
|
4692
|
+
if (result.host) console.log(` detected: ${result.host}`);
|
|
4693
|
+
console.log([" next steps:", ...result.next.map((step, i) => ` ${i + 1}. ${step}`)].join("\n"));
|
|
4694
|
+
}
|
|
4695
|
+
// src/cli/commands/migrate.ts
|
|
4696
|
+
async function migrateCommand(cfg, values) {
|
|
4697
|
+
const lockBefore = stateFile(cfg);
|
|
4698
|
+
const result = migrateCatalogs(cfg, {
|
|
4699
|
+
write: values.write,
|
|
4700
|
+
plurals: values.plurals
|
|
4701
|
+
});
|
|
4702
|
+
const from = result.detected.length ? result.detected.join(", ") : "no known i18n library";
|
|
4703
|
+
const changes = result.braces.length + result.plurals.length;
|
|
4704
|
+
const before = result.remembered.length ? `, ${counted(result.remembered.length, "language")} converted before` : "";
|
|
4705
|
+
if (changes === 0 && result.skipped.length === 0) {
|
|
4706
|
+
console.log(`[verbaly] catalogs need nothing (${from}${before}) ✓`);
|
|
4707
|
+
return;
|
|
4708
|
+
}
|
|
4709
|
+
const verb = values.write ? "ported" : "would port";
|
|
4710
|
+
const note = values.write ? "" : " (report only, use --write to apply)";
|
|
4711
|
+
console.log(`[verbaly] ${verb} ${counted(changes, "message")} from ${from}${before}${note}`);
|
|
4712
|
+
for (const entry of result.braces) console.log(` ${entry.locale} ${entry.key} ${entry.before} → ${entry.after}`);
|
|
4713
|
+
for (const entry of result.plurals) console.log(` ${entry.locale} ${entry.key} ${entry.from.join(" + ")} → ${entry.after}`);
|
|
4714
|
+
if (!values.plurals && changes > 0) console.log(" --plurals also merges _one/_other into one message with variants");
|
|
4715
|
+
if (result.skipped.length > 0) {
|
|
4716
|
+
console.log(" needs a human:");
|
|
4717
|
+
for (const entry of result.skipped) console.log(` ${entry.locale} ${entry.key} ${entry.reason}`);
|
|
4718
|
+
}
|
|
4719
|
+
if (values.write && result.braces.length > 0) console.log(` ${STATE_FILE} remembers the converted languages, so a later run never reads them as i18next again`);
|
|
4720
|
+
sayLockNews(cfg, lockBefore);
|
|
4721
|
+
if (values.write) console.log(" next: run verbaly check");
|
|
4722
|
+
}
|
|
4723
|
+
// src/catalog/pseudo.ts
|
|
4724
|
+
const ACCENTS = {
|
|
4725
|
+
a: "á",
|
|
4726
|
+
b: "ƀ",
|
|
4727
|
+
c: "ç",
|
|
4728
|
+
d: "đ",
|
|
4729
|
+
e: "é",
|
|
4730
|
+
f: "ƒ",
|
|
4731
|
+
g: "ğ",
|
|
4732
|
+
h: "ĥ",
|
|
4733
|
+
i: "í",
|
|
4734
|
+
j: "ĵ",
|
|
4735
|
+
k: "ķ",
|
|
4736
|
+
l: "ĺ",
|
|
4737
|
+
m: "ɱ",
|
|
4738
|
+
n: "ñ",
|
|
4739
|
+
o: "ó",
|
|
4740
|
+
p: "þ",
|
|
4741
|
+
q: "ǫ",
|
|
4742
|
+
r: "ŕ",
|
|
4743
|
+
s: "š",
|
|
4744
|
+
t: "ţ",
|
|
4745
|
+
u: "ú",
|
|
4746
|
+
v: "ṽ",
|
|
4747
|
+
w: "ŵ",
|
|
4748
|
+
x: "ẋ",
|
|
4749
|
+
y: "ý",
|
|
4750
|
+
z: "ž",
|
|
4751
|
+
A: "Á",
|
|
4752
|
+
B: "Ɓ",
|
|
4753
|
+
C: "Ç",
|
|
4754
|
+
D: "Đ",
|
|
4755
|
+
E: "É",
|
|
4756
|
+
F: "Ƒ",
|
|
4757
|
+
G: "Ğ",
|
|
4758
|
+
H: "Ĥ",
|
|
4759
|
+
I: "Í",
|
|
4760
|
+
J: "Ĵ",
|
|
4761
|
+
K: "Ķ",
|
|
4762
|
+
L: "Ĺ",
|
|
4763
|
+
M: "Ḿ",
|
|
4764
|
+
N: "Ñ",
|
|
4765
|
+
O: "Ó",
|
|
4766
|
+
P: "Þ",
|
|
4767
|
+
Q: "Ǫ",
|
|
4768
|
+
R: "Ŕ",
|
|
4769
|
+
S: "Š",
|
|
4770
|
+
T: "Ţ",
|
|
4771
|
+
U: "Ú",
|
|
4772
|
+
V: "Ṽ",
|
|
4773
|
+
W: "Ŵ",
|
|
4774
|
+
X: "Ẍ",
|
|
4775
|
+
Y: "Ý",
|
|
4776
|
+
Z: "Ž"
|
|
4777
|
+
};
|
|
4778
|
+
const TAG_AT = /<\/?[a-zA-Z][\w-]*\/?>/y;
|
|
4779
|
+
function pseudoLocalize(message) {
|
|
4780
|
+
let out = "";
|
|
4781
|
+
let letters = 0;
|
|
4782
|
+
let i = 0;
|
|
4783
|
+
while (i < message.length) {
|
|
4784
|
+
const two = message.slice(i, i + 2);
|
|
4785
|
+
if (two === "{{" || two === "}}" || two === "||" || two === "##") {
|
|
4786
|
+
out += two;
|
|
4787
|
+
i += 2;
|
|
4788
|
+
continue;
|
|
4789
|
+
}
|
|
4790
|
+
const ch = message[i];
|
|
4791
|
+
if (ch === "{") {
|
|
4792
|
+
const end = matchBrace(message, i);
|
|
4793
|
+
out += message.slice(i, end);
|
|
4794
|
+
i = end;
|
|
4795
|
+
continue;
|
|
4796
|
+
}
|
|
4797
|
+
if (ch === "<") {
|
|
4798
|
+
TAG_AT.lastIndex = i;
|
|
4799
|
+
const m = TAG_AT.exec(message);
|
|
4800
|
+
if (m) {
|
|
4801
|
+
out += m[0];
|
|
4802
|
+
i += m[0].length;
|
|
4803
|
+
continue;
|
|
4804
|
+
}
|
|
4805
|
+
}
|
|
4806
|
+
const mapped = ACCENTS[ch];
|
|
4807
|
+
if (mapped) {
|
|
4808
|
+
out += mapped;
|
|
4809
|
+
letters += 1;
|
|
4810
|
+
} else out += ch;
|
|
4811
|
+
i += 1;
|
|
4812
|
+
}
|
|
4813
|
+
const pad = "~".repeat(Math.ceil(letters / 3));
|
|
4814
|
+
return `⟦${out}${pad ? " " + pad : ""}⟧`;
|
|
4815
|
+
}
|
|
4816
|
+
function matchBrace(message, start) {
|
|
4817
|
+
let depth = 0;
|
|
4818
|
+
for (let i = start; i < message.length; i++) {
|
|
4819
|
+
const two = message.slice(i, i + 2);
|
|
4820
|
+
if (two === "{{" || two === "}}") {
|
|
4821
|
+
i += 1;
|
|
4822
|
+
continue;
|
|
4823
|
+
}
|
|
4824
|
+
const ch = message[i];
|
|
4825
|
+
if (ch === "{") depth += 1;
|
|
4826
|
+
else if (ch === "}") {
|
|
4827
|
+
depth -= 1;
|
|
4828
|
+
if (depth === 0) return i + 1;
|
|
4829
|
+
}
|
|
4830
|
+
}
|
|
4831
|
+
return message.length;
|
|
4832
|
+
}
|
|
4833
|
+
function pseudoCatalogs(cfg, catalogs, locale = PSEUDO_LOCALE) {
|
|
4834
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
4835
|
+
const target = emptyCatalog();
|
|
4836
|
+
for (const [key, msg] of Object.entries(source)) target[key] = msg ? pseudoLocalize(msg) : "";
|
|
4837
|
+
catalogs[locale] = target;
|
|
4838
|
+
return Object.keys(target).filter((key) => target[key]);
|
|
4839
|
+
}
|
|
4840
|
+
// src/cli/commands/pseudo.ts
|
|
4841
|
+
async function pseudoCommand(cfg, values) {
|
|
4842
|
+
const catalogs = shippedCatalogs(cfg, loadCatalogs(cfg), await extractProject(cfg), { newKeys: false });
|
|
4843
|
+
const locale = values.locale ?? "en-XA";
|
|
4844
|
+
const keys = pseudoCatalogs(cfg, catalogs, locale);
|
|
4845
|
+
writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
4846
|
+
console.log(`[verbaly] ${counted(keys.length, "message")} pseudo-localized → ${locale}`);
|
|
4847
|
+
}
|
|
4848
|
+
// src/emit/render.ts
|
|
4436
4849
|
function localeInstance(catalogs, locale, sourceLocale) {
|
|
4437
4850
|
return createVerbaly({
|
|
4438
4851
|
locale,
|
|
@@ -4881,92 +5294,144 @@ function escapeAttr(text) {
|
|
|
4881
5294
|
function decodeEntities(text) {
|
|
4882
5295
|
return text.replace(/"/g, "\"").replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
4883
5296
|
}
|
|
4884
|
-
// src/
|
|
4885
|
-
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
|
|
4889
|
-
|
|
4890
|
-
|
|
4891
|
-
|
|
4892
|
-
|
|
4893
|
-
|
|
4894
|
-
|
|
4895
|
-
persistent: options.persistent ?? true
|
|
4896
|
-
}, listener);
|
|
4897
|
-
watcher.on("error", (error) => {
|
|
4898
|
-
close(name);
|
|
4899
|
-
console.warn(`[verbaly] stopped watching ${name || "the project"}:`, error);
|
|
4900
|
-
});
|
|
4901
|
-
watchers.set(name, watcher);
|
|
4902
|
-
};
|
|
4903
|
-
if (options.native ?? NATIVE_TREE) open("", true, (_event, file) => {
|
|
4904
|
-
if (file) onFile(file);
|
|
5297
|
+
// src/cli/commands/render.ts
|
|
5298
|
+
async function renderCommand(cfg, values) {
|
|
5299
|
+
const result = await renderSite(cfg, {
|
|
5300
|
+
site: values.site,
|
|
5301
|
+
locales: csv(values.locales),
|
|
5302
|
+
attribute: values.attribute,
|
|
5303
|
+
base: values.base,
|
|
5304
|
+
baseUrl: values["base-url"],
|
|
5305
|
+
sitemap: values.sitemap,
|
|
5306
|
+
redirect: values.redirect,
|
|
5307
|
+
clean: values.clean
|
|
4905
5308
|
});
|
|
4906
|
-
|
|
4907
|
-
|
|
4908
|
-
|
|
4909
|
-
|
|
4910
|
-
|
|
4911
|
-
|
|
4912
|
-
|
|
4913
|
-
|
|
4914
|
-
|
|
4915
|
-
|
|
4916
|
-
|
|
4917
|
-
|
|
4918
|
-
|
|
4919
|
-
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
|
|
5309
|
+
console.log(`[verbaly] ${counted(result.files, "page")} × ${counted(result.locales.length, "locale")} (${result.locales.join(", ")})`);
|
|
5310
|
+
for (const line of formatRenderWarnings(result, cfg.sourceLocale)) console.warn(line);
|
|
5311
|
+
}
|
|
5312
|
+
// src/cli/commands/review.ts
|
|
5313
|
+
async function reviewCommand(cfg, values, positionals) {
|
|
5314
|
+
const registry = await extractProject(cfg);
|
|
5315
|
+
const catalogs = loadCatalogs(cfg);
|
|
5316
|
+
const shipped = shippedCatalogs(cfg, catalogs, registry);
|
|
5317
|
+
const state = loadState(cfg);
|
|
5318
|
+
const only = new Set(positionals.slice(1));
|
|
5319
|
+
const wanted = ({ locale, key }) => (!values.locale || locale === values.locale) && (only.size === 0 || only.has(key));
|
|
5320
|
+
const drafts = Object.entries(effectiveDrafts(state.drafts, catalogs)).flatMap(([locale, keys]) => keys.map((key) => ({
|
|
5321
|
+
locale,
|
|
5322
|
+
key
|
|
5323
|
+
}))).filter(wanted);
|
|
5324
|
+
const outdated = outdatedTranslations(cfg, shipped, state.fingerprints).filter(wanted);
|
|
5325
|
+
if (drafts.length === 0 && outdated.length === 0) {
|
|
5326
|
+
console.log("[verbaly] nothing awaiting review ✓");
|
|
5327
|
+
return;
|
|
4925
5328
|
}
|
|
4926
|
-
|
|
4927
|
-
for (const
|
|
5329
|
+
if (values.approve) {
|
|
5330
|
+
for (const [locale, keys] of byLocale(drafts)) {
|
|
5331
|
+
clearDrafts(state.drafts, locale, keys);
|
|
5332
|
+
console.log(` ${locale}: ${keys.length} approved`);
|
|
5333
|
+
}
|
|
5334
|
+
acceptOutdated(cfg, shipped, state.fingerprints, outdated);
|
|
5335
|
+
for (const [locale, keys] of byLocale(outdated)) console.log(` ${locale}: ${keys.length} kept for the new source text`);
|
|
5336
|
+
const lockBefore = stateFile(cfg);
|
|
5337
|
+
saveState(cfg, state);
|
|
5338
|
+
sayLockNews(cfg, lockBefore);
|
|
5339
|
+
const reviewed = new Set([...drafts, ...outdated].map(({ locale, key }) => `${locale}:${key}`));
|
|
5340
|
+
console.log(`[verbaly] ${counted(reviewed.size, "translation")} marked reviewed ✓`);
|
|
5341
|
+
return;
|
|
5342
|
+
}
|
|
5343
|
+
const source = shipped[cfg.sourceLocale] ?? {};
|
|
5344
|
+
const show = (locale, keys) => {
|
|
5345
|
+
console.log(` ${locale}:`);
|
|
5346
|
+
for (const key of keys) {
|
|
5347
|
+
const translated = catalogs[locale]?.[key] ?? "";
|
|
5348
|
+
console.log(` ${key}: "${truncate(source[key] ?? "", 60)}" → "${truncate(translated, 60)}"`);
|
|
5349
|
+
}
|
|
4928
5350
|
};
|
|
5351
|
+
if (drafts.length > 0) {
|
|
5352
|
+
console.log(`[verbaly] ${counted(drafts.length, "machine translation")} awaiting review (--approve to accept)`);
|
|
5353
|
+
for (const [locale, keys] of byLocale(drafts)) show(locale, keys);
|
|
5354
|
+
}
|
|
5355
|
+
if (outdated.length > 0) {
|
|
5356
|
+
console.log(`[verbaly] ${counted(outdated.length, "translation")} written for an older source text (update them, or --approve keeps them)`);
|
|
5357
|
+
for (const [locale, keys] of byLocale(outdated)) show(locale, keys);
|
|
5358
|
+
}
|
|
4929
5359
|
}
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
4935
|
-
|
|
4936
|
-
|
|
4937
|
-
|
|
5360
|
+
// src/cli/commands/status.ts
|
|
5361
|
+
async function statusCommand(cfg, values) {
|
|
5362
|
+
const registry = await extractProject(cfg);
|
|
5363
|
+
const { state, problem } = readState(cfg);
|
|
5364
|
+
if (problem) console.warn(`[verbaly] ${problem}, so drafts and outdated translations are not counted`);
|
|
5365
|
+
const result = status(cfg, loadCatalogs(cfg), registry, state.drafts, state.fingerprints);
|
|
5366
|
+
console.log(values.json ? JSON.stringify(result) : formatStatusResult(result));
|
|
5367
|
+
}
|
|
5368
|
+
// src/cli/commands/translate.ts
|
|
5369
|
+
async function translateCommand(cfg, values) {
|
|
5370
|
+
if (!values["dry-run"]) loadState(cfg);
|
|
5371
|
+
const registry = await extractProject(cfg);
|
|
5372
|
+
const catalogs = shippedCatalogs(cfg, loadCatalogs(cfg), registry, { newKeys: false });
|
|
5373
|
+
const result = await translateCatalogs(cfg, catalogs, await resolveProvider(cfg, values.model), {
|
|
5374
|
+
locales: csv(values.locales),
|
|
5375
|
+
batchSize: cfg.translate.batchSize,
|
|
5376
|
+
concurrency: cfg.translate.concurrency,
|
|
5377
|
+
retries: cfg.translate.retries,
|
|
5378
|
+
dryRun: values["dry-run"],
|
|
5379
|
+
origins: values["dry-run"] ? void 0 : await collectOrigins(cfg, registry),
|
|
5380
|
+
onProgress: values["dry-run"] ? void 0 : reportProgress
|
|
5381
|
+
});
|
|
5382
|
+
if (values["dry-run"]) {
|
|
5383
|
+
const entries = Object.entries(result.pending);
|
|
5384
|
+
if (entries.length === 0) {
|
|
5385
|
+
console.log("[verbaly] nothing to translate ✓");
|
|
4938
5386
|
return;
|
|
4939
5387
|
}
|
|
4940
|
-
|
|
4941
|
-
|
|
4942
|
-
await run();
|
|
4943
|
-
} catch (error) {
|
|
4944
|
-
console.warn("[verbaly] watch run failed:", error);
|
|
4945
|
-
} finally {
|
|
4946
|
-
running = false;
|
|
4947
|
-
if (queued) {
|
|
4948
|
-
queued = false;
|
|
4949
|
-
schedule();
|
|
4950
|
-
}
|
|
4951
|
-
}
|
|
5388
|
+
for (const [locale, keys] of entries) console.log(` ${locale}: ${keys.length} missing: ${keys.join(", ")}`);
|
|
5389
|
+
return;
|
|
4952
5390
|
}
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
5391
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
5392
|
+
const writes = [];
|
|
5393
|
+
for (const [locale, keys] of Object.entries(result.translated)) {
|
|
5394
|
+
const catalog = catalogs[locale] ?? {};
|
|
5395
|
+
const written = mergeTranslations(cfg, locale, catalog, keys);
|
|
5396
|
+
writes.push({
|
|
5397
|
+
locale,
|
|
5398
|
+
draft: true,
|
|
5399
|
+
entries: written.map((key) => ({
|
|
5400
|
+
key,
|
|
5401
|
+
text: catalog[key]
|
|
5402
|
+
}))
|
|
5403
|
+
});
|
|
5404
|
+
console.log(` ${locale}: +${written.length} translated (draft)`);
|
|
5405
|
+
const kept = keys.length - written.length;
|
|
5406
|
+
if (kept > 0) console.log(` ${locale}: ${counted(kept, "message")} kept as written while this ran`);
|
|
5407
|
+
}
|
|
5408
|
+
const lockBefore = stateFile(cfg);
|
|
5409
|
+
if (writes.length > 0) recordTranslations(cfg, source, writes);
|
|
5410
|
+
sayLockNews(cfg, lockBefore);
|
|
5411
|
+
for (const [locale, keys] of Object.entries(result.invalid)) console.warn(` ${locale}: ${keys.length} rejected (params/tags not preserved): ${keys.join(", ")}`);
|
|
5412
|
+
for (const line of formatTranslateFailures(result.failed)) console.error(line);
|
|
5413
|
+
if (result.failed.length > 0) process.exitCode = 1;
|
|
5414
|
+
if (Object.keys(result.translated).length === 0 && Object.keys(result.invalid).length === 0 && result.failed.length === 0) console.log("[verbaly] nothing to translate ✓");
|
|
5415
|
+
}
|
|
5416
|
+
function reportProgress(progress) {
|
|
5417
|
+
const head = ` ${progress.locale} ${progress.batch}/${progress.batches}`;
|
|
5418
|
+
if (progress.error) console.warn(`${head}: failed (${progress.error}), continuing`);
|
|
5419
|
+
else console.log(`${head}: ${counted(progress.keys, "message")}`);
|
|
5420
|
+
}
|
|
5421
|
+
// src/cli/commands/typegen.ts
|
|
5422
|
+
async function typegenCommand(cfg) {
|
|
5423
|
+
const registry = await extractProject(cfg);
|
|
5424
|
+
const source = shippedCatalogs(cfg, loadCatalogs(cfg), registry)[cfg.sourceLocale] ?? {};
|
|
5425
|
+
const written = writeDts(cfg, source);
|
|
5426
|
+
if (!written) {
|
|
5427
|
+
console.log("[verbaly] no types written: dts is false in your verbaly config");
|
|
5428
|
+
return;
|
|
4956
5429
|
}
|
|
4957
|
-
const
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
if (file.includes("node_modules/") || file.endsWith(".d.ts")) return;
|
|
4961
|
-
if (catalogDir && file.startsWith(`${catalogDir}/`)) return;
|
|
4962
|
-
if (SOURCE_FILE_RE.test(file)) schedule();
|
|
4963
|
-
});
|
|
4964
|
-
return () => {
|
|
4965
|
-
clearTimeout(timer);
|
|
4966
|
-
close();
|
|
4967
|
-
};
|
|
5430
|
+
const where = relative(cfg.root, written.file).replaceAll("\\", "/");
|
|
5431
|
+
const how = written.changed ? "written to" : "up to date in";
|
|
5432
|
+
console.log(`[verbaly] types ${how} ${where} (${counted(Object.keys(source).length, "message")})`);
|
|
4968
5433
|
}
|
|
4969
|
-
// src/wrap.ts
|
|
5434
|
+
// src/scan/wrap.ts
|
|
4970
5435
|
const WRAP_ATTRS = /* @__PURE__ */ new Set([
|
|
4971
5436
|
"title",
|
|
4972
5437
|
"alt",
|
|
@@ -5241,7 +5706,38 @@ function trailingWs(text) {
|
|
|
5241
5706
|
function line(node) {
|
|
5242
5707
|
return node.loc?.start?.line ?? 1;
|
|
5243
5708
|
}
|
|
5244
|
-
// src/
|
|
5709
|
+
// src/cli/commands/wrap.ts
|
|
5710
|
+
async function wrapCommand(cfg, values) {
|
|
5711
|
+
const result = await wrapProject(cfg, { write: values.write });
|
|
5712
|
+
if (result.wrapped.length === 0 && result.skipped.length === 0) {
|
|
5713
|
+
console.log(`[verbaly] nothing to wrap (${counted(result.files, "file")} scanned) ✓`);
|
|
5714
|
+
return;
|
|
5715
|
+
}
|
|
5716
|
+
const blocked = new Set(result.blocked.map((entry) => entry.file));
|
|
5717
|
+
const done = result.changed.filter((file) => !blocked.has(file));
|
|
5718
|
+
const texts = result.wrapped.filter((entry) => !blocked.has(entry.file));
|
|
5719
|
+
const verb = values.write ? "wrapped" : "would wrap";
|
|
5720
|
+
const note = values.write ? "" : " (report only, use --write to apply)";
|
|
5721
|
+
const counts = values.write ? `${counted(texts.length, "text")} in ${counted(done.length, "file")}` : `${counted(result.wrapped.length, "text")} in ${counted(result.changed.length, "file")}`;
|
|
5722
|
+
console.log(`[verbaly] ${verb} ${counts}${note}`);
|
|
5723
|
+
for (const entry of values.write ? texts : result.wrapped) {
|
|
5724
|
+
const attr = entry.kind === "attribute" ? `${entry.attribute} → ` : "";
|
|
5725
|
+
console.log(` ${entry.file}:${entry.line} ${attr}"${entry.text}"`);
|
|
5726
|
+
}
|
|
5727
|
+
if (result.skipped.length > 0) {
|
|
5728
|
+
console.log(" needs a human:");
|
|
5729
|
+
for (const entry of result.skipped) console.log(` ${entry.file}:${entry.line} "${entry.text}" (${entry.reason})`);
|
|
5730
|
+
}
|
|
5731
|
+
if (result.blocked.length > 0) {
|
|
5732
|
+
console.log(` nothing written in ${counted(result.blocked.length, "file")} with no t in scope:`);
|
|
5733
|
+
for (const entry of result.blocked) {
|
|
5734
|
+
const how = entry.client ? "\"use client\", so const t = useT()" : "no \"use client\": check which side renders it before you pick the binding";
|
|
5735
|
+
console.log(` ${entry.file} ${counted(entry.texts, "text")}, ${how}`);
|
|
5736
|
+
}
|
|
5737
|
+
console.log(" next: add t there, run verbaly wrap --write again, then verbaly extract");
|
|
5738
|
+
} else if (values.write && done.length > 0) console.log(" next: run verbaly extract");
|
|
5739
|
+
}
|
|
5740
|
+
// src/cli/run.ts
|
|
5245
5741
|
const HELP = `verbaly · i18n compiler
|
|
5246
5742
|
|
|
5247
5743
|
Usage:
|
|
@@ -5293,45 +5789,23 @@ Options:
|
|
|
5293
5789
|
Config file: verbaly.config.{js,mjs,ts,mts,json} at root (flags win).
|
|
5294
5790
|
The claude provider needs @anthropic-ai/sdk installed and ANTHROPIC_API_KEY set.
|
|
5295
5791
|
`;
|
|
5792
|
+
const COMMANDS = {
|
|
5793
|
+
doctor: doctorCommand,
|
|
5794
|
+
extract: extractCommand,
|
|
5795
|
+
typegen: typegenCommand,
|
|
5796
|
+
migrate: migrateCommand,
|
|
5797
|
+
wrap: wrapCommand,
|
|
5798
|
+
status: statusCommand,
|
|
5799
|
+
check: checkCommand,
|
|
5800
|
+
translate: translateCommand,
|
|
5801
|
+
review: reviewCommand,
|
|
5802
|
+
export: exportCommand,
|
|
5803
|
+
import: importCommand,
|
|
5804
|
+
render: renderCommand,
|
|
5805
|
+
pseudo: pseudoCommand
|
|
5806
|
+
};
|
|
5296
5807
|
async function runCli(args = process.argv.slice(2)) {
|
|
5297
|
-
const { positionals, values } =
|
|
5298
|
-
args,
|
|
5299
|
-
allowPositionals: true,
|
|
5300
|
-
options: {
|
|
5301
|
-
root: { type: "string" },
|
|
5302
|
-
dir: { type: "string" },
|
|
5303
|
-
source: { type: "string" },
|
|
5304
|
-
locales: { type: "string" },
|
|
5305
|
-
prune: { type: "boolean" },
|
|
5306
|
-
watch: { type: "boolean" },
|
|
5307
|
-
write: { type: "boolean" },
|
|
5308
|
-
json: { type: "boolean" },
|
|
5309
|
-
drafts: { type: "boolean" },
|
|
5310
|
-
outdated: { type: "boolean" },
|
|
5311
|
-
draft: { type: "boolean" },
|
|
5312
|
-
approve: { type: "boolean" },
|
|
5313
|
-
reporter: { type: "string" },
|
|
5314
|
-
model: { type: "string" },
|
|
5315
|
-
locale: { type: "string" },
|
|
5316
|
-
site: { type: "string" },
|
|
5317
|
-
attribute: { type: "string" },
|
|
5318
|
-
base: { type: "string" },
|
|
5319
|
-
"base-url": { type: "string" },
|
|
5320
|
-
redirect: { type: "boolean" },
|
|
5321
|
-
sitemap: { type: "boolean" },
|
|
5322
|
-
clean: { type: "boolean" },
|
|
5323
|
-
"dry-run": { type: "boolean" },
|
|
5324
|
-
plurals: { type: "boolean" },
|
|
5325
|
-
format: { type: "string" },
|
|
5326
|
-
out: { type: "string" },
|
|
5327
|
-
missing: { type: "boolean" },
|
|
5328
|
-
overwrite: { type: "boolean" },
|
|
5329
|
-
help: {
|
|
5330
|
-
type: "boolean",
|
|
5331
|
-
short: "h"
|
|
5332
|
-
}
|
|
5333
|
-
}
|
|
5334
|
-
});
|
|
5808
|
+
const { positionals, values } = parseCli(args);
|
|
5335
5809
|
const command = positionals[0];
|
|
5336
5810
|
if (values.help || !command) {
|
|
5337
5811
|
console.log(HELP);
|
|
@@ -5339,442 +5813,20 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
5339
5813
|
return;
|
|
5340
5814
|
}
|
|
5341
5815
|
if (rejectStrayFlags(command, values)) return;
|
|
5342
|
-
if (command === "init")
|
|
5343
|
-
const result = await init({
|
|
5344
|
-
root: values.root,
|
|
5345
|
-
dir: values.dir,
|
|
5346
|
-
sourceLocale: values.source,
|
|
5347
|
-
locales: csv(values.locales)
|
|
5348
|
-
});
|
|
5349
|
-
if (result.created.length) console.log(`[verbaly] created: ${result.created.join(", ")}`);
|
|
5350
|
-
if (result.skipped.length) console.log(` kept (already there): ${result.skipped.join(", ")}`);
|
|
5351
|
-
for (const { from, to } of result.renamed) console.log(` wrote ${to} for ${from}, since a locale tag takes a hyphen`);
|
|
5352
|
-
for (const locale of result.refused) console.warn(` left out "${locale}": it is not a locale tag, use one like es or pt-BR`);
|
|
5353
|
-
if (result.found) console.log(` catalogs: ${result.found}, kept where they already are`);
|
|
5354
|
-
if (result.host) console.log(` detected: ${result.host}`);
|
|
5355
|
-
console.log([" next steps:", ...result.next.map((step, i) => ` ${i + 1}. ${step}`)].join("\n"));
|
|
5356
|
-
return;
|
|
5357
|
-
}
|
|
5816
|
+
if (command === "init") return initCommand(values);
|
|
5358
5817
|
const cfg = await loadConfig(values.root ?? process.cwd(), {
|
|
5359
5818
|
dir: values.dir,
|
|
5360
5819
|
sourceLocale: values.source,
|
|
5361
5820
|
locales: csv(values.locales)
|
|
5362
5821
|
});
|
|
5363
|
-
if (command
|
|
5364
|
-
const result = await doctor(cfg);
|
|
5365
|
-
console.log(`[verbaly] doctor: ${counted(result.entries.length, "check")}`);
|
|
5366
|
-
for (const entry of result.entries) {
|
|
5367
|
-
const line = formatDoctorEntry(entry);
|
|
5368
|
-
if (entry.level === "error") console.error(line);
|
|
5369
|
-
else if (entry.level === "warn") console.warn(line);
|
|
5370
|
-
else console.log(line);
|
|
5371
|
-
}
|
|
5372
|
-
if (result.ok) console.log(`[verbaly] ${formatDoctorHealth(result)} ✓`);
|
|
5373
|
-
else {
|
|
5374
|
-
console.error("[verbaly] doctor found problems");
|
|
5375
|
-
process.exitCode = 1;
|
|
5376
|
-
}
|
|
5377
|
-
return;
|
|
5378
|
-
}
|
|
5379
|
-
if (command === "extract") {
|
|
5380
|
-
const dryRun = values["dry-run"];
|
|
5381
|
-
if (values.watch && (dryRun || values.prune)) {
|
|
5382
|
-
console.error("[verbaly] --watch runs alone: prune is a deliberate one-shot action and dry-run writes nothing");
|
|
5383
|
-
process.exitCode = 1;
|
|
5384
|
-
return;
|
|
5385
|
-
}
|
|
5386
|
-
const reporter = values.watch ? createDevReporter(cfg.root, (line) => console.warn(` ${line}`)) : void 0;
|
|
5387
|
-
async function runExtract() {
|
|
5388
|
-
const result = await syncProject(cfg, {
|
|
5389
|
-
prune: values.prune,
|
|
5390
|
-
dryRun
|
|
5391
|
-
});
|
|
5392
|
-
if (result.pruneBlocked) {
|
|
5393
|
-
const unread = result.registry.parseErrors().length;
|
|
5394
|
-
console.warn(` prune skipped: an unparsed file may read any key (${counted(unread, "file")} below), so it waits until every file parses`);
|
|
5395
|
-
}
|
|
5396
|
-
for (const [locale, keys] of Object.entries(result.pruned)) console.log(dryRun ? ` ${locale}: would prune ${keys.length}: ${keys.join(", ")}` : ` ${locale}: -${keys.length} pruned`);
|
|
5397
|
-
const origin = result.inCode === result.messages ? "" : ` (${result.inCode} in code)`;
|
|
5398
|
-
console.log(`[verbaly] ${counted(result.messages, "message")}${origin} · locales: ${cfg.locales.join(", ")}${dryRun ? " (dry run, nothing written)" : ""}`);
|
|
5399
|
-
for (const [locale, keys] of Object.entries(result.added)) console.log(` ${locale}: ${dryRun ? `would add ${keys.length}` : `+${keys.length}`}`);
|
|
5400
|
-
const warnings = result.findings.filter((finding) => {
|
|
5401
|
-
if (finding.kind !== "replaced" && finding.kind !== "lock") return true;
|
|
5402
|
-
console.log(` ${formatFinding(finding, cfg.root)}`);
|
|
5403
|
-
return false;
|
|
5404
|
-
});
|
|
5405
|
-
if (reporter) reporter.report("extract", warnings);
|
|
5406
|
-
else for (const finding of warnings) console.warn(` ${formatFinding(finding, cfg.root)}`);
|
|
5407
|
-
if (result.stateProblem) console.warn(` ${result.stateProblem}, so drafts and outdated translations were not updated`);
|
|
5408
|
-
}
|
|
5409
|
-
await runExtract();
|
|
5410
|
-
if (values.watch) {
|
|
5411
|
-
watchProject(cfg, runExtract);
|
|
5412
|
-
console.log("[verbaly] watching for source changes (ctrl+c to stop)");
|
|
5413
|
-
}
|
|
5414
|
-
return;
|
|
5415
|
-
}
|
|
5416
|
-
if (command === "typegen") {
|
|
5417
|
-
const registry = await extractProject(cfg);
|
|
5418
|
-
const source = shippedCatalogs(cfg, loadCatalogs(cfg), registry)[cfg.sourceLocale] ?? {};
|
|
5419
|
-
const written = writeDts(cfg, source);
|
|
5420
|
-
if (!written) {
|
|
5421
|
-
console.log("[verbaly] no types written: dts is false in your verbaly config");
|
|
5422
|
-
return;
|
|
5423
|
-
}
|
|
5424
|
-
const where = relative(cfg.root, written.file).replaceAll("\\", "/");
|
|
5425
|
-
const how = written.changed ? "written to" : "up to date in";
|
|
5426
|
-
console.log(`[verbaly] types ${how} ${where} (${counted(Object.keys(source).length, "message")})`);
|
|
5427
|
-
return;
|
|
5428
|
-
}
|
|
5429
|
-
if (command === "migrate") {
|
|
5430
|
-
const lockBefore = stateFile(cfg);
|
|
5431
|
-
const result = migrateCatalogs(cfg, {
|
|
5432
|
-
write: values.write,
|
|
5433
|
-
plurals: values.plurals
|
|
5434
|
-
});
|
|
5435
|
-
const from = result.detected.length ? result.detected.join(", ") : "no known i18n library";
|
|
5436
|
-
const changes = result.braces.length + result.plurals.length;
|
|
5437
|
-
const before = result.remembered.length ? `, ${counted(result.remembered.length, "language")} converted before` : "";
|
|
5438
|
-
if (changes === 0 && result.skipped.length === 0) {
|
|
5439
|
-
console.log(`[verbaly] catalogs need nothing (${from}${before}) ✓`);
|
|
5440
|
-
return;
|
|
5441
|
-
}
|
|
5442
|
-
const verb = values.write ? "ported" : "would port";
|
|
5443
|
-
const note = values.write ? "" : " (report only, use --write to apply)";
|
|
5444
|
-
console.log(`[verbaly] ${verb} ${counted(changes, "message")} from ${from}${before}${note}`);
|
|
5445
|
-
for (const entry of result.braces) console.log(` ${entry.locale} ${entry.key} ${entry.before} → ${entry.after}`);
|
|
5446
|
-
for (const entry of result.plurals) console.log(` ${entry.locale} ${entry.key} ${entry.from.join(" + ")} → ${entry.after}`);
|
|
5447
|
-
if (!values.plurals && changes > 0) console.log(" --plurals also merges _one/_other into one message with variants");
|
|
5448
|
-
if (result.skipped.length > 0) {
|
|
5449
|
-
console.log(" needs a human:");
|
|
5450
|
-
for (const entry of result.skipped) console.log(` ${entry.locale} ${entry.key} ${entry.reason}`);
|
|
5451
|
-
}
|
|
5452
|
-
if (values.write && result.braces.length > 0) console.log(` ${STATE_FILE} remembers the converted languages, so a later run never reads them as i18next again`);
|
|
5453
|
-
sayLockNews(cfg, lockBefore);
|
|
5454
|
-
if (values.write) console.log(" next: run verbaly check");
|
|
5455
|
-
return;
|
|
5456
|
-
}
|
|
5457
|
-
if (command === "wrap") {
|
|
5458
|
-
const result = await wrapProject(cfg, { write: values.write });
|
|
5459
|
-
if (result.wrapped.length === 0 && result.skipped.length === 0) {
|
|
5460
|
-
console.log(`[verbaly] nothing to wrap (${counted(result.files, "file")} scanned) ✓`);
|
|
5461
|
-
return;
|
|
5462
|
-
}
|
|
5463
|
-
const blocked = new Set(result.blocked.map((entry) => entry.file));
|
|
5464
|
-
const done = result.changed.filter((file) => !blocked.has(file));
|
|
5465
|
-
const texts = result.wrapped.filter((entry) => !blocked.has(entry.file));
|
|
5466
|
-
const verb = values.write ? "wrapped" : "would wrap";
|
|
5467
|
-
const note = values.write ? "" : " (report only, use --write to apply)";
|
|
5468
|
-
const counts = values.write ? `${counted(texts.length, "text")} in ${counted(done.length, "file")}` : `${counted(result.wrapped.length, "text")} in ${counted(result.changed.length, "file")}`;
|
|
5469
|
-
console.log(`[verbaly] ${verb} ${counts}${note}`);
|
|
5470
|
-
for (const entry of values.write ? texts : result.wrapped) {
|
|
5471
|
-
const attr = entry.kind === "attribute" ? `${entry.attribute} → ` : "";
|
|
5472
|
-
console.log(` ${entry.file}:${entry.line} ${attr}"${entry.text}"`);
|
|
5473
|
-
}
|
|
5474
|
-
if (result.skipped.length > 0) {
|
|
5475
|
-
console.log(" needs a human:");
|
|
5476
|
-
for (const entry of result.skipped) console.log(` ${entry.file}:${entry.line} "${entry.text}" (${entry.reason})`);
|
|
5477
|
-
}
|
|
5478
|
-
if (result.blocked.length > 0) {
|
|
5479
|
-
console.log(` nothing written in ${counted(result.blocked.length, "file")} with no t in scope:`);
|
|
5480
|
-
for (const entry of result.blocked) {
|
|
5481
|
-
const how = entry.client ? "\"use client\", so const t = useT()" : "no \"use client\": check which side renders it before you pick the binding";
|
|
5482
|
-
console.log(` ${entry.file} ${counted(entry.texts, "text")}, ${how}`);
|
|
5483
|
-
}
|
|
5484
|
-
console.log(" next: add t there, run verbaly wrap --write again, then verbaly extract");
|
|
5485
|
-
} else if (values.write && done.length > 0) console.log(" next: run verbaly extract");
|
|
5486
|
-
return;
|
|
5487
|
-
}
|
|
5488
|
-
if (command === "status") {
|
|
5489
|
-
const registry = await extractProject(cfg);
|
|
5490
|
-
const { state, problem } = readState(cfg);
|
|
5491
|
-
if (problem) console.warn(`[verbaly] ${problem}, so drafts and outdated translations are not counted`);
|
|
5492
|
-
const result = status(cfg, loadCatalogs(cfg), registry, state.drafts, state.fingerprints);
|
|
5493
|
-
console.log(values.json ? JSON.stringify(result) : formatStatusResult(result));
|
|
5494
|
-
return;
|
|
5495
|
-
}
|
|
5496
|
-
if (command === "check") {
|
|
5497
|
-
const reporter = values.reporter ?? "text";
|
|
5498
|
-
if (reporter !== "text" && reporter !== "github") {
|
|
5499
|
-
console.error(`[verbaly] unknown reporter "${values.reporter}", use text or github`);
|
|
5500
|
-
process.exitCode = 1;
|
|
5501
|
-
return;
|
|
5502
|
-
}
|
|
5503
|
-
const registry = await extractProject(cfg);
|
|
5504
|
-
const catalogs = loadCatalogs(cfg);
|
|
5505
|
-
const state = checkState(cfg, values.drafts === true || values.outdated === true);
|
|
5506
|
-
const result = check(cfg, catalogs, registry, state.fingerprints);
|
|
5507
|
-
const unreviewed = values.drafts ? effectiveDrafts(state.drafts, catalogs) : {};
|
|
5508
|
-
const outdated = values.outdated ? result.outdated : [];
|
|
5509
|
-
const gated = { outdatedFails: values.outdated === true };
|
|
5510
|
-
if (reporter === "github") for (const line of githubCheckAnnotations(result, registry, cfg, gated)) console.error(line);
|
|
5511
|
-
else {
|
|
5512
|
-
const warnings = formatCheckWarnings(result, cfg.root, gated);
|
|
5513
|
-
if (warnings) console.warn(`[verbaly] warnings (the gate still passes)\n${warnings}`);
|
|
5514
|
-
}
|
|
5515
|
-
const awaiting = [...Object.entries(unreviewed).map(([locale, keys]) => ` [${locale}] ${keys.length} unreviewed: ${keys.join(", ")}`), ...byLocale(outdated).map(([locale, keys]) => ` [${locale}] ${keys.length} outdated: ${keys.join(", ")}`)];
|
|
5516
|
-
if (result.ok && awaiting.length === 0) {
|
|
5517
|
-
console.log("[verbaly] all translations complete ✓");
|
|
5518
|
-
return;
|
|
5519
|
-
}
|
|
5520
|
-
if (!result.ok) {
|
|
5521
|
-
const brokenCount = result.broken.filter((entry) => entry.severity === "error").length;
|
|
5522
|
-
const report = reporter === "github" ? `[verbaly] check failed: ${result.missing.length} missing, ${result.unknown.length} unknown, ${brokenCount} broken, ${counted(result.collisions.length, "collision")}` : `[verbaly] check failed\n${formatCheckResult(result, cfg.root)}`;
|
|
5523
|
-
console.error(`${report}\n${checkNextSteps(result)}`);
|
|
5524
|
-
}
|
|
5525
|
-
if (awaiting.length > 0) {
|
|
5526
|
-
for (const line of awaiting) console.error(line);
|
|
5527
|
-
console.error("[verbaly] check failed: translations awaiting review (run verbaly review, then --approve what holds)");
|
|
5528
|
-
}
|
|
5529
|
-
process.exitCode = 1;
|
|
5530
|
-
return;
|
|
5531
|
-
}
|
|
5532
|
-
if (command === "translate") {
|
|
5533
|
-
if (!values["dry-run"]) loadState(cfg);
|
|
5534
|
-
const registry = await extractProject(cfg);
|
|
5535
|
-
const catalogs = shippedCatalogs(cfg, loadCatalogs(cfg), registry, { newKeys: false });
|
|
5536
|
-
const result = await translateCatalogs(cfg, catalogs, await resolveProvider(cfg, values.model), {
|
|
5537
|
-
locales: csv(values.locales),
|
|
5538
|
-
batchSize: cfg.translate.batchSize,
|
|
5539
|
-
concurrency: cfg.translate.concurrency,
|
|
5540
|
-
retries: cfg.translate.retries,
|
|
5541
|
-
dryRun: values["dry-run"],
|
|
5542
|
-
origins: values["dry-run"] ? void 0 : await collectOrigins(cfg, registry),
|
|
5543
|
-
onProgress: values["dry-run"] ? void 0 : reportProgress
|
|
5544
|
-
});
|
|
5545
|
-
if (values["dry-run"]) {
|
|
5546
|
-
const entries = Object.entries(result.pending);
|
|
5547
|
-
if (entries.length === 0) {
|
|
5548
|
-
console.log("[verbaly] nothing to translate ✓");
|
|
5549
|
-
return;
|
|
5550
|
-
}
|
|
5551
|
-
for (const [locale, keys] of entries) console.log(` ${locale}: ${keys.length} missing: ${keys.join(", ")}`);
|
|
5552
|
-
return;
|
|
5553
|
-
}
|
|
5554
|
-
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
5555
|
-
const writes = [];
|
|
5556
|
-
for (const [locale, keys] of Object.entries(result.translated)) {
|
|
5557
|
-
const catalog = catalogs[locale] ?? {};
|
|
5558
|
-
const written = mergeTranslations(cfg, locale, catalog, keys);
|
|
5559
|
-
writes.push({
|
|
5560
|
-
locale,
|
|
5561
|
-
draft: true,
|
|
5562
|
-
entries: written.map((key) => ({
|
|
5563
|
-
key,
|
|
5564
|
-
text: catalog[key]
|
|
5565
|
-
}))
|
|
5566
|
-
});
|
|
5567
|
-
console.log(` ${locale}: +${written.length} translated (draft)`);
|
|
5568
|
-
const kept = keys.length - written.length;
|
|
5569
|
-
if (kept > 0) console.log(` ${locale}: ${counted(kept, "message")} kept as written while this ran`);
|
|
5570
|
-
}
|
|
5571
|
-
const lockBefore = stateFile(cfg);
|
|
5572
|
-
if (writes.length > 0) recordTranslations(cfg, source, writes);
|
|
5573
|
-
sayLockNews(cfg, lockBefore);
|
|
5574
|
-
for (const [locale, keys] of Object.entries(result.invalid)) console.warn(` ${locale}: ${keys.length} rejected (params/tags not preserved): ${keys.join(", ")}`);
|
|
5575
|
-
for (const line of formatTranslateFailures(result.failed)) console.error(line);
|
|
5576
|
-
if (result.failed.length > 0) process.exitCode = 1;
|
|
5577
|
-
if (Object.keys(result.translated).length === 0 && Object.keys(result.invalid).length === 0 && result.failed.length === 0) console.log("[verbaly] nothing to translate ✓");
|
|
5578
|
-
return;
|
|
5579
|
-
}
|
|
5580
|
-
if (command === "review") {
|
|
5581
|
-
const registry = await extractProject(cfg);
|
|
5582
|
-
const catalogs = loadCatalogs(cfg);
|
|
5583
|
-
const shipped = shippedCatalogs(cfg, catalogs, registry);
|
|
5584
|
-
const state = loadState(cfg);
|
|
5585
|
-
const only = new Set(positionals.slice(1));
|
|
5586
|
-
const wanted = ({ locale, key }) => (!values.locale || locale === values.locale) && (only.size === 0 || only.has(key));
|
|
5587
|
-
const drafts = Object.entries(effectiveDrafts(state.drafts, catalogs)).flatMap(([locale, keys]) => keys.map((key) => ({
|
|
5588
|
-
locale,
|
|
5589
|
-
key
|
|
5590
|
-
}))).filter(wanted);
|
|
5591
|
-
const outdated = outdatedTranslations(cfg, shipped, state.fingerprints).filter(wanted);
|
|
5592
|
-
if (drafts.length === 0 && outdated.length === 0) {
|
|
5593
|
-
console.log("[verbaly] nothing awaiting review ✓");
|
|
5594
|
-
return;
|
|
5595
|
-
}
|
|
5596
|
-
if (values.approve) {
|
|
5597
|
-
for (const [locale, keys] of byLocale(drafts)) {
|
|
5598
|
-
clearDrafts(state.drafts, locale, keys);
|
|
5599
|
-
console.log(` ${locale}: ${keys.length} approved`);
|
|
5600
|
-
}
|
|
5601
|
-
acceptOutdated(cfg, shipped, state.fingerprints, outdated);
|
|
5602
|
-
for (const [locale, keys] of byLocale(outdated)) console.log(` ${locale}: ${keys.length} kept for the new source text`);
|
|
5603
|
-
const lockBefore = stateFile(cfg);
|
|
5604
|
-
saveState(cfg, state);
|
|
5605
|
-
sayLockNews(cfg, lockBefore);
|
|
5606
|
-
const reviewed = new Set([...drafts, ...outdated].map(({ locale, key }) => `${locale}:${key}`));
|
|
5607
|
-
console.log(`[verbaly] ${counted(reviewed.size, "translation")} marked reviewed ✓`);
|
|
5608
|
-
return;
|
|
5609
|
-
}
|
|
5610
|
-
const source = shipped[cfg.sourceLocale] ?? {};
|
|
5611
|
-
const show = (locale, keys) => {
|
|
5612
|
-
console.log(` ${locale}:`);
|
|
5613
|
-
for (const key of keys) {
|
|
5614
|
-
const translated = catalogs[locale]?.[key] ?? "";
|
|
5615
|
-
console.log(` ${key}: "${truncate(source[key] ?? "", 60)}" → "${truncate(translated, 60)}"`);
|
|
5616
|
-
}
|
|
5617
|
-
};
|
|
5618
|
-
if (drafts.length > 0) {
|
|
5619
|
-
console.log(`[verbaly] ${counted(drafts.length, "machine translation")} awaiting review (--approve to accept)`);
|
|
5620
|
-
for (const [locale, keys] of byLocale(drafts)) show(locale, keys);
|
|
5621
|
-
}
|
|
5622
|
-
if (outdated.length > 0) {
|
|
5623
|
-
console.log(`[verbaly] ${counted(outdated.length, "translation")} written for an older source text (update them, or --approve keeps them)`);
|
|
5624
|
-
for (const [locale, keys] of byLocale(outdated)) show(locale, keys);
|
|
5625
|
-
}
|
|
5626
|
-
return;
|
|
5627
|
-
}
|
|
5628
|
-
if (command === "export") {
|
|
5629
|
-
const format = values.format ?? "xliff";
|
|
5630
|
-
if (![
|
|
5631
|
-
"xliff",
|
|
5632
|
-
"csv",
|
|
5633
|
-
"po",
|
|
5634
|
-
"android-xml",
|
|
5635
|
-
"ios-strings"
|
|
5636
|
-
].includes(format)) {
|
|
5637
|
-
console.error(`[verbaly] unknown format "${values.format}", use xliff, csv, po, android-xml or ios-strings`);
|
|
5638
|
-
process.exitCode = 1;
|
|
5639
|
-
return;
|
|
5640
|
-
}
|
|
5641
|
-
if (values.missing && isMobileFormat(format)) {
|
|
5642
|
-
console.error(`[verbaly] --missing is for translator formats (xliff, csv): ${format} already skips untranslated keys so the app falls back to the source locale`);
|
|
5643
|
-
process.exitCode = 1;
|
|
5644
|
-
return;
|
|
5645
|
-
}
|
|
5646
|
-
const registry = await extractProject(cfg);
|
|
5647
|
-
const result = exportCatalogs(cfg, shippedCatalogs(cfg, loadCatalogs(cfg), registry, { newKeys: false }), {
|
|
5648
|
-
locales: csv(values.locales),
|
|
5649
|
-
format,
|
|
5650
|
-
out: values.out,
|
|
5651
|
-
missing: values.missing,
|
|
5652
|
-
origins: isMobileFormat(format) ? void 0 : await collectOrigins(cfg, registry)
|
|
5653
|
-
});
|
|
5654
|
-
if (result.files.length === 0) {
|
|
5655
|
-
console.log("[verbaly] no target locales to export (add locales to your config)");
|
|
5656
|
-
return;
|
|
5657
|
-
}
|
|
5658
|
-
const note = isMobileFormat(result.format) ? "untranslated skipped" : "untranslated";
|
|
5659
|
-
console.log(`[verbaly] exported ${counted(result.files.length, "locale")} (${result.format}) → ${result.dir}`);
|
|
5660
|
-
for (const file of result.files) console.log(` ${file.locale}: ${counted(file.total, "message")} (${file.untranslated} ${note}) → ${file.path}`);
|
|
5661
|
-
return;
|
|
5662
|
-
}
|
|
5663
|
-
if (command === "import") {
|
|
5664
|
-
const files = positionals.slice(1);
|
|
5665
|
-
if (files.length === 0) {
|
|
5666
|
-
console.error("[verbaly] import needs at least one file: verbaly import verbaly-export/es.xlf");
|
|
5667
|
-
process.exitCode = 1;
|
|
5668
|
-
return;
|
|
5669
|
-
}
|
|
5670
|
-
if (!values["dry-run"]) loadState(cfg);
|
|
5671
|
-
const registry = await extractProject(cfg);
|
|
5672
|
-
const catalogs = shippedCatalogs(cfg, loadCatalogs(cfg), registry, { newKeys: false });
|
|
5673
|
-
const result = importCatalogs(cfg, catalogs, files, {
|
|
5674
|
-
locale: values.locale,
|
|
5675
|
-
overwrite: values.overwrite,
|
|
5676
|
-
dryRun: values["dry-run"]
|
|
5677
|
-
});
|
|
5678
|
-
for (const { file, from, to } of result.mapped) console.log(` ${file}: "${from}" read as ${to}`);
|
|
5679
|
-
for (const { file, locale, reason } of result.unmatched) {
|
|
5680
|
-
if (reason === "undeclared") {
|
|
5681
|
-
console.warn(` ${file}: "${locale}" is not one of this project's locales (${cfg.locales.join(", ")}), so nothing was imported from it`);
|
|
5682
|
-
console.warn(` fix: add ${locale} to locales in your verbaly config, or pass --locale with one of them`);
|
|
5683
|
-
} else {
|
|
5684
|
-
console.warn(` ${file}: "${locale}" is not a locale tag, so nothing was imported from it`);
|
|
5685
|
-
console.warn(" fix: pass --locale with the tag it stands for, like pt-BR");
|
|
5686
|
-
}
|
|
5687
|
-
process.exitCode = 1;
|
|
5688
|
-
}
|
|
5689
|
-
const writes = [];
|
|
5690
|
-
for (const [locale, keys] of Object.entries(result.imported)) {
|
|
5691
|
-
if (!values["dry-run"]) {
|
|
5692
|
-
const catalog = catalogs[locale] ?? {};
|
|
5693
|
-
writeCatalog(cfg, locale, catalog);
|
|
5694
|
-
const seen = result.sources[locale] ?? {};
|
|
5695
|
-
const entries = keys.map((key) => ({
|
|
5696
|
-
key,
|
|
5697
|
-
text: catalog[key],
|
|
5698
|
-
source: seen[key]
|
|
5699
|
-
}));
|
|
5700
|
-
writes.push({
|
|
5701
|
-
locale,
|
|
5702
|
-
entries,
|
|
5703
|
-
draft: values.draft === true
|
|
5704
|
-
});
|
|
5705
|
-
}
|
|
5706
|
-
const verb = values["dry-run"] ? "would import" : "imported";
|
|
5707
|
-
console.log(` ${locale}: +${keys.length} ${verb}${values.draft ? " (draft)" : ""}`);
|
|
5708
|
-
}
|
|
5709
|
-
const lockBefore = stateFile(cfg);
|
|
5710
|
-
if (writes.length > 0) recordTranslations(cfg, catalogs[cfg.sourceLocale] ?? {}, writes);
|
|
5711
|
-
sayLockNews(cfg, lockBefore);
|
|
5712
|
-
for (const [locale, keys] of Object.entries(result.skipped)) console.log(` ${locale}: ${keys.length} already translated, kept (use --overwrite to replace)`);
|
|
5713
|
-
for (const [locale, keys] of Object.entries(result.rejected)) console.warn(` ${locale}: ${keys.length} rejected (params/tags not preserved): ${keys.join(", ")}`);
|
|
5714
|
-
for (const [locale, keys] of Object.entries(result.unknown)) console.warn(` ${locale}: ${counted(keys.length, "unknown key")} ignored (not in the source catalog): ${keys.join(", ")}`);
|
|
5715
|
-
if (Object.keys(result.imported).length === 0 && result.unmatched.length === 0) console.log("[verbaly] nothing to import ✓");
|
|
5716
|
-
return;
|
|
5717
|
-
}
|
|
5718
|
-
if (command === "render") {
|
|
5719
|
-
const result = await renderSite(cfg, {
|
|
5720
|
-
site: values.site,
|
|
5721
|
-
locales: csv(values.locales),
|
|
5722
|
-
attribute: values.attribute,
|
|
5723
|
-
base: values.base,
|
|
5724
|
-
baseUrl: values["base-url"],
|
|
5725
|
-
sitemap: values.sitemap,
|
|
5726
|
-
redirect: values.redirect,
|
|
5727
|
-
clean: values.clean
|
|
5728
|
-
});
|
|
5729
|
-
console.log(`[verbaly] ${counted(result.files, "page")} × ${counted(result.locales.length, "locale")} (${result.locales.join(", ")})`);
|
|
5730
|
-
for (const line of formatRenderWarnings(result, cfg.sourceLocale)) console.warn(line);
|
|
5731
|
-
return;
|
|
5732
|
-
}
|
|
5733
|
-
if (command === "pseudo") {
|
|
5734
|
-
const catalogs = shippedCatalogs(cfg, loadCatalogs(cfg), await extractProject(cfg), { newKeys: false });
|
|
5735
|
-
const locale = values.locale ?? "en-XA";
|
|
5736
|
-
const keys = pseudoCatalogs(cfg, catalogs, locale);
|
|
5737
|
-
writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
5738
|
-
console.log(`[verbaly] ${counted(keys.length, "message")} pseudo-localized → ${locale}`);
|
|
5739
|
-
return;
|
|
5740
|
-
}
|
|
5822
|
+
if (Object.hasOwn(COMMANDS, command)) return COMMANDS[command](cfg, values, positionals);
|
|
5741
5823
|
console.error(`[verbaly] unknown command "${command}"\n${HELP}`);
|
|
5742
5824
|
process.exitCode = 1;
|
|
5743
5825
|
}
|
|
5744
|
-
function csv(value) {
|
|
5745
|
-
return value?.split(",").map((item) => item.trim()).filter(Boolean);
|
|
5746
|
-
}
|
|
5747
|
-
function reportProgress(progress) {
|
|
5748
|
-
const head = ` ${progress.locale} ${progress.batch}/${progress.batches}`;
|
|
5749
|
-
if (progress.error) console.warn(`${head}: failed (${progress.error}), continuing`);
|
|
5750
|
-
else console.log(`${head}: ${counted(progress.keys, "message")}`);
|
|
5751
|
-
}
|
|
5752
5826
|
function formatCliError(error) {
|
|
5753
5827
|
const message = error instanceof Error ? error.message : String(error);
|
|
5754
5828
|
return message.startsWith("[verbaly]") ? message : `[verbaly] ${message}`;
|
|
5755
5829
|
}
|
|
5756
|
-
function checkState(cfg, gating) {
|
|
5757
|
-
if (gating) return loadState(cfg);
|
|
5758
|
-
const { state, problem } = readState(cfg);
|
|
5759
|
-
if (problem) console.warn(`[verbaly] ${problem}, so outdated translations are not reported`);
|
|
5760
|
-
return state;
|
|
5761
|
-
}
|
|
5762
|
-
function sayLockNews(cfg, before) {
|
|
5763
|
-
const news = lockNews(cfg, before);
|
|
5764
|
-
if (news) console.log(` ${formatFinding({
|
|
5765
|
-
kind: "lock",
|
|
5766
|
-
...news
|
|
5767
|
-
}, cfg.root)}`);
|
|
5768
|
-
}
|
|
5769
|
-
function byLocale(entries) {
|
|
5770
|
-
const out = /* @__PURE__ */ new Map();
|
|
5771
|
-
for (const { locale, key } of entries) {
|
|
5772
|
-
const keys = out.get(locale);
|
|
5773
|
-
if (keys) keys.push(key);
|
|
5774
|
-
else out.set(locale, [key]);
|
|
5775
|
-
}
|
|
5776
|
-
return [...out];
|
|
5777
|
-
}
|
|
5778
5830
|
const COMMON_FLAGS = /* @__PURE__ */ new Set([
|
|
5779
5831
|
"root",
|
|
5780
5832
|
"dir",
|
|
@@ -5824,13 +5876,12 @@ const COMMAND_FLAGS = {
|
|
|
5824
5876
|
]
|
|
5825
5877
|
};
|
|
5826
5878
|
function rejectStrayFlags(command, values) {
|
|
5827
|
-
|
|
5828
|
-
|
|
5829
|
-
const allowed = /* @__PURE__ */ new Set([...COMMON_FLAGS, ...own]);
|
|
5879
|
+
if (!Object.hasOwn(COMMAND_FLAGS, command)) return false;
|
|
5880
|
+
const allowed = /* @__PURE__ */ new Set([...COMMON_FLAGS, ...COMMAND_FLAGS[command]]);
|
|
5830
5881
|
const stray = Object.keys(values).filter((k) => values[k] !== void 0 && !allowed.has(k));
|
|
5831
5882
|
if (stray.length === 0) return false;
|
|
5832
5883
|
for (const flag of stray) console.error(`[verbaly] --${flag} is not a "${command}" flag${flag === "locale" ? " (did you mean --locales?)" : ""}`);
|
|
5833
5884
|
process.exitCode = 1;
|
|
5834
5885
|
return true;
|
|
5835
5886
|
}
|
|
5836
|
-
export {
|
|
5887
|
+
export { validatePair as $, check as A, readState as B, isTransformTarget as C, transformSource as D, runBuildGate as E, clearDrafts as F, createDevReporter as G, saveState as H, effectiveDrafts as I, generateDts as J, formatCollision as K, loadState as L, formatCheckWarnings as M, syncProject as N, writeBuildTypes as O, STATE_FILE as P, validateMessage as Q, markDrafts as R, createSourceFilter as S, resolveVirtualId as T, updateState as U, recordTranslations as V, collisionEntries as W, generateRuntimeModule as X, generateLocaleModule as Y, writeDts as Z, status as _, isModuleNotFound as _t, renderSite as a, stableKey as at, RESOLVED_VIRTUAL_ID as b, targetLocales as bt, formatTranslateFailures as c, MessageRegistry as ct, translateCatalogs as d, loadCatalogs as dt, collectOrigins as et, writeDrafts as f, needsIcu as ft, formatStatusResult as g, writeCatalog as gt, formatDoctorHealth as h, readCatalog as ht, formatRenderWarnings as i, syncCatalogs as it, formatCheckResult as j, transformCode as k, mergeTranslations as l, clientCatalogs as lt, formatDoctorEntry as m, parseCatalog as mt, runCli as n, pruneCatalogs as nt, init as o, APP_ID as ot, doctor as p, needsRelative as pt, formatFinding as q, wrapProject as r, shippedCatalogs as rt, watchTree as s, LOCALE_IMPORT as st, formatCliError as t, extractProject as tt, resolveProvider as u, counted as ut, reportOutsideInclude as v, loadConfig as vt, loadVirtualModule as w, SOURCE_FILE_RE as x, LOCALE_MODULE_PREFIX as y, resolveConfig as yt, outdatedTranslations as z };
|