@verbaly/compiler 0.29.0 → 0.31.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -7
- package/dist/cli.js +350 -82
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +32 -3
- package/dist/index.js +358 -90
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { parse } from "@babel/parser";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
4
4
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
5
|
-
import { RICH_TAGS, createVerbaly, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
|
|
5
|
+
import { RICH_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import picomatch from "picomatch";
|
|
8
8
|
import MagicString from "magic-string";
|
|
@@ -42,7 +42,7 @@ function analyze(code, file, options = {}) {
|
|
|
42
42
|
});
|
|
43
43
|
const tagged = [];
|
|
44
44
|
const usedKeys = [];
|
|
45
|
-
walk(ast.program, (node) => {
|
|
45
|
+
walk$1(ast.program, (node) => {
|
|
46
46
|
if (node.type === "TaggedTemplateExpression") {
|
|
47
47
|
const tag = node.tag;
|
|
48
48
|
const explicit = explicitId(tag, names);
|
|
@@ -206,7 +206,7 @@ function selfClosedSource(code, opening) {
|
|
|
206
206
|
}
|
|
207
207
|
function containsTaggedT(node, names) {
|
|
208
208
|
let found = false;
|
|
209
|
-
walk(node, (n) => {
|
|
209
|
+
walk$1(node, (n) => {
|
|
210
210
|
if (n.type !== "TaggedTemplateExpression") return;
|
|
211
211
|
const tag = n.tag;
|
|
212
212
|
if (isTReference(tag, names) || explicitId(tag, names)) found = true;
|
|
@@ -271,14 +271,14 @@ function uniqueName(base, source, taken) {
|
|
|
271
271
|
taken.set(name, source);
|
|
272
272
|
return name;
|
|
273
273
|
}
|
|
274
|
-
function walk(node, visit) {
|
|
274
|
+
function walk$1(node, visit) {
|
|
275
275
|
visit(node);
|
|
276
276
|
for (const key in node) {
|
|
277
277
|
if (SKIP_KEYS.has(key)) continue;
|
|
278
278
|
const value = node[key];
|
|
279
279
|
if (Array.isArray(value)) {
|
|
280
|
-
for (const item of value) if (isNode(item)) walk(item, visit);
|
|
281
|
-
} else if (isNode(value)) walk(value, visit);
|
|
280
|
+
for (const item of value) if (isNode(item)) walk$1(item, visit);
|
|
281
|
+
} else if (isNode(value)) walk$1(value, visit);
|
|
282
282
|
}
|
|
283
283
|
}
|
|
284
284
|
function isNode(value) {
|
|
@@ -466,12 +466,175 @@ function writeCatalog(cfg, locale, catalog) {
|
|
|
466
466
|
return serialized;
|
|
467
467
|
}
|
|
468
468
|
//#endregion
|
|
469
|
+
//#region src/validate.ts
|
|
470
|
+
const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
|
|
471
|
+
"zero",
|
|
472
|
+
"one",
|
|
473
|
+
"two",
|
|
474
|
+
"few",
|
|
475
|
+
"many"
|
|
476
|
+
]);
|
|
477
|
+
const EXACT = /^=\d+$/;
|
|
478
|
+
function shapes(message) {
|
|
479
|
+
const out = /* @__PURE__ */ new Map();
|
|
480
|
+
walk(parse$1(message), out);
|
|
481
|
+
return out;
|
|
482
|
+
}
|
|
483
|
+
function walk(nodes, out) {
|
|
484
|
+
for (const node of nodes) {
|
|
485
|
+
if (node.kind !== "param" || !node.variants) continue;
|
|
486
|
+
const selectors = node.variants.map(([key]) => key);
|
|
487
|
+
if (!out.has(node.name)) out.set(node.name, {
|
|
488
|
+
selectors,
|
|
489
|
+
plural: selectors.some((key) => PLURAL_CATEGORIES.has(key)),
|
|
490
|
+
ordinal: node.ordinal === true
|
|
491
|
+
});
|
|
492
|
+
for (const [, body] of node.variants) walk(body, out);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function paramNames(message) {
|
|
496
|
+
const names = /* @__PURE__ */ new Set();
|
|
497
|
+
collect(parse$1(message), names);
|
|
498
|
+
return [...names].sort();
|
|
499
|
+
}
|
|
500
|
+
function collect(nodes, out) {
|
|
501
|
+
for (const node of nodes) {
|
|
502
|
+
if (node.kind !== "param") continue;
|
|
503
|
+
out.add(node.name);
|
|
504
|
+
if (node.variants) for (const [, body] of node.variants) collect(body, out);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
508
|
+
function markupNames(message) {
|
|
509
|
+
const out = /* @__PURE__ */ new Set();
|
|
510
|
+
for (const match of message.matchAll(TAG)) if (match[1] || match[3]) out.add(match[2]);
|
|
511
|
+
return out;
|
|
512
|
+
}
|
|
513
|
+
function tagTokens(message, markup) {
|
|
514
|
+
const out = [];
|
|
515
|
+
for (const match of message.matchAll(TAG)) if (markup.has(match[2])) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
516
|
+
return out.sort();
|
|
517
|
+
}
|
|
518
|
+
function surplus(a, b) {
|
|
519
|
+
const rest = [...b];
|
|
520
|
+
const out = [];
|
|
521
|
+
for (const item of a) {
|
|
522
|
+
const at = rest.indexOf(item);
|
|
523
|
+
if (at < 0) out.push(item);
|
|
524
|
+
else rest.splice(at, 1);
|
|
525
|
+
}
|
|
526
|
+
return out;
|
|
527
|
+
}
|
|
528
|
+
function list(items) {
|
|
529
|
+
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
530
|
+
}
|
|
531
|
+
function validateMessage(message, locale) {
|
|
532
|
+
const issues = [];
|
|
533
|
+
for (const [name, shape] of shapes(message)) {
|
|
534
|
+
if (!shape.plural) continue;
|
|
535
|
+
if (!shape.selectors.includes("other")) {
|
|
536
|
+
issues.push({
|
|
537
|
+
severity: "error",
|
|
538
|
+
message: `the plural block for {${name}} has no "other" case, so any count it does not list renders empty`
|
|
539
|
+
});
|
|
540
|
+
continue;
|
|
541
|
+
}
|
|
542
|
+
if (!locale) continue;
|
|
543
|
+
const missing = reachableCategories(locale, shape.ordinal).filter((category) => category !== "other" && !shape.selectors.includes(category));
|
|
544
|
+
if (missing.length > 0) issues.push({
|
|
545
|
+
severity: "warning",
|
|
546
|
+
message: `${locale} also needs the ${list(missing)} plural ${missing.length > 1 ? "forms" : "form"} for {${name}}`
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
return issues;
|
|
550
|
+
}
|
|
551
|
+
function validatePair(source, translated) {
|
|
552
|
+
const issues = [];
|
|
553
|
+
const lostParams = surplus(paramNames(source), paramNames(translated));
|
|
554
|
+
const newParams = surplus(paramNames(translated), paramNames(source));
|
|
555
|
+
if (lostParams.length > 0) issues.push({
|
|
556
|
+
severity: "error",
|
|
557
|
+
message: `${list(lostParams.map((name) => `{${name}}`))} ${lostParams.length > 1 ? "are" : "is"} missing, so ${lostParams.length > 1 ? "their values" : "its value"} never reaches the text`
|
|
558
|
+
});
|
|
559
|
+
if (newParams.length > 0) issues.push({
|
|
560
|
+
severity: "error",
|
|
561
|
+
message: `${list(newParams.map((name) => `{${name}}`))} ${newParams.length > 1 ? "are" : "is"} not in the source message, so ${newParams.length > 1 ? "they render" : "it renders"} as literal text`
|
|
562
|
+
});
|
|
563
|
+
const markup = /* @__PURE__ */ new Set([...markupNames(source), ...markupNames(translated)]);
|
|
564
|
+
const lostTags = tagReport(surplus(tagTokens(source, markup), tagTokens(translated, markup)));
|
|
565
|
+
const newTags = tagReport(surplus(tagTokens(translated, markup), tagTokens(source, markup)));
|
|
566
|
+
if (lostTags.length > 0) issues.push({
|
|
567
|
+
severity: "error",
|
|
568
|
+
message: `${list(lostTags)} ${lostTags.length > 1 ? "are" : "is"} missing from the translation`
|
|
569
|
+
});
|
|
570
|
+
if (newTags.length > 0) issues.push({
|
|
571
|
+
severity: "error",
|
|
572
|
+
message: `${list(newTags)} ${newTags.length > 1 ? "are" : "is"} not in the source message`
|
|
573
|
+
});
|
|
574
|
+
const target = shapes(translated);
|
|
575
|
+
for (const [name, shape] of shapes(source)) {
|
|
576
|
+
const other = target.get(name);
|
|
577
|
+
if (!other) {
|
|
578
|
+
issues.push({
|
|
579
|
+
severity: "error",
|
|
580
|
+
message: `the ${shape.plural ? "plural" : "select"} block for {${name}} was flattened into plain text`
|
|
581
|
+
});
|
|
582
|
+
continue;
|
|
583
|
+
}
|
|
584
|
+
if (shape.selectors.includes("other") && !other.selectors.includes("other") && !other.plural) issues.push({
|
|
585
|
+
severity: "error",
|
|
586
|
+
message: `the "other" case for {${name}} is missing, so unlisted values render empty`
|
|
587
|
+
});
|
|
588
|
+
const lostExact = shape.selectors.filter((key) => EXACT.test(key)).filter((key) => !other.selectors.includes(key));
|
|
589
|
+
if (lostExact.length > 0) issues.push({
|
|
590
|
+
severity: "warning",
|
|
591
|
+
message: `the ${list(lostExact)} ${lostExact.length > 1 ? "cases" : "case"} for {${name}} ${lostExact.length > 1 ? "are" : "is"} missing, so ${lostExact.length > 1 ? "those counts" : "that count"} falls back to "other"`
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
return issues;
|
|
595
|
+
}
|
|
596
|
+
function tagReport(tokens) {
|
|
597
|
+
const out = [];
|
|
598
|
+
const seen = /* @__PURE__ */ new Set();
|
|
599
|
+
for (const token of tokens) {
|
|
600
|
+
const name = token.replaceAll("/", "");
|
|
601
|
+
if (seen.has(name)) continue;
|
|
602
|
+
seen.add(name);
|
|
603
|
+
out.push(token.endsWith("/") ? `<${name}/>` : `<${name}>`);
|
|
604
|
+
}
|
|
605
|
+
return out;
|
|
606
|
+
}
|
|
607
|
+
const COUNT_PROBE = 200;
|
|
608
|
+
const reachable = /* @__PURE__ */ new Map();
|
|
609
|
+
function reachableCategories(locale, ordinal) {
|
|
610
|
+
const type = ordinal ? "ordinal" : "cardinal";
|
|
611
|
+
const cacheKey = `${locale}\u0000${type}`;
|
|
612
|
+
const hit = reachable.get(cacheKey);
|
|
613
|
+
if (hit) return hit;
|
|
614
|
+
let out;
|
|
615
|
+
try {
|
|
616
|
+
const rules = new Intl.PluralRules(locale, { type });
|
|
617
|
+
const seen = /* @__PURE__ */ new Set();
|
|
618
|
+
for (let n = 0; n <= COUNT_PROBE; n++) seen.add(rules.select(n));
|
|
619
|
+
out = [...seen];
|
|
620
|
+
} catch {
|
|
621
|
+
out = [];
|
|
622
|
+
}
|
|
623
|
+
reachable.set(cacheKey, out);
|
|
624
|
+
return out;
|
|
625
|
+
}
|
|
626
|
+
//#endregion
|
|
469
627
|
//#region src/check.ts
|
|
628
|
+
function gatePasses(result) {
|
|
629
|
+
return result.missing.length === 0 && result.unknown.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
630
|
+
}
|
|
470
631
|
function check(cfg, catalogs, registry) {
|
|
471
|
-
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
472
632
|
const extracted = registry.messages();
|
|
633
|
+
const flat = {};
|
|
634
|
+
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
635
|
+
const source = flat[cfg.sourceLocale] ?? {};
|
|
473
636
|
const unknown = [];
|
|
474
|
-
for (const [key, files] of registry.usedKeys()) if (!(extracted.has(key) || cfg.locales.some((locale) =>
|
|
637
|
+
for (const [key, files] of registry.usedKeys()) if (!(extracted.has(key) || cfg.locales.some((locale) => flat[locale]?.[key] !== void 0))) unknown.push({
|
|
475
638
|
key,
|
|
476
639
|
files
|
|
477
640
|
});
|
|
@@ -484,16 +647,42 @@ function check(cfg, catalogs, registry) {
|
|
|
484
647
|
});
|
|
485
648
|
for (const locale of cfg.locales) {
|
|
486
649
|
if (locale === cfg.sourceLocale) continue;
|
|
487
|
-
for (const key of needed) if (!
|
|
650
|
+
for (const key of needed) if (!flat[locale]?.[key]) missing.push({
|
|
488
651
|
locale,
|
|
489
652
|
key,
|
|
490
653
|
source: source[key] ?? extracted.get(key)?.message
|
|
491
654
|
});
|
|
492
655
|
}
|
|
656
|
+
const broken = [];
|
|
657
|
+
const add = (locale, key, issues) => {
|
|
658
|
+
for (const issue of issues) broken.push({
|
|
659
|
+
locale,
|
|
660
|
+
key,
|
|
661
|
+
severity: issue.severity,
|
|
662
|
+
issue: issue.message
|
|
663
|
+
});
|
|
664
|
+
};
|
|
665
|
+
const sourceText = { ...source };
|
|
666
|
+
for (const [key, entry] of extracted) if (!sourceText[key]) sourceText[key] = entry.message;
|
|
667
|
+
for (const [key, text] of Object.entries(sourceText)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
668
|
+
for (const locale of cfg.locales) {
|
|
669
|
+
if (locale === cfg.sourceLocale) continue;
|
|
670
|
+
for (const [key, translated] of Object.entries(flat[locale] ?? {})) {
|
|
671
|
+
if (!translated) continue;
|
|
672
|
+
add(locale, key, validateMessage(translated, locale));
|
|
673
|
+
const text = sourceText[key];
|
|
674
|
+
if (text) add(locale, key, validatePair(text, translated));
|
|
675
|
+
}
|
|
676
|
+
}
|
|
493
677
|
return {
|
|
494
|
-
ok:
|
|
678
|
+
ok: gatePasses({
|
|
679
|
+
missing,
|
|
680
|
+
unknown,
|
|
681
|
+
broken
|
|
682
|
+
}),
|
|
495
683
|
missing,
|
|
496
|
-
unknown
|
|
684
|
+
unknown,
|
|
685
|
+
broken
|
|
497
686
|
};
|
|
498
687
|
}
|
|
499
688
|
function formatCheckResult(result) {
|
|
@@ -509,8 +698,23 @@ function formatCheckResult(result) {
|
|
|
509
698
|
lines.push("unknown keys (not in any catalog):");
|
|
510
699
|
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.join(", ")})`);
|
|
511
700
|
}
|
|
701
|
+
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
702
|
+
if (errors.length > 0) {
|
|
703
|
+
lines.push("broken translations:");
|
|
704
|
+
for (const entry of errors) lines.push(` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
705
|
+
}
|
|
512
706
|
return lines.join("\n");
|
|
513
707
|
}
|
|
708
|
+
function checkNextSteps(result) {
|
|
709
|
+
const steps = [];
|
|
710
|
+
if (result.missing.length > 0) steps.push("missing: run `npx verbaly extract` to scaffold the keys, then fill them (or run `npx verbaly translate`)");
|
|
711
|
+
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`");
|
|
712
|
+
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");
|
|
713
|
+
return steps.join("\n");
|
|
714
|
+
}
|
|
715
|
+
function formatCheckWarnings(result) {
|
|
716
|
+
return result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`).join("\n");
|
|
717
|
+
}
|
|
514
718
|
function truncate(text, max) {
|
|
515
719
|
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
516
720
|
}
|
|
@@ -551,6 +755,19 @@ function githubCheckAnnotations(result, registry, root) {
|
|
|
551
755
|
const file = entry.files[0];
|
|
552
756
|
lines.push(file ? `::error file=${escapeProperty(relative(root, file).replaceAll("\\", "/"))}::${text}` : `::error::${text}`);
|
|
553
757
|
}
|
|
758
|
+
for (const entry of result.broken) {
|
|
759
|
+
const origin = messages.get(entry.key);
|
|
760
|
+
const text = escapeData(`[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
761
|
+
const command = entry.severity === "error" ? "error" : "warning";
|
|
762
|
+
if (!origin) {
|
|
763
|
+
lines.push(`::${command}::${text}`);
|
|
764
|
+
continue;
|
|
765
|
+
}
|
|
766
|
+
const file = relative(root, origin.file).replaceAll("\\", "/");
|
|
767
|
+
const content = readSource(origin.file);
|
|
768
|
+
const line = content === void 0 ? void 0 : lineAt(content, origin.start);
|
|
769
|
+
lines.push(`::${command} file=${escapeProperty(file)}${line ? `,line=${line}` : ""}::${text}`);
|
|
770
|
+
}
|
|
554
771
|
return lines;
|
|
555
772
|
}
|
|
556
773
|
function lineAt(content, offset) {
|
|
@@ -567,13 +784,6 @@ function escapeProperty(text) {
|
|
|
567
784
|
}
|
|
568
785
|
//#endregion
|
|
569
786
|
//#region src/params.ts
|
|
570
|
-
const PLURAL_KEYS = /* @__PURE__ */ new Set([
|
|
571
|
-
"zero",
|
|
572
|
-
"one",
|
|
573
|
-
"two",
|
|
574
|
-
"few",
|
|
575
|
-
"many"
|
|
576
|
-
]);
|
|
577
787
|
const NUMBER_FORMATS = /* @__PURE__ */ new Set([
|
|
578
788
|
"number",
|
|
579
789
|
"integer",
|
|
@@ -593,7 +803,7 @@ function visit(nodes, out) {
|
|
|
593
803
|
out.set(node.name, types);
|
|
594
804
|
if (node.variants) {
|
|
595
805
|
for (const [key, body] of node.variants) {
|
|
596
|
-
if (
|
|
806
|
+
if (PLURAL_CATEGORIES.has(key) || /^=\d+$/.test(key)) types.add("number");
|
|
597
807
|
else if (key !== "other") types.add("string");
|
|
598
808
|
visit(body, out);
|
|
599
809
|
}
|
|
@@ -991,9 +1201,13 @@ function transformSource(code, id, registry) {
|
|
|
991
1201
|
};
|
|
992
1202
|
}
|
|
993
1203
|
function runBuildGate(cfg, registry, failOnMissing) {
|
|
994
|
-
|
|
995
|
-
const result =
|
|
996
|
-
|
|
1204
|
+
const found = check(cfg, loadCatalogs(cfg), registry);
|
|
1205
|
+
const result = failOnMissing === false ? {
|
|
1206
|
+
...found,
|
|
1207
|
+
missing: []
|
|
1208
|
+
} : found;
|
|
1209
|
+
if (gatePasses(result)) return;
|
|
1210
|
+
throw new Error(`[verbaly] build blocked\n${formatCheckResult(result)}\n${checkNextSteps(result)}`);
|
|
997
1211
|
}
|
|
998
1212
|
//#endregion
|
|
999
1213
|
//#region src/registry.ts
|
|
@@ -1087,28 +1301,80 @@ function pruneCatalogs(cfg, catalogs, registry) {
|
|
|
1087
1301
|
}
|
|
1088
1302
|
//#endregion
|
|
1089
1303
|
//#region src/init.ts
|
|
1090
|
-
const
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1304
|
+
const HOSTS = [
|
|
1305
|
+
{
|
|
1306
|
+
name: "nuxt",
|
|
1307
|
+
dep: "nuxt",
|
|
1308
|
+
pkg: "@verbaly/nuxt",
|
|
1309
|
+
wire: "add '@verbaly/nuxt' to the modules in nuxt.config"
|
|
1310
|
+
},
|
|
1311
|
+
{
|
|
1312
|
+
name: "next",
|
|
1313
|
+
dep: "next",
|
|
1314
|
+
pkg: "@verbaly/next",
|
|
1315
|
+
wire: "wrap the export of next.config with withVerbaly"
|
|
1316
|
+
},
|
|
1317
|
+
{
|
|
1318
|
+
name: "sveltekit",
|
|
1319
|
+
dep: "@sveltejs/kit",
|
|
1320
|
+
pkg: "@verbaly/vite",
|
|
1321
|
+
wire: "add verbaly() to the plugins in vite.config (per-request locale: @verbaly/sveltekit)"
|
|
1322
|
+
},
|
|
1323
|
+
{
|
|
1324
|
+
name: "astro",
|
|
1325
|
+
dep: "astro",
|
|
1326
|
+
pkg: "@verbaly/astro",
|
|
1327
|
+
wire: "add verbaly() to the integrations in astro.config"
|
|
1328
|
+
},
|
|
1329
|
+
{
|
|
1330
|
+
name: "vite",
|
|
1331
|
+
dep: "vite",
|
|
1332
|
+
pkg: "@verbaly/vite",
|
|
1333
|
+
wire: "add verbaly() to the plugins in vite.config"
|
|
1334
|
+
},
|
|
1335
|
+
{
|
|
1336
|
+
name: "webpack",
|
|
1337
|
+
dep: "webpack",
|
|
1338
|
+
pkg: "@verbaly/unplugin",
|
|
1339
|
+
wire: "add the verbaly webpack plugin to your build config"
|
|
1340
|
+
},
|
|
1341
|
+
{
|
|
1342
|
+
name: "rspack",
|
|
1343
|
+
dep: "@rspack/core",
|
|
1344
|
+
pkg: "@verbaly/unplugin",
|
|
1345
|
+
wire: "add the verbaly rspack plugin to your build config"
|
|
1346
|
+
},
|
|
1347
|
+
{
|
|
1348
|
+
name: "rollup",
|
|
1349
|
+
dep: "rollup",
|
|
1350
|
+
pkg: "@verbaly/unplugin",
|
|
1351
|
+
wire: "add the verbaly rollup plugin to your build config"
|
|
1352
|
+
},
|
|
1353
|
+
{
|
|
1354
|
+
name: "esbuild",
|
|
1355
|
+
dep: "esbuild",
|
|
1356
|
+
pkg: "@verbaly/unplugin",
|
|
1357
|
+
wire: "add the verbaly esbuild plugin to your build config"
|
|
1358
|
+
}
|
|
1097
1359
|
];
|
|
1098
|
-
|
|
1360
|
+
const WIRING_PACKAGES = [...new Set(HOSTS.map((host) => host.pkg))];
|
|
1361
|
+
function readDependencies(root) {
|
|
1099
1362
|
const path = join(root, "package.json");
|
|
1100
|
-
if (!existsSync(path)) return
|
|
1363
|
+
if (!existsSync(path)) return {};
|
|
1101
1364
|
try {
|
|
1102
1365
|
const pkg = JSON.parse(readFileSync(path, "utf8"));
|
|
1103
|
-
|
|
1366
|
+
return {
|
|
1104
1367
|
...pkg.dependencies,
|
|
1105
1368
|
...pkg.devDependencies
|
|
1106
1369
|
};
|
|
1107
|
-
return BUNDLERS.find((name) => deps[name]);
|
|
1108
1370
|
} catch {
|
|
1109
|
-
return;
|
|
1371
|
+
return {};
|
|
1110
1372
|
}
|
|
1111
1373
|
}
|
|
1374
|
+
function detectHost(root) {
|
|
1375
|
+
const deps = readDependencies(root);
|
|
1376
|
+
return HOSTS.find((host) => deps[host.dep]);
|
|
1377
|
+
}
|
|
1112
1378
|
function configSource(options, typescript) {
|
|
1113
1379
|
const fields = [` sourceLocale: '${options.sourceLocale ?? "en"}',`];
|
|
1114
1380
|
if (options.locales?.length) fields.push(` locales: [${options.locales.map((l) => `'${l}'`).join(", ")}],`);
|
|
@@ -1140,16 +1406,12 @@ function init(options = {}) {
|
|
|
1140
1406
|
created.push(label);
|
|
1141
1407
|
}
|
|
1142
1408
|
}
|
|
1143
|
-
const
|
|
1144
|
-
const next = [];
|
|
1145
|
-
if (bundler === "astro") next.push("install the integration: pnpm add -D @verbaly/astro", "add verbaly() to the integrations in astro.config");
|
|
1146
|
-
else if (bundler === "vite") next.push("install the plugin: pnpm add -D @verbaly/vite", "add verbaly() to the plugins in vite.config");
|
|
1147
|
-
else if (bundler) next.push("install the plugin: pnpm add -D @verbaly/unplugin", `add the verbaly ${bundler} plugin to your build config`);
|
|
1148
|
-
else next.push("run \"verbaly extract\" after writing your first t`…` message");
|
|
1409
|
+
const host = detectHost(root);
|
|
1410
|
+
const next = host ? [`pnpm add -D ${host.pkg}`, host.wire] : ["run \"verbaly extract\" after writing your first t`…` message"];
|
|
1149
1411
|
return {
|
|
1150
1412
|
created,
|
|
1151
1413
|
skipped,
|
|
1152
|
-
|
|
1414
|
+
host: host?.name,
|
|
1153
1415
|
configFile,
|
|
1154
1416
|
next
|
|
1155
1417
|
};
|
|
@@ -1195,11 +1457,11 @@ async function doctor(cfg) {
|
|
|
1195
1457
|
}
|
|
1196
1458
|
try {
|
|
1197
1459
|
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
1198
|
-
const bad =
|
|
1460
|
+
const bad = badLeaf(parsed);
|
|
1199
1461
|
if (bad) {
|
|
1200
1462
|
catalogsHealthy = false;
|
|
1201
|
-
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad
|
|
1202
|
-
} else catalogs[locale] = parsed;
|
|
1463
|
+
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
1464
|
+
} else catalogs[locale] = flatten(parsed);
|
|
1203
1465
|
} catch {
|
|
1204
1466
|
catalogsHealthy = false;
|
|
1205
1467
|
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
@@ -1209,21 +1471,22 @@ async function doctor(cfg) {
|
|
|
1209
1471
|
}
|
|
1210
1472
|
const source = catalogs[cfg.sourceLocale];
|
|
1211
1473
|
if (source && Object.keys(source).length === 0) warn("source", `source catalog ${cfg.sourceLocale}.json is empty`, "write your first t`…` message and run `npx verbaly extract`");
|
|
1212
|
-
const deps =
|
|
1213
|
-
const
|
|
1214
|
-
const
|
|
1215
|
-
if (!
|
|
1216
|
-
else if (
|
|
1217
|
-
else
|
|
1218
|
-
|
|
1219
|
-
if (source
|
|
1474
|
+
const deps = readDependencies(cfg.root);
|
|
1475
|
+
const host = detectHost(cfg.root);
|
|
1476
|
+
const installed = WIRING_PACKAGES.find((pkg) => deps[pkg]);
|
|
1477
|
+
if (!host) ok("plugin", "no bundler detected, the CLI flow (extract/check) applies");
|
|
1478
|
+
else if (installed) ok("plugin", `${installed} installed for ${host.name}`);
|
|
1479
|
+
else warn("plugin", `${host.name} detected but ${host.pkg} is not installed`, `pnpm add -D ${host.pkg} and ${host.wire}`);
|
|
1480
|
+
const scanning = cfg.include.length > 0;
|
|
1481
|
+
if (!scanning) ok("sources", "source scanning is off (include: []), the catalogs are the source of truth");
|
|
1482
|
+
if (source && scanning && cfg.dts !== false) {
|
|
1220
1483
|
const dtsPath = cfg.dts ?? join(cfg.root, "verbaly.d.ts");
|
|
1221
1484
|
if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
|
|
1222
1485
|
else if (readFileSync(dtsPath, "utf8") !== generateDts(source)) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
|
|
1223
1486
|
else ok("types", "verbaly.d.ts is up to date");
|
|
1224
1487
|
}
|
|
1225
1488
|
const registry = await extractProject(cfg);
|
|
1226
|
-
if (source) {
|
|
1489
|
+
if (source && scanning) {
|
|
1227
1490
|
const extracted = registry.messages();
|
|
1228
1491
|
const used = registry.usedKeys();
|
|
1229
1492
|
const orphans = Object.keys(source).filter((key) => !extracted.has(key) && !used.has(key));
|
|
@@ -1237,6 +1500,13 @@ async function doctor(cfg) {
|
|
|
1237
1500
|
const locales = [...new Set(result.missing.map((m) => m.locale))];
|
|
1238
1501
|
warn("translations", `${result.missing.length} missing ${result.missing.length === 1 ? "translation" : "translations"} (${locales.join(", ")})`, "run `npx verbaly translate` or fill the catalogs (`npx verbaly check` for details)");
|
|
1239
1502
|
}
|
|
1503
|
+
const broken = result.broken.filter((entry) => entry.severity === "error");
|
|
1504
|
+
if (broken.length > 0) {
|
|
1505
|
+
const locales = [...new Set(broken.map((b) => b.locale))];
|
|
1506
|
+
error("translations", `${broken.length} broken ${broken.length === 1 ? "translation" : "translations"} (${locales.join(", ")}): present but not rendering what the source renders`, "run `npx verbaly check` to read what each one lost");
|
|
1507
|
+
}
|
|
1508
|
+
const warnings = result.broken.filter((entry) => entry.severity === "warning");
|
|
1509
|
+
if (warnings.length > 0) warn("translations", `${warnings.length} structural ${warnings.length === 1 ? "warning" : "warnings"} (plural forms a language asks for)`, "run `npx verbaly check` to read them, they never fail the build");
|
|
1240
1510
|
if (result.ok) ok("translations", "all translations complete");
|
|
1241
1511
|
}
|
|
1242
1512
|
return {
|
|
@@ -1248,15 +1518,13 @@ function preview(keys) {
|
|
|
1248
1518
|
const head = keys.slice(0, PREVIEW).join(", ");
|
|
1249
1519
|
return keys.length > PREVIEW ? `${head}, …` : head;
|
|
1250
1520
|
}
|
|
1251
|
-
function
|
|
1252
|
-
|
|
1253
|
-
const
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
} catch {
|
|
1259
|
-
return {};
|
|
1521
|
+
function badLeaf(tree, prefix = "") {
|
|
1522
|
+
for (const [key, value] of Object.entries(tree)) {
|
|
1523
|
+
const path = prefix ? `${prefix}.${key}` : key;
|
|
1524
|
+
if (typeof value === "string") continue;
|
|
1525
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) return path;
|
|
1526
|
+
const bad = badLeaf(value, path);
|
|
1527
|
+
if (bad) return bad;
|
|
1260
1528
|
}
|
|
1261
1529
|
}
|
|
1262
1530
|
//#endregion
|
|
@@ -1712,24 +1980,12 @@ async function resolveProvider(cfg, model) {
|
|
|
1712
1980
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1713
1981
|
}
|
|
1714
1982
|
function structureMatches(source, translated) {
|
|
1715
|
-
return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
|
|
1716
|
-
}
|
|
1717
|
-
function paramNames(message) {
|
|
1718
1983
|
try {
|
|
1719
|
-
return [...
|
|
1984
|
+
return ![...validateMessage(translated), ...validatePair(source, translated)].some((issue) => issue.severity === "error");
|
|
1720
1985
|
} catch {
|
|
1721
|
-
return
|
|
1986
|
+
return false;
|
|
1722
1987
|
}
|
|
1723
1988
|
}
|
|
1724
|
-
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
1725
|
-
function tagTokens(message) {
|
|
1726
|
-
const out = [];
|
|
1727
|
-
for (const match of message.matchAll(TAG)) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
1728
|
-
return out.sort();
|
|
1729
|
-
}
|
|
1730
|
-
function sameMembers(a, b) {
|
|
1731
|
-
return a.length === b.length && a.every((value, i) => value === b[i]);
|
|
1732
|
-
}
|
|
1733
1989
|
//#endregion
|
|
1734
1990
|
//#region src/exchange.ts
|
|
1735
1991
|
function isMobileFormat(format) {
|
|
@@ -2272,19 +2528,30 @@ function decodeEntities(text) {
|
|
|
2272
2528
|
//#endregion
|
|
2273
2529
|
//#region src/status.ts
|
|
2274
2530
|
function status(cfg, catalogs, registry, drafts = {}) {
|
|
2275
|
-
const
|
|
2276
|
-
const
|
|
2277
|
-
const
|
|
2531
|
+
const extracted = registry.messages();
|
|
2532
|
+
const flat = {};
|
|
2533
|
+
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
2534
|
+
const source = flat[cfg.sourceLocale] ?? {};
|
|
2535
|
+
const needed = /* @__PURE__ */ new Set([...extracted.keys(), ...Object.keys(source)]);
|
|
2536
|
+
const live = effectiveDrafts(drafts, flat);
|
|
2278
2537
|
const locales = [];
|
|
2279
2538
|
for (const locale of cfg.locales) {
|
|
2280
2539
|
if (locale === cfg.sourceLocale) continue;
|
|
2540
|
+
const catalog = flat[locale] ?? {};
|
|
2281
2541
|
let translated = 0;
|
|
2282
|
-
for (const key of needed) if (
|
|
2542
|
+
for (const key of needed) if (catalog[key]) translated += 1;
|
|
2543
|
+
let broken = 0;
|
|
2544
|
+
for (const [key, text] of Object.entries(catalog)) {
|
|
2545
|
+
if (!text) continue;
|
|
2546
|
+
const from = source[key] ?? extracted.get(key)?.message;
|
|
2547
|
+
if ([...validateMessage(text, locale), ...from ? validatePair(from, text) : []].some((issue) => issue.severity === "error")) broken += 1;
|
|
2548
|
+
}
|
|
2283
2549
|
locales.push({
|
|
2284
2550
|
locale,
|
|
2285
2551
|
translated,
|
|
2286
2552
|
total: needed.size,
|
|
2287
|
-
drafts: live[locale]?.length ?? 0
|
|
2553
|
+
drafts: live[locale]?.length ?? 0,
|
|
2554
|
+
broken
|
|
2288
2555
|
});
|
|
2289
2556
|
}
|
|
2290
2557
|
return {
|
|
@@ -2299,11 +2566,12 @@ function formatStatusResult(result) {
|
|
|
2299
2566
|
lines.push(" no target locales (add locales to your config)");
|
|
2300
2567
|
return lines.join("\n");
|
|
2301
2568
|
}
|
|
2302
|
-
for (const { locale, translated, total, drafts } of result.locales) {
|
|
2569
|
+
for (const { locale, translated, total, drafts, broken } of result.locales) {
|
|
2303
2570
|
const pct = total === 0 ? 100 : Math.floor(translated / total * 100);
|
|
2304
|
-
const mark = translated === total ? " ✓" : "";
|
|
2305
|
-
const
|
|
2306
|
-
|
|
2571
|
+
const mark = translated === total && broken === 0 ? " ✓" : "";
|
|
2572
|
+
const notes = [drafts > 0 ? `${drafts} unreviewed` : "", broken > 0 ? `${broken} broken` : ""].filter(Boolean);
|
|
2573
|
+
const note = notes.length > 0 ? `, ${notes.join(", ")}` : "";
|
|
2574
|
+
lines.push(` ${locale}: ${translated}/${total} translated (${pct}%${note})${mark}`);
|
|
2307
2575
|
}
|
|
2308
2576
|
return lines.join("\n");
|
|
2309
2577
|
}
|
|
@@ -2402,7 +2670,7 @@ function wrapCode(code, file) {
|
|
|
2402
2670
|
const s = new MagicString(code);
|
|
2403
2671
|
const visited = /* @__PURE__ */ new Set();
|
|
2404
2672
|
let changed = false;
|
|
2405
|
-
walk(ast, (node) => {
|
|
2673
|
+
walk$1(ast, (node) => {
|
|
2406
2674
|
if (node.type !== "JSXElement" && node.type !== "JSXFragment") return;
|
|
2407
2675
|
if (visited.has(node)) return;
|
|
2408
2676
|
processElement(node);
|
|
@@ -2521,7 +2789,7 @@ function wrapCode(code, file) {
|
|
|
2521
2789
|
return text.length > 60 ? text.slice(0, 59) + "…" : text;
|
|
2522
2790
|
}
|
|
2523
2791
|
function markSubtree(node) {
|
|
2524
|
-
walk(node, (n) => {
|
|
2792
|
+
walk$1(node, (n) => {
|
|
2525
2793
|
if (n.type === "JSXElement" || n.type === "JSXFragment") visited.add(n);
|
|
2526
2794
|
});
|
|
2527
2795
|
}
|
|
@@ -2535,7 +2803,7 @@ function hasVerbalyAttr(opening) {
|
|
|
2535
2803
|
}
|
|
2536
2804
|
function usesT(node) {
|
|
2537
2805
|
let found = false;
|
|
2538
|
-
walk(node, (n) => {
|
|
2806
|
+
walk$1(node, (n) => {
|
|
2539
2807
|
if (n.type === "TaggedTemplateExpression" && isTReference(n.tag, T_NAMES)) found = true;
|
|
2540
2808
|
if (n.type === "CallExpression" && isTReference(n.callee, T_NAMES)) found = true;
|
|
2541
2809
|
});
|
|
@@ -2543,7 +2811,7 @@ function usesT(node) {
|
|
|
2543
2811
|
}
|
|
2544
2812
|
function containsJsx(node) {
|
|
2545
2813
|
let found = false;
|
|
2546
|
-
walk(node, (n) => {
|
|
2814
|
+
walk$1(node, (n) => {
|
|
2547
2815
|
if (n.type === "JSXElement" || n.type === "JSXFragment") found = true;
|
|
2548
2816
|
});
|
|
2549
2817
|
return found;
|
|
@@ -2561,6 +2829,6 @@ function line(node) {
|
|
|
2561
2829
|
return node.loc?.start?.line ?? 1;
|
|
2562
2830
|
}
|
|
2563
2831
|
//#endregion
|
|
2564
|
-
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, PSEUDO_LOCALE, RESOLVED_VIRTUAL_ID, SFC_FILE_RE, SOURCE_FILE_RE, VIRTUAL_ID, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter,
|
|
2832
|
+
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, PLURAL_CATEGORIES, PSEUDO_LOCALE, RESOLVED_VIRTUAL_ID, SFC_FILE_RE, SOURCE_FILE_RE, VIRTUAL_ID, WIRING_PACKAGES, analyze, analyzeFile, analyzeSfc, catalogPath, check, checkNextSteps, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter, detectHost, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatCheckWarnings, formatStatusResult, gatePasses, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, readDependencies, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, validateMessage, validatePair, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
2565
2833
|
|
|
2566
2834
|
//# sourceMappingURL=index.js.map
|