@verbaly/compiler 0.72.0 → 0.73.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 +74 -6
- package/dist/cli.js.map +1 -1
- package/dist/index.js +75 -7
- 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;
|
|
@@ -1056,10 +1099,35 @@ function surplus(a, b) {
|
|
|
1056
1099
|
function list(items) {
|
|
1057
1100
|
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
1058
1101
|
}
|
|
1059
|
-
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*[\w.]
|
|
1102
|
+
const ESCAPED_SYNTAX = /(?<!\{)\{\{\s*([\w.]+)(\s*[|:][^{}]*)\}\}/;
|
|
1103
|
+
const ESCAPED_ICU = /(?<!\{)\{\{\s*([\w.]+)\s*,\s*(plural|selectordinal|select|number|date|time)\b/;
|
|
1104
|
+
const ICU_SLICE = /^\{\{([\w.]+), (\w+), …\}\}$/;
|
|
1105
|
+
const ESCAPED_OPEN = /(?<!\{)\{\{\{([\w.]+)\}(\s*\|\s*[^\s|{}:]+\s*:|:[A-Za-z_][\w-]*)/;
|
|
1060
1106
|
function escapedSyntax(message) {
|
|
1107
|
+
const icu = ESCAPED_ICU.exec(message);
|
|
1108
|
+
if (icu) return `{{${icu[1]}, ${icu[2]}, …}}`;
|
|
1109
|
+
const open = ESCAPED_OPEN.exec(message);
|
|
1110
|
+
if (open) return `{\${${open[1]}}${open[2]}…`;
|
|
1061
1111
|
return ESCAPED_SYNTAX.exec(message)?.[0];
|
|
1062
1112
|
}
|
|
1113
|
+
function shownSlice(slice) {
|
|
1114
|
+
return slice.startsWith("{{") ? slice.slice(1, -1) : slice;
|
|
1115
|
+
}
|
|
1116
|
+
function inlineBlock(slice) {
|
|
1117
|
+
if (slice.startsWith("{${")) return "the same block with its closing }";
|
|
1118
|
+
const icu = ICU_SLICE.exec(slice);
|
|
1119
|
+
if (icu) {
|
|
1120
|
+
const [, name = "", type = ""] = icu;
|
|
1121
|
+
if (type === "plural") return `{\${${name}} | one: … | other: …}`;
|
|
1122
|
+
if (type === "select") return `{\${${name}} | …: … | other: …}`;
|
|
1123
|
+
if (type === "selectordinal") return "t(key, params), with the ICU message in the catalog";
|
|
1124
|
+
return `{\${${name}}:${type}}`;
|
|
1125
|
+
}
|
|
1126
|
+
const match = ESCAPED_SYNTAX.exec(slice);
|
|
1127
|
+
if (!match) return slice;
|
|
1128
|
+
const [, name = "", tail = ""] = match;
|
|
1129
|
+
return `{\${${name}}${tail.trimStart().startsWith(":") ? `:${tail.trimStart().slice(1).trimStart()}` : tail}}`;
|
|
1130
|
+
}
|
|
1063
1131
|
function validateMessage(message, locale) {
|
|
1064
1132
|
const issues = [];
|
|
1065
1133
|
for (const [name, shape] of shapes(message)) {
|
|
@@ -1238,7 +1306,7 @@ function formatFinding(finding, root) {
|
|
|
1238
1306
|
const names = finding.params.map((name) => `{${name}}`).join(", ");
|
|
1239
1307
|
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
1308
|
}
|
|
1241
|
-
case "escaped": return `${sourcePlace(finding.site, root)}: ${finding.slice} renders as literal text
|
|
1309
|
+
case "escaped": return `${sourcePlace(finding.site, root)}: ${shownSlice(finding.slice)} renders as literal text: write ${inlineBlock(finding.slice)}`;
|
|
1242
1310
|
case "unparsed": return `${sourcePlace(finding, root)}: could not be parsed (${finding.message}), its messages were not extracted`;
|
|
1243
1311
|
case "outdated": return `[${finding.locale}] ${finding.key}: translated from an older source text, update it or keep it with \`npx verbaly review --approve\``;
|
|
1244
1312
|
case "lock": {
|
|
@@ -3004,7 +3072,7 @@ async function doctor(cfg) {
|
|
|
3004
3072
|
file: rel(msg.file),
|
|
3005
3073
|
slice: escapedSyntax(msg.message)
|
|
3006
3074
|
})).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})`,
|
|
3075
|
+
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
3076
|
}
|
|
3009
3077
|
if (source && scanning) {
|
|
3010
3078
|
const extracted = registry.messages();
|