@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/dist/index.d.ts
CHANGED
|
@@ -63,6 +63,7 @@ interface TranslateResult {
|
|
|
63
63
|
pending: Record<string, string[]>;
|
|
64
64
|
}
|
|
65
65
|
declare function translateCatalogs(cfg: ResolvedConfig, catalogs: Catalogs, provider: TranslateProvider, options?: TranslateOptions): Promise<TranslateResult>;
|
|
66
|
+
declare function resolveProvider(cfg: ResolvedConfig, model?: string): Promise<TranslateProvider>;
|
|
66
67
|
declare function structureMatches(source: string, translated: string): boolean;
|
|
67
68
|
//#endregion
|
|
68
69
|
//#region src/config.d.ts
|
|
@@ -204,7 +205,7 @@ declare function clearDrafts(drafts: Drafts, locale: string, keys?: string[]): v
|
|
|
204
205
|
declare function effectiveDrafts(drafts: Drafts, catalogs: Catalogs): Drafts;
|
|
205
206
|
//#endregion
|
|
206
207
|
//#region src/exchange.d.ts
|
|
207
|
-
type ExchangeFormat = 'xliff' | 'csv';
|
|
208
|
+
type ExchangeFormat = 'xliff' | 'csv' | 'po';
|
|
208
209
|
type MobileFormat = 'android-xml' | 'ios-strings';
|
|
209
210
|
type ExportFormat = ExchangeFormat | MobileFormat;
|
|
210
211
|
declare function isMobileFormat(format: ExportFormat): format is MobileFormat;
|
|
@@ -247,6 +248,7 @@ declare function parseExchangeFile(file: string, localeOverride?: string): Parse
|
|
|
247
248
|
//#endregion
|
|
248
249
|
//#region src/extract.d.ts
|
|
249
250
|
declare function extractProject(cfg: ResolvedConfig): Promise<MessageRegistry>;
|
|
251
|
+
declare function collectOrigins(cfg: ResolvedConfig): Promise<Record<string, string[]>>;
|
|
250
252
|
interface SyncResult {
|
|
251
253
|
added: Record<string, string[]>;
|
|
252
254
|
}
|
|
@@ -381,5 +383,5 @@ interface WrapCodeResult {
|
|
|
381
383
|
}
|
|
382
384
|
declare function wrapCode(code: string, file: string): WrapCodeResult;
|
|
383
385
|
//#endregion
|
|
384
|
-
export { type Alternate, type Analysis, type AnalyzeOptions, type Catalog, type Catalogs, type CheckResult, type ClaudeProviderOptions, DRAFTS_FILE, type DoctorEntry, type DoctorResult, type Drafts, type ExchangeFormat, type ExportFormat, type ExportOptions, type ExportResult, type ExportedFile, type ImportOptions, type ImportResult, type InitOptions, type InitResult, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type MobileFormat, PSEUDO_LOCALE, type ParamType, type PluginOptions, RESOLVED_VIRTUAL_ID, type RenderConfig, type RenderHtmlOptions, type RenderHtmlResult, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SFC_FILE_RE, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TaggedMessage, type TaggedParam, type TransComponent, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type UsedKey, VIRTUAL_ID, type VerbalyConfig, type WatchProjectOptions, type WrapCodeResult, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
386
|
+
export { type Alternate, type Analysis, type AnalyzeOptions, type Catalog, type Catalogs, type CheckResult, type ClaudeProviderOptions, DRAFTS_FILE, type DoctorEntry, type DoctorResult, type Drafts, type ExchangeFormat, type ExportFormat, type ExportOptions, type ExportResult, type ExportedFile, type ImportOptions, type ImportResult, type InitOptions, type InitResult, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type MobileFormat, PSEUDO_LOCALE, type ParamType, type PluginOptions, RESOLVED_VIRTUAL_ID, type RenderConfig, type RenderHtmlOptions, type RenderHtmlResult, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SFC_FILE_RE, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TaggedMessage, type TaggedParam, type TransComponent, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type UsedKey, VIRTUAL_ID, type VerbalyConfig, type WatchProjectOptions, type WrapCodeResult, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
385
387
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -7,6 +7,18 @@ import { pathToFileURL } from "node:url";
|
|
|
7
7
|
import picomatch from "picomatch";
|
|
8
8
|
import MagicString from "magic-string";
|
|
9
9
|
import { glob } from "tinyglobby";
|
|
10
|
+
//#region \0rolldown/runtime.js
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __exportAll = (all, no_symbols) => {
|
|
13
|
+
let target = {};
|
|
14
|
+
for (var name in all) __defProp(target, name, {
|
|
15
|
+
get: all[name],
|
|
16
|
+
enumerable: true
|
|
17
|
+
});
|
|
18
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
19
|
+
return target;
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
10
22
|
//#region src/key.ts
|
|
11
23
|
function stableKey(message) {
|
|
12
24
|
return createHash("sha256").update(message).digest("base64url").slice(0, 8);
|
|
@@ -284,6 +296,8 @@ const COMMENT_RE = /<!--[\s\S]*?-->/g;
|
|
|
284
296
|
const FRONTMATTER_RE = /^(\uFEFF?\s*---\r?\n)([\s\S]*?)\r?\n---(?=\r?\n|$)/;
|
|
285
297
|
const CANDIDATE_SVELTE = /(?<![\w$])\$?t(?=[`(]|\.id\()/g;
|
|
286
298
|
const CANDIDATE_VUE = /(?<![\w$])t(?=[`(]|\.id\()/g;
|
|
299
|
+
const MUSTACHE_RE = /\{\{[\s\S]*?\}\}/g;
|
|
300
|
+
const DIRECTIVE_RE = /\s(?:v-[\w-]+(?::[^\s=>/]+)?|[:@][^\s=>/]+)\s*=\s*("[^"]*"|'[^']*')/g;
|
|
287
301
|
function analyzeSfc(code, file) {
|
|
288
302
|
const svelte = file.endsWith(".svelte");
|
|
289
303
|
const options = svelte ? { tNames: ["t", "$t"] } : void 0;
|
|
@@ -298,10 +312,14 @@ function analyzeSfc(code, file) {
|
|
|
298
312
|
}
|
|
299
313
|
const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
|
|
300
314
|
const candidates = svelte ? CANDIDATE_SVELTE : CANDIDATE_VUE;
|
|
315
|
+
const ranges = file.endsWith(".vue") ? vueExpressionRanges(markup) : braceRanges(markup);
|
|
316
|
+
let range = 0;
|
|
301
317
|
let consumedTo = 0;
|
|
302
318
|
for (const match of markup.matchAll(candidates)) {
|
|
303
319
|
const start = match.index;
|
|
304
320
|
if (start < consumedTo) continue;
|
|
321
|
+
while (range < ranges.length && ranges[range][1] <= start) range += 1;
|
|
322
|
+
if (range >= ranges.length || start < ranges[range][0]) continue;
|
|
305
323
|
const end = expressionExtent(markup, start);
|
|
306
324
|
if (end === void 0) continue;
|
|
307
325
|
merge(analyzeSegment(code.slice(start, end), file, options), start, true);
|
|
@@ -339,6 +357,31 @@ function analyzeSegment(code, file, options) {
|
|
|
339
357
|
function blank(source, re) {
|
|
340
358
|
return source.replace(re, (m) => " ".repeat(m.length));
|
|
341
359
|
}
|
|
360
|
+
function braceRanges(markup) {
|
|
361
|
+
const ranges = [];
|
|
362
|
+
let i = 0;
|
|
363
|
+
while (i < markup.length) {
|
|
364
|
+
if (markup[i] === "{") {
|
|
365
|
+
const end = balancedEnd(markup, i);
|
|
366
|
+
if (end !== void 0) {
|
|
367
|
+
ranges.push([i + 1, end - 1]);
|
|
368
|
+
i = end;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
i += 1;
|
|
373
|
+
}
|
|
374
|
+
return ranges;
|
|
375
|
+
}
|
|
376
|
+
function vueExpressionRanges(markup) {
|
|
377
|
+
const ranges = [];
|
|
378
|
+
for (const match of markup.matchAll(MUSTACHE_RE)) ranges.push([match.index + 2, match.index + match[0].length - 2]);
|
|
379
|
+
for (const match of markup.matchAll(DIRECTIVE_RE)) {
|
|
380
|
+
const valueStart = match.index + match[0].length - match[1].length + 1;
|
|
381
|
+
ranges.push([valueStart, valueStart + match[1].length - 2]);
|
|
382
|
+
}
|
|
383
|
+
return ranges.sort((a, b) => a[0] - b[0]);
|
|
384
|
+
}
|
|
342
385
|
function expressionExtent(code, start) {
|
|
343
386
|
let i = start;
|
|
344
387
|
if (code[i] === "$") i += 1;
|
|
@@ -768,7 +811,7 @@ function pseudoLocalize(message) {
|
|
|
768
811
|
}
|
|
769
812
|
const ch = message[i];
|
|
770
813
|
if (ch === "{") {
|
|
771
|
-
const end = matchBrace(message, i);
|
|
814
|
+
const end = matchBrace$1(message, i);
|
|
772
815
|
out += message.slice(i, end);
|
|
773
816
|
i = end;
|
|
774
817
|
continue;
|
|
@@ -792,7 +835,7 @@ function pseudoLocalize(message) {
|
|
|
792
835
|
const pad = "~".repeat(Math.ceil(letters / 3));
|
|
793
836
|
return `⟦${out}${pad ? " " + pad : ""}⟧`;
|
|
794
837
|
}
|
|
795
|
-
function matchBrace(message, start) {
|
|
838
|
+
function matchBrace$1(message, start) {
|
|
796
839
|
let depth = 0;
|
|
797
840
|
for (let i = start; i < message.length; i++) {
|
|
798
841
|
const two = message.slice(i, i + 2);
|
|
@@ -1005,6 +1048,12 @@ async function extractProject(cfg) {
|
|
|
1005
1048
|
for (const file of files) registry.update(file, analyzeFile(readFileSync(file, "utf8"), file));
|
|
1006
1049
|
return registry;
|
|
1007
1050
|
}
|
|
1051
|
+
async function collectOrigins(cfg) {
|
|
1052
|
+
const registry = await extractProject(cfg);
|
|
1053
|
+
const origins = {};
|
|
1054
|
+
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
1055
|
+
return origins;
|
|
1056
|
+
}
|
|
1008
1057
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
1009
1058
|
const added = {};
|
|
1010
1059
|
const source = catalogs[cfg.sourceLocale] ??= {};
|
|
@@ -1271,6 +1320,222 @@ function effectiveDrafts(drafts, catalogs) {
|
|
|
1271
1320
|
return out;
|
|
1272
1321
|
}
|
|
1273
1322
|
//#endregion
|
|
1323
|
+
//#region src/inline.ts
|
|
1324
|
+
const OPEN_TAG = /^<([a-zA-Z][a-zA-Z0-9-]*)\s*(\/?)>/;
|
|
1325
|
+
function messageToInline(text) {
|
|
1326
|
+
return renderParts(parseParts(text, 0).parts, /* @__PURE__ */ new Map());
|
|
1327
|
+
}
|
|
1328
|
+
function inlineToMessage(xml) {
|
|
1329
|
+
let out = "";
|
|
1330
|
+
let text = "";
|
|
1331
|
+
let i = 0;
|
|
1332
|
+
const flush = () => {
|
|
1333
|
+
out += unescapeXml(text);
|
|
1334
|
+
text = "";
|
|
1335
|
+
};
|
|
1336
|
+
while (i < xml.length) {
|
|
1337
|
+
if (xml[i] === "<") {
|
|
1338
|
+
const slice = xml.slice(i);
|
|
1339
|
+
const ph = /^<ph\b([^>]*?)\/\s*>/.exec(slice) ?? /^<ph\b([^>]*)>\s*<\/ph\s*>/.exec(slice);
|
|
1340
|
+
if (ph) {
|
|
1341
|
+
flush();
|
|
1342
|
+
out += attr(ph[1], "disp") ?? `{${attr(ph[1], "id") ?? "ph"}}`;
|
|
1343
|
+
i += ph[0].length;
|
|
1344
|
+
continue;
|
|
1345
|
+
}
|
|
1346
|
+
const pc = /^<pc\b([^>]*)>/.exec(slice);
|
|
1347
|
+
if (pc) {
|
|
1348
|
+
const close = findPcClose(xml, i + pc[0].length);
|
|
1349
|
+
if (close) {
|
|
1350
|
+
flush();
|
|
1351
|
+
const id = attr(pc[1], "id") ?? "pc";
|
|
1352
|
+
out += attr(pc[1], "dispStart") ?? `<${id}>`;
|
|
1353
|
+
out += inlineToMessage(xml.slice(i + pc[0].length, close.innerEnd));
|
|
1354
|
+
out += attr(pc[1], "dispEnd") ?? `</${id}>`;
|
|
1355
|
+
i = close.end;
|
|
1356
|
+
continue;
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
text += xml[i];
|
|
1361
|
+
i += 1;
|
|
1362
|
+
}
|
|
1363
|
+
flush();
|
|
1364
|
+
return out;
|
|
1365
|
+
}
|
|
1366
|
+
function escapeXml(text) {
|
|
1367
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
1368
|
+
}
|
|
1369
|
+
function unescapeXml(text) {
|
|
1370
|
+
return text.replace(/&(lt|gt|amp|quot|apos|#x?[0-9a-fA-F]+);/g, (_, entity) => {
|
|
1371
|
+
if (entity === "lt") return "<";
|
|
1372
|
+
if (entity === "gt") return ">";
|
|
1373
|
+
if (entity === "amp") return "&";
|
|
1374
|
+
if (entity === "quot") return "\"";
|
|
1375
|
+
if (entity === "apos") return "'";
|
|
1376
|
+
const code = entity.startsWith("#x") ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10);
|
|
1377
|
+
return String.fromCodePoint(code);
|
|
1378
|
+
});
|
|
1379
|
+
}
|
|
1380
|
+
function parseParts(text, pos, closeTag) {
|
|
1381
|
+
const parts = [];
|
|
1382
|
+
let plain = "";
|
|
1383
|
+
const flush = () => {
|
|
1384
|
+
if (plain) parts.push({
|
|
1385
|
+
kind: "text",
|
|
1386
|
+
text: plain
|
|
1387
|
+
});
|
|
1388
|
+
plain = "";
|
|
1389
|
+
};
|
|
1390
|
+
let i = pos;
|
|
1391
|
+
while (i < text.length) {
|
|
1392
|
+
const pair = text[i] + (text[i + 1] ?? "");
|
|
1393
|
+
if (pair === "{{" || pair === "}}") {
|
|
1394
|
+
plain += pair;
|
|
1395
|
+
i += 2;
|
|
1396
|
+
continue;
|
|
1397
|
+
}
|
|
1398
|
+
if (text[i] === "{") {
|
|
1399
|
+
const end = matchBrace(text, i);
|
|
1400
|
+
if (end === -1) {
|
|
1401
|
+
plain += "{";
|
|
1402
|
+
i += 1;
|
|
1403
|
+
continue;
|
|
1404
|
+
}
|
|
1405
|
+
const src = text.slice(i, end + 1);
|
|
1406
|
+
if (hasTopLevelPipe(src)) plain += src;
|
|
1407
|
+
else {
|
|
1408
|
+
flush();
|
|
1409
|
+
parts.push({
|
|
1410
|
+
kind: "ph",
|
|
1411
|
+
name: paramName(src),
|
|
1412
|
+
src
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
i = end + 1;
|
|
1416
|
+
continue;
|
|
1417
|
+
}
|
|
1418
|
+
if (text[i] === "<") {
|
|
1419
|
+
if (closeTag && text.startsWith(`</${closeTag}>`, i)) {
|
|
1420
|
+
flush();
|
|
1421
|
+
return {
|
|
1422
|
+
parts,
|
|
1423
|
+
end: i + closeTag.length + 3,
|
|
1424
|
+
closed: true
|
|
1425
|
+
};
|
|
1426
|
+
}
|
|
1427
|
+
const open = OPEN_TAG.exec(text.slice(i));
|
|
1428
|
+
if (open) {
|
|
1429
|
+
const [openSrc, name, selfClose] = open;
|
|
1430
|
+
if (selfClose) {
|
|
1431
|
+
flush();
|
|
1432
|
+
parts.push({
|
|
1433
|
+
kind: "ph",
|
|
1434
|
+
name: idSafe(name),
|
|
1435
|
+
src: openSrc
|
|
1436
|
+
});
|
|
1437
|
+
i += openSrc.length;
|
|
1438
|
+
continue;
|
|
1439
|
+
}
|
|
1440
|
+
const inner = parseParts(text, i + openSrc.length, name);
|
|
1441
|
+
if (inner.closed) {
|
|
1442
|
+
flush();
|
|
1443
|
+
parts.push({
|
|
1444
|
+
kind: "pc",
|
|
1445
|
+
name: idSafe(name),
|
|
1446
|
+
openSrc,
|
|
1447
|
+
closeSrc: `</${name}>`,
|
|
1448
|
+
children: inner.parts
|
|
1449
|
+
});
|
|
1450
|
+
i = inner.end;
|
|
1451
|
+
continue;
|
|
1452
|
+
}
|
|
1453
|
+
}
|
|
1454
|
+
plain += "<";
|
|
1455
|
+
i += 1;
|
|
1456
|
+
continue;
|
|
1457
|
+
}
|
|
1458
|
+
plain += text[i];
|
|
1459
|
+
i += 1;
|
|
1460
|
+
}
|
|
1461
|
+
flush();
|
|
1462
|
+
return {
|
|
1463
|
+
parts,
|
|
1464
|
+
end: i,
|
|
1465
|
+
closed: false
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1468
|
+
function matchBrace(text, start) {
|
|
1469
|
+
let depth = 0;
|
|
1470
|
+
for (let i = start; i < text.length; i++) {
|
|
1471
|
+
const pair = text[i] + (text[i + 1] ?? "");
|
|
1472
|
+
if (pair === "{{" || pair === "}}") {
|
|
1473
|
+
i += 1;
|
|
1474
|
+
continue;
|
|
1475
|
+
}
|
|
1476
|
+
if (text[i] === "{") depth += 1;
|
|
1477
|
+
else if (text[i] === "}") {
|
|
1478
|
+
depth -= 1;
|
|
1479
|
+
if (depth === 0) return i;
|
|
1480
|
+
}
|
|
1481
|
+
}
|
|
1482
|
+
return -1;
|
|
1483
|
+
}
|
|
1484
|
+
function hasTopLevelPipe(src) {
|
|
1485
|
+
let depth = 0;
|
|
1486
|
+
for (let i = 0; i < src.length; i++) {
|
|
1487
|
+
const pair = src[i] + (src[i + 1] ?? "");
|
|
1488
|
+
if (pair === "{{" || pair === "}}" || pair === "||") {
|
|
1489
|
+
i += 1;
|
|
1490
|
+
continue;
|
|
1491
|
+
}
|
|
1492
|
+
if (src[i] === "{") depth += 1;
|
|
1493
|
+
else if (src[i] === "}") depth -= 1;
|
|
1494
|
+
else if (src[i] === "|" && depth === 1) return true;
|
|
1495
|
+
}
|
|
1496
|
+
return false;
|
|
1497
|
+
}
|
|
1498
|
+
function paramName(src) {
|
|
1499
|
+
const name = /^\{\s*([^{}|:\s]+)/.exec(src)?.[1];
|
|
1500
|
+
return name ? idSafe(name) : "ph";
|
|
1501
|
+
}
|
|
1502
|
+
function idSafe(name) {
|
|
1503
|
+
return name.replace(/[^a-zA-Z0-9._-]/g, "_") || "ph";
|
|
1504
|
+
}
|
|
1505
|
+
function renderParts(parts, taken) {
|
|
1506
|
+
let out = "";
|
|
1507
|
+
for (const part of parts) if (part.kind === "text") out += escapeXml(part.text);
|
|
1508
|
+
else if (part.kind === "ph") out += `<ph id="${uniqueId(part.name, taken)}" disp="${escapeXml(part.src)}"/>`;
|
|
1509
|
+
else {
|
|
1510
|
+
const id = uniqueId(part.name, taken);
|
|
1511
|
+
out += `<pc id="${id}" dispStart="${escapeXml(part.openSrc)}" dispEnd="${escapeXml(part.closeSrc)}">`;
|
|
1512
|
+
out += renderParts(part.children, taken);
|
|
1513
|
+
out += "</pc>";
|
|
1514
|
+
}
|
|
1515
|
+
return out;
|
|
1516
|
+
}
|
|
1517
|
+
function uniqueId(name, taken) {
|
|
1518
|
+
const count = taken.get(name) ?? 0;
|
|
1519
|
+
taken.set(name, count + 1);
|
|
1520
|
+
return count === 0 ? name : `${name}${count + 1}`;
|
|
1521
|
+
}
|
|
1522
|
+
function findPcClose(xml, from) {
|
|
1523
|
+
const tags = /<pc\b[^>]*>|<\/pc\s*>/g;
|
|
1524
|
+
tags.lastIndex = from;
|
|
1525
|
+
let depth = 1;
|
|
1526
|
+
for (let match = tags.exec(xml); match; match = tags.exec(xml)) {
|
|
1527
|
+
depth += match[0].startsWith("</") ? -1 : 1;
|
|
1528
|
+
if (depth === 0) return {
|
|
1529
|
+
innerEnd: match.index,
|
|
1530
|
+
end: tags.lastIndex
|
|
1531
|
+
};
|
|
1532
|
+
}
|
|
1533
|
+
}
|
|
1534
|
+
function attr(attrs, name) {
|
|
1535
|
+
const value = new RegExp(`\\b${name}\\s*=\\s*"([^"]*)"`).exec(attrs)?.[1];
|
|
1536
|
+
return value === void 0 ? void 0 : unescapeXml(value);
|
|
1537
|
+
}
|
|
1538
|
+
//#endregion
|
|
1274
1539
|
//#region src/mobile.ts
|
|
1275
1540
|
function androidValuesDir(locale, sourceLocale) {
|
|
1276
1541
|
if (locale === sourceLocale) return "values";
|
|
@@ -1310,6 +1575,97 @@ function iosText(text) {
|
|
|
1310
1575
|
return text.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\t/g, "\\t");
|
|
1311
1576
|
}
|
|
1312
1577
|
//#endregion
|
|
1578
|
+
//#region src/po.ts
|
|
1579
|
+
function toPo(sourceLocale, locale, entries) {
|
|
1580
|
+
return [
|
|
1581
|
+
[
|
|
1582
|
+
"msgid \"\"",
|
|
1583
|
+
"msgstr \"\"",
|
|
1584
|
+
`${poString("Project-Id-Version: verbaly\n")}`,
|
|
1585
|
+
`${poString("MIME-Version: 1.0\n")}`,
|
|
1586
|
+
`${poString("Content-Type: text/plain; charset=UTF-8\n")}`,
|
|
1587
|
+
`${poString("Content-Transfer-Encoding: 8bit\n")}`,
|
|
1588
|
+
`${poString(`Language: ${locale}\n`)}`,
|
|
1589
|
+
`${poString(`X-Source-Language: ${sourceLocale}\n`)}`
|
|
1590
|
+
].join("\n"),
|
|
1591
|
+
...entries.map(({ key, source, target, location }) => [
|
|
1592
|
+
...location?.length ? [`#: ${location.join(" ")}`] : [],
|
|
1593
|
+
`msgctxt ${poString(key)}`,
|
|
1594
|
+
`msgid ${poString(source)}`,
|
|
1595
|
+
`msgstr ${poString(target)}`
|
|
1596
|
+
].join("\n")),
|
|
1597
|
+
""
|
|
1598
|
+
].join("\n\n");
|
|
1599
|
+
}
|
|
1600
|
+
function parsePo(content) {
|
|
1601
|
+
const entries = {};
|
|
1602
|
+
let locale;
|
|
1603
|
+
let msgctxt;
|
|
1604
|
+
let msgid;
|
|
1605
|
+
let msgstr;
|
|
1606
|
+
let fuzzy = false;
|
|
1607
|
+
let field;
|
|
1608
|
+
const finish = () => {
|
|
1609
|
+
if (msgid === "" && msgctxt === void 0) {
|
|
1610
|
+
const lang = /^Language:[ \t]*(.+?)[ \t]*$/m.exec(msgstr ?? "")?.[1];
|
|
1611
|
+
if (lang) locale = lang;
|
|
1612
|
+
} else if (msgctxt !== void 0 && msgid !== void 0) entries[msgctxt] = fuzzy ? "" : msgstr ?? "";
|
|
1613
|
+
msgctxt = msgid = msgstr = field = void 0;
|
|
1614
|
+
fuzzy = false;
|
|
1615
|
+
};
|
|
1616
|
+
for (const raw of content.replace(/^\uFEFF/, "").split(/\r\n|[\r\n]/)) {
|
|
1617
|
+
const line = raw.trim();
|
|
1618
|
+
if (line === "") {
|
|
1619
|
+
finish();
|
|
1620
|
+
continue;
|
|
1621
|
+
}
|
|
1622
|
+
if (line.startsWith("#")) {
|
|
1623
|
+
if (/^#,.*\bfuzzy\b/.test(line)) fuzzy = true;
|
|
1624
|
+
continue;
|
|
1625
|
+
}
|
|
1626
|
+
const keyword = /^(msgctxt|msgid_plural|msgid|msgstr(?:\[\d+\])?)\s+(.*)$/.exec(line);
|
|
1627
|
+
if (keyword) {
|
|
1628
|
+
const [, name, rest] = keyword;
|
|
1629
|
+
const value = poUnquote(rest);
|
|
1630
|
+
if (value === void 0) continue;
|
|
1631
|
+
if (name === "msgctxt") {
|
|
1632
|
+
msgctxt = value;
|
|
1633
|
+
field = "msgctxt";
|
|
1634
|
+
} else if (name === "msgid") {
|
|
1635
|
+
msgid = value;
|
|
1636
|
+
field = "msgid";
|
|
1637
|
+
} else if (name === "msgstr" || name === "msgstr[0]") {
|
|
1638
|
+
msgstr = value;
|
|
1639
|
+
field = "msgstr";
|
|
1640
|
+
} else field = "other";
|
|
1641
|
+
continue;
|
|
1642
|
+
}
|
|
1643
|
+
const continuation = poUnquote(line);
|
|
1644
|
+
if (continuation === void 0 || field === void 0 || field === "other") continue;
|
|
1645
|
+
if (field === "msgctxt") msgctxt = (msgctxt ?? "") + continuation;
|
|
1646
|
+
else if (field === "msgid") msgid = (msgid ?? "") + continuation;
|
|
1647
|
+
else msgstr = (msgstr ?? "") + continuation;
|
|
1648
|
+
}
|
|
1649
|
+
finish();
|
|
1650
|
+
return {
|
|
1651
|
+
locale,
|
|
1652
|
+
entries
|
|
1653
|
+
};
|
|
1654
|
+
}
|
|
1655
|
+
function poString(text) {
|
|
1656
|
+
return `"${text.replace(/\\/g, "\\\\").replace(/"/g, "\\\"").replace(/\n/g, "\\n").replace(/\t/g, "\\t").replace(/\r/g, "\\r")}"`;
|
|
1657
|
+
}
|
|
1658
|
+
function poUnquote(text) {
|
|
1659
|
+
const match = /^"((?:[^"\\]|\\.)*)"$/.exec(text);
|
|
1660
|
+
if (!match) return void 0;
|
|
1661
|
+
return match[1].replace(/\\(.)/g, (_, ch) => {
|
|
1662
|
+
if (ch === "n") return "\n";
|
|
1663
|
+
if (ch === "t") return " ";
|
|
1664
|
+
if (ch === "r") return "\r";
|
|
1665
|
+
return ch;
|
|
1666
|
+
});
|
|
1667
|
+
}
|
|
1668
|
+
//#endregion
|
|
1313
1669
|
//#region src/translate.ts
|
|
1314
1670
|
async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
1315
1671
|
const batchSize = options.batchSize ?? 20;
|
|
@@ -1349,6 +1705,12 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
1349
1705
|
}
|
|
1350
1706
|
return result;
|
|
1351
1707
|
}
|
|
1708
|
+
async function resolveProvider(cfg, model) {
|
|
1709
|
+
const configured = cfg.translate.provider;
|
|
1710
|
+
if (typeof configured === "function") return configured;
|
|
1711
|
+
const { claudeProvider } = await Promise.resolve().then(() => claude_exports);
|
|
1712
|
+
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1713
|
+
}
|
|
1352
1714
|
function structureMatches(source, translated) {
|
|
1353
1715
|
return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
|
|
1354
1716
|
}
|
|
@@ -1391,8 +1753,8 @@ function exportCatalogs(cfg, catalogs, options = {}) {
|
|
|
1391
1753
|
}));
|
|
1392
1754
|
const untranslated = all.filter((entry) => !entry.target);
|
|
1393
1755
|
const entries = options.missing ? untranslated : all;
|
|
1394
|
-
const path = join(dir, `${locale}.${format === "
|
|
1395
|
-
writeFileSync(path, format === "csv" ? toCsv(entries) : toXliff(cfg.sourceLocale, locale, entries));
|
|
1756
|
+
const path = join(dir, `${locale}.${format === "xliff" ? "xlf" : format}`);
|
|
1757
|
+
writeFileSync(path, format === "csv" ? toCsv(entries) : format === "po" ? toPo(cfg.sourceLocale, locale, entries) : toXliff(cfg.sourceLocale, locale, entries));
|
|
1396
1758
|
files.push({
|
|
1397
1759
|
locale,
|
|
1398
1760
|
path,
|
|
@@ -1480,7 +1842,14 @@ function parseExchangeFile(file, localeOverride) {
|
|
|
1480
1842
|
locale: localeOverride ?? basename(file).replace(/\.csv$/i, ""),
|
|
1481
1843
|
entries: parseCsv(content)
|
|
1482
1844
|
};
|
|
1483
|
-
|
|
1845
|
+
if (/\.po$/i.test(file)) {
|
|
1846
|
+
const parsed = parsePo(content);
|
|
1847
|
+
return {
|
|
1848
|
+
locale: localeOverride ?? parsed.locale ?? basename(file).replace(/\.po$/i, ""),
|
|
1849
|
+
entries: parsed.entries
|
|
1850
|
+
};
|
|
1851
|
+
}
|
|
1852
|
+
throw new Error(`[verbaly] ${file}: unsupported format, expected .xlf, .xliff, .csv or .po.`);
|
|
1484
1853
|
}
|
|
1485
1854
|
function toXliff(sourceLocale, locale, entries) {
|
|
1486
1855
|
const units = entries.map(({ key, source, target, location }) => [
|
|
@@ -1491,8 +1860,8 @@ function toXliff(sourceLocale, locale, entries) {
|
|
|
1491
1860
|
" </notes>"
|
|
1492
1861
|
] : [],
|
|
1493
1862
|
` <segment state="${target ? "translated" : "initial"}">`,
|
|
1494
|
-
` <source>${
|
|
1495
|
-
` <target>${
|
|
1863
|
+
` <source>${messageToInline(source)}</source>`,
|
|
1864
|
+
` <target>${messageToInline(target)}</target>`,
|
|
1496
1865
|
" </segment>",
|
|
1497
1866
|
" </unit>"
|
|
1498
1867
|
].join("\n")).join("\n");
|
|
@@ -1513,30 +1882,16 @@ function parseXliff(content) {
|
|
|
1513
1882
|
const id = /\bid\s*=\s*"([^"]*)"/.exec(match[1])?.[1];
|
|
1514
1883
|
if (!id) continue;
|
|
1515
1884
|
const target = /<target\b[^>]*>([\s\S]*?)<\/target>/.exec(match[2])?.[1];
|
|
1516
|
-
entries[unescapeXml(id)] = target === void 0 ? "" :
|
|
1885
|
+
entries[unescapeXml(id)] = target === void 0 ? "" : inlineToMessage(stripCdata(target));
|
|
1517
1886
|
}
|
|
1518
1887
|
return {
|
|
1519
1888
|
locale,
|
|
1520
1889
|
entries
|
|
1521
1890
|
};
|
|
1522
1891
|
}
|
|
1523
|
-
function escapeXml(text) {
|
|
1524
|
-
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
1525
|
-
}
|
|
1526
1892
|
function stripCdata(text) {
|
|
1527
1893
|
return text.replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, "$1");
|
|
1528
1894
|
}
|
|
1529
|
-
function unescapeXml(text) {
|
|
1530
|
-
return text.replace(/&(lt|gt|amp|quot|apos|#x?[0-9a-fA-F]+);/g, (_, entity) => {
|
|
1531
|
-
if (entity === "lt") return "<";
|
|
1532
|
-
if (entity === "gt") return ">";
|
|
1533
|
-
if (entity === "amp") return "&";
|
|
1534
|
-
if (entity === "quot") return "\"";
|
|
1535
|
-
if (entity === "apos") return "'";
|
|
1536
|
-
const code = entity.startsWith("#x") ? parseInt(entity.slice(2), 16) : parseInt(entity.slice(1), 10);
|
|
1537
|
-
return String.fromCodePoint(code);
|
|
1538
|
-
});
|
|
1539
|
-
}
|
|
1540
1895
|
function toCsv(entries) {
|
|
1541
1896
|
return [
|
|
1542
1897
|
"key,source,target,location",
|
|
@@ -1590,6 +1945,12 @@ function csvRows(content) {
|
|
|
1590
1945
|
}
|
|
1591
1946
|
//#endregion
|
|
1592
1947
|
//#region src/providers/claude.ts
|
|
1948
|
+
var claude_exports = /* @__PURE__ */ __exportAll({
|
|
1949
|
+
batchFormat: () => batchFormat,
|
|
1950
|
+
buildPrompt: () => buildPrompt,
|
|
1951
|
+
claudeProvider: () => claudeProvider,
|
|
1952
|
+
loadSdk: () => loadSdk
|
|
1953
|
+
});
|
|
1593
1954
|
const DEFAULT_MODEL = "claude-sonnet-5";
|
|
1594
1955
|
const SYSTEM = `You translate UI strings for the Verbaly i18n library.
|
|
1595
1956
|
Rules:
|
|
@@ -1758,7 +2119,8 @@ async function renderSite(cfg, options = {}) {
|
|
|
1758
2119
|
const attribute = options.attribute ?? cfg.render.attribute;
|
|
1759
2120
|
const baseUrl = (options.baseUrl ?? cfg.render.baseUrl)?.replace(/\/+$/, "");
|
|
1760
2121
|
const wantHreflang = (options.hreflang ?? cfg.render.hreflang ?? true) && baseUrl !== void 0;
|
|
1761
|
-
const
|
|
2122
|
+
const sitemap = options.sitemap ?? cfg.render.sitemap ?? false;
|
|
2123
|
+
const wantSitemap = sitemap !== false && baseUrl !== void 0;
|
|
1762
2124
|
const clean = options.clean ?? cfg.render.clean ?? false;
|
|
1763
2125
|
const catalogs = loadCatalogs(cfg);
|
|
1764
2126
|
if (clean) {
|
|
@@ -1801,7 +2163,7 @@ async function renderSite(cfg, options = {}) {
|
|
|
1801
2163
|
writeFileSync(out, result.html);
|
|
1802
2164
|
}
|
|
1803
2165
|
}
|
|
1804
|
-
if (wantSitemap && urls.length) writeFileSync(join(site, typeof
|
|
2166
|
+
if (wantSitemap && urls.length) writeFileSync(join(site, typeof sitemap === "string" ? sitemap : "sitemap-i18n.xml"), buildSitemap(urls));
|
|
1805
2167
|
return {
|
|
1806
2168
|
files: files.length,
|
|
1807
2169
|
locales: [...locales],
|
|
@@ -2199,6 +2561,6 @@ function line(node) {
|
|
|
2199
2561
|
return node.loc?.start?.line ?? 1;
|
|
2200
2562
|
}
|
|
2201
2563
|
//#endregion
|
|
2202
|
-
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, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
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, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
2203
2565
|
|
|
2204
2566
|
//# sourceMappingURL=index.js.map
|