@verbaly/compiler 0.65.0 → 0.67.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 +50 -9
- package/dist/{claude--aMMLA-N.js → claude-BmD8W9uW.js} +29 -8
- package/dist/claude-BmD8W9uW.js.map +1 -0
- package/dist/{claude-arq_js_3.js → claude-D-j-ZiYH.js} +29 -8
- package/dist/claude-D-j-ZiYH.js.map +1 -0
- package/dist/cli.js +643 -204
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +69 -8
- package/dist/index.js +499 -119
- package/dist/index.js.map +1 -1
- package/dist/{config-DYlbHrPc.js → text-M4XbWU6N.js} +17 -3
- package/dist/text-M4XbWU6N.js.map +1 -0
- package/package.json +4 -4
- package/dist/claude--aMMLA-N.js.map +0 -1
- package/dist/claude-arq_js_3.js.map +0 -1
- package/dist/config-DYlbHrPc.js.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
3
3
|
import { parseArgs } from "node:util";
|
|
4
|
-
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync,
|
|
4
|
+
import { existsSync, lstatSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
5
5
|
import { CATALOG_SCRIPT, LOCALE_STORAGE_KEY, RICH_TAGS, VOID_TAGS, alternateLinks, createVerbaly, flatten, localeDirection, normalizeLink, parse, parseIcu, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
|
+
import { createHash } from "node:crypto";
|
|
7
8
|
import { glob } from "tinyglobby";
|
|
8
9
|
import { parse as parse$1 } from "@babel/parser";
|
|
9
|
-
import { createHash } from "node:crypto";
|
|
10
10
|
import MagicString from "magic-string";
|
|
11
11
|
import picomatch from "picomatch";
|
|
12
12
|
//#region src/catalog.ts
|
|
13
|
+
function emptyCatalog() {
|
|
14
|
+
return Object.create(null);
|
|
15
|
+
}
|
|
16
|
+
function own(catalog, key) {
|
|
17
|
+
return Object.hasOwn(catalog, key) ? catalog[key] : void 0;
|
|
18
|
+
}
|
|
13
19
|
function catalogPath(cfg, locale) {
|
|
14
20
|
return join(cfg.dir, `${locale}.json`);
|
|
15
21
|
}
|
|
@@ -36,8 +42,9 @@ function readTree(cfg, locale) {
|
|
|
36
42
|
return {};
|
|
37
43
|
}
|
|
38
44
|
const parsed = parseTree(content);
|
|
39
|
-
if (parsed)
|
|
40
|
-
throw new Error(`[verbaly] ${catalogPath(cfg, locale)} is not
|
|
45
|
+
if (parsed === void 0) throw new Error(`[verbaly] ${catalogPath(cfg, locale)} is not valid JSON, fix or delete the file`);
|
|
46
|
+
if (!isTree(parsed)) throw new Error(`[verbaly] ${catalogPath(cfg, locale)} is not a JSON object of messages, fix or delete the file`);
|
|
47
|
+
return parsed;
|
|
41
48
|
}
|
|
42
49
|
function parseTree(content) {
|
|
43
50
|
const body = content.charCodeAt(0) === 65279 ? content.slice(1) : content;
|
|
@@ -47,6 +54,9 @@ function parseTree(content) {
|
|
|
47
54
|
return;
|
|
48
55
|
}
|
|
49
56
|
}
|
|
57
|
+
function isTree(value) {
|
|
58
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
59
|
+
}
|
|
50
60
|
function loadCatalogs(cfg) {
|
|
51
61
|
const catalogs = {};
|
|
52
62
|
for (const locale of cfg.locales) catalogs[locale] = readCatalog(cfg, locale);
|
|
@@ -116,7 +126,8 @@ function writeCatalog(cfg, locale, catalog) {
|
|
|
116
126
|
} catch {
|
|
117
127
|
mkdirSync(cfg.dir, { recursive: true });
|
|
118
128
|
}
|
|
119
|
-
const
|
|
129
|
+
const parsed = existing === void 0 ? void 0 : parseTree(existing);
|
|
130
|
+
const previous = isTree(parsed) ? parsed : void 0;
|
|
120
131
|
const serialized = serializeCatalog(catalog, wantsNesting(cfg, locale, existing, previous), previous);
|
|
121
132
|
if (existing === serialized) return serialized;
|
|
122
133
|
writeFileSync(path, serialized);
|
|
@@ -241,7 +252,7 @@ function matchBrace$1(message, start) {
|
|
|
241
252
|
}
|
|
242
253
|
function pseudoCatalogs(cfg, catalogs, locale = PSEUDO_LOCALE) {
|
|
243
254
|
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
244
|
-
const target =
|
|
255
|
+
const target = emptyCatalog();
|
|
245
256
|
for (const [key, msg] of Object.entries(source)) target[key] = msg ? pseudoLocalize(msg) : "";
|
|
246
257
|
catalogs[locale] = target;
|
|
247
258
|
return Object.keys(target).filter((key) => target[key]);
|
|
@@ -294,9 +305,18 @@ async function loadConfigFile(root) {
|
|
|
294
305
|
if (existsSync(path)) return loadTsConfig(path);
|
|
295
306
|
}
|
|
296
307
|
const jsonPath = join(root, "verbaly.config.json");
|
|
297
|
-
if (existsSync(jsonPath)) return
|
|
308
|
+
if (existsSync(jsonPath)) return readJsonConfig(jsonPath);
|
|
298
309
|
return {};
|
|
299
310
|
}
|
|
311
|
+
function readJsonConfig(path) {
|
|
312
|
+
const content = readFileSync(path, "utf8");
|
|
313
|
+
const body = content.charCodeAt(0) === 65279 ? content.slice(1) : content;
|
|
314
|
+
try {
|
|
315
|
+
return JSON.parse(body);
|
|
316
|
+
} catch (error) {
|
|
317
|
+
throw new Error(`[verbaly] ${path} is not valid JSON: ${error.message}`, { cause: error });
|
|
318
|
+
}
|
|
319
|
+
}
|
|
300
320
|
async function loadTsConfig(path) {
|
|
301
321
|
try {
|
|
302
322
|
const { bundleRequire } = await import("bundle-require");
|
|
@@ -501,6 +521,213 @@ async function init(input = {}) {
|
|
|
501
521
|
};
|
|
502
522
|
}
|
|
503
523
|
//#endregion
|
|
524
|
+
//#region src/location.ts
|
|
525
|
+
function lineAt(content, offset) {
|
|
526
|
+
let line = 1;
|
|
527
|
+
const end = Math.min(offset, content.length);
|
|
528
|
+
for (let i = 0; i < end; i++) if (content[i] === "\n") line += 1;
|
|
529
|
+
return line;
|
|
530
|
+
}
|
|
531
|
+
function createLocator() {
|
|
532
|
+
const contents = /* @__PURE__ */ new Map();
|
|
533
|
+
return (file, offset) => {
|
|
534
|
+
if (!contents.has(file)) try {
|
|
535
|
+
contents.set(file, readFileSync(file, "utf8"));
|
|
536
|
+
} catch {
|
|
537
|
+
contents.set(file, void 0);
|
|
538
|
+
}
|
|
539
|
+
const content = contents.get(file);
|
|
540
|
+
return content === void 0 ? void 0 : lineAt(content, offset);
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
//#endregion
|
|
544
|
+
//#region src/key.ts
|
|
545
|
+
function stableKey(message) {
|
|
546
|
+
return createHash("sha256").update(message).digest("base64url").slice(0, 8);
|
|
547
|
+
}
|
|
548
|
+
//#endregion
|
|
549
|
+
//#region src/state.ts
|
|
550
|
+
const STATE_FILE = ".verbaly-state.json";
|
|
551
|
+
const LEGACY_DRAFTS_FILE = ".verbaly-drafts.json";
|
|
552
|
+
function statePath(cfg, file = STATE_FILE) {
|
|
553
|
+
return join(cfg.dir, file);
|
|
554
|
+
}
|
|
555
|
+
function loadState(cfg) {
|
|
556
|
+
const current = readJson(statePath(cfg));
|
|
557
|
+
if (current !== void 0) {
|
|
558
|
+
if (!isObject(current)) throw corrupt(statePath(cfg));
|
|
559
|
+
return {
|
|
560
|
+
drafts: readDrafts(current.drafts),
|
|
561
|
+
fingerprints: readFingerprints(current.fingerprints)
|
|
562
|
+
};
|
|
563
|
+
}
|
|
564
|
+
const legacy = readJson(statePath(cfg, LEGACY_DRAFTS_FILE));
|
|
565
|
+
if (legacy === void 0) return {
|
|
566
|
+
drafts: {},
|
|
567
|
+
fingerprints: {}
|
|
568
|
+
};
|
|
569
|
+
if (!isObject(legacy)) throw corrupt(statePath(cfg, LEGACY_DRAFTS_FILE));
|
|
570
|
+
return {
|
|
571
|
+
drafts: readDrafts(legacy),
|
|
572
|
+
fingerprints: {}
|
|
573
|
+
};
|
|
574
|
+
}
|
|
575
|
+
function saveState(cfg, state) {
|
|
576
|
+
const serialized = serializeState(state);
|
|
577
|
+
const path = statePath(cfg);
|
|
578
|
+
const legacy = statePath(cfg, LEGACY_DRAFTS_FILE);
|
|
579
|
+
if (serialized === void 0) {
|
|
580
|
+
rmSync(path, { force: true });
|
|
581
|
+
rmSync(legacy, { force: true });
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
let existing;
|
|
585
|
+
try {
|
|
586
|
+
existing = readFileSync(path, "utf8");
|
|
587
|
+
} catch {
|
|
588
|
+
mkdirSync(cfg.dir, { recursive: true });
|
|
589
|
+
}
|
|
590
|
+
if (existing !== serialized) writeFileSync(path, serialized);
|
|
591
|
+
if (existsSync(legacy)) rmSync(legacy);
|
|
592
|
+
}
|
|
593
|
+
function updateState(cfg, catalogs) {
|
|
594
|
+
const state = loadState(cfg);
|
|
595
|
+
const next = {
|
|
596
|
+
drafts: effectiveDrafts(state.drafts, catalogs),
|
|
597
|
+
fingerprints: refreshFingerprints(cfg, catalogs, state.fingerprints)
|
|
598
|
+
};
|
|
599
|
+
saveState(cfg, next);
|
|
600
|
+
return next;
|
|
601
|
+
}
|
|
602
|
+
function effectiveDrafts(drafts, catalogs) {
|
|
603
|
+
const out = {};
|
|
604
|
+
for (const [locale, keys] of Object.entries(drafts)) {
|
|
605
|
+
const catalog = catalogs[locale] ?? {};
|
|
606
|
+
const live = keys.filter((key) => own(catalog, key));
|
|
607
|
+
if (live.length) out[locale] = live;
|
|
608
|
+
}
|
|
609
|
+
return out;
|
|
610
|
+
}
|
|
611
|
+
function fingerprint(text) {
|
|
612
|
+
return stableKey(text);
|
|
613
|
+
}
|
|
614
|
+
function tracked(key, text) {
|
|
615
|
+
return stableKey(text) !== key;
|
|
616
|
+
}
|
|
617
|
+
function stamp(source, translated) {
|
|
618
|
+
return `${fingerprint(source)}.${fingerprint(translated)}`;
|
|
619
|
+
}
|
|
620
|
+
function refreshFingerprints(cfg, catalogs, previous) {
|
|
621
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
622
|
+
const out = {};
|
|
623
|
+
for (const locale of targetLocales(cfg)) {
|
|
624
|
+
const catalog = catalogs[locale] ?? {};
|
|
625
|
+
const before = previous[locale] ?? {};
|
|
626
|
+
const after = emptyCatalog();
|
|
627
|
+
for (const [key, text] of Object.entries(source)) {
|
|
628
|
+
const translated = own(catalog, key);
|
|
629
|
+
if (!text || !translated || !tracked(key, text)) continue;
|
|
630
|
+
const was = own(before, key);
|
|
631
|
+
after[key] = was !== void 0 && translationOf(was) === fingerprint(translated) ? was : stamp(text, translated);
|
|
632
|
+
}
|
|
633
|
+
if (Object.keys(after).length) out[locale] = after;
|
|
634
|
+
}
|
|
635
|
+
return out;
|
|
636
|
+
}
|
|
637
|
+
function outdatedTranslations(cfg, catalogs, fingerprints) {
|
|
638
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
639
|
+
const out = [];
|
|
640
|
+
for (const locale of targetLocales(cfg)) {
|
|
641
|
+
const catalog = catalogs[locale] ?? {};
|
|
642
|
+
for (const [key, was] of Object.entries(fingerprints[locale] ?? {})) {
|
|
643
|
+
const text = own(source, key);
|
|
644
|
+
const translated = own(catalog, key);
|
|
645
|
+
if (!text || !translated) continue;
|
|
646
|
+
if (translationOf(was) !== fingerprint(translated)) continue;
|
|
647
|
+
if (sourceOf(was) !== fingerprint(text)) out.push({
|
|
648
|
+
locale,
|
|
649
|
+
key
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return out.sort((a, b) => a.locale.localeCompare(b.locale) || a.key.localeCompare(b.key));
|
|
654
|
+
}
|
|
655
|
+
function acceptOutdated(cfg, catalogs, fingerprints, entries) {
|
|
656
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
657
|
+
for (const { locale, key } of entries) {
|
|
658
|
+
const text = own(source, key);
|
|
659
|
+
const translated = own(catalogs[locale] ?? {}, key);
|
|
660
|
+
if (!text || !translated) continue;
|
|
661
|
+
(fingerprints[locale] ??= emptyCatalog())[key] = stamp(text, translated);
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
function sourceOf(stamped) {
|
|
665
|
+
return stamped.slice(0, stamped.indexOf("."));
|
|
666
|
+
}
|
|
667
|
+
function translationOf(stamped) {
|
|
668
|
+
return stamped.slice(stamped.indexOf(".") + 1);
|
|
669
|
+
}
|
|
670
|
+
function serializeState(state) {
|
|
671
|
+
const drafts = {};
|
|
672
|
+
for (const locale of Object.keys(state.drafts).sort()) {
|
|
673
|
+
const keys = [...new Set(state.drafts[locale])].sort();
|
|
674
|
+
if (keys.length) drafts[locale] = keys;
|
|
675
|
+
}
|
|
676
|
+
const fingerprints = {};
|
|
677
|
+
for (const locale of Object.keys(state.fingerprints).sort()) {
|
|
678
|
+
const entries = state.fingerprints[locale] ?? {};
|
|
679
|
+
const keys = Object.keys(entries).sort();
|
|
680
|
+
if (!keys.length) continue;
|
|
681
|
+
fingerprints[locale] = Object.fromEntries(keys.map((key) => [key, entries[key]]));
|
|
682
|
+
}
|
|
683
|
+
const out = {};
|
|
684
|
+
if (Object.keys(drafts).length) out.drafts = drafts;
|
|
685
|
+
if (Object.keys(fingerprints).length) out.fingerprints = fingerprints;
|
|
686
|
+
if (!out.drafts && !out.fingerprints) return void 0;
|
|
687
|
+
return JSON.stringify(out, null, 2) + "\n";
|
|
688
|
+
}
|
|
689
|
+
function readJson(path) {
|
|
690
|
+
let content;
|
|
691
|
+
try {
|
|
692
|
+
content = readFileSync(path, "utf8");
|
|
693
|
+
} catch {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
try {
|
|
697
|
+
return JSON.parse(content.charCodeAt(0) === 65279 ? content.slice(1) : content);
|
|
698
|
+
} catch (error) {
|
|
699
|
+
throw corrupt(path, error);
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function corrupt(path, cause) {
|
|
703
|
+
return new Error(`[verbaly] ${path} is not valid JSON, fix or delete the file`, { cause });
|
|
704
|
+
}
|
|
705
|
+
function isObject(value) {
|
|
706
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
707
|
+
}
|
|
708
|
+
function readDrafts(value) {
|
|
709
|
+
const out = {};
|
|
710
|
+
if (!isObject(value)) return out;
|
|
711
|
+
for (const [locale, keys] of Object.entries(value)) if (Array.isArray(keys)) out[locale] = keys.filter((key) => typeof key === "string");
|
|
712
|
+
return out;
|
|
713
|
+
}
|
|
714
|
+
function readFingerprints(value) {
|
|
715
|
+
const out = {};
|
|
716
|
+
if (!isObject(value)) return out;
|
|
717
|
+
for (const [locale, entries] of Object.entries(value)) {
|
|
718
|
+
if (!isObject(entries)) continue;
|
|
719
|
+
const kept = emptyCatalog();
|
|
720
|
+
for (const [key, stamped] of Object.entries(entries)) if (typeof stamped === "string" && stamped.includes(".")) kept[key] = stamped;
|
|
721
|
+
out[locale] = kept;
|
|
722
|
+
}
|
|
723
|
+
return out;
|
|
724
|
+
}
|
|
725
|
+
//#endregion
|
|
726
|
+
//#region src/text.ts
|
|
727
|
+
function counted(count, noun, many = `${noun}s`) {
|
|
728
|
+
return `${count} ${count === 1 ? noun : many}`;
|
|
729
|
+
}
|
|
730
|
+
//#endregion
|
|
504
731
|
//#region src/validate.ts
|
|
505
732
|
const read = (message) => parse(message, parseIcu);
|
|
506
733
|
const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
|
|
@@ -653,7 +880,7 @@ function reachableCategories(locale, ordinal) {
|
|
|
653
880
|
if (hit) return hit;
|
|
654
881
|
let out;
|
|
655
882
|
try {
|
|
656
|
-
const rules = new Intl.PluralRules(locale, { type });
|
|
883
|
+
const rules = new Intl.PluralRules(locale.replace(/_/g, "-"), { type });
|
|
657
884
|
const seen = /* @__PURE__ */ new Set();
|
|
658
885
|
for (let n = 0; n <= COUNT_PROBE; n++) seen.add(rules.select(n));
|
|
659
886
|
out = [...seen];
|
|
@@ -668,8 +895,14 @@ function reachableCategories(locale, ordinal) {
|
|
|
668
895
|
function gatePasses(result) {
|
|
669
896
|
return result.missing.length === 0 && result.unknown.length === 0 && !result.broken.some((entry) => entry.severity === "error");
|
|
670
897
|
}
|
|
671
|
-
function check(cfg, catalogs, registry) {
|
|
898
|
+
function check(cfg, catalogs, registry, fingerprints = {}) {
|
|
672
899
|
const extracted = registry.messages();
|
|
900
|
+
const locate = createLocator();
|
|
901
|
+
const site = (msg) => ({
|
|
902
|
+
file: msg.file,
|
|
903
|
+
line: locate(msg.file, msg.start),
|
|
904
|
+
message: msg.message
|
|
905
|
+
});
|
|
673
906
|
const flat = {};
|
|
674
907
|
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
675
908
|
const source = flat[cfg.sourceLocale] ?? {};
|
|
@@ -703,8 +936,17 @@ function check(cfg, catalogs, registry) {
|
|
|
703
936
|
issue: issue.message
|
|
704
937
|
});
|
|
705
938
|
};
|
|
706
|
-
const sourceText =
|
|
707
|
-
|
|
939
|
+
const sourceText = Object.assign(emptyCatalog(), source);
|
|
940
|
+
const divergent = [];
|
|
941
|
+
for (const [key, entry] of extracted) {
|
|
942
|
+
const written = sourceText[key];
|
|
943
|
+
if (written && written !== entry.message) divergent.push({
|
|
944
|
+
key,
|
|
945
|
+
catalog: written,
|
|
946
|
+
code: site(entry)
|
|
947
|
+
});
|
|
948
|
+
sourceText[key] = entry.message;
|
|
949
|
+
}
|
|
708
950
|
for (const [key, text] of Object.entries(sourceText)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
709
951
|
for (const locale of cfg.locales) {
|
|
710
952
|
if (locale === cfg.sourceLocale) continue;
|
|
@@ -727,6 +969,8 @@ function check(cfg, catalogs, registry) {
|
|
|
727
969
|
});
|
|
728
970
|
}
|
|
729
971
|
}
|
|
972
|
+
const collisions = collisionEntries(registry, locate);
|
|
973
|
+
const outdated = outdatedTranslations(cfg, flat, fingerprints);
|
|
730
974
|
return {
|
|
731
975
|
ok: gatePasses({
|
|
732
976
|
missing,
|
|
@@ -736,7 +980,10 @@ function check(cfg, catalogs, registry) {
|
|
|
736
980
|
missing,
|
|
737
981
|
unknown,
|
|
738
982
|
broken,
|
|
739
|
-
extra
|
|
983
|
+
extra,
|
|
984
|
+
collisions,
|
|
985
|
+
divergent,
|
|
986
|
+
outdated
|
|
740
987
|
};
|
|
741
988
|
}
|
|
742
989
|
function formatCheckResult(result, root) {
|
|
@@ -768,9 +1015,33 @@ function checkNextSteps(result, cliReachable = true) {
|
|
|
768
1015
|
if (steps.length > 0 && !cliReachable) steps.push(`the verbaly command is not linked in this project: ${CLI_INSTALL_FIX}`);
|
|
769
1016
|
return steps.join("\n");
|
|
770
1017
|
}
|
|
771
|
-
function
|
|
1018
|
+
function collisionEntries(registry, locate = createLocator()) {
|
|
1019
|
+
return registry.collisions().map(({ key, kept, dropped }) => ({
|
|
1020
|
+
key,
|
|
1021
|
+
sites: [kept, ...dropped].map((msg) => ({
|
|
1022
|
+
file: msg.file,
|
|
1023
|
+
line: locate(msg.file, msg.start),
|
|
1024
|
+
message: msg.message
|
|
1025
|
+
}))
|
|
1026
|
+
}));
|
|
1027
|
+
}
|
|
1028
|
+
function sourcePlace(at, root) {
|
|
1029
|
+
const file = root ? relative(root, at.file).replaceAll("\\", "/") : at.file;
|
|
1030
|
+
return at.line ? `${file}:${at.line}` : file;
|
|
1031
|
+
}
|
|
1032
|
+
function quoted(at, root) {
|
|
1033
|
+
return `"${truncate(at.message, 40)}" (${sourcePlace(at, root)})`;
|
|
1034
|
+
}
|
|
1035
|
+
function formatCollision({ key, sites }, root) {
|
|
1036
|
+
const [kept, ...others] = sites;
|
|
1037
|
+
return ` ${key}: one key with ${counted(sites.length, "text")}, and every place shows ${quoted(kept, root)}, never ${others.map((at) => quoted(at, root)).join(" or ")}: give each text its own key`;
|
|
1038
|
+
}
|
|
1039
|
+
function formatCheckWarnings(result, root) {
|
|
772
1040
|
const lines = result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
773
1041
|
for (const entry of result.extra) lines.push(entry.files.length > 0 ? ` [${entry.locale}] ${entry.key}: only this translation has it, so the source language shows the key itself` : ` [${entry.locale}] ${entry.key}: only this translation has it, and no code reads it`);
|
|
1042
|
+
for (const entry of result.collisions) lines.push(formatCollision(entry, root));
|
|
1043
|
+
for (const { key, catalog, code } of result.divergent) lines.push(` ${key}: the source catalog says "${truncate(catalog, 40)}" and the code says ${quoted(code, root)}: the code wins, so edit the text there (extract writes it into the catalog)`);
|
|
1044
|
+
for (const { locale, key } of result.outdated) lines.push(` [${locale}] ${key}: translated from an older source text, update it or keep it with \`npx verbaly review --approve\``);
|
|
774
1045
|
return lines.join("\n");
|
|
775
1046
|
}
|
|
776
1047
|
function truncate(text, max) {
|
|
@@ -778,15 +1049,9 @@ function truncate(text, max) {
|
|
|
778
1049
|
}
|
|
779
1050
|
function githubCheckAnnotations(result, registry, root) {
|
|
780
1051
|
const messages = registry.messages();
|
|
781
|
-
const
|
|
782
|
-
const
|
|
783
|
-
|
|
784
|
-
contents.set(file, readFileSync(file, "utf8"));
|
|
785
|
-
} catch {
|
|
786
|
-
contents.set(file, void 0);
|
|
787
|
-
}
|
|
788
|
-
return contents.get(file);
|
|
789
|
-
};
|
|
1052
|
+
const locate = createLocator();
|
|
1053
|
+
const rel = (file) => escapeProperty(relative(root, file).replaceAll("\\", "/"));
|
|
1054
|
+
const at = (file, line) => `file=${rel(file)}${line ? `,line=${line}` : ""}`;
|
|
790
1055
|
const lines = [];
|
|
791
1056
|
const byKey = /* @__PURE__ */ new Map();
|
|
792
1057
|
for (const entry of result.missing) {
|
|
@@ -801,17 +1066,13 @@ function githubCheckAnnotations(result, registry, root) {
|
|
|
801
1066
|
const origin = messages.get(entry.key);
|
|
802
1067
|
const hint = entry.source ? `: "${truncate(entry.source, 60)}"` : "";
|
|
803
1068
|
const text = escapeData(`missing [${entry.locales.join(", ")}] ${entry.key}${hint}`);
|
|
804
|
-
if (origin) {
|
|
805
|
-
|
|
806
|
-
const content = readSource(origin.file);
|
|
807
|
-
const line = content === void 0 ? void 0 : lineAt(content, origin.start);
|
|
808
|
-
lines.push(`::error file=${escapeProperty(file)}${line ? `,line=${line}` : ""}::${text}`);
|
|
809
|
-
} else lines.push(`::error::${text}`);
|
|
1069
|
+
if (origin) lines.push(`::error ${at(origin.file, locate(origin.file, origin.start))}::${text}`);
|
|
1070
|
+
else lines.push(`::error::${text}`);
|
|
810
1071
|
}
|
|
811
1072
|
for (const entry of result.unknown) {
|
|
812
1073
|
const text = escapeData(`unknown key "${entry.key}" (not in any catalog)`);
|
|
813
1074
|
const file = entry.files[0];
|
|
814
|
-
lines.push(file ? `::error file=${
|
|
1075
|
+
lines.push(file ? `::error file=${rel(file)}::${text}` : `::error::${text}`);
|
|
815
1076
|
}
|
|
816
1077
|
for (const entry of result.broken) {
|
|
817
1078
|
const origin = messages.get(entry.key);
|
|
@@ -821,24 +1082,27 @@ function githubCheckAnnotations(result, registry, root) {
|
|
|
821
1082
|
lines.push(`::${command}::${text}`);
|
|
822
1083
|
continue;
|
|
823
1084
|
}
|
|
824
|
-
|
|
825
|
-
const content = readSource(origin.file);
|
|
826
|
-
const line = content === void 0 ? void 0 : lineAt(content, origin.start);
|
|
827
|
-
lines.push(`::${command} file=${escapeProperty(file)}${line ? `,line=${line}` : ""}::${text}`);
|
|
1085
|
+
lines.push(`::${command} ${at(origin.file, locate(origin.file, origin.start))}::${text}`);
|
|
828
1086
|
}
|
|
829
1087
|
for (const entry of result.extra) {
|
|
830
1088
|
const text = escapeData(`[${entry.locale}] ${entry.key}: only this translation has it`);
|
|
831
1089
|
const file = entry.files[0];
|
|
832
|
-
lines.push(file ? `::warning file=${
|
|
1090
|
+
lines.push(file ? `::warning file=${rel(file)}::${text}` : `::warning::${text}`);
|
|
1091
|
+
}
|
|
1092
|
+
for (const { key, sites } of result.collisions) {
|
|
1093
|
+
const text = escapeData(`"${key}" has ${counted(sites.length, "text")}, and every place shows the first one`);
|
|
1094
|
+
for (const place of sites) lines.push(`::warning ${at(place.file, place.line)}::${text}`);
|
|
1095
|
+
}
|
|
1096
|
+
for (const { key, code } of result.divergent) {
|
|
1097
|
+
const text = escapeData(`"${key}": the source catalog has another text, the code's one ships`);
|
|
1098
|
+
lines.push(`::warning ${at(code.file, code.line)}::${text}`);
|
|
1099
|
+
}
|
|
1100
|
+
for (const { locale, key } of result.outdated) {
|
|
1101
|
+
const text = escapeData(`[${locale}] ${key}: translated from an older source text`);
|
|
1102
|
+
lines.push(`::warning::${text}`);
|
|
833
1103
|
}
|
|
834
1104
|
return lines;
|
|
835
1105
|
}
|
|
836
|
-
function lineAt(content, offset) {
|
|
837
|
-
let line = 1;
|
|
838
|
-
const end = Math.min(offset, content.length);
|
|
839
|
-
for (let i = 0; i < end; i++) if (content[i] === "\n") line += 1;
|
|
840
|
-
return line;
|
|
841
|
-
}
|
|
842
1106
|
function escapeData(text) {
|
|
843
1107
|
return text.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");
|
|
844
1108
|
}
|
|
@@ -959,7 +1223,9 @@ declare module 'virtual:verbaly/locale/*' {
|
|
|
959
1223
|
`;
|
|
960
1224
|
}
|
|
961
1225
|
function writeDts(cfg, catalog, file) {
|
|
962
|
-
file
|
|
1226
|
+
const target = file === void 0 ? cfg.dts : file;
|
|
1227
|
+
if (target === false) return;
|
|
1228
|
+
file = target ?? join(cfg.root, "verbaly.d.ts");
|
|
963
1229
|
const content = generateDts(catalog, { inlineCatalog: cfg.render.inlineCatalog === true });
|
|
964
1230
|
try {
|
|
965
1231
|
if (readFileSync(file, "utf8") === content) return;
|
|
@@ -968,11 +1234,6 @@ function writeDts(cfg, catalog, file) {
|
|
|
968
1234
|
writeFileSync(file, content);
|
|
969
1235
|
}
|
|
970
1236
|
//#endregion
|
|
971
|
-
//#region src/text.ts
|
|
972
|
-
function counted(count, noun, many = `${noun}s`) {
|
|
973
|
-
return `${count} ${count === 1 ? noun : many}`;
|
|
974
|
-
}
|
|
975
|
-
//#endregion
|
|
976
1237
|
//#region src/bundle.ts
|
|
977
1238
|
function isExcluded(key, prefixes) {
|
|
978
1239
|
return prefixes.some((prefix) => key === prefix || key.startsWith(`${prefix}.`));
|
|
@@ -1006,14 +1267,6 @@ function formatBundleIssue(issue) {
|
|
|
1006
1267
|
return `bundle.exclude "${issue.prefix}" ${issue.problem}`;
|
|
1007
1268
|
}
|
|
1008
1269
|
//#endregion
|
|
1009
|
-
//#region src/warn.ts
|
|
1010
|
-
const warned = /* @__PURE__ */ new Set();
|
|
1011
|
-
function warnOnce(message, dedupeKey = message) {
|
|
1012
|
-
if (warned.has(dedupeKey)) return;
|
|
1013
|
-
warned.add(dedupeKey);
|
|
1014
|
-
console.warn(`[verbaly] ${message}`);
|
|
1015
|
-
}
|
|
1016
|
-
//#endregion
|
|
1017
1270
|
//#region src/registry.ts
|
|
1018
1271
|
var MessageRegistry = class {
|
|
1019
1272
|
files = /* @__PURE__ */ new Map();
|
|
@@ -1025,15 +1278,21 @@ var MessageRegistry = class {
|
|
|
1025
1278
|
}
|
|
1026
1279
|
messages() {
|
|
1027
1280
|
const out = /* @__PURE__ */ new Map();
|
|
1281
|
+
for (const analysis of this.files.values()) for (const msg of analysis.tagged) if (!out.has(msg.key)) out.set(msg.key, msg);
|
|
1282
|
+
return out;
|
|
1283
|
+
}
|
|
1284
|
+
collisions() {
|
|
1285
|
+
const seen = /* @__PURE__ */ new Map();
|
|
1028
1286
|
for (const analysis of this.files.values()) for (const msg of analysis.tagged) {
|
|
1029
|
-
const
|
|
1030
|
-
if (
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1287
|
+
const found = seen.get(msg.key);
|
|
1288
|
+
if (!found) seen.set(msg.key, {
|
|
1289
|
+
key: msg.key,
|
|
1290
|
+
kept: msg,
|
|
1291
|
+
dropped: []
|
|
1292
|
+
});
|
|
1293
|
+
else if (msg.message !== found.kept.message && !found.dropped.some((other) => other.message === msg.message)) found.dropped.push(msg);
|
|
1035
1294
|
}
|
|
1036
|
-
return
|
|
1295
|
+
return [...seen.values()].filter((entry) => entry.dropped.length > 0);
|
|
1037
1296
|
}
|
|
1038
1297
|
usedKeys(strict = false) {
|
|
1039
1298
|
const out = /* @__PURE__ */ new Map();
|
|
@@ -1048,6 +1307,9 @@ var MessageRegistry = class {
|
|
|
1048
1307
|
strayImports() {
|
|
1049
1308
|
return [...this.files.values()].flatMap((analysis) => analysis.strayImports);
|
|
1050
1309
|
}
|
|
1310
|
+
missed() {
|
|
1311
|
+
return [...this.files.values()].flatMap((analysis) => analysis.missed ?? []);
|
|
1312
|
+
}
|
|
1051
1313
|
parseErrors() {
|
|
1052
1314
|
const out = [];
|
|
1053
1315
|
for (const [file, analysis] of this.files) if (analysis.parseError) out.push({
|
|
@@ -1067,11 +1329,6 @@ var MessageRegistry = class {
|
|
|
1067
1329
|
}
|
|
1068
1330
|
};
|
|
1069
1331
|
//#endregion
|
|
1070
|
-
//#region src/key.ts
|
|
1071
|
-
function stableKey(message) {
|
|
1072
|
-
return createHash("sha256").update(message).digest("base64url").slice(0, 8);
|
|
1073
|
-
}
|
|
1074
|
-
//#endregion
|
|
1075
1332
|
//#region src/analyze.ts
|
|
1076
1333
|
function parseErrorMessage(error) {
|
|
1077
1334
|
return error instanceof Error ? error.message : String(error);
|
|
@@ -1096,12 +1353,22 @@ function analyze(code, file, options = {}) {
|
|
|
1096
1353
|
const tagged = [];
|
|
1097
1354
|
const usedKeys = [];
|
|
1098
1355
|
const strayImports = [];
|
|
1356
|
+
const missed = [];
|
|
1357
|
+
const renamed = renamedBindings(ast.program, names);
|
|
1099
1358
|
walk(ast.program, (node) => {
|
|
1100
1359
|
if (node.type === "ImportDeclaration") collectStrayImports(node, file, strayImports);
|
|
1101
1360
|
else if (node.type === "TaggedTemplateExpression") {
|
|
1102
1361
|
const tag = node.tag;
|
|
1103
1362
|
const explicit = explicitId(tag, names);
|
|
1104
|
-
if (!explicit && !isTReference(tag, names))
|
|
1363
|
+
if (!explicit && !isTReference(tag, names)) {
|
|
1364
|
+
const name = renamedTag(tag, renamed);
|
|
1365
|
+
if (name) missed.push({
|
|
1366
|
+
name,
|
|
1367
|
+
file,
|
|
1368
|
+
start: node.start
|
|
1369
|
+
});
|
|
1370
|
+
return;
|
|
1371
|
+
}
|
|
1105
1372
|
const quasi = node.quasi;
|
|
1106
1373
|
const message = buildMessage(code, quasi, names);
|
|
1107
1374
|
if (!message) return;
|
|
@@ -1122,7 +1389,15 @@ function analyze(code, file, options = {}) {
|
|
|
1122
1389
|
collectDeclaredKeys(args[0], file, usedKeys);
|
|
1123
1390
|
return;
|
|
1124
1391
|
}
|
|
1125
|
-
if (!isTReference(callee, names))
|
|
1392
|
+
if (!isTReference(callee, names)) {
|
|
1393
|
+
const key = renamed.has(identifierName(callee)) ? staticString(args[0]) : void 0;
|
|
1394
|
+
if (key !== void 0) usedKeys.push({
|
|
1395
|
+
key,
|
|
1396
|
+
file,
|
|
1397
|
+
loose: true
|
|
1398
|
+
});
|
|
1399
|
+
return;
|
|
1400
|
+
}
|
|
1126
1401
|
const key = staticString(args[0]);
|
|
1127
1402
|
if (key !== void 0) usedKeys.push(usedKey(key, file, args[0]));
|
|
1128
1403
|
} else if (node.type === "JSXElement") handleTrans(code, node, file, tagged, usedKeys, names);
|
|
@@ -1130,9 +1405,53 @@ function analyze(code, file, options = {}) {
|
|
|
1130
1405
|
return {
|
|
1131
1406
|
tagged,
|
|
1132
1407
|
usedKeys,
|
|
1133
|
-
strayImports
|
|
1408
|
+
strayImports,
|
|
1409
|
+
...missed.length > 0 && { missed }
|
|
1134
1410
|
};
|
|
1135
1411
|
}
|
|
1412
|
+
function renamedBindings(program, names) {
|
|
1413
|
+
const out = /* @__PURE__ */ new Set();
|
|
1414
|
+
const add = (node) => {
|
|
1415
|
+
const name = identifierName(node);
|
|
1416
|
+
if (name && !names.has(name)) out.add(name);
|
|
1417
|
+
};
|
|
1418
|
+
walk(program, (node) => {
|
|
1419
|
+
if (node.type === "ImportDeclaration") {
|
|
1420
|
+
if (node.source.value !== "virtual:verbaly") return;
|
|
1421
|
+
for (const spec of node.specifiers) if (spec.type === "ImportSpecifier" && identifierName(spec.imported) === "t") add(spec.local);
|
|
1422
|
+
} else if (node.type === "VariableDeclarator") {
|
|
1423
|
+
const id = node.id;
|
|
1424
|
+
const init = node.init;
|
|
1425
|
+
if (id.type === "Identifier" && init && handsOutT(init)) add(id);
|
|
1426
|
+
if (id.type !== "ObjectPattern") return;
|
|
1427
|
+
for (const property of id.properties) {
|
|
1428
|
+
if (property.type !== "ObjectProperty" || property.computed) continue;
|
|
1429
|
+
if (identifierName(property.key) === "t") add(property.value);
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
});
|
|
1433
|
+
return out;
|
|
1434
|
+
}
|
|
1435
|
+
function handsOutT(init) {
|
|
1436
|
+
const call = init.type === "AwaitExpression" ? init.argument : init;
|
|
1437
|
+
if (call.type !== "CallExpression") return false;
|
|
1438
|
+
const callee = identifierName(call.callee);
|
|
1439
|
+
return init.type === "AwaitExpression" ? callee === "getT" : callee === "useT";
|
|
1440
|
+
}
|
|
1441
|
+
function identifierName(node) {
|
|
1442
|
+
return node?.type === "Identifier" ? node.name : "";
|
|
1443
|
+
}
|
|
1444
|
+
function renamedTag(tag, renamed) {
|
|
1445
|
+
if (renamed.size === 0) return void 0;
|
|
1446
|
+
const name = identifierName(tag);
|
|
1447
|
+
if (renamed.has(name)) return name;
|
|
1448
|
+
if (tag.type !== "CallExpression") return void 0;
|
|
1449
|
+
const callee = tag.callee;
|
|
1450
|
+
if (callee.type !== "MemberExpression" || callee.computed) return void 0;
|
|
1451
|
+
if (identifierName(callee.property) !== "id") return void 0;
|
|
1452
|
+
const object = identifierName(callee.object);
|
|
1453
|
+
return renamed.has(object) ? object : void 0;
|
|
1454
|
+
}
|
|
1136
1455
|
function collectDeclaredKeys(node, file, out) {
|
|
1137
1456
|
if (node?.type !== "ObjectExpression") return;
|
|
1138
1457
|
for (const property of node.properties) {
|
|
@@ -1554,31 +1873,40 @@ async function extractProject(cfg) {
|
|
|
1554
1873
|
}
|
|
1555
1874
|
async function collectOrigins(cfg, reuse) {
|
|
1556
1875
|
const registry = reuse ?? await extractProject(cfg);
|
|
1557
|
-
const origins =
|
|
1876
|
+
const origins = Object.create(null);
|
|
1558
1877
|
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
1559
1878
|
return origins;
|
|
1560
1879
|
}
|
|
1561
1880
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
1562
1881
|
const added = {};
|
|
1563
|
-
const
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1882
|
+
const replaced = [];
|
|
1883
|
+
const source = catalogs[cfg.sourceLocale] ??= emptyCatalog();
|
|
1884
|
+
for (const [key, msg] of registry.messages()) {
|
|
1885
|
+
const before = own(source, key);
|
|
1886
|
+
if (before !== msg.message) {
|
|
1887
|
+
if (before) replaced.push(key);
|
|
1888
|
+
source[key] = msg.message;
|
|
1889
|
+
(added[cfg.sourceLocale] ??= []).push(key);
|
|
1890
|
+
}
|
|
1567
1891
|
}
|
|
1568
1892
|
const needed = Object.keys(source);
|
|
1569
1893
|
for (const locale of cfg.locales) {
|
|
1570
1894
|
if (locale === cfg.sourceLocale) continue;
|
|
1571
|
-
const catalog = catalogs[locale] ??=
|
|
1572
|
-
for (const key of needed) if (catalog
|
|
1895
|
+
const catalog = catalogs[locale] ??= emptyCatalog();
|
|
1896
|
+
for (const key of needed) if (own(catalog, key) === void 0) {
|
|
1573
1897
|
catalog[key] = "";
|
|
1574
1898
|
(added[locale] ??= []).push(key);
|
|
1575
1899
|
}
|
|
1576
1900
|
}
|
|
1577
|
-
return {
|
|
1901
|
+
return {
|
|
1902
|
+
added,
|
|
1903
|
+
replaced
|
|
1904
|
+
};
|
|
1578
1905
|
}
|
|
1579
1906
|
function pruneCatalogs(cfg, catalogs, registry) {
|
|
1580
|
-
const keep = /* @__PURE__ */ new Set([...registry.messages().keys(), ...registry.usedKeys().keys()]);
|
|
1581
1907
|
const removed = {};
|
|
1908
|
+
if (registry.parseErrors().length > 0) return removed;
|
|
1909
|
+
const keep = /* @__PURE__ */ new Set([...registry.messages().keys(), ...registry.usedKeys().keys()]);
|
|
1582
1910
|
for (const locale of cfg.locales) {
|
|
1583
1911
|
const catalog = catalogs[locale];
|
|
1584
1912
|
if (!catalog) continue;
|
|
@@ -1590,6 +1918,31 @@ function pruneCatalogs(cfg, catalogs, registry) {
|
|
|
1590
1918
|
return removed;
|
|
1591
1919
|
}
|
|
1592
1920
|
//#endregion
|
|
1921
|
+
//#region src/drafts.ts
|
|
1922
|
+
function loadDrafts(cfg) {
|
|
1923
|
+
return loadState(cfg).drafts;
|
|
1924
|
+
}
|
|
1925
|
+
function saveDrafts(cfg, drafts) {
|
|
1926
|
+
saveState(cfg, {
|
|
1927
|
+
...loadState(cfg),
|
|
1928
|
+
drafts
|
|
1929
|
+
});
|
|
1930
|
+
}
|
|
1931
|
+
function markDrafts(drafts, locale, keys) {
|
|
1932
|
+
if (!keys.length) return;
|
|
1933
|
+
drafts[locale] = [.../* @__PURE__ */ new Set([...drafts[locale] ?? [], ...keys])];
|
|
1934
|
+
}
|
|
1935
|
+
function clearDrafts(drafts, locale, keys) {
|
|
1936
|
+
if (!drafts[locale]) return;
|
|
1937
|
+
if (!keys) {
|
|
1938
|
+
delete drafts[locale];
|
|
1939
|
+
return;
|
|
1940
|
+
}
|
|
1941
|
+
const drop = new Set(keys);
|
|
1942
|
+
drafts[locale] = drafts[locale].filter((key) => !drop.has(key));
|
|
1943
|
+
if (!drafts[locale].length) delete drafts[locale];
|
|
1944
|
+
}
|
|
1945
|
+
//#endregion
|
|
1593
1946
|
//#region src/doctor.ts
|
|
1594
1947
|
const PREVIEW = 5;
|
|
1595
1948
|
function readRoutingChoice(root, configFile) {
|
|
@@ -1652,17 +2005,22 @@ async function doctor(cfg) {
|
|
|
1652
2005
|
error(`locale ${locale}`, `${rel(file)} is missing`, "run `npx verbaly extract` to create it");
|
|
1653
2006
|
continue;
|
|
1654
2007
|
}
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
const bad = badLeaf(parsed);
|
|
1658
|
-
if (bad) {
|
|
1659
|
-
catalogsHealthy = false;
|
|
1660
|
-
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
1661
|
-
} else catalogs[locale] = flatten(parsed);
|
|
1662
|
-
} catch {
|
|
2008
|
+
const parsed = parseTree(readFileSync(file, "utf8"));
|
|
2009
|
+
if (parsed === void 0) {
|
|
1663
2010
|
catalogsHealthy = false;
|
|
1664
2011
|
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
2012
|
+
continue;
|
|
2013
|
+
}
|
|
2014
|
+
if (!isTree(parsed)) {
|
|
2015
|
+
catalogsHealthy = false;
|
|
2016
|
+
error(`locale ${locale}`, `${rel(file)} is not a JSON object of messages`, "a catalog is an object of texts (groups of text are fine): fix the file");
|
|
2017
|
+
continue;
|
|
1665
2018
|
}
|
|
2019
|
+
const bad = badLeaf(parsed);
|
|
2020
|
+
if (bad) {
|
|
2021
|
+
catalogsHealthy = false;
|
|
2022
|
+
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad}"`, "catalog values are text (groups of text are fine); fix the value");
|
|
2023
|
+
} else catalogs[locale] = flatten(parsed);
|
|
1666
2024
|
}
|
|
1667
2025
|
if (catalogsHealthy) {
|
|
1668
2026
|
const how = cfg.localesDeclared ? "" : ", no locales set, so the catalog files decide";
|
|
@@ -1679,6 +2037,7 @@ async function doctor(cfg) {
|
|
|
1679
2037
|
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`");
|
|
1680
2038
|
const mirrors = cfg.render.site !== void 0 || Object.keys(cfg.render).length > 0;
|
|
1681
2039
|
if (cfg.routing === "no-prefix" && mirrors) warn("routing", "routing is \"no-prefix\" but a render section is configured, and render writes one url tree per locale", "drop the render section, or set routing to \"prefix-except-source\" so the helpers agree with the urls");
|
|
2040
|
+
else if (cfg.routing === "prefix-all" && mirrors) warn("routing", `routing is "prefix-all" but render keeps ${cfg.sourceLocale} at the root, so its links point at a /${cfg.sourceLocale}/ tree nobody wrote`, "set routing to \"prefix-except-source\", the url tree render writes");
|
|
1682
2041
|
else {
|
|
1683
2042
|
const how = (configFile ? readRoutingChoice(cfg.root, configFile) : false) ? "" : " (from your setup, no routing set)";
|
|
1684
2043
|
ok("routing", `${cfg.routing}${how}: ${ROUTING_SAYS[cfg.routing]}`);
|
|
@@ -1709,6 +2068,18 @@ async function doctor(cfg) {
|
|
|
1709
2068
|
const files = [...new Set(stray.map((entry) => rel(entry.file)))];
|
|
1710
2069
|
error("imports", `t is imported from a verbaly package, which never exports it, in ${counted(files.length, "file")} (${preview(files)})`, "t comes from your instance (React: const t = useT()) or from virtual:verbaly");
|
|
1711
2070
|
}
|
|
2071
|
+
const missed = registry.missed();
|
|
2072
|
+
if (missed.length > 0) {
|
|
2073
|
+
const locate = createLocator();
|
|
2074
|
+
const first = missed[0];
|
|
2075
|
+
const line = locate(first.file, first.start);
|
|
2076
|
+
warn("sources", `a t under another name keeps ${counted(missed.length, "text")} out of extraction (${rel(first.file)}${line ? `:${line}` : ""}: ${first.name}\`…\`)`, "name it t (const t = useT()), the only name verbaly reads, or use t(key) with the key in the catalog");
|
|
2077
|
+
}
|
|
2078
|
+
const positional = [...registry.messages().values()].filter((msg) => msg.params.some((param) => /^_\d+$/.test(param.name)));
|
|
2079
|
+
if (positional.length > 0) {
|
|
2080
|
+
const first = positional[0];
|
|
2081
|
+
warn("messages", `a translator gets a nameless value like {_0} in ${counted(positional.length, "message")} (${rel(first.file)}: "${first.message}")`, "put the value in a named variable first (const date = formatDate(d)), so the message says {date}");
|
|
2082
|
+
}
|
|
1712
2083
|
const escaped = [...registry.messages().values()].map((msg) => ({
|
|
1713
2084
|
file: rel(msg.file),
|
|
1714
2085
|
slice: escapedSyntax(msg.message)
|
|
@@ -1719,7 +2090,8 @@ async function doctor(cfg) {
|
|
|
1719
2090
|
const extracted = registry.messages();
|
|
1720
2091
|
const used = registry.usedKeys();
|
|
1721
2092
|
const orphans = Object.keys(source).filter((key) => !extracted.has(key) && !used.has(key));
|
|
1722
|
-
|
|
2093
|
+
const unread = registry.parseErrors().length > 0;
|
|
2094
|
+
if (orphans.length > 0) warn("orphans", `${counted(orphans.length, "catalog key")} no longer referenced in code (${preview(orphans)})`, unread ? "fix the files that do not parse first, they may use these, and `npx verbaly extract --prune` leaves every key alone until then" : "run `npx verbaly extract --prune` to drop them");
|
|
1723
2095
|
else ok("orphans", "no orphan keys");
|
|
1724
2096
|
}
|
|
1725
2097
|
const excluded = cfg.bundle.exclude ?? [];
|
|
@@ -1728,8 +2100,24 @@ async function doctor(cfg) {
|
|
|
1728
2100
|
for (const issue of issues) warn("bundle", formatBundleIssue(issue), issue.fix);
|
|
1729
2101
|
if (issues.length === 0) ok("bundle", `${counted(excluded.length, "group")} kept out of the client (${preview(excluded)})`);
|
|
1730
2102
|
}
|
|
2103
|
+
let state = {
|
|
2104
|
+
drafts: {},
|
|
2105
|
+
fingerprints: {}
|
|
2106
|
+
};
|
|
2107
|
+
try {
|
|
2108
|
+
state = loadState(cfg);
|
|
2109
|
+
} catch (error) {
|
|
2110
|
+
warn("state", error.message.replace(/^\[verbaly\] /, ""), `delete ${STATE_FILE}: drafts and outdated tracking start again from the catalogs`);
|
|
2111
|
+
}
|
|
1731
2112
|
if (catalogsHealthy) {
|
|
1732
|
-
const
|
|
2113
|
+
const stale = Object.entries(state.drafts).flatMap(([locale, keys]) => {
|
|
2114
|
+
const live = new Set(effectiveDrafts({ [locale]: keys }, catalogs)[locale] ?? []);
|
|
2115
|
+
return keys.filter((key) => !live.has(key)).map((key) => `${locale}: ${key}`);
|
|
2116
|
+
});
|
|
2117
|
+
if (stale.length > 0) warn("drafts", `${STATE_FILE} lists ${counted(stale.length, "draft")} whose translation is gone (${preview(stale)})`, "run `npx verbaly extract`, which drops them");
|
|
2118
|
+
}
|
|
2119
|
+
if (catalogsHealthy) {
|
|
2120
|
+
const result = check(cfg, catalogs, registry, state.fingerprints);
|
|
1733
2121
|
if (result.unknown.length > 0) error("keys", `${counted(result.unknown.length, "unknown key")} used in code (${preview(result.unknown.map((u) => u.key))})`, "fix the key or add it to the catalogs (`npx verbaly check` for details)");
|
|
1734
2122
|
if (result.missing.length > 0) {
|
|
1735
2123
|
const locales = [...new Set(result.missing.map((m) => m.locale))];
|
|
@@ -1746,6 +2134,18 @@ async function doctor(cfg) {
|
|
|
1746
2134
|
}
|
|
1747
2135
|
const warnings = result.broken.filter((entry) => entry.severity === "warning");
|
|
1748
2136
|
if (warnings.length > 0) warn("translations", `${counted(warnings.length, "structural warning")} (plural forms a language asks for)`, "run `npx verbaly check` to read them, they never fail the build");
|
|
2137
|
+
if (result.collisions.length > 0) {
|
|
2138
|
+
const first = result.collisions[0];
|
|
2139
|
+
warn("keys", `${counted(result.collisions.length, "key")} written with more than one text, and every place shows the first (${first.key}: ${first.sites.map((at) => sourcePlace(at, cfg.root)).join(", ")})`, "give each text its own key (`npx verbaly check` lists every place)");
|
|
2140
|
+
}
|
|
2141
|
+
if (result.divergent.length > 0) {
|
|
2142
|
+
const first = result.divergent[0];
|
|
2143
|
+
warn("source texts", `the code and ${cfg.sourceLocale}.json disagree on ${counted(result.divergent.length, "text")}, and the code's text ships (${first.key}, ${sourcePlace(first.code, cfg.root)})`, "edit those texts in the code; `npx verbaly extract` writes them into the catalog");
|
|
2144
|
+
}
|
|
2145
|
+
if (result.outdated.length > 0) {
|
|
2146
|
+
const shown = result.outdated.map((entry) => `${entry.locale}: ${entry.key}`);
|
|
2147
|
+
warn("outdated", `${counted(result.outdated.length, "translation")} written for an older source text (${preview(shown)})`, "update them, or keep them with `npx verbaly review --approve`");
|
|
2148
|
+
}
|
|
1749
2149
|
if (result.ok) ok("translations", "all translations complete");
|
|
1750
2150
|
}
|
|
1751
2151
|
return {
|
|
@@ -1758,66 +2158,6 @@ function preview(keys) {
|
|
|
1758
2158
|
return keys.length > PREVIEW ? `${head}, …` : head;
|
|
1759
2159
|
}
|
|
1760
2160
|
//#endregion
|
|
1761
|
-
//#region src/drafts.ts
|
|
1762
|
-
const DRAFTS_FILE = ".verbaly-drafts.json";
|
|
1763
|
-
function draftsPath(cfg) {
|
|
1764
|
-
return join(cfg.dir, DRAFTS_FILE);
|
|
1765
|
-
}
|
|
1766
|
-
function loadDrafts(cfg) {
|
|
1767
|
-
let content;
|
|
1768
|
-
try {
|
|
1769
|
-
content = readFileSync(draftsPath(cfg), "utf8");
|
|
1770
|
-
} catch {
|
|
1771
|
-
return {};
|
|
1772
|
-
}
|
|
1773
|
-
try {
|
|
1774
|
-
return JSON.parse(content.replace(/^\uFEFF/, ""));
|
|
1775
|
-
} catch (error) {
|
|
1776
|
-
throw new Error(`[verbaly] ${draftsPath(cfg)} is not valid JSON, fix or delete the file`, { cause: error });
|
|
1777
|
-
}
|
|
1778
|
-
}
|
|
1779
|
-
function serializeDrafts(drafts) {
|
|
1780
|
-
const sorted = {};
|
|
1781
|
-
for (const locale of Object.keys(drafts).sort()) {
|
|
1782
|
-
const keys = [...new Set(drafts[locale])].sort();
|
|
1783
|
-
if (keys.length) sorted[locale] = keys;
|
|
1784
|
-
}
|
|
1785
|
-
return JSON.stringify(sorted, null, 2) + "\n";
|
|
1786
|
-
}
|
|
1787
|
-
function saveDrafts(cfg, drafts) {
|
|
1788
|
-
const serialized = serializeDrafts(drafts);
|
|
1789
|
-
const path = draftsPath(cfg);
|
|
1790
|
-
try {
|
|
1791
|
-
if (readFileSync(path, "utf8") === serialized) return;
|
|
1792
|
-
} catch {
|
|
1793
|
-
mkdirSync(cfg.dir, { recursive: true });
|
|
1794
|
-
}
|
|
1795
|
-
writeFileSync(path, serialized);
|
|
1796
|
-
}
|
|
1797
|
-
function markDrafts(drafts, locale, keys) {
|
|
1798
|
-
if (!keys.length) return;
|
|
1799
|
-
drafts[locale] = [.../* @__PURE__ */ new Set([...drafts[locale] ?? [], ...keys])];
|
|
1800
|
-
}
|
|
1801
|
-
function clearDrafts(drafts, locale, keys) {
|
|
1802
|
-
if (!drafts[locale]) return;
|
|
1803
|
-
if (!keys) {
|
|
1804
|
-
delete drafts[locale];
|
|
1805
|
-
return;
|
|
1806
|
-
}
|
|
1807
|
-
const drop = new Set(keys);
|
|
1808
|
-
drafts[locale] = drafts[locale].filter((key) => !drop.has(key));
|
|
1809
|
-
if (!drafts[locale].length) delete drafts[locale];
|
|
1810
|
-
}
|
|
1811
|
-
function effectiveDrafts(drafts, catalogs) {
|
|
1812
|
-
const out = {};
|
|
1813
|
-
for (const [locale, keys] of Object.entries(drafts)) {
|
|
1814
|
-
const catalog = catalogs[locale] ?? {};
|
|
1815
|
-
const live = keys.filter((key) => catalog[key]);
|
|
1816
|
-
if (live.length) out[locale] = live;
|
|
1817
|
-
}
|
|
1818
|
-
return out;
|
|
1819
|
-
}
|
|
1820
|
-
//#endregion
|
|
1821
2161
|
//#region src/inline.ts
|
|
1822
2162
|
const OPEN_TAG = /^<([a-zA-Z][a-zA-Z0-9-]*)\s*(\/?)>/;
|
|
1823
2163
|
function messageToInline(text) {
|
|
@@ -2216,8 +2556,8 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
2216
2556
|
};
|
|
2217
2557
|
const jobs = [];
|
|
2218
2558
|
for (const locale of targets) {
|
|
2219
|
-
const catalog = catalogs[locale] ??=
|
|
2220
|
-
const missing = Object.keys(source).filter((key) => source[key] && !catalog
|
|
2559
|
+
const catalog = catalogs[locale] ??= emptyCatalog();
|
|
2560
|
+
const missing = Object.keys(source).filter((key) => source[key] && !own(catalog, key));
|
|
2221
2561
|
if (missing.length === 0) continue;
|
|
2222
2562
|
if (options.dryRun) {
|
|
2223
2563
|
result.pending[locale] = missing;
|
|
@@ -2238,7 +2578,7 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
2238
2578
|
const { locale, keys } = job;
|
|
2239
2579
|
const catalog = catalogs[locale];
|
|
2240
2580
|
const messages = Object.fromEntries(keys.map((key) => [key, source[key]]));
|
|
2241
|
-
const origins = options.origins ? Object.fromEntries(keys.filter((key) => options.origins
|
|
2581
|
+
const origins = options.origins ? Object.fromEntries(keys.filter((key) => Object.hasOwn(options.origins, key)).map((key) => [key, options.origins[key]])) : void 0;
|
|
2242
2582
|
let out;
|
|
2243
2583
|
try {
|
|
2244
2584
|
out = await attempt(() => provider({
|
|
@@ -2307,7 +2647,7 @@ function formatTranslateFailures(failed) {
|
|
|
2307
2647
|
async function resolveProvider(cfg, model) {
|
|
2308
2648
|
const configured = cfg.translate.provider;
|
|
2309
2649
|
if (typeof configured === "function") return configured;
|
|
2310
|
-
const { claudeProvider } = await import("./claude-
|
|
2650
|
+
const { claudeProvider } = await import("./claude-BmD8W9uW.js");
|
|
2311
2651
|
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
2312
2652
|
}
|
|
2313
2653
|
function structureMatches(source, translated) {
|
|
@@ -2349,14 +2689,15 @@ function exportCatalogs(cfg, catalogs, options = {}) {
|
|
|
2349
2689
|
const targets = targetLocales(cfg, options.locales);
|
|
2350
2690
|
if (isMobileFormat(format)) return exportMobile(cfg, catalogs, format, dir, source, targets);
|
|
2351
2691
|
const files = [];
|
|
2692
|
+
const origins = options.origins;
|
|
2352
2693
|
mkdirSync(dir, { recursive: true });
|
|
2353
2694
|
for (const locale of targets) {
|
|
2354
|
-
const catalog = catalogs[locale] ??
|
|
2695
|
+
const catalog = catalogs[locale] ?? emptyCatalog();
|
|
2355
2696
|
const all = Object.keys(source).filter((key) => source[key]).sort().map((key) => ({
|
|
2356
2697
|
key,
|
|
2357
2698
|
source: source[key],
|
|
2358
|
-
target: catalog
|
|
2359
|
-
location:
|
|
2699
|
+
target: own(catalog, key) ?? "",
|
|
2700
|
+
location: origins && Object.hasOwn(origins, key) ? origins[key] : void 0
|
|
2360
2701
|
}));
|
|
2361
2702
|
const untranslated = all.filter((entry) => !entry.target);
|
|
2362
2703
|
const entries = options.missing ? untranslated : all;
|
|
@@ -2383,7 +2724,7 @@ function exportMobile(cfg, catalogs, format, dir, source, targets) {
|
|
|
2383
2724
|
const catalog = locale === cfg.sourceLocale ? source : catalogs[locale] ?? {};
|
|
2384
2725
|
const entries = keys.map((key) => ({
|
|
2385
2726
|
key,
|
|
2386
|
-
text: catalog
|
|
2727
|
+
text: own(catalog, key) ?? ""
|
|
2387
2728
|
})).filter((entry) => entry.text);
|
|
2388
2729
|
const relative = format === "android-xml" ? join(androidValuesDir(locale, cfg.sourceLocale), "strings.xml") : join(`${locale}.lproj`, "Localizable.strings");
|
|
2389
2730
|
const path = join(dir, relative);
|
|
@@ -2436,18 +2777,19 @@ function importCatalogs(cfg, catalogs, files, options = {}) {
|
|
|
2436
2777
|
from: detected,
|
|
2437
2778
|
to: locale
|
|
2438
2779
|
});
|
|
2439
|
-
const catalog = catalogs[locale] ??=
|
|
2780
|
+
const catalog = catalogs[locale] ??= emptyCatalog();
|
|
2440
2781
|
for (const [key, text] of Object.entries(parsed.entries)) {
|
|
2441
2782
|
if (!text.trim()) continue;
|
|
2442
|
-
|
|
2783
|
+
const from = own(source, key);
|
|
2784
|
+
if (!from) {
|
|
2443
2785
|
(result.unknown[locale] ??= []).push(key);
|
|
2444
2786
|
continue;
|
|
2445
2787
|
}
|
|
2446
|
-
if (catalog
|
|
2788
|
+
if (own(catalog, key) && !options.overwrite) {
|
|
2447
2789
|
(result.skipped[locale] ??= []).push(key);
|
|
2448
2790
|
continue;
|
|
2449
2791
|
}
|
|
2450
|
-
if (!structureMatches(
|
|
2792
|
+
if (!structureMatches(from, text)) {
|
|
2451
2793
|
(result.rejected[locale] ??= []).push(key);
|
|
2452
2794
|
continue;
|
|
2453
2795
|
}
|
|
@@ -2690,7 +3032,7 @@ function migrateCatalogs(cfg, options = {}) {
|
|
|
2690
3032
|
const written = [];
|
|
2691
3033
|
for (const locale of cfg.locales) {
|
|
2692
3034
|
const catalog = readCatalog(cfg, locale);
|
|
2693
|
-
const out =
|
|
3035
|
+
const out = Object.assign(emptyCatalog(), catalog);
|
|
2694
3036
|
for (const [key, value] of Object.entries(catalog)) {
|
|
2695
3037
|
if (typeof value !== "string") continue;
|
|
2696
3038
|
if (NESTED.test(value)) {
|
|
@@ -2743,6 +3085,14 @@ function migrateCatalogs(cfg, options = {}) {
|
|
|
2743
3085
|
};
|
|
2744
3086
|
}
|
|
2745
3087
|
//#endregion
|
|
3088
|
+
//#region src/warn.ts
|
|
3089
|
+
const warned = /* @__PURE__ */ new Set();
|
|
3090
|
+
function warnOnce(message, dedupeKey = message) {
|
|
3091
|
+
if (warned.has(dedupeKey)) return;
|
|
3092
|
+
warned.add(dedupeKey);
|
|
3093
|
+
console.warn(`[verbaly] ${message}`);
|
|
3094
|
+
}
|
|
3095
|
+
//#endregion
|
|
2746
3096
|
//#region src/render.ts
|
|
2747
3097
|
function localeInstance(catalogs, locale, sourceLocale) {
|
|
2748
3098
|
return createVerbaly({
|
|
@@ -2871,7 +3221,7 @@ function renderHtml(html, options) {
|
|
|
2871
3221
|
};
|
|
2872
3222
|
}
|
|
2873
3223
|
function pageSlice(v, locale, used) {
|
|
2874
|
-
const slice =
|
|
3224
|
+
const slice = Object.create(null);
|
|
2875
3225
|
for (const key of used) {
|
|
2876
3226
|
const found = v.inspect(key);
|
|
2877
3227
|
slice[key] = found?.from === locale ? found.source : "";
|
|
@@ -3186,13 +3536,14 @@ function decodeEntities(text) {
|
|
|
3186
3536
|
}
|
|
3187
3537
|
//#endregion
|
|
3188
3538
|
//#region src/status.ts
|
|
3189
|
-
function status(cfg, catalogs, registry, drafts = {}) {
|
|
3539
|
+
function status(cfg, catalogs, registry, drafts = {}, fingerprints = {}) {
|
|
3190
3540
|
const extracted = registry.messages();
|
|
3191
3541
|
const flat = {};
|
|
3192
3542
|
for (const locale of cfg.locales) flat[locale] = flatten(catalogs[locale] ?? {});
|
|
3193
3543
|
const source = flat[cfg.sourceLocale] ?? {};
|
|
3194
3544
|
const needed = /* @__PURE__ */ new Set([...extracted.keys(), ...Object.keys(source)]);
|
|
3195
3545
|
const live = effectiveDrafts(drafts, flat);
|
|
3546
|
+
const outdated = outdatedTranslations(cfg, flat, fingerprints);
|
|
3196
3547
|
const locales = [];
|
|
3197
3548
|
for (const locale of cfg.locales) {
|
|
3198
3549
|
if (locale === cfg.sourceLocale) continue;
|
|
@@ -3210,7 +3561,8 @@ function status(cfg, catalogs, registry, drafts = {}) {
|
|
|
3210
3561
|
translated,
|
|
3211
3562
|
total: needed.size,
|
|
3212
3563
|
drafts: live[locale]?.length ?? 0,
|
|
3213
|
-
broken
|
|
3564
|
+
broken,
|
|
3565
|
+
outdated: outdated.filter((entry) => entry.locale === locale).length
|
|
3214
3566
|
});
|
|
3215
3567
|
}
|
|
3216
3568
|
return {
|
|
@@ -3225,10 +3577,14 @@ function formatStatusResult(result) {
|
|
|
3225
3577
|
lines.push(" no target locales (add locales to your config)");
|
|
3226
3578
|
return lines.join("\n");
|
|
3227
3579
|
}
|
|
3228
|
-
for (const { locale, translated, total, drafts, broken } of result.locales) {
|
|
3580
|
+
for (const { locale, translated, total, drafts, broken, outdated } of result.locales) {
|
|
3229
3581
|
const pct = total === 0 ? 100 : Math.floor(translated / total * 100);
|
|
3230
|
-
const mark = translated === total && broken === 0 ? " ✓" : "";
|
|
3231
|
-
const notes = [
|
|
3582
|
+
const mark = translated === total && broken === 0 && outdated === 0 ? " ✓" : "";
|
|
3583
|
+
const notes = [
|
|
3584
|
+
drafts > 0 ? `${drafts} unreviewed` : "",
|
|
3585
|
+
broken > 0 ? `${broken} broken` : "",
|
|
3586
|
+
outdated > 0 ? `${outdated} outdated` : ""
|
|
3587
|
+
].filter(Boolean);
|
|
3232
3588
|
const note = notes.length > 0 ? `, ${notes.join(", ")}` : "";
|
|
3233
3589
|
lines.push(` ${locale}: ${translated}/${total} translated (${pct}%${note})${mark}`);
|
|
3234
3590
|
}
|
|
@@ -3263,17 +3619,17 @@ function watchTree(root, options, onFile) {
|
|
|
3263
3619
|
close(name);
|
|
3264
3620
|
if (name !== options.keep && (name === "node_modules" || name.startsWith("."))) return;
|
|
3265
3621
|
try {
|
|
3266
|
-
if (!
|
|
3267
|
-
|
|
3268
|
-
|
|
3622
|
+
if (!lstatSync(join(root, name)).isDirectory()) return;
|
|
3623
|
+
open(name, true, (_event, file) => {
|
|
3624
|
+
if (file) onFile(`${name}/${file}`);
|
|
3625
|
+
});
|
|
3626
|
+
} catch (error) {
|
|
3627
|
+
if (error.code !== "ENOENT") console.warn(`[verbaly] could not watch ${name}:`, error);
|
|
3269
3628
|
}
|
|
3270
|
-
open(name, true, (_event, file) => {
|
|
3271
|
-
if (file) onFile(`${name}/${file}`);
|
|
3272
|
-
});
|
|
3273
3629
|
};
|
|
3274
|
-
open("", false, (
|
|
3630
|
+
open("", false, (event, file) => {
|
|
3275
3631
|
if (!file) return;
|
|
3276
|
-
sync(file);
|
|
3632
|
+
if (event === "rename") sync(file);
|
|
3277
3633
|
onFile(file);
|
|
3278
3634
|
});
|
|
3279
3635
|
for (const entry of readdirSync(root, { withFileTypes: true })) if (entry.isDirectory()) sync(entry.name);
|
|
@@ -3610,7 +3966,7 @@ Usage:
|
|
|
3610
3966
|
verbaly status translation coverage per locale, at a glance
|
|
3611
3967
|
verbaly check verify translations are complete (CI)
|
|
3612
3968
|
verbaly translate fill missing translations via a provider (default: claude)
|
|
3613
|
-
verbaly review list
|
|
3969
|
+
verbaly review list translations awaiting review: machine drafts and outdated ones (--approve accepts them)
|
|
3614
3970
|
verbaly export write translator files (XLIFF 2.0, CSV, gettext PO) or mobile resources (Android, iOS)
|
|
3615
3971
|
verbaly import <files…> fill catalogs back from translated XLIFF/CSV/PO files
|
|
3616
3972
|
verbaly pseudo generate a pseudo-locale catalog for i18n QA (default: en-XA)
|
|
@@ -3627,6 +3983,7 @@ Options:
|
|
|
3627
3983
|
--plurals also merge _one/_other into one message with variants (migrate)
|
|
3628
3984
|
--json machine-readable output (status)
|
|
3629
3985
|
--drafts also fail on unreviewed machine translations (check)
|
|
3986
|
+
--outdated also fail on translations written for an older source text (check)
|
|
3630
3987
|
--approve mark listed drafts as reviewed (review)
|
|
3631
3988
|
--reporter <name> failure format: text (default) or github annotations (check)
|
|
3632
3989
|
--model <id> model override for the claude provider (translate)
|
|
@@ -3635,6 +3992,7 @@ Options:
|
|
|
3635
3992
|
--out <path> export directory (export, default: verbaly-export)
|
|
3636
3993
|
--missing export only untranslated entries (export)
|
|
3637
3994
|
--overwrite replace existing translations on import (import)
|
|
3995
|
+
--draft mark what the files bring as drafts a person still has to review (import)
|
|
3638
3996
|
--locale <id> pseudo-locale id (pseudo) / one locale only (review, import)
|
|
3639
3997
|
--site <path> built site directory (render, default: dist)
|
|
3640
3998
|
--attribute <name> base data attribute (render, default: data-verbaly)
|
|
@@ -3661,6 +4019,8 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3661
4019
|
write: { type: "boolean" },
|
|
3662
4020
|
json: { type: "boolean" },
|
|
3663
4021
|
drafts: { type: "boolean" },
|
|
4022
|
+
outdated: { type: "boolean" },
|
|
4023
|
+
draft: { type: "boolean" },
|
|
3664
4024
|
approve: { type: "boolean" },
|
|
3665
4025
|
reporter: { type: "string" },
|
|
3666
4026
|
model: { type: "string" },
|
|
@@ -3696,7 +4056,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3696
4056
|
root: values.root,
|
|
3697
4057
|
dir: values.dir,
|
|
3698
4058
|
sourceLocale: values.source,
|
|
3699
|
-
locales: values.locales
|
|
4059
|
+
locales: csv(values.locales)
|
|
3700
4060
|
});
|
|
3701
4061
|
if (result.created.length) console.log(`[verbaly] created: ${result.created.join(", ")}`);
|
|
3702
4062
|
if (result.skipped.length) console.log(` kept (already there): ${result.skipped.join(", ")}`);
|
|
@@ -3709,7 +4069,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3709
4069
|
const cfg = await loadConfig(values.root ?? process.cwd(), {
|
|
3710
4070
|
dir: values.dir,
|
|
3711
4071
|
sourceLocale: values.source,
|
|
3712
|
-
locales: values.locales
|
|
4072
|
+
locales: csv(values.locales)
|
|
3713
4073
|
});
|
|
3714
4074
|
if (command === "doctor") {
|
|
3715
4075
|
const result = await doctor(cfg);
|
|
@@ -3739,18 +4099,25 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3739
4099
|
const catalogs = loadCatalogs(cfg);
|
|
3740
4100
|
if (values.prune) {
|
|
3741
4101
|
const removed = pruneCatalogs(cfg, catalogs, registry);
|
|
4102
|
+
const unread = registry.parseErrors().length;
|
|
4103
|
+
if (unread > 0) console.warn(` prune skipped: an unparsed file may read any key (${counted(unread, "file")} below), so it waits until every file parses`);
|
|
3742
4104
|
for (const [locale, keys] of Object.entries(removed)) console.log(dryRun ? ` ${locale}: would prune ${keys.length}: ${keys.join(", ")}` : ` ${locale}: -${keys.length} pruned`);
|
|
3743
4105
|
}
|
|
3744
|
-
const { added } = syncCatalogs(cfg, catalogs, registry);
|
|
4106
|
+
const { added, replaced } = syncCatalogs(cfg, catalogs, registry);
|
|
3745
4107
|
if (!dryRun) {
|
|
3746
4108
|
for (const locale of cfg.locales) writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
3747
|
-
|
|
4109
|
+
writeDts(cfg, catalogs[cfg.sourceLocale] ?? {});
|
|
4110
|
+
updateState(cfg, catalogs);
|
|
3748
4111
|
}
|
|
3749
4112
|
const total = registry.messages().size;
|
|
3750
4113
|
console.log(`[verbaly] ${counted(total, "message")} · locales: ${cfg.locales.join(", ")}${dryRun ? " (dry run, nothing written)" : ""}`);
|
|
3751
4114
|
for (const [locale, keys] of Object.entries(added)) console.log(` ${locale}: ${dryRun ? `would add ${keys.length}` : `+${keys.length}`}`);
|
|
4115
|
+
reportReplaced(cfg, registry, replaced, dryRun);
|
|
4116
|
+
for (const entry of collisionEntries(registry)) console.warn(formatCollision(entry, cfg.root));
|
|
3752
4117
|
reportParseErrors(cfg, registry);
|
|
3753
4118
|
reportEscapedSyntax(cfg, registry);
|
|
4119
|
+
reportMissed(cfg, registry);
|
|
4120
|
+
reportPositional(cfg, registry);
|
|
3754
4121
|
}
|
|
3755
4122
|
await runExtract();
|
|
3756
4123
|
if (values.watch) {
|
|
@@ -3816,7 +4183,8 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3816
4183
|
}
|
|
3817
4184
|
if (command === "status") {
|
|
3818
4185
|
const registry = await extractProject(cfg);
|
|
3819
|
-
const
|
|
4186
|
+
const state = loadState(cfg);
|
|
4187
|
+
const result = status(cfg, loadCatalogs(cfg), registry, state.drafts, state.fingerprints);
|
|
3820
4188
|
console.log(values.json ? JSON.stringify(result) : formatStatusResult(result));
|
|
3821
4189
|
return;
|
|
3822
4190
|
}
|
|
@@ -3829,21 +4197,24 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3829
4197
|
}
|
|
3830
4198
|
const registry = await extractProject(cfg);
|
|
3831
4199
|
const catalogs = loadCatalogs(cfg);
|
|
3832
|
-
const
|
|
3833
|
-
const
|
|
4200
|
+
const state = checkState(cfg, values.drafts === true || values.outdated === true);
|
|
4201
|
+
const result = check(cfg, catalogs, registry, state.fingerprints);
|
|
4202
|
+
const unreviewed = values.drafts ? effectiveDrafts(state.drafts, catalogs) : {};
|
|
3834
4203
|
const draftKeys = Object.entries(unreviewed);
|
|
4204
|
+
const outdated = values.outdated ? result.outdated : [];
|
|
3835
4205
|
if (reporter === "github") for (const line of githubCheckAnnotations(result, registry, cfg.root)) console.error(line);
|
|
3836
4206
|
else {
|
|
3837
|
-
const warnings = formatCheckWarnings(result);
|
|
4207
|
+
const warnings = formatCheckWarnings(result, cfg.root);
|
|
3838
4208
|
if (warnings) console.warn(`[verbaly] warnings (the gate still passes)\n${warnings}`);
|
|
3839
4209
|
}
|
|
3840
|
-
if (result.ok && draftKeys.length === 0) {
|
|
4210
|
+
if (result.ok && draftKeys.length === 0 && outdated.length === 0) {
|
|
3841
4211
|
console.log("[verbaly] all translations complete ✓");
|
|
3842
4212
|
return;
|
|
3843
4213
|
}
|
|
3844
|
-
if (result.ok
|
|
4214
|
+
if (result.ok) {
|
|
3845
4215
|
for (const [locale, keys] of draftKeys) console.error(` [${locale}] ${keys.length} unreviewed: ${keys.join(", ")}`);
|
|
3846
|
-
console.error(
|
|
4216
|
+
for (const [locale, keys] of byLocale(outdated)) console.error(` [${locale}] ${keys.length} outdated: ${keys.join(", ")}`);
|
|
4217
|
+
console.error("[verbaly] check failed: translations awaiting review (run verbaly review, then --approve what holds)");
|
|
3847
4218
|
process.exitCode = 1;
|
|
3848
4219
|
return;
|
|
3849
4220
|
}
|
|
@@ -3856,7 +4227,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3856
4227
|
if (command === "translate") {
|
|
3857
4228
|
const catalogs = loadCatalogs(cfg);
|
|
3858
4229
|
const result = await translateCatalogs(cfg, catalogs, await resolveProvider(cfg, values.model), {
|
|
3859
|
-
locales: values.locales
|
|
4230
|
+
locales: csv(values.locales),
|
|
3860
4231
|
batchSize: cfg.translate.batchSize,
|
|
3861
4232
|
concurrency: cfg.translate.concurrency,
|
|
3862
4233
|
retries: cfg.translate.retries,
|
|
@@ -3890,28 +4261,40 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3890
4261
|
}
|
|
3891
4262
|
if (command === "review") {
|
|
3892
4263
|
const catalogs = loadCatalogs(cfg);
|
|
3893
|
-
const
|
|
3894
|
-
const live = effectiveDrafts(drafts, catalogs);
|
|
4264
|
+
const state = loadState(cfg);
|
|
4265
|
+
const live = effectiveDrafts(state.drafts, catalogs);
|
|
3895
4266
|
const targets = values.locale ? { [values.locale]: live[values.locale] ?? [] } : live;
|
|
3896
4267
|
const entries = Object.entries(targets).filter(([, keys]) => keys.length);
|
|
3897
|
-
|
|
3898
|
-
|
|
4268
|
+
const outdated = outdatedTranslations(cfg, catalogs, state.fingerprints).filter((entry) => !values.locale || entry.locale === values.locale);
|
|
4269
|
+
if (entries.length === 0 && outdated.length === 0) {
|
|
4270
|
+
console.log("[verbaly] nothing awaiting review ✓");
|
|
3899
4271
|
return;
|
|
3900
4272
|
}
|
|
3901
4273
|
if (values.approve) {
|
|
3902
4274
|
let count = 0;
|
|
3903
4275
|
for (const [locale, keys] of entries) {
|
|
3904
|
-
clearDrafts(drafts, locale, keys);
|
|
4276
|
+
clearDrafts(state.drafts, locale, keys);
|
|
3905
4277
|
count += keys.length;
|
|
3906
4278
|
console.log(` ${locale}: ${keys.length} approved`);
|
|
3907
4279
|
}
|
|
3908
|
-
|
|
4280
|
+
acceptOutdated(cfg, catalogs, state.fingerprints, outdated);
|
|
4281
|
+
for (const [locale, keys] of byLocale(outdated)) {
|
|
4282
|
+
console.log(` ${locale}: ${keys.length} kept for the new source text`);
|
|
4283
|
+
count += keys.length;
|
|
4284
|
+
}
|
|
4285
|
+
saveState(cfg, state);
|
|
3909
4286
|
console.log(`[verbaly] ${counted(count, "translation")} marked reviewed ✓`);
|
|
3910
4287
|
return;
|
|
3911
4288
|
}
|
|
3912
|
-
|
|
3913
|
-
|
|
3914
|
-
|
|
4289
|
+
if (entries.length > 0) {
|
|
4290
|
+
const total = entries.reduce((sum, [, keys]) => sum + keys.length, 0);
|
|
4291
|
+
console.log(`[verbaly] ${counted(total, "machine translation")} awaiting review (--approve to accept)`);
|
|
4292
|
+
for (const [locale, keys] of entries) console.log(` ${locale}: ${keys.join(", ")}`);
|
|
4293
|
+
}
|
|
4294
|
+
if (outdated.length > 0) {
|
|
4295
|
+
console.log(`[verbaly] ${counted(outdated.length, "translation")} written for an older source text (update them, or --approve keeps them)`);
|
|
4296
|
+
for (const [locale, keys] of byLocale(outdated)) console.log(` ${locale}: ${keys.join(", ")}`);
|
|
4297
|
+
}
|
|
3915
4298
|
return;
|
|
3916
4299
|
}
|
|
3917
4300
|
if (command === "export") {
|
|
@@ -3933,7 +4316,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3933
4316
|
return;
|
|
3934
4317
|
}
|
|
3935
4318
|
const result = exportCatalogs(cfg, loadCatalogs(cfg), {
|
|
3936
|
-
locales: values.locales
|
|
4319
|
+
locales: csv(values.locales),
|
|
3937
4320
|
format,
|
|
3938
4321
|
out: values.out,
|
|
3939
4322
|
missing: values.missing,
|
|
@@ -3977,11 +4360,12 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3977
4360
|
for (const [locale, keys] of Object.entries(result.imported)) {
|
|
3978
4361
|
if (!values["dry-run"]) {
|
|
3979
4362
|
writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
3980
|
-
|
|
4363
|
+
if (values.draft) markDrafts(drafts, locale, keys);
|
|
4364
|
+
else clearDrafts(drafts, locale, keys);
|
|
3981
4365
|
draftsChanged = true;
|
|
3982
4366
|
}
|
|
3983
4367
|
const verb = values["dry-run"] ? "would import" : "imported";
|
|
3984
|
-
console.log(` ${locale}: +${keys.length} ${verb}`);
|
|
4368
|
+
console.log(` ${locale}: +${keys.length} ${verb}${values.draft ? " (draft)" : ""}`);
|
|
3985
4369
|
}
|
|
3986
4370
|
if (draftsChanged) saveDrafts(cfg, drafts);
|
|
3987
4371
|
for (const [locale, keys] of Object.entries(result.skipped)) console.log(` ${locale}: ${keys.length} already translated, kept (use --overwrite to replace)`);
|
|
@@ -3993,7 +4377,7 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
3993
4377
|
if (command === "render") {
|
|
3994
4378
|
const result = await renderSite(cfg, {
|
|
3995
4379
|
site: values.site,
|
|
3996
|
-
locales: values.locales
|
|
4380
|
+
locales: csv(values.locales),
|
|
3997
4381
|
attribute: values.attribute,
|
|
3998
4382
|
base: values.base,
|
|
3999
4383
|
baseUrl: values["base-url"],
|
|
@@ -4016,6 +4400,9 @@ async function runCli(args = process.argv.slice(2)) {
|
|
|
4016
4400
|
console.error(`[verbaly] unknown command "${command}"\n${HELP}`);
|
|
4017
4401
|
process.exitCode = 1;
|
|
4018
4402
|
}
|
|
4403
|
+
function csv(value) {
|
|
4404
|
+
return value?.split(",").map((item) => item.trim()).filter(Boolean);
|
|
4405
|
+
}
|
|
4019
4406
|
function reportProgress(progress) {
|
|
4020
4407
|
const head = ` ${progress.locale} ${progress.batch}/${progress.batches}`;
|
|
4021
4408
|
if (progress.error) console.warn(`${head}: failed (${progress.error}), continuing`);
|
|
@@ -4031,6 +4418,53 @@ function reportParseErrors(cfg, registry) {
|
|
|
4031
4418
|
console.warn(` ${rel}: could not be parsed (${message}), its messages were not extracted`);
|
|
4032
4419
|
}
|
|
4033
4420
|
}
|
|
4421
|
+
function reportReplaced(cfg, registry, replaced, dryRun) {
|
|
4422
|
+
if (replaced.length === 0) return;
|
|
4423
|
+
const messages = registry.messages();
|
|
4424
|
+
const locate = createLocator();
|
|
4425
|
+
const verb = dryRun ? "would get" : "got";
|
|
4426
|
+
for (const key of replaced) {
|
|
4427
|
+
const msg = messages.get(key);
|
|
4428
|
+
if (!msg) continue;
|
|
4429
|
+
const line = locate(msg.file, msg.start);
|
|
4430
|
+
const place = `${relative(cfg.root, msg.file).replaceAll("\\", "/")}${line ? `:${line}` : ""}`;
|
|
4431
|
+
console.warn(` ${cfg.sourceLocale}: ${key} ${verb} the text written in ${place}, the code owns it: edit it there`);
|
|
4432
|
+
}
|
|
4433
|
+
}
|
|
4434
|
+
function reportMissed(cfg, registry) {
|
|
4435
|
+
const locate = createLocator();
|
|
4436
|
+
for (const { name, file, start } of registry.missed()) {
|
|
4437
|
+
const line = locate(file, start);
|
|
4438
|
+
const place = `${relative(cfg.root, file).replaceAll("\\", "/")}${line ? `:${line}` : ""}`;
|
|
4439
|
+
console.warn(` ${place}: ${name}\`…\` is never extracted, so it stays in the source language: name it t`);
|
|
4440
|
+
}
|
|
4441
|
+
}
|
|
4442
|
+
function reportPositional(cfg, registry) {
|
|
4443
|
+
for (const msg of registry.messages().values()) {
|
|
4444
|
+
const nameless = msg.params.filter((param) => /^_\d+$/.test(param.name));
|
|
4445
|
+
if (nameless.length === 0) continue;
|
|
4446
|
+
const file = relative(cfg.root, msg.file).replaceAll("\\", "/");
|
|
4447
|
+
const names = nameless.map((param) => `{${param.name}}`).join(", ");
|
|
4448
|
+
console.warn(` ${file}: ${names} in "${msg.message}" reaches the translator without a name, put the value in a named variable first`);
|
|
4449
|
+
}
|
|
4450
|
+
}
|
|
4451
|
+
function checkState(cfg, gating) {
|
|
4452
|
+
try {
|
|
4453
|
+
return loadState(cfg);
|
|
4454
|
+
} catch (error) {
|
|
4455
|
+
if (gating) throw error;
|
|
4456
|
+
console.warn(`${formatCliError(error)}, so outdated translations are not reported`);
|
|
4457
|
+
return {
|
|
4458
|
+
drafts: {},
|
|
4459
|
+
fingerprints: {}
|
|
4460
|
+
};
|
|
4461
|
+
}
|
|
4462
|
+
}
|
|
4463
|
+
function byLocale(entries) {
|
|
4464
|
+
const out = /* @__PURE__ */ new Map();
|
|
4465
|
+
for (const { locale, key } of entries) out.set(locale, [...out.get(locale) ?? [], key]);
|
|
4466
|
+
return [...out];
|
|
4467
|
+
}
|
|
4034
4468
|
function reportEscapedSyntax(cfg, registry) {
|
|
4035
4469
|
for (const msg of registry.messages().values()) {
|
|
4036
4470
|
const slice = escapedSyntax(msg.message);
|
|
@@ -4058,7 +4492,11 @@ const COMMAND_FLAGS = {
|
|
|
4058
4492
|
wrap: ["write"],
|
|
4059
4493
|
migrate: ["write", "plurals"],
|
|
4060
4494
|
status: ["json"],
|
|
4061
|
-
check: [
|
|
4495
|
+
check: [
|
|
4496
|
+
"reporter",
|
|
4497
|
+
"drafts",
|
|
4498
|
+
"outdated"
|
|
4499
|
+
],
|
|
4062
4500
|
translate: ["model", "dry-run"],
|
|
4063
4501
|
review: ["approve", "locale"],
|
|
4064
4502
|
export: [
|
|
@@ -4069,7 +4507,8 @@ const COMMAND_FLAGS = {
|
|
|
4069
4507
|
import: [
|
|
4070
4508
|
"locale",
|
|
4071
4509
|
"overwrite",
|
|
4072
|
-
"dry-run"
|
|
4510
|
+
"dry-run",
|
|
4511
|
+
"draft"
|
|
4073
4512
|
],
|
|
4074
4513
|
pseudo: ["locale"],
|
|
4075
4514
|
render: [
|
|
@@ -4099,6 +4538,6 @@ runCli().catch((error) => {
|
|
|
4099
4538
|
process.exitCode = 1;
|
|
4100
4539
|
});
|
|
4101
4540
|
//#endregion
|
|
4102
|
-
export { isModuleNotFound as t };
|
|
4541
|
+
export { isModuleNotFound as n, counted as t };
|
|
4103
4542
|
|
|
4104
4543
|
//# sourceMappingURL=cli.js.map
|