@verbaly/compiler 0.72.0 → 0.74.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 +2 -0
- package/dist/cli.js +112 -14
- package/dist/cli.js.map +1 -1
- package/dist/index.js +113 -15
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -122,6 +122,8 @@ t(titles[slug]);
|
|
|
122
122
|
|
|
123
123
|
**A translation knows which source text it was written for.** For readable keys, the second and third rows, Verbaly keeps a short fingerprint of the source text and of the translation in `verbaly.lock`, next to your config. It is taken the moment the translation is written, by `translate`, `import`, Studio or an agent, so a source that changes right after is still caught; `import` takes it from the source text the translator's file carries, so a file made for an older text reads as outdated at once. When the source text changes and the translation does not, the translation is **outdated**: `status`, `check` and `doctor` say so, `review` lists it with both texts, and `review --approve` keeps it if it still holds. Editing the translation clears it on its own. Hash keys never need this, because a new text is a new key.
|
|
124
124
|
|
|
125
|
+
**Plurals, selects and formats go in the template, with the value in `${…}`.** `` t`You have {${count} | one: # message | other: # messages}` `` and `` t`Total: {${total}:currency/EUR}` `` extract as the same blocks a catalog holds, and the call becomes `t('<key>', { count })`. Inside a block the catalog syntax holds: `#` is the number, and `||`, `##` and `{{` are escapes. A block written around a bare name (`{count | …}`), one that never closes, and ICU written in the template stay text, and `extract` shows how to write each.
|
|
126
|
+
|
|
125
127
|
Four details that catch people out:
|
|
126
128
|
|
|
127
129
|
- **Keep the files that write `t` inside `include`.** The default reads `src/` and `app/`. A file outside them, like `components/` in a Next app without `src/`, is never extracted, so its text shows in the source language in every language. `@verbaly/next`, the Vite plugin and unplugin say so once when they start, with the pattern to add, and `doctor` lists every such file.
|
package/dist/cli.js
CHANGED
|
@@ -643,13 +643,19 @@ function isTReference(node, names) {
|
|
|
643
643
|
}
|
|
644
644
|
return false;
|
|
645
645
|
}
|
|
646
|
+
const BLOCK_TAIL = /^(?:\s*\|\s*[^\s|{}:]+\s*:|:[A-Za-z_][\w-]*)/;
|
|
646
647
|
function buildMessage(code, quasi, names) {
|
|
647
648
|
const quasis = quasi.quasis;
|
|
648
649
|
const expressions = quasi.expressions;
|
|
650
|
+
const texts = quasis.map(cookedValue);
|
|
649
651
|
const params = [];
|
|
650
652
|
const taken = /* @__PURE__ */ new Map();
|
|
651
|
-
|
|
652
|
-
for (let i = 0; i <
|
|
653
|
+
const tokens = [];
|
|
654
|
+
for (let i = 0; i < texts.length; i++) {
|
|
655
|
+
const chunk = texts[i];
|
|
656
|
+
const head = i < expressions.length && chunk.endsWith("{") && BLOCK_TAIL.test(texts[i + 1]);
|
|
657
|
+
for (const char of head ? chunk.slice(0, -1) : chunk) tokens.push(char);
|
|
658
|
+
if (i === expressions.length) break;
|
|
653
659
|
const expr = expressions[i];
|
|
654
660
|
if (!expr) return void 0;
|
|
655
661
|
if (containsTaggedT(expr, names)) return void 0;
|
|
@@ -660,13 +666,50 @@ function buildMessage(code, quasi, names) {
|
|
|
660
666
|
start: expr.start,
|
|
661
667
|
end: expr.end
|
|
662
668
|
});
|
|
663
|
-
|
|
669
|
+
tokens.push({
|
|
670
|
+
name,
|
|
671
|
+
head
|
|
672
|
+
});
|
|
673
|
+
}
|
|
674
|
+
let text = "";
|
|
675
|
+
for (let k = 0; k < tokens.length; k++) {
|
|
676
|
+
const token = tokens[k];
|
|
677
|
+
if (typeof token === "string") text += token === "{" || token === "}" ? token + token : token;
|
|
678
|
+
else if (!token.head) text += `{${token.name}}`;
|
|
679
|
+
else {
|
|
680
|
+
const end = blockEnd(tokens, k, false) ?? blockEnd(tokens, k, true);
|
|
681
|
+
if (end === void 0) return {
|
|
682
|
+
text: texts.map((chunk, i) => escapeText(chunk) + (params[i] ? `{${params[i].name}}` : "")).join(""),
|
|
683
|
+
params
|
|
684
|
+
};
|
|
685
|
+
for (let m = k; m <= end; m++) text += tokenText(tokens[m]);
|
|
686
|
+
k = end;
|
|
687
|
+
}
|
|
664
688
|
}
|
|
665
689
|
return {
|
|
666
690
|
text,
|
|
667
691
|
params
|
|
668
692
|
};
|
|
669
693
|
}
|
|
694
|
+
function tokenText(token) {
|
|
695
|
+
if (typeof token === "string") return token;
|
|
696
|
+
return token.head ? `{${token.name}` : `{${token.name}}`;
|
|
697
|
+
}
|
|
698
|
+
function blockEnd(tokens, start, pairs) {
|
|
699
|
+
let depth = 0;
|
|
700
|
+
for (let k = start; k < tokens.length; k++) {
|
|
701
|
+
const token = tokens[k];
|
|
702
|
+
if (typeof token !== "string") {
|
|
703
|
+
if (token.head) depth += 1;
|
|
704
|
+
} else if (token === "{" || token === "}") {
|
|
705
|
+
if (tokens[k + 1] === token && (token === "{" || !(pairs && depth > 1))) k += 1;
|
|
706
|
+
else {
|
|
707
|
+
depth += token === "{" ? 1 : -1;
|
|
708
|
+
if (depth === 0) return k;
|
|
709
|
+
}
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
}
|
|
670
713
|
function cookedValue(element) {
|
|
671
714
|
if (!element) return "";
|
|
672
715
|
const value = element.value;
|
|
@@ -989,7 +1032,26 @@ function byCodeUnit(a, b) {
|
|
|
989
1032
|
}
|
|
990
1033
|
//#endregion
|
|
991
1034
|
//#region src/validate.ts
|
|
992
|
-
|
|
1035
|
+
let remembered = /* @__PURE__ */ new Map();
|
|
1036
|
+
let reading;
|
|
1037
|
+
function read(message) {
|
|
1038
|
+
if (!reading) return parse(message, parseIcu);
|
|
1039
|
+
let tree = reading.get(message);
|
|
1040
|
+
if (!tree) {
|
|
1041
|
+
tree = remembered.get(message) ?? parse(message, parseIcu);
|
|
1042
|
+
reading.set(message, tree);
|
|
1043
|
+
}
|
|
1044
|
+
return tree;
|
|
1045
|
+
}
|
|
1046
|
+
function gatePass(run) {
|
|
1047
|
+
reading = /* @__PURE__ */ new Map();
|
|
1048
|
+
try {
|
|
1049
|
+
return run();
|
|
1050
|
+
} finally {
|
|
1051
|
+
remembered = reading;
|
|
1052
|
+
reading = void 0;
|
|
1053
|
+
}
|
|
1054
|
+
}
|
|
993
1055
|
const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
|
|
994
1056
|
"zero",
|
|
995
1057
|
"one",
|
|
@@ -1056,10 +1118,35 @@ function surplus(a, b) {
|
|
|
1056
1118
|
function list(items) {
|
|
1057
1119
|
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
1058
1120
|
}
|
|
1059
|
-
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*[\w.]
|
|
1121
|
+
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*([\w.]+)(\s*[|:][^{}]*)\}\}/;
|
|
1122
|
+
const ESCAPED_ICU = /(?<!\{)\{\{\s*([\w.]+)\s*,\s*(plural|selectordinal|select|number|date|time)\b/;
|
|
1123
|
+
const ICU_SLICE = /^\{\{([\w.]+), (\w+), …\}\}$/;
|
|
1124
|
+
const ESCAPED_OPEN = /(?<!\{)\{\{\{([\w.]+)\}(\s*\|\s*[^\s|{}:]+\s*:|:[A-Za-z_][\w-]*)/;
|
|
1060
1125
|
function escapedSyntax(message) {
|
|
1126
|
+
const icu = ESCAPED_ICU.exec(message);
|
|
1127
|
+
if (icu) return `{{${icu[1]}, ${icu[2]}, …}}`;
|
|
1128
|
+
const open = ESCAPED_OPEN.exec(message);
|
|
1129
|
+
if (open) return `{\${${open[1]}}${open[2]}…`;
|
|
1061
1130
|
return ESCAPED_SYNTAX.exec(message)?.[0];
|
|
1062
1131
|
}
|
|
1132
|
+
function shownSlice(slice) {
|
|
1133
|
+
return slice.startsWith("{{") ? slice.slice(1, -1) : slice;
|
|
1134
|
+
}
|
|
1135
|
+
function inlineBlock(slice) {
|
|
1136
|
+
if (slice.startsWith("{${")) return "the same block with its closing }";
|
|
1137
|
+
const icu = ICU_SLICE.exec(slice);
|
|
1138
|
+
if (icu) {
|
|
1139
|
+
const [, name = "", type = ""] = icu;
|
|
1140
|
+
if (type === "plural") return `{\${${name}} | one: … | other: …}`;
|
|
1141
|
+
if (type === "select") return `{\${${name}} | …: … | other: …}`;
|
|
1142
|
+
if (type === "selectordinal") return "t(key, params), with the ICU message in the catalog";
|
|
1143
|
+
return `{\${${name}}:${type}}`;
|
|
1144
|
+
}
|
|
1145
|
+
const match = ESCAPED_SYNTAX.exec(slice);
|
|
1146
|
+
if (!match) return slice;
|
|
1147
|
+
const [, name = "", tail = ""] = match;
|
|
1148
|
+
return `{\${${name}}${tail.trimStart().startsWith(":") ? `:${tail.trimStart().slice(1).trimStart()}` : tail}}`;
|
|
1149
|
+
}
|
|
1063
1150
|
function validateMessage(message, locale) {
|
|
1064
1151
|
const issues = [];
|
|
1065
1152
|
for (const [name, shape] of shapes(message)) {
|
|
@@ -1238,7 +1325,7 @@ function formatFinding(finding, root) {
|
|
|
1238
1325
|
const names = finding.params.map((name) => `{${name}}`).join(", ");
|
|
1239
1326
|
return `${sourcePlace(finding.site, root)}: ${names} in "${truncate(finding.site.message, 60)}" reaches the translator without a name, put the value in a named variable first`;
|
|
1240
1327
|
}
|
|
1241
|
-
case "escaped": return `${sourcePlace(finding.site, root)}: ${finding.slice} renders as literal text
|
|
1328
|
+
case "escaped": return `${sourcePlace(finding.site, root)}: ${shownSlice(finding.slice)} renders as literal text: write ${inlineBlock(finding.slice)}`;
|
|
1242
1329
|
case "unparsed": return `${sourcePlace(finding, root)}: could not be parsed (${finding.message}), its messages were not extracted`;
|
|
1243
1330
|
case "outdated": return `[${finding.locale}] ${finding.key}: translated from an older source text, update it or keep it with \`npx verbaly review --approve\``;
|
|
1244
1331
|
case "lock": {
|
|
@@ -2315,6 +2402,9 @@ function gatePasses(result) {
|
|
|
2315
2402
|
return result.missing.length === 0 && result.unknown.length === 0 && result.collisions.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
2316
2403
|
}
|
|
2317
2404
|
function check(cfg, catalogs, registry, fingerprints = {}) {
|
|
2405
|
+
return gatePass(() => checkPass(cfg, catalogs, registry, fingerprints));
|
|
2406
|
+
}
|
|
2407
|
+
function checkPass(cfg, catalogs, registry, fingerprints) {
|
|
2318
2408
|
const extracted = registry.messages();
|
|
2319
2409
|
const flat = {};
|
|
2320
2410
|
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
@@ -2808,14 +2898,20 @@ function suggestedPattern(rel) {
|
|
|
2808
2898
|
const slash = rel.indexOf("/");
|
|
2809
2899
|
return slash < 0 ? rel : `${rel.slice(0, slash)}/**/*.${SOURCE_EXTENSIONS}`;
|
|
2810
2900
|
}
|
|
2811
|
-
function
|
|
2812
|
-
if (cfg.include.length === 0) return () => void 0;
|
|
2901
|
+
function outsidePath(cfg) {
|
|
2813
2902
|
const included = picomatch(cfg.include);
|
|
2814
2903
|
const excluded = picomatch(cfg.exclude);
|
|
2815
|
-
return (
|
|
2904
|
+
return (id) => {
|
|
2816
2905
|
const rel = relativeId(cfg, id);
|
|
2817
|
-
|
|
2818
|
-
|
|
2906
|
+
return rel.startsWith("..") || included(rel) || excluded(rel) ? void 0 : rel;
|
|
2907
|
+
};
|
|
2908
|
+
}
|
|
2909
|
+
function createOutsideCheck(cfg) {
|
|
2910
|
+
if (cfg.include.length === 0) return () => void 0;
|
|
2911
|
+
const outside = outsidePath(cfg);
|
|
2912
|
+
return (code, id) => {
|
|
2913
|
+
const rel = outside(id);
|
|
2914
|
+
if (rel === void 0 || !TAGGED_HINT.test(code)) return void 0;
|
|
2819
2915
|
if (analyzeFile(code, id).tagged.length === 0) return void 0;
|
|
2820
2916
|
return {
|
|
2821
2917
|
file: id,
|
|
@@ -2826,6 +2922,7 @@ function createOutsideCheck(cfg) {
|
|
|
2826
2922
|
async function filesOutsideInclude(cfg) {
|
|
2827
2923
|
if (cfg.include.length === 0) return [];
|
|
2828
2924
|
const check = createOutsideCheck(cfg);
|
|
2925
|
+
const outside = outsidePath(cfg);
|
|
2829
2926
|
const files = await glob([`**/*.${SOURCE_EXTENSIONS}`], {
|
|
2830
2927
|
cwd: cfg.root,
|
|
2831
2928
|
ignore: [...NOT_SOURCE, ...cfg.exclude],
|
|
@@ -2833,14 +2930,15 @@ async function filesOutsideInclude(cfg) {
|
|
|
2833
2930
|
});
|
|
2834
2931
|
const found = [];
|
|
2835
2932
|
for (const file of files.sort()) {
|
|
2933
|
+
if (outside(file) === void 0) continue;
|
|
2836
2934
|
let code;
|
|
2837
2935
|
try {
|
|
2838
2936
|
code = readFileSync(file, "utf8");
|
|
2839
2937
|
} catch {
|
|
2840
2938
|
continue;
|
|
2841
2939
|
}
|
|
2842
|
-
const
|
|
2843
|
-
if (
|
|
2940
|
+
const entry = check(code, file);
|
|
2941
|
+
if (entry) found.push(entry);
|
|
2844
2942
|
}
|
|
2845
2943
|
return found;
|
|
2846
2944
|
}
|
|
@@ -3004,7 +3102,7 @@ async function doctor(cfg) {
|
|
|
3004
3102
|
file: rel(msg.file),
|
|
3005
3103
|
slice: escapedSyntax(msg.message)
|
|
3006
3104
|
})).filter((entry) => entry.slice !== void 0);
|
|
3007
|
-
if (escaped.length > 0) warn("messages", `a block ships as literal text in ${counted(escaped.length, "extracted message")} (${escaped[0].file}: ${escaped[0].slice})`,
|
|
3105
|
+
if (escaped.length > 0) warn("messages", `a block ships as literal text in ${counted(escaped.length, "extracted message")} (${escaped[0].file}: ${shownSlice(escaped[0].slice)})`, `in a tagged template, write ${inlineBlock(escaped[0].slice)}`);
|
|
3008
3106
|
}
|
|
3009
3107
|
if (source && scanning) {
|
|
3010
3108
|
const extracted = registry.messages();
|