@verbaly/compiler 0.27.0 → 0.29.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 +4 -3
- package/dist/cli.js +374 -41
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.js +387 -25
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -26,8 +26,8 @@ npx verbaly extract --prune # drop orphaned keys
|
|
|
26
26
|
npx verbaly status # translation coverage per locale, at a glance
|
|
27
27
|
npx verbaly check # exit 1 if anything is missing (CI)
|
|
28
28
|
npx verbaly translate # fill missing translations via Claude (or your provider)
|
|
29
|
-
npx verbaly export # translator files (XLIFF 2.0, CSV) or mobile resources (Android, iOS)
|
|
30
|
-
npx verbaly import <files> # fill catalogs back from translated XLIFF/CSV files
|
|
29
|
+
npx verbaly export # translator files (XLIFF 2.0, CSV, gettext PO) or mobile resources (Android, iOS)
|
|
30
|
+
npx verbaly import <files> # fill catalogs back from translated XLIFF/CSV/PO files
|
|
31
31
|
npx verbaly pseudo # generate a pseudo-locale catalog for i18n QA (en-XA)
|
|
32
32
|
npx verbaly render # pre-fill data-verbaly HTML per locale (SSG, kills the FOUC)
|
|
33
33
|
```
|
|
@@ -51,10 +51,11 @@ Catalogs are **flat JSON**: most TMS platforms (Crowdin, Lokalise, Phrase, …)
|
|
|
51
51
|
```bash
|
|
52
52
|
npx verbaly export # verbaly-export/<locale>.xlf (XLIFF 2.0, source + target per unit)
|
|
53
53
|
npx verbaly export --format csv # spreadsheet-friendly: key,source,target,location
|
|
54
|
+
npx verbaly export --format po # gettext PO (msgctxt = key, works with any PO editor)
|
|
54
55
|
npx verbaly import verbaly-export/es.xlf # fill the catalog back
|
|
55
56
|
```
|
|
56
57
|
|
|
57
|
-
`export` writes one file per target locale with the source text alongside the current translation (`--missing` exports only the untranslated entries). Every entry carries **where the text lives in your source** (XLIFF `location` notes, a `location` column in CSV), so translators and TMS tools see the context instead of guessing it. `import` reads XLIFF 2.0/1.2 or
|
|
58
|
+
`export` writes one file per target locale with the source text alongside the current translation (`--missing` exports only the untranslated entries). Every entry carries **where the text lives in your source** (XLIFF `location` notes, a `location` column in CSV, `#:` comments in PO), so translators and TMS tools see the context instead of guessing it. In XLIFF, `{params}` and rich tags travel as **protected inline codes with semantic ids** (`<ph id="name"/>`, `<pc id="em">`), so TMS editors show them as untouchable chips instead of editable raw syntax. `import` reads XLIFF 2.0/1.2, CSV or PO back (PO entries flagged `fuzzy` count as untranslated) and **validates every entry like `translate` does**: a translation that drops a `{param}`, a variant block or an `<em>` tag is rejected and reported, so a translator's typo can't break your UI. Existing translations are kept unless `--overwrite`; `--dry-run` previews everything.
|
|
58
59
|
|
|
59
60
|
## 📱 Mobile resources
|
|
60
61
|
|
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
3
2
|
import { parseArgs } from "node:util";
|
|
4
3
|
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
4
|
+
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
5
5
|
import { RICH_TAGS, createVerbaly, localeDirection, normalizeLink, parse, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import { glob } from "tinyglobby";
|
|
@@ -331,7 +331,7 @@ function pseudoLocalize(message) {
|
|
|
331
331
|
}
|
|
332
332
|
const ch = message[i];
|
|
333
333
|
if (ch === "{") {
|
|
334
|
-
const end = matchBrace(message, i);
|
|
334
|
+
const end = matchBrace$1(message, i);
|
|
335
335
|
out += message.slice(i, end);
|
|
336
336
|
i = end;
|
|
337
337
|
continue;
|
|
@@ -355,7 +355,7 @@ function pseudoLocalize(message) {
|
|
|
355
355
|
const pad = "~".repeat(Math.ceil(letters / 3));
|
|
356
356
|
return `⟦${out}${pad ? " " + pad : ""}⟧`;
|
|
357
357
|
}
|
|
358
|
-
function matchBrace(message, start) {
|
|
358
|
+
function matchBrace$1(message, start) {
|
|
359
359
|
let depth = 0;
|
|
360
360
|
for (let i = start; i < message.length; i++) {
|
|
361
361
|
const two = message.slice(i, i + 2);
|
|
@@ -769,6 +769,8 @@ const COMMENT_RE = /<!--[\s\S]*?-->/g;
|
|
|
769
769
|
const FRONTMATTER_RE = /^(\uFEFF?\s*---\r?\n)([\s\S]*?)\r?\n---(?=\r?\n|$)/;
|
|
770
770
|
const CANDIDATE_SVELTE = /(?<![\w$])\$?t(?=[`(]|\.id\()/g;
|
|
771
771
|
const CANDIDATE_VUE = /(?<![\w$])t(?=[`(]|\.id\()/g;
|
|
772
|
+
const MUSTACHE_RE = /\{\{[\s\S]*?\}\}/g;
|
|
773
|
+
const DIRECTIVE_RE = /\s(?:v-[\w-]+(?::[^\s=>/]+)?|[:@][^\s=>/]+)\s*=\s*("[^"]*"|'[^']*')/g;
|
|
772
774
|
function analyzeSfc(code, file) {
|
|
773
775
|
const svelte = file.endsWith(".svelte");
|
|
774
776
|
const options = svelte ? { tNames: ["t", "$t"] } : void 0;
|
|
@@ -783,10 +785,14 @@ function analyzeSfc(code, file) {
|
|
|
783
785
|
}
|
|
784
786
|
const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
|
|
785
787
|
const candidates = svelte ? CANDIDATE_SVELTE : CANDIDATE_VUE;
|
|
788
|
+
const ranges = file.endsWith(".vue") ? vueExpressionRanges(markup) : braceRanges(markup);
|
|
789
|
+
let range = 0;
|
|
786
790
|
let consumedTo = 0;
|
|
787
791
|
for (const match of markup.matchAll(candidates)) {
|
|
788
792
|
const start = match.index;
|
|
789
793
|
if (start < consumedTo) continue;
|
|
794
|
+
while (range < ranges.length && ranges[range][1] <= start) range += 1;
|
|
795
|
+
if (range >= ranges.length || start < ranges[range][0]) continue;
|
|
790
796
|
const end = expressionExtent(markup, start);
|
|
791
797
|
if (end === void 0) continue;
|
|
792
798
|
merge(analyzeSegment(code.slice(start, end), file, options), start, true);
|
|
@@ -824,6 +830,31 @@ function analyzeSegment(code, file, options) {
|
|
|
824
830
|
function blank(source, re) {
|
|
825
831
|
return source.replace(re, (m) => " ".repeat(m.length));
|
|
826
832
|
}
|
|
833
|
+
function braceRanges(markup) {
|
|
834
|
+
const ranges = [];
|
|
835
|
+
let i = 0;
|
|
836
|
+
while (i < markup.length) {
|
|
837
|
+
if (markup[i] === "{") {
|
|
838
|
+
const end = balancedEnd(markup, i);
|
|
839
|
+
if (end !== void 0) {
|
|
840
|
+
ranges.push([i + 1, end - 1]);
|
|
841
|
+
i = end;
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
i += 1;
|
|
846
|
+
}
|
|
847
|
+
return ranges;
|
|
848
|
+
}
|
|
849
|
+
function vueExpressionRanges(markup) {
|
|
850
|
+
const ranges = [];
|
|
851
|
+
for (const match of markup.matchAll(MUSTACHE_RE)) ranges.push([match.index + 2, match.index + match[0].length - 2]);
|
|
852
|
+
for (const match of markup.matchAll(DIRECTIVE_RE)) {
|
|
853
|
+
const valueStart = match.index + match[0].length - match[1].length + 1;
|
|
854
|
+
ranges.push([valueStart, valueStart + match[1].length - 2]);
|
|
855
|
+
}
|
|
856
|
+
return ranges.sort((a, b) => a[0] - b[0]);
|
|
857
|
+
}
|
|
827
858
|
function expressionExtent(code, start) {
|
|
828
859
|
let i = start;
|
|
829
860
|
if (code[i] === "$") i += 1;
|
|
@@ -877,6 +908,12 @@ async function extractProject(cfg) {
|
|
|
877
908
|
for (const file of files) registry.update(file, analyzeFile(readFileSync(file, "utf8"), file));
|
|
878
909
|
return registry;
|
|
879
910
|
}
|
|
911
|
+
async function collectOrigins(cfg) {
|
|
912
|
+
const registry = await extractProject(cfg);
|
|
913
|
+
const origins = {};
|
|
914
|
+
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
915
|
+
return origins;
|
|
916
|
+
}
|
|
880
917
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
881
918
|
const added = {};
|
|
882
919
|
const source = catalogs[cfg.sourceLocale] ??= {};
|
|
@@ -1143,6 +1180,222 @@ function effectiveDrafts(drafts, catalogs) {
|
|
|
1143
1180
|
return out;
|
|
1144
1181
|
}
|
|
1145
1182
|
//#endregion
|
|
1183
|
+
//#region src/inline.ts
|
|
1184
|
+
const OPEN_TAG = /^<([a-zA-Z][a-zA-Z0-9-]*)\s*(\/?)>/;
|
|
1185
|
+
function messageToInline(text) {
|
|
1186
|
+
return renderParts(parseParts(text, 0).parts, /* @__PURE__ */ new Map());
|
|
1187
|
+
}
|
|
1188
|
+
function inlineToMessage(xml) {
|
|
1189
|
+
let out = "";
|
|
1190
|
+
let text = "";
|
|
1191
|
+
let i = 0;
|
|
1192
|
+
const flush = () => {
|
|
1193
|
+
out += unescapeXml(text);
|
|
1194
|
+
text = "";
|
|
1195
|
+
};
|
|
1196
|
+
while (i < xml.length) {
|
|
1197
|
+
if (xml[i] === "<") {
|
|
1198
|
+
const slice = xml.slice(i);
|
|
1199
|
+
const ph = /^<ph\b([^>]*?)\/\s*>/.exec(slice) ?? /^<ph\b([^>]*)>\s*<\/ph\s*>/.exec(slice);
|
|
1200
|
+
if (ph) {
|
|
1201
|
+
flush();
|
|
1202
|
+
out += attr(ph[1], "disp") ?? `{${attr(ph[1], "id") ?? "ph"}}`;
|
|
1203
|
+
i += ph[0].length;
|
|
1204
|
+
continue;
|
|
1205
|
+
}
|
|
1206
|
+
const pc = /^<pc\b([^>]*)>/.exec(slice);
|
|
1207
|
+
if (pc) {
|
|
1208
|
+
const close = findPcClose(xml, i + pc[0].length);
|
|
1209
|
+
if (close) {
|
|
1210
|
+
flush();
|
|
1211
|
+
const id = attr(pc[1], "id") ?? "pc";
|
|
1212
|
+
out += attr(pc[1], "dispStart") ?? `<${id}>`;
|
|
1213
|
+
out += inlineToMessage(xml.slice(i + pc[0].length, close.innerEnd));
|
|
1214
|
+
out += attr(pc[1], "dispEnd") ?? `</${id}>`;
|
|
1215
|
+
i = close.end;
|
|
1216
|
+
continue;
|
|
1217
|
+
}
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
text += xml[i];
|
|
1221
|
+
i += 1;
|
|
1222
|
+
}
|
|
1223
|
+
flush();
|
|
1224
|
+
return out;
|
|
1225
|
+
}
|
|
1226
|
+
function escapeXml(text) {
|
|
1227
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
1228
|
+
}
|
|
1229
|
+
function unescapeXml(text) {
|
|
1230
|
+
return text.replace(/&(lt|gt|amp|quot|apos|#x?[0-9a-fA-F]+);/g, (_, entity) => {
|
|
1231
|
+
if (entity === "lt") return "<";
|
|
1232
|
+
if (entity === "gt") return ">";
|
|
1233
|
+
if (entity === "amp") return "&";
|
|
1234
|
+
if (entity === "quot") return "\"";
|
|
1235
|
+
if (entity === "apos") return "'";
|
|
1236
|
+
const code = entity.startsWith("#x") ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10);
|
|
1237
|
+
return String.fromCodePoint(code);
|
|
1238
|
+
});
|
|
1239
|
+
}
|
|
1240
|
+
function parseParts(text, pos, closeTag) {
|
|
1241
|
+
const parts = [];
|
|
1242
|
+
let plain = "";
|
|
1243
|
+
const flush = () => {
|
|
1244
|
+
if (plain) parts.push({
|
|
1245
|
+
kind: "text",
|
|
1246
|
+
text: plain
|
|
1247
|
+
});
|
|
1248
|
+
plain = "";
|
|
1249
|
+
};
|
|
1250
|
+
let i = pos;
|
|
1251
|
+
while (i < text.length) {
|
|
1252
|
+
const pair = text[i] + (text[i + 1] ?? "");
|
|
1253
|
+
if (pair === "{{" || pair === "}}") {
|
|
1254
|
+
plain += pair;
|
|
1255
|
+
i += 2;
|
|
1256
|
+
continue;
|
|
1257
|
+
}
|
|
1258
|
+
if (text[i] === "{") {
|
|
1259
|
+
const end = matchBrace(text, i);
|
|
1260
|
+
if (end === -1) {
|
|
1261
|
+
plain += "{";
|
|
1262
|
+
i += 1;
|
|
1263
|
+
continue;
|
|
1264
|
+
}
|
|
1265
|
+
const src = text.slice(i, end + 1);
|
|
1266
|
+
if (hasTopLevelPipe(src)) plain += src;
|
|
1267
|
+
else {
|
|
1268
|
+
flush();
|
|
1269
|
+
parts.push({
|
|
1270
|
+
kind: "ph",
|
|
1271
|
+
name: paramName(src),
|
|
1272
|
+
src
|
|
1273
|
+
});
|
|
1274
|
+
}
|
|
1275
|
+
i = end + 1;
|
|
1276
|
+
continue;
|
|
1277
|
+
}
|
|
1278
|
+
if (text[i] === "<") {
|
|
1279
|
+
if (closeTag && text.startsWith(`</${closeTag}>`, i)) {
|
|
1280
|
+
flush();
|
|
1281
|
+
return {
|
|
1282
|
+
parts,
|
|
1283
|
+
end: i + closeTag.length + 3,
|
|
1284
|
+
closed: true
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
const open = OPEN_TAG.exec(text.slice(i));
|
|
1288
|
+
if (open) {
|
|
1289
|
+
const [openSrc, name, selfClose] = open;
|
|
1290
|
+
if (selfClose) {
|
|
1291
|
+
flush();
|
|
1292
|
+
parts.push({
|
|
1293
|
+
kind: "ph",
|
|
1294
|
+
name: idSafe(name),
|
|
1295
|
+
src: openSrc
|
|
1296
|
+
});
|
|
1297
|
+
i += openSrc.length;
|
|
1298
|
+
continue;
|
|
1299
|
+
}
|
|
1300
|
+
const inner = parseParts(text, i + openSrc.length, name);
|
|
1301
|
+
if (inner.closed) {
|
|
1302
|
+
flush();
|
|
1303
|
+
parts.push({
|
|
1304
|
+
kind: "pc",
|
|
1305
|
+
name: idSafe(name),
|
|
1306
|
+
openSrc,
|
|
1307
|
+
closeSrc: `</${name}>`,
|
|
1308
|
+
children: inner.parts
|
|
1309
|
+
});
|
|
1310
|
+
i = inner.end;
|
|
1311
|
+
continue;
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
plain += "<";
|
|
1315
|
+
i += 1;
|
|
1316
|
+
continue;
|
|
1317
|
+
}
|
|
1318
|
+
plain += text[i];
|
|
1319
|
+
i += 1;
|
|
1320
|
+
}
|
|
1321
|
+
flush();
|
|
1322
|
+
return {
|
|
1323
|
+
parts,
|
|
1324
|
+
end: i,
|
|
1325
|
+
closed: false
|
|
1326
|
+
};
|
|
1327
|
+
}
|
|
1328
|
+
function matchBrace(text, start) {
|
|
1329
|
+
let depth = 0;
|
|
1330
|
+
for (let i = start; i < text.length; i++) {
|
|
1331
|
+
const pair = text[i] + (text[i + 1] ?? "");
|
|
1332
|
+
if (pair === "{{" || pair === "}}") {
|
|
1333
|
+
i += 1;
|
|
1334
|
+
continue;
|
|
1335
|
+
}
|
|
1336
|
+
if (text[i] === "{") depth += 1;
|
|
1337
|
+
else if (text[i] === "}") {
|
|
1338
|
+
depth -= 1;
|
|
1339
|
+
if (depth === 0) return i;
|
|
1340
|
+
}
|
|
1341
|
+
}
|
|
1342
|
+
return -1;
|
|
1343
|
+
}
|
|
1344
|
+
function hasTopLevelPipe(src) {
|
|
1345
|
+
let depth = 0;
|
|
1346
|
+
for (let i = 0; i < src.length; i++) {
|
|
1347
|
+
const pair = src[i] + (src[i + 1] ?? "");
|
|
1348
|
+
if (pair === "{{" || pair === "}}" || pair === "||") {
|
|
1349
|
+
i += 1;
|
|
1350
|
+
continue;
|
|
1351
|
+
}
|
|
1352
|
+
if (src[i] === "{") depth += 1;
|
|
1353
|
+
else if (src[i] === "}") depth -= 1;
|
|
1354
|
+
else if (src[i] === "|" && depth === 1) return true;
|
|
1355
|
+
}
|
|
1356
|
+
return false;
|
|
1357
|
+
}
|
|
1358
|
+
function paramName(src) {
|
|
1359
|
+
const name = /^\{\s*([^{}|:\s]+)/.exec(src)?.[1];
|
|
1360
|
+
return name ? idSafe(name) : "ph";
|
|
1361
|
+
}
|
|
1362
|
+
function idSafe(name) {
|
|
1363
|
+
return name.replace(/[^a-zA-Z0-9._-]/g, "_") || "ph";
|
|
1364
|
+
}
|
|
1365
|
+
function renderParts(parts, taken) {
|
|
1366
|
+
let out = "";
|
|
1367
|
+
for (const part of parts) if (part.kind === "text") out += escapeXml(part.text);
|
|
1368
|
+
else if (part.kind === "ph") out += `<ph id="${uniqueId(part.name, taken)}" disp="${escapeXml(part.src)}"/>`;
|
|
1369
|
+
else {
|
|
1370
|
+
const id = uniqueId(part.name, taken);
|
|
1371
|
+
out += `<pc id="${id}" dispStart="${escapeXml(part.openSrc)}" dispEnd="${escapeXml(part.closeSrc)}">`;
|
|
1372
|
+
out += renderParts(part.children, taken);
|
|
1373
|
+
out += "</pc>";
|
|
1374
|
+
}
|
|
1375
|
+
return out;
|
|
1376
|
+
}
|
|
1377
|
+
function uniqueId(name, taken) {
|
|
1378
|
+
const count = taken.get(name) ?? 0;
|
|
1379
|
+
taken.set(name, count + 1);
|
|
1380
|
+
return count === 0 ? name : `${name}${count + 1}`;
|
|
1381
|
+
}
|
|
1382
|
+
function findPcClose(xml, from) {
|
|
1383
|
+
const tags = /<pc\b[^>]*>|<\/pc\s*>/g;
|
|
1384
|
+
tags.lastIndex = from;
|
|
1385
|
+
let depth = 1;
|
|
1386
|
+
for (let match = tags.exec(xml); match; match = tags.exec(xml)) {
|
|
1387
|
+
depth += match[0].startsWith("</") ? -1 : 1;
|
|
1388
|
+
if (depth === 0) return {
|
|
1389
|
+
innerEnd: match.index,
|
|
1390
|
+
end: tags.lastIndex
|
|
1391
|
+
};
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
function attr(attrs, name) {
|
|
1395
|
+
const value = new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`).exec(attrs)?.[1];
|
|
1396
|
+
return value === void 0 ? void 0 : unescapeXml(value);
|
|
1397
|
+
}
|
|
1398
|
+
//#endregion
|
|
1146
1399
|
//#region src/mobile.ts
|
|
1147
1400
|
function androidValuesDir(locale, sourceLocale) {
|
|
1148
1401
|
if (locale === sourceLocale) return "values";
|
|
@@ -1182,6 +1435,97 @@ function iosText(text) {
|
|
|
1182
1435
|
return text.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\t/g, "\\t");
|
|
1183
1436
|
}
|
|
1184
1437
|
//#endregion
|
|
1438
|
+
//#region src/po.ts
|
|
1439
|
+
function toPo(sourceLocale, locale, entries) {
|
|
1440
|
+
return [
|
|
1441
|
+
[
|
|
1442
|
+
"msgid \"\"",
|
|
1443
|
+
"msgstr \"\"",
|
|
1444
|
+
`${poString("Project-Id-Version: verbaly\n")}`,
|
|
1445
|
+
`${poString("MIME-Version: 1.0\n")}`,
|
|
1446
|
+
`${poString("Content-Type: text/plain; charset=UTF-8\n")}`,
|
|
1447
|
+
`${poString("Content-Transfer-Encoding: 8bit\n")}`,
|
|
1448
|
+
`${poString(`Language: ${locale}\n`)}`,
|
|
1449
|
+
`${poString(`X-Source-Language: ${sourceLocale}\n`)}`
|
|
1450
|
+
].join("\n"),
|
|
1451
|
+
...entries.map(({ key, source, target, location }) => [
|
|
1452
|
+
...location?.length ? [`#: ${location.join(" ")}`] : [],
|
|
1453
|
+
`msgctxt ${poString(key)}`,
|
|
1454
|
+
`msgid ${poString(source)}`,
|
|
1455
|
+
`msgstr ${poString(target)}`
|
|
1456
|
+
].join("\n")),
|
|
1457
|
+
""
|
|
1458
|
+
].join("\n\n");
|
|
1459
|
+
}
|
|
1460
|
+
function parsePo(content) {
|
|
1461
|
+
const entries = {};
|
|
1462
|
+
let locale;
|
|
1463
|
+
let msgctxt;
|
|
1464
|
+
let msgid;
|
|
1465
|
+
let msgstr;
|
|
1466
|
+
let fuzzy = false;
|
|
1467
|
+
let field;
|
|
1468
|
+
const finish = () => {
|
|
1469
|
+
if (msgid === "" && msgctxt === void 0) {
|
|
1470
|
+
const lang = /^Language:[ \t]*(.+?)[ \t]*$/m.exec(msgstr ?? "")?.[1];
|
|
1471
|
+
if (lang) locale = lang;
|
|
1472
|
+
} else if (msgctxt !== void 0 && msgid !== void 0) entries[msgctxt] = fuzzy ? "" : msgstr ?? "";
|
|
1473
|
+
msgctxt = msgid = msgstr = field = void 0;
|
|
1474
|
+
fuzzy = false;
|
|
1475
|
+
};
|
|
1476
|
+
for (const raw of content.replace(/^\uFEFF/, "").split(/\r\n|[\r\n]/)) {
|
|
1477
|
+
const line = raw.trim();
|
|
1478
|
+
if (line === "") {
|
|
1479
|
+
finish();
|
|
1480
|
+
continue;
|
|
1481
|
+
}
|
|
1482
|
+
if (line.startsWith("#")) {
|
|
1483
|
+
if (/^#,.*\bfuzzy\b/.test(line)) fuzzy = true;
|
|
1484
|
+
continue;
|
|
1485
|
+
}
|
|
1486
|
+
const keyword = /^(msgctxt|msgid_plural|msgid|msgstr(?:\[\d+\])?)\s+(.*)$/.exec(line);
|
|
1487
|
+
if (keyword) {
|
|
1488
|
+
const [, name, rest] = keyword;
|
|
1489
|
+
const value = poUnquote(rest);
|
|
1490
|
+
if (value === void 0) continue;
|
|
1491
|
+
if (name === "msgctxt") {
|
|
1492
|
+
msgctxt = value;
|
|
1493
|
+
field = "msgctxt";
|
|
1494
|
+
} else if (name === "msgid") {
|
|
1495
|
+
msgid = value;
|
|
1496
|
+
field = "msgid";
|
|
1497
|
+
} else if (name === "msgstr" || name === "msgstr[0]") {
|
|
1498
|
+
msgstr = value;
|
|
1499
|
+
field = "msgstr";
|
|
1500
|
+
} else field = "other";
|
|
1501
|
+
continue;
|
|
1502
|
+
}
|
|
1503
|
+
const continuation = poUnquote(line);
|
|
1504
|
+
if (continuation === void 0 || field === void 0 || field === "other") continue;
|
|
1505
|
+
if (field === "msgctxt") msgctxt = (msgctxt ?? "") + continuation;
|
|
1506
|
+
else if (field === "msgid") msgid = (msgid ?? "") + continuation;
|
|
1507
|
+
else msgstr = (msgstr ?? "") + continuation;
|
|
1508
|
+
}
|
|
1509
|
+
finish();
|
|
1510
|
+
return {
|
|
1511
|
+
locale,
|
|
1512
|
+
entries
|
|
1513
|
+
};
|
|
1514
|
+
}
|
|
1515
|
+
function poString(text) {
|
|
1516
|
+
return `"${text.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\t/g, "\\t").replace(/\r/g, "\\r")}"`;
|
|
1517
|
+
}
|
|
1518
|
+
function poUnquote(text) {
|
|
1519
|
+
const match = /^"((?:[^"\\]|\\.)*)"$/.exec(text);
|
|
1520
|
+
if (!match) return void 0;
|
|
1521
|
+
return match[1].replace(/\\(.)/g, (_, ch) => {
|
|
1522
|
+
if (ch === "n") return "\n";
|
|
1523
|
+
if (ch === "t") return " ";
|
|
1524
|
+
if (ch === "r") return "\r";
|
|
1525
|
+
return ch;
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
//#endregion
|
|
1185
1529
|
//#region src/translate.ts
|
|
1186
1530
|
async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
1187
1531
|
const batchSize = options.batchSize ?? 20;
|
|
@@ -1221,6 +1565,12 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
1221
1565
|
}
|
|
1222
1566
|
return result;
|
|
1223
1567
|
}
|
|
1568
|
+
async function resolveProvider(cfg, model) {
|
|
1569
|
+
const configured = cfg.translate.provider;
|
|
1570
|
+
if (typeof configured === "function") return configured;
|
|
1571
|
+
const { claudeProvider } = await import("./claude-h9WA0ppg.js");
|
|
1572
|
+
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1573
|
+
}
|
|
1224
1574
|
function structureMatches(source, translated) {
|
|
1225
1575
|
return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
|
|
1226
1576
|
}
|
|
@@ -1263,8 +1613,8 @@ function exportCatalogs(cfg, catalogs, options = {}) {
|
|
|
1263
1613
|
}));
|
|
1264
1614
|
const untranslated = all.filter((entry) => !entry.target);
|
|
1265
1615
|
const entries = options.missing ? untranslated : all;
|
|
1266
|
-
const path = join(dir, `${locale}.${format === "
|
|
1267
|
-
writeFileSync(path, format === "csv" ? toCsv(entries) : toXliff(cfg.sourceLocale, locale, entries));
|
|
1616
|
+
const path = join(dir, `${locale}.${format === "xliff" ? "xlf" : format}`);
|
|
1617
|
+
writeFileSync(path, format === "csv" ? toCsv(entries) : format === "po" ? toPo(cfg.sourceLocale, locale, entries) : toXliff(cfg.sourceLocale, locale, entries));
|
|
1268
1618
|
files.push({
|
|
1269
1619
|
locale,
|
|
1270
1620
|
path,
|
|
@@ -1352,7 +1702,14 @@ function parseExchangeFile(file, localeOverride) {
|
|
|
1352
1702
|
locale: localeOverride ?? basename(file).replace(/\.csv$/i, ""),
|
|
1353
1703
|
entries: parseCsv(content)
|
|
1354
1704
|
};
|
|
1355
|
-
|
|
1705
|
+
if (/\.po$/i.test(file)) {
|
|
1706
|
+
const parsed = parsePo(content);
|
|
1707
|
+
return {
|
|
1708
|
+
locale: localeOverride ?? parsed.locale ?? basename(file).replace(/\.po$/i, ""),
|
|
1709
|
+
entries: parsed.entries
|
|
1710
|
+
};
|
|
1711
|
+
}
|
|
1712
|
+
throw new Error(`[verbaly] ${file}: unsupported format, expected .xlf, .xliff, .csv or .po.`);
|
|
1356
1713
|
}
|
|
1357
1714
|
function toXliff(sourceLocale, locale, entries) {
|
|
1358
1715
|
const units = entries.map(({ key, source, target, location }) => [
|
|
@@ -1363,8 +1720,8 @@ function toXliff(sourceLocale, locale, entries) {
|
|
|
1363
1720
|
" </notes>"
|
|
1364
1721
|
] : [],
|
|
1365
1722
|
` <segment state="${target ? "translated" : "initial"}">`,
|
|
1366
|
-
` <source>${
|
|
1367
|
-
` <target>${
|
|
1723
|
+
` <source>${messageToInline(source)}</source>`,
|
|
1724
|
+
` <target>${messageToInline(target)}</target>`,
|
|
1368
1725
|
" </segment>",
|
|
1369
1726
|
" </unit>"
|
|
1370
1727
|
].join("\n")).join("\n");
|
|
@@ -1385,30 +1742,16 @@ function parseXliff(content) {
|
|
|
1385
1742
|
const id = /\bid\s*=\s*"([^"]*)"/.exec(match[1])?.[1];
|
|
1386
1743
|
if (!id) continue;
|
|
1387
1744
|
const target = /<target\b[^>]*>([\s\S]*?)<\/target>/.exec(match[2])?.[1];
|
|
1388
|
-
entries[unescapeXml(id)] = target === void 0 ? "" :
|
|
1745
|
+
entries[unescapeXml(id)] = target === void 0 ? "" : inlineToMessage(stripCdata(target));
|
|
1389
1746
|
}
|
|
1390
1747
|
return {
|
|
1391
1748
|
locale,
|
|
1392
1749
|
entries
|
|
1393
1750
|
};
|
|
1394
1751
|
}
|
|
1395
|
-
function escapeXml(text) {
|
|
1396
|
-
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
1397
|
-
}
|
|
1398
1752
|
function stripCdata(text) {
|
|
1399
1753
|
return text.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1");
|
|
1400
1754
|
}
|
|
1401
|
-
function unescapeXml(text) {
|
|
1402
|
-
return text.replace(/&(lt|gt|amp|quot|apos|#x?[0-9a-fA-F]+);/g, (_, entity) => {
|
|
1403
|
-
if (entity === "lt") return "<";
|
|
1404
|
-
if (entity === "gt") return ">";
|
|
1405
|
-
if (entity === "amp") return "&";
|
|
1406
|
-
if (entity === "quot") return "\"";
|
|
1407
|
-
if (entity === "apos") return "'";
|
|
1408
|
-
const code = entity.startsWith("#x") ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10);
|
|
1409
|
-
return String.fromCodePoint(code);
|
|
1410
|
-
});
|
|
1411
|
-
}
|
|
1412
1755
|
function toCsv(entries) {
|
|
1413
1756
|
return [
|
|
1414
1757
|
"key,source,target,location",
|
|
@@ -1581,7 +1924,8 @@ async function renderSite(cfg, options = {}) {
|
|
|
1581
1924
|
const attribute = options.attribute ?? cfg.render.attribute;
|
|
1582
1925
|
const baseUrl = (options.baseUrl ?? cfg.render.baseUrl)?.replace(/\/+$/, "");
|
|
1583
1926
|
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
1584
|
-
const
|
|
1927
|
+
const sitemap = options.sitemap ?? cfg.render.sitemap ?? false;
|
|
1928
|
+
const wantSitemap = sitemap !== false && baseUrl !== void 0;
|
|
1585
1929
|
const clean = options.clean ?? cfg.render.clean ?? false;
|
|
1586
1930
|
const catalogs = loadCatalogs(cfg);
|
|
1587
1931
|
if (clean) {
|
|
@@ -1624,7 +1968,7 @@ async function renderSite(cfg, options = {}) {
|
|
|
1624
1968
|
writeFileSync(out, result.html);
|
|
1625
1969
|
}
|
|
1626
1970
|
}
|
|
1627
|
-
if (wantSitemap && urls.length) writeFileSync(join(site, typeof
|
|
1971
|
+
if (wantSitemap && urls.length) writeFileSync(join(site, typeof sitemap === "string" ? sitemap : "sitemap-i18n.xml"), buildSitemap(urls));
|
|
1628
1972
|
return {
|
|
1629
1973
|
files: files.length,
|
|
1630
1974
|
locales: [...locales],
|
|
@@ -2035,8 +2379,8 @@ Usage:
|
|
|
2035
2379
|
verbaly check verify translations are complete (CI)
|
|
2036
2380
|
verbaly translate fill missing translations via a provider (default: claude)
|
|
2037
2381
|
verbaly review list machine translations awaiting review (--approve marks them reviewed)
|
|
2038
|
-
verbaly export write translator files (XLIFF 2.0, CSV) or mobile resources (Android, iOS)
|
|
2039
|
-
verbaly import <files…> fill catalogs back from translated XLIFF/CSV files
|
|
2382
|
+
verbaly export write translator files (XLIFF 2.0, CSV, gettext PO) or mobile resources (Android, iOS)
|
|
2383
|
+
verbaly import <files…> fill catalogs back from translated XLIFF/CSV/PO files
|
|
2040
2384
|
verbaly pseudo generate a pseudo-locale catalog for i18n QA (default: en-XA)
|
|
2041
2385
|
verbaly render pre-fill data-verbaly HTML per locale (SSG, kills the FOUC)
|
|
2042
2386
|
|
|
@@ -2054,7 +2398,7 @@ Options:
|
|
|
2054
2398
|
--reporter <name> failure format: text (default) or github annotations (check)
|
|
2055
2399
|
--model <id> model override for the claude provider (translate)
|
|
2056
2400
|
--dry-run list what would happen, write nothing (translate, import, extract)
|
|
2057
|
-
--format <f> export format: xliff (default), csv, android-xml or ios-strings (export)
|
|
2401
|
+
--format <f> export format: xliff (default), csv, po, android-xml or ios-strings (export)
|
|
2058
2402
|
--out <path> export directory (export, default: verbaly-export)
|
|
2059
2403
|
--missing export only untranslated entries (export)
|
|
2060
2404
|
--overwrite replace existing translations on import (import)
|
|
@@ -2293,10 +2637,11 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
2293
2637
|
if (![
|
|
2294
2638
|
"xliff",
|
|
2295
2639
|
"csv",
|
|
2640
|
+
"po",
|
|
2296
2641
|
"android-xml",
|
|
2297
2642
|
"ios-strings"
|
|
2298
2643
|
].includes(format)) {
|
|
2299
|
-
console.error(`[verbaly] unknown format "${values.format}", use xliff, csv, android-xml or ios-strings`);
|
|
2644
|
+
console.error(`[verbaly] unknown format "${values.format}", use xliff, csv, po, android-xml or ios-strings`);
|
|
2300
2645
|
process.exitCode = 1;
|
|
2301
2646
|
return;
|
|
2302
2647
|
}
|
|
@@ -2425,18 +2770,6 @@ function rejectStrayFlags(command, values) {
|
|
|
2425
2770
|
process.exitCode = 1;
|
|
2426
2771
|
return true;
|
|
2427
2772
|
}
|
|
2428
|
-
async function collectOrigins(cfg) {
|
|
2429
|
-
const registry = await extractProject(cfg);
|
|
2430
|
-
const origins = {};
|
|
2431
|
-
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
2432
|
-
return origins;
|
|
2433
|
-
}
|
|
2434
|
-
async function resolveProvider(cfg, model) {
|
|
2435
|
-
const configured = cfg.translate.provider;
|
|
2436
|
-
if (typeof configured === "function") return configured;
|
|
2437
|
-
const { claudeProvider } = await import("./claude-h9WA0ppg.js");
|
|
2438
|
-
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
2439
|
-
}
|
|
2440
2773
|
//#endregion
|
|
2441
2774
|
//#region src/cli.ts
|
|
2442
2775
|
runCli().catch((error) => {
|