@verbaly/compiler 0.11.0 → 0.13.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 +3 -1
- package/dist/claude-Bi_Ex2hm.js +53 -0
- package/dist/claude-Bi_Ex2hm.js.map +1 -0
- package/dist/cli.js +1150 -975
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +150 -105
- package/dist/index.js +1080 -968
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
- package/dist/claude-XSRCRBAQ.js +0 -63
- package/dist/claude-XSRCRBAQ.js.map +0 -1
package/dist/cli.js
CHANGED
|
@@ -1,145 +1,151 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
4
|
+
import { dirname, join, relative, resolve } from "node:path";
|
|
5
|
+
import { RICH_TAGS, createVerbaly, parse, parseTags, safeHref } from "verbaly";
|
|
6
|
+
import { pathToFileURL } from "node:url";
|
|
7
|
+
import { glob } from "tinyglobby";
|
|
8
|
+
import { parse as parse$1 } from "@babel/parser";
|
|
9
|
+
import { createHash } from "node:crypto";
|
|
10
|
+
import MagicString from "magic-string";
|
|
11
|
+
//#region src/catalog.ts
|
|
9
12
|
function catalogPath(cfg, locale) {
|
|
10
|
-
|
|
13
|
+
return join(cfg.dir, `${locale}.json`);
|
|
11
14
|
}
|
|
12
15
|
function readCatalog(cfg, locale) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(readFileSync(catalogPath(cfg, locale), "utf8"));
|
|
18
|
+
} catch {
|
|
19
|
+
return {};
|
|
20
|
+
}
|
|
18
21
|
}
|
|
19
22
|
function loadCatalogs(cfg) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
+
const catalogs = {};
|
|
24
|
+
for (const locale of cfg.locales) catalogs[locale] = readCatalog(cfg, locale);
|
|
25
|
+
return catalogs;
|
|
23
26
|
}
|
|
24
27
|
function serializeCatalog(catalog) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
28
|
+
const sorted = {};
|
|
29
|
+
for (const key of Object.keys(catalog).sort()) {
|
|
30
|
+
const value = catalog[key];
|
|
31
|
+
if (value !== void 0) sorted[key] = value;
|
|
32
|
+
}
|
|
33
|
+
return JSON.stringify(sorted, null, 2) + "\n";
|
|
31
34
|
}
|
|
32
35
|
function writeCatalog(cfg, locale, catalog) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
const serialized = serializeCatalog(catalog);
|
|
37
|
+
mkdirSync(cfg.dir, { recursive: true });
|
|
38
|
+
writeFileSync(catalogPath(cfg, locale), serialized);
|
|
39
|
+
return serialized;
|
|
37
40
|
}
|
|
38
|
-
|
|
39
|
-
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/check.ts
|
|
40
43
|
function check(cfg, catalogs, registry) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
44
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
45
|
+
const extracted = registry.messages();
|
|
46
|
+
const unknown = [];
|
|
47
|
+
for (const [key, files] of registry.usedKeys()) if (!(extracted.has(key) || cfg.locales.some((locale) => catalogs[locale]?.[key] !== void 0))) unknown.push({
|
|
48
|
+
key,
|
|
49
|
+
files
|
|
50
|
+
});
|
|
51
|
+
const needed = /* @__PURE__ */ new Set([...extracted.keys(), ...Object.keys(source)]);
|
|
52
|
+
const missing = [];
|
|
53
|
+
for (const key of extracted.keys()) if (!source[key]) missing.push({
|
|
54
|
+
locale: cfg.sourceLocale,
|
|
55
|
+
key,
|
|
56
|
+
source: extracted.get(key)?.message
|
|
57
|
+
});
|
|
58
|
+
for (const locale of cfg.locales) {
|
|
59
|
+
if (locale === cfg.sourceLocale) continue;
|
|
60
|
+
for (const key of needed) if (!catalogs[locale]?.[key]) missing.push({
|
|
61
|
+
locale,
|
|
62
|
+
key,
|
|
63
|
+
source: source[key] ?? extracted.get(key)?.message
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
ok: missing.length === 0 && unknown.length === 0,
|
|
68
|
+
missing,
|
|
69
|
+
unknown
|
|
70
|
+
};
|
|
64
71
|
}
|
|
65
72
|
function formatCheckResult(result) {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
80
|
-
return lines.join("\n");
|
|
73
|
+
const lines = [];
|
|
74
|
+
if (result.missing.length > 0) {
|
|
75
|
+
lines.push("missing translations:");
|
|
76
|
+
for (const entry of result.missing) {
|
|
77
|
+
const hint = entry.source ? ` — "${truncate(entry.source, 40)}"` : "";
|
|
78
|
+
lines.push(` [${entry.locale}] ${entry.key}${hint}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
if (result.unknown.length > 0) {
|
|
82
|
+
lines.push("unknown keys (not in any catalog):");
|
|
83
|
+
for (const entry of result.unknown) lines.push(` ${entry.key} — used in ${entry.files.join(", ")}`);
|
|
84
|
+
}
|
|
85
|
+
return lines.join("\n");
|
|
81
86
|
}
|
|
82
87
|
function truncate(text, max) {
|
|
83
|
-
|
|
88
|
+
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
84
89
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/params.ts
|
|
92
|
+
const PLURAL_KEYS = /* @__PURE__ */ new Set([
|
|
93
|
+
"zero",
|
|
94
|
+
"one",
|
|
95
|
+
"two",
|
|
96
|
+
"few",
|
|
97
|
+
"many"
|
|
98
|
+
]);
|
|
99
|
+
const NUMBER_FORMATS = /* @__PURE__ */ new Set([
|
|
100
|
+
"number",
|
|
101
|
+
"integer",
|
|
102
|
+
"percent",
|
|
103
|
+
"currency"
|
|
104
|
+
]);
|
|
105
|
+
const DATE_FORMATS = /* @__PURE__ */ new Set(["date", "time"]);
|
|
95
106
|
function collectParams(message) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
107
|
+
const out = /* @__PURE__ */ new Map();
|
|
108
|
+
visit(parse(message), out);
|
|
109
|
+
return out;
|
|
99
110
|
}
|
|
100
111
|
function visit(nodes, out) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
} else {
|
|
117
|
-
types.add("unknown");
|
|
118
|
-
}
|
|
119
|
-
}
|
|
112
|
+
for (const node of nodes) {
|
|
113
|
+
if (node.kind !== "param") continue;
|
|
114
|
+
const types = out.get(node.name) ?? /* @__PURE__ */ new Set();
|
|
115
|
+
out.set(node.name, types);
|
|
116
|
+
if (node.variants) {
|
|
117
|
+
for (const [key, body] of node.variants) {
|
|
118
|
+
if (PLURAL_KEYS.has(key) || /^=\d+$/.test(key)) types.add("number");
|
|
119
|
+
else if (key !== "other") types.add("string");
|
|
120
|
+
visit(body, out);
|
|
121
|
+
}
|
|
122
|
+
if (types.size === 0) types.add("string");
|
|
123
|
+
} else if (node.format && NUMBER_FORMATS.has(node.format)) types.add("number");
|
|
124
|
+
else if (node.format && DATE_FORMATS.has(node.format)) types.add("date");
|
|
125
|
+
else types.add("unknown");
|
|
126
|
+
}
|
|
120
127
|
}
|
|
121
128
|
function renderParamType(types) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
129
|
+
if (types.has("unknown") || types.size === 0) return "unknown";
|
|
130
|
+
const parts = [];
|
|
131
|
+
if (types.has("number")) parts.push("number");
|
|
132
|
+
if (types.has("string")) parts.push("string");
|
|
133
|
+
if (types.has("date")) parts.push("Date | number | string");
|
|
134
|
+
return parts.join(" | ");
|
|
128
135
|
}
|
|
129
|
-
|
|
130
|
-
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/codegen.ts
|
|
131
138
|
function generateDts(entries) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
return `// generated by verbaly \u2014 do not edit
|
|
139
|
+
const lines = [];
|
|
140
|
+
for (const [key, message] of [...entries.entries()].sort(([a], [b]) => a.localeCompare(b))) {
|
|
141
|
+
const params = collectParams(message);
|
|
142
|
+
if (params.size === 0) lines.push(` ${JSON.stringify(key)}: never;`);
|
|
143
|
+
else {
|
|
144
|
+
const fields = [...params.entries()].map(([name, types]) => `${JSON.stringify(name)}: ${renderParamType(types)}`).join("; ");
|
|
145
|
+
lines.push(` ${JSON.stringify(key)}: { ${fields} };`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
return `// generated by verbaly — do not edit
|
|
143
149
|
declare module 'virtual:verbaly' {
|
|
144
150
|
export interface VerbalyMessages {
|
|
145
151
|
${lines.join("\n")}
|
|
@@ -171,830 +177,979 @@ declare module 'virtual:verbaly/locale/*' {
|
|
|
171
177
|
`;
|
|
172
178
|
}
|
|
173
179
|
function writeDts(cfg, entries) {
|
|
174
|
-
|
|
180
|
+
writeFileSync(join(cfg.root, "verbaly.d.ts"), generateDts(entries));
|
|
175
181
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
import { existsSync, readFileSync as readFileSync2, readdirSync } from "fs";
|
|
179
|
-
import { join as join3, resolve } from "path";
|
|
180
|
-
import { pathToFileURL } from "url";
|
|
182
|
+
//#endregion
|
|
183
|
+
//#region src/config.ts
|
|
181
184
|
function resolveConfig(config = {}) {
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
185
|
+
const root = resolve(config.root ?? process.cwd());
|
|
186
|
+
const dir = resolve(root, config.dir ?? "locales");
|
|
187
|
+
const sourceLocale = config.sourceLocale ?? "en";
|
|
188
|
+
const locales = /* @__PURE__ */ new Set([sourceLocale, ...config.locales ?? []]);
|
|
189
|
+
if (existsSync(dir)) {
|
|
190
|
+
for (const file of readdirSync(dir)) if (file.endsWith(".json")) locales.add(file.slice(0, -5));
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
root,
|
|
194
|
+
dir,
|
|
195
|
+
sourceLocale,
|
|
196
|
+
locales: [...locales],
|
|
197
|
+
include: config.include ?? ["src/**/*.{js,jsx,ts,tsx,mjs,mts}"],
|
|
198
|
+
exclude: config.exclude ?? ["**/node_modules/**", "**/dist/**"],
|
|
199
|
+
translate: config.translate ?? {},
|
|
200
|
+
render: config.render ?? {}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const CONFIG_FILES = [
|
|
204
|
+
"verbaly.config.js",
|
|
205
|
+
"verbaly.config.mjs",
|
|
206
|
+
"verbaly.config.ts",
|
|
207
|
+
"verbaly.config.mts",
|
|
208
|
+
"verbaly.config.json"
|
|
209
|
+
];
|
|
210
|
+
function findConfigFile(root) {
|
|
211
|
+
return CONFIG_FILES.find((name) => existsSync(join(root, name)));
|
|
201
212
|
}
|
|
202
213
|
async function loadConfigFile(root) {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
const jsonPath = join3(root, "verbaly.config.json");
|
|
215
|
-
if (existsSync(jsonPath)) {
|
|
216
|
-
return JSON.parse(readFileSync2(jsonPath, "utf8"));
|
|
217
|
-
}
|
|
218
|
-
return {};
|
|
214
|
+
for (const name of ["verbaly.config.js", "verbaly.config.mjs"]) {
|
|
215
|
+
const path = join(root, name);
|
|
216
|
+
if (existsSync(path)) return (await import(pathToFileURL(path).href)).default ?? {};
|
|
217
|
+
}
|
|
218
|
+
for (const name of ["verbaly.config.ts", "verbaly.config.mts"]) {
|
|
219
|
+
const path = join(root, name);
|
|
220
|
+
if (existsSync(path)) return loadTsConfig(path);
|
|
221
|
+
}
|
|
222
|
+
const jsonPath = join(root, "verbaly.config.json");
|
|
223
|
+
if (existsSync(jsonPath)) return JSON.parse(readFileSync(jsonPath, "utf8"));
|
|
224
|
+
return {};
|
|
219
225
|
}
|
|
220
226
|
async function loadTsConfig(path) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
throw error;
|
|
232
|
-
}
|
|
227
|
+
try {
|
|
228
|
+
const { bundleRequire } = await import("bundle-require");
|
|
229
|
+
const { mod } = await bundleRequire({ filepath: path });
|
|
230
|
+
return mod.default ?? {};
|
|
231
|
+
} catch (error) {
|
|
232
|
+
if (isModuleNotFound(error, "esbuild")) throw new Error(`[verbaly] ${path} needs esbuild — install it: pnpm add -D esbuild`, { cause: error });
|
|
233
|
+
throw error;
|
|
234
|
+
}
|
|
233
235
|
}
|
|
234
236
|
function isModuleNotFound(error, name) {
|
|
235
|
-
|
|
237
|
+
return error instanceof Error && error.code === "ERR_MODULE_NOT_FOUND" && error.message.includes(name);
|
|
236
238
|
}
|
|
237
239
|
async function loadConfig(root, overrides = {}) {
|
|
238
|
-
|
|
239
|
-
|
|
240
|
+
return resolveConfig({
|
|
241
|
+
...await loadConfigFile(root),
|
|
242
|
+
...definedOnly(overrides),
|
|
243
|
+
root
|
|
244
|
+
});
|
|
240
245
|
}
|
|
241
246
|
function definedOnly(config) {
|
|
242
|
-
|
|
243
|
-
Object.entries(config).filter(([, value]) => value !== void 0)
|
|
244
|
-
);
|
|
247
|
+
return Object.fromEntries(Object.entries(config).filter(([, value]) => value !== void 0));
|
|
245
248
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
import { readFileSync as readFileSync3 } from "fs";
|
|
249
|
-
import { glob } from "tinyglobby";
|
|
250
|
-
|
|
251
|
-
// src/analyze.ts
|
|
252
|
-
import { parse as parse2 } from "@babel/parser";
|
|
253
|
-
|
|
254
|
-
// src/key.ts
|
|
255
|
-
import { createHash } from "crypto";
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/key.ts
|
|
256
251
|
function stableKey(message) {
|
|
257
|
-
|
|
252
|
+
return createHash("sha256").update(message).digest("base64url").slice(0, 8);
|
|
258
253
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
254
|
+
//#endregion
|
|
255
|
+
//#region src/analyze.ts
|
|
256
|
+
const SKIP_KEYS = /* @__PURE__ */ new Set([
|
|
257
|
+
"loc",
|
|
258
|
+
"leadingComments",
|
|
259
|
+
"trailingComments",
|
|
260
|
+
"innerComments",
|
|
261
|
+
"extra"
|
|
262
|
+
]);
|
|
262
263
|
function analyze(code, file) {
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
264
|
+
const ast = parse$1(code, {
|
|
265
|
+
sourceType: "module",
|
|
266
|
+
errorRecovery: true,
|
|
267
|
+
plugins: /\.[jt]sx$/.test(file) ? ["typescript", "jsx"] : ["typescript"]
|
|
268
|
+
});
|
|
269
|
+
const tagged = [];
|
|
270
|
+
const usedKeys = [];
|
|
271
|
+
walk(ast.program, (node) => {
|
|
272
|
+
if (node.type === "TaggedTemplateExpression") {
|
|
273
|
+
const tag = node.tag;
|
|
274
|
+
const explicit = explicitId(tag);
|
|
275
|
+
if (!explicit && !isTReference(tag)) return;
|
|
276
|
+
const quasi = node.quasi;
|
|
277
|
+
const message = buildMessage(code, quasi);
|
|
278
|
+
if (!message) return;
|
|
279
|
+
tagged.push({
|
|
280
|
+
key: explicit ? explicit.key : stableKey(message.text),
|
|
281
|
+
message: message.text,
|
|
282
|
+
params: message.params,
|
|
283
|
+
start: node.start,
|
|
284
|
+
end: node.end,
|
|
285
|
+
tagStart: explicit ? explicit.refStart : tag.start,
|
|
286
|
+
tagEnd: explicit ? explicit.refEnd : tag.end,
|
|
287
|
+
file
|
|
288
|
+
});
|
|
289
|
+
} else if (node.type === "CallExpression") {
|
|
290
|
+
const callee = node.callee;
|
|
291
|
+
if (!isTReference(callee)) return;
|
|
292
|
+
const first = node.arguments[0];
|
|
293
|
+
if (first?.type === "StringLiteral") usedKeys.push({
|
|
294
|
+
key: first.value,
|
|
295
|
+
file
|
|
296
|
+
});
|
|
297
|
+
} else if (node.type === "JSXElement") handleTrans(code, node, file, tagged, usedKeys);
|
|
298
|
+
});
|
|
299
|
+
return {
|
|
300
|
+
tagged,
|
|
301
|
+
usedKeys
|
|
302
|
+
};
|
|
302
303
|
}
|
|
303
304
|
function handleTrans(code, node, file, tagged, usedKeys) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
305
|
+
const opening = node.openingElement;
|
|
306
|
+
const nameNode = opening.name;
|
|
307
|
+
if (nameNode.type !== "JSXIdentifier" || nameNode.name !== "Trans") return;
|
|
308
|
+
const attrs = opening.attributes;
|
|
309
|
+
const idAttr = attrs.find((a) => a.type === "JSXAttribute" && a.name.name === "id");
|
|
310
|
+
const children = node.children;
|
|
311
|
+
if (idAttr) {
|
|
312
|
+
const value = idAttr.value;
|
|
313
|
+
if (value?.type !== "StringLiteral") return;
|
|
314
|
+
const id = value.value;
|
|
315
|
+
if (attrs.length === 1 && children?.length) {
|
|
316
|
+
const built = buildTransMessage(code, children);
|
|
317
|
+
if (built?.text.trim()) {
|
|
318
|
+
tagged.push({
|
|
319
|
+
key: id,
|
|
320
|
+
message: built.text,
|
|
321
|
+
params: built.params,
|
|
322
|
+
start: node.start,
|
|
323
|
+
end: node.end,
|
|
324
|
+
tagStart: nameNode.start,
|
|
325
|
+
tagEnd: nameNode.end,
|
|
326
|
+
file,
|
|
327
|
+
jsx: {
|
|
328
|
+
name: nameNode.name,
|
|
329
|
+
components: built.components
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
usedKeys.push({
|
|
336
|
+
key: id,
|
|
337
|
+
file
|
|
338
|
+
});
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (attrs.length > 0) return;
|
|
342
|
+
if (!children?.length) return;
|
|
343
|
+
const built = buildTransMessage(code, children);
|
|
344
|
+
if (!built || !built.text.trim()) return;
|
|
345
|
+
tagged.push({
|
|
346
|
+
key: stableKey(built.text),
|
|
347
|
+
message: built.text,
|
|
348
|
+
params: built.params,
|
|
349
|
+
start: node.start,
|
|
350
|
+
end: node.end,
|
|
351
|
+
tagStart: nameNode.start,
|
|
352
|
+
tagEnd: nameNode.end,
|
|
353
|
+
file,
|
|
354
|
+
jsx: {
|
|
355
|
+
name: nameNode.name,
|
|
356
|
+
components: built.components
|
|
357
|
+
}
|
|
358
|
+
});
|
|
349
359
|
}
|
|
350
360
|
function explicitId(tag) {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
361
|
+
if (tag.type !== "CallExpression") return void 0;
|
|
362
|
+
const callee = tag.callee;
|
|
363
|
+
if (callee.type !== "MemberExpression" || callee.computed) return void 0;
|
|
364
|
+
const prop = callee.property;
|
|
365
|
+
if (prop.type !== "Identifier" || prop.name !== "id") return void 0;
|
|
366
|
+
const obj = callee.object;
|
|
367
|
+
if (!isTReference(obj)) return void 0;
|
|
368
|
+
const args = tag.arguments;
|
|
369
|
+
const first = args[0];
|
|
370
|
+
if (args.length !== 1 || first?.type !== "StringLiteral") return void 0;
|
|
371
|
+
return {
|
|
372
|
+
key: first.value,
|
|
373
|
+
refStart: obj.start,
|
|
374
|
+
refEnd: obj.end
|
|
375
|
+
};
|
|
362
376
|
}
|
|
363
377
|
function buildTransMessage(code, children) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
378
|
+
const params = [];
|
|
379
|
+
const components = [];
|
|
380
|
+
const takenParams = /* @__PURE__ */ new Map();
|
|
381
|
+
const takenTags = /* @__PURE__ */ new Map();
|
|
382
|
+
function walkChildren(nodes) {
|
|
383
|
+
let text = "";
|
|
384
|
+
for (const child of nodes) if (child.type === "JSXText") text += escapeText(cleanJsxText(child.value));
|
|
385
|
+
else if (child.type === "JSXExpressionContainer") {
|
|
386
|
+
const expr = child.expression;
|
|
387
|
+
if (expr.type === "JSXEmptyExpression") continue;
|
|
388
|
+
if (expr.type === "StringLiteral") {
|
|
389
|
+
text += escapeText(expr.value);
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
if (expr.type === "TaggedTemplateExpression") return void 0;
|
|
393
|
+
const source = code.slice(expr.start, expr.end);
|
|
394
|
+
const name = uniqueName(deriveName(expr, params.length), source, takenParams);
|
|
395
|
+
params.push({
|
|
396
|
+
name,
|
|
397
|
+
start: expr.start,
|
|
398
|
+
end: expr.end
|
|
399
|
+
});
|
|
400
|
+
text += `{${name}}`;
|
|
401
|
+
} else if (child.type === "JSXElement") {
|
|
402
|
+
const opening = child.openingElement;
|
|
403
|
+
const nameNode = opening.name;
|
|
404
|
+
if (nameNode.type !== "JSXIdentifier" || nameNode.name === "Trans") return void 0;
|
|
405
|
+
const source = selfClosedSource(code, opening);
|
|
406
|
+
const tag = uniqueName(nameNode.name.toLowerCase(), source, takenTags);
|
|
407
|
+
if (!components.some((c) => c.name === tag)) components.push({
|
|
408
|
+
name: tag,
|
|
409
|
+
source
|
|
410
|
+
});
|
|
411
|
+
if (!child.closingElement) text += `<${tag}/>`;
|
|
412
|
+
else {
|
|
413
|
+
const inner = walkChildren(child.children);
|
|
414
|
+
if (inner === void 0) return void 0;
|
|
415
|
+
text += `<${tag}>${inner}</${tag}>`;
|
|
416
|
+
}
|
|
417
|
+
} else return;
|
|
418
|
+
return text;
|
|
419
|
+
}
|
|
420
|
+
const text = walkChildren(children);
|
|
421
|
+
if (text === void 0) return void 0;
|
|
422
|
+
return {
|
|
423
|
+
text,
|
|
424
|
+
params,
|
|
425
|
+
components
|
|
426
|
+
};
|
|
408
427
|
}
|
|
409
428
|
function cleanJsxText(raw) {
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
429
|
+
const lines = raw.split(/\r\n|[\r\n]/);
|
|
430
|
+
let out = "";
|
|
431
|
+
for (let i = 0; i < lines.length; i++) {
|
|
432
|
+
let line = lines[i].replace(/\t/g, " ");
|
|
433
|
+
if (i !== 0) line = line.replace(/^ +/, "");
|
|
434
|
+
if (i !== lines.length - 1) line = line.replace(/ +$/, "");
|
|
435
|
+
if (!line) continue;
|
|
436
|
+
if (out && i !== 0) out += " ";
|
|
437
|
+
out += line;
|
|
438
|
+
}
|
|
439
|
+
return out;
|
|
421
440
|
}
|
|
422
441
|
function selfClosedSource(code, opening) {
|
|
423
|
-
|
|
424
|
-
|
|
442
|
+
const src = code.slice(opening.start, opening.end);
|
|
443
|
+
return src.endsWith("/>") ? src : `${src.slice(0, -1).trimEnd()} />`;
|
|
425
444
|
}
|
|
426
445
|
function isTReference(node) {
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
446
|
+
if (node.type === "Identifier") return node.name === "t";
|
|
447
|
+
if (node.type === "MemberExpression" && !node.computed) {
|
|
448
|
+
const prop = node.property;
|
|
449
|
+
return prop.type === "Identifier" && prop.name === "t";
|
|
450
|
+
}
|
|
451
|
+
return false;
|
|
433
452
|
}
|
|
434
453
|
function buildMessage(code, quasi) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
454
|
+
const quasis = quasi.quasis;
|
|
455
|
+
const expressions = quasi.expressions;
|
|
456
|
+
const params = [];
|
|
457
|
+
const taken = /* @__PURE__ */ new Map();
|
|
458
|
+
let text = escapeText(cookedValue(quasis[0]));
|
|
459
|
+
for (let i = 0; i < expressions.length; i++) {
|
|
460
|
+
const expr = expressions[i];
|
|
461
|
+
if (!expr) return void 0;
|
|
462
|
+
const source = code.slice(expr.start, expr.end);
|
|
463
|
+
const name = uniqueName(deriveName(expr, i), source, taken);
|
|
464
|
+
params.push({
|
|
465
|
+
name,
|
|
466
|
+
start: expr.start,
|
|
467
|
+
end: expr.end
|
|
468
|
+
});
|
|
469
|
+
text += `{${name}}` + escapeText(cookedValue(quasis[i + 1]));
|
|
470
|
+
}
|
|
471
|
+
return {
|
|
472
|
+
text,
|
|
473
|
+
params
|
|
474
|
+
};
|
|
449
475
|
}
|
|
450
476
|
function cookedValue(element) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
477
|
+
if (!element) return "";
|
|
478
|
+
const value = element.value;
|
|
479
|
+
return value.cooked ?? value.raw;
|
|
454
480
|
}
|
|
455
481
|
function escapeText(text) {
|
|
456
|
-
|
|
482
|
+
return text.replace(/[{}]/g, (m) => m + m);
|
|
457
483
|
}
|
|
458
484
|
function deriveName(expr, index) {
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
485
|
+
if (expr.type === "Identifier") return expr.name;
|
|
486
|
+
if (expr.type === "MemberExpression" && !expr.computed) {
|
|
487
|
+
const prop = expr.property;
|
|
488
|
+
if (prop.type === "Identifier") return prop.name;
|
|
489
|
+
}
|
|
490
|
+
return `_${index}`;
|
|
465
491
|
}
|
|
466
492
|
function uniqueName(base, source, taken) {
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
}
|
|
476
|
-
function walk(node,
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
} else if (isNode(value)) {
|
|
485
|
-
walk(value, visit2);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
493
|
+
let name = base;
|
|
494
|
+
let n = 2;
|
|
495
|
+
while (taken.has(name) && taken.get(name) !== source) {
|
|
496
|
+
name = `${base}${n}`;
|
|
497
|
+
n += 1;
|
|
498
|
+
}
|
|
499
|
+
taken.set(name, source);
|
|
500
|
+
return name;
|
|
501
|
+
}
|
|
502
|
+
function walk(node, visit) {
|
|
503
|
+
visit(node);
|
|
504
|
+
for (const [key, value] of Object.entries(node)) {
|
|
505
|
+
if (SKIP_KEYS.has(key)) continue;
|
|
506
|
+
if (Array.isArray(value)) {
|
|
507
|
+
for (const item of value) if (isNode(item)) walk(item, visit);
|
|
508
|
+
} else if (isNode(value)) walk(value, visit);
|
|
509
|
+
}
|
|
488
510
|
}
|
|
489
511
|
function isNode(value) {
|
|
490
|
-
|
|
512
|
+
return typeof value === "object" && value !== null && typeof value.type === "string";
|
|
491
513
|
}
|
|
492
|
-
|
|
493
|
-
|
|
514
|
+
//#endregion
|
|
515
|
+
//#region src/registry.ts
|
|
494
516
|
var MessageRegistry = class {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
for (const used of analysis.usedKeys) {
|
|
524
|
-
const files = out.get(used.key) ?? [];
|
|
525
|
-
if (!files.includes(used.file)) files.push(used.file);
|
|
526
|
-
out.set(used.key, files);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
return out;
|
|
530
|
-
}
|
|
517
|
+
files = /* @__PURE__ */ new Map();
|
|
518
|
+
update(file, analysis) {
|
|
519
|
+
this.files.set(file, analysis);
|
|
520
|
+
}
|
|
521
|
+
remove(file) {
|
|
522
|
+
this.files.delete(file);
|
|
523
|
+
}
|
|
524
|
+
messages() {
|
|
525
|
+
const out = /* @__PURE__ */ new Map();
|
|
526
|
+
for (const analysis of this.files.values()) for (const msg of analysis.tagged) {
|
|
527
|
+
const existing = out.get(msg.key);
|
|
528
|
+
if (existing) {
|
|
529
|
+
if (existing.message !== msg.message) console.warn(`[verbaly] key collision "${msg.key}": ${JSON.stringify(existing.message)} vs ${JSON.stringify(msg.message)} — second dropped.`);
|
|
530
|
+
continue;
|
|
531
|
+
}
|
|
532
|
+
out.set(msg.key, msg);
|
|
533
|
+
}
|
|
534
|
+
return out;
|
|
535
|
+
}
|
|
536
|
+
usedKeys() {
|
|
537
|
+
const out = /* @__PURE__ */ new Map();
|
|
538
|
+
for (const analysis of this.files.values()) for (const used of analysis.usedKeys) {
|
|
539
|
+
const files = out.get(used.key) ?? [];
|
|
540
|
+
if (!files.includes(used.file)) files.push(used.file);
|
|
541
|
+
out.set(used.key, files);
|
|
542
|
+
}
|
|
543
|
+
return out;
|
|
544
|
+
}
|
|
531
545
|
};
|
|
532
|
-
|
|
533
|
-
|
|
546
|
+
//#endregion
|
|
547
|
+
//#region src/extract.ts
|
|
534
548
|
async function extractProject(cfg) {
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
}
|
|
544
|
-
return registry;
|
|
549
|
+
const files = await glob(cfg.include, {
|
|
550
|
+
cwd: cfg.root,
|
|
551
|
+
ignore: cfg.exclude,
|
|
552
|
+
absolute: true
|
|
553
|
+
});
|
|
554
|
+
const registry = new MessageRegistry();
|
|
555
|
+
for (const file of files) registry.update(file, analyze(readFileSync(file, "utf8"), file));
|
|
556
|
+
return registry;
|
|
545
557
|
}
|
|
546
558
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
}
|
|
564
|
-
}
|
|
565
|
-
}
|
|
566
|
-
return { added };
|
|
559
|
+
const added = {};
|
|
560
|
+
const source = catalogs[cfg.sourceLocale] ??= {};
|
|
561
|
+
for (const [key, msg] of registry.messages()) if (source[key] !== msg.message) {
|
|
562
|
+
source[key] = msg.message;
|
|
563
|
+
(added[cfg.sourceLocale] ??= []).push(key);
|
|
564
|
+
}
|
|
565
|
+
const needed = Object.keys(source);
|
|
566
|
+
for (const locale of cfg.locales) {
|
|
567
|
+
if (locale === cfg.sourceLocale) continue;
|
|
568
|
+
const catalog = catalogs[locale] ??= {};
|
|
569
|
+
for (const key of needed) if (catalog[key] === void 0) {
|
|
570
|
+
catalog[key] = "";
|
|
571
|
+
(added[locale] ??= []).push(key);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
return { added };
|
|
567
575
|
}
|
|
568
576
|
function pruneCatalogs(cfg, catalogs, registry) {
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
}
|
|
581
|
-
return removed;
|
|
577
|
+
const keep = /* @__PURE__ */ new Set([...registry.messages().keys(), ...registry.usedKeys().keys()]);
|
|
578
|
+
const removed = {};
|
|
579
|
+
for (const locale of cfg.locales) {
|
|
580
|
+
const catalog = catalogs[locale];
|
|
581
|
+
if (!catalog) continue;
|
|
582
|
+
for (const key of Object.keys(catalog)) if (!keep.has(key)) {
|
|
583
|
+
delete catalog[key];
|
|
584
|
+
(removed[locale] ??= []).push(key);
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
return removed;
|
|
582
588
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
589
|
+
//#endregion
|
|
590
|
+
//#region src/init.ts
|
|
591
|
+
const BUNDLERS = [
|
|
592
|
+
"vite",
|
|
593
|
+
"webpack",
|
|
594
|
+
"rollup",
|
|
595
|
+
"rspack",
|
|
596
|
+
"esbuild"
|
|
597
|
+
];
|
|
598
|
+
function detectBundler(root) {
|
|
599
|
+
const path = join(root, "package.json");
|
|
600
|
+
if (!existsSync(path)) return void 0;
|
|
601
|
+
try {
|
|
602
|
+
const pkg = JSON.parse(readFileSync(path, "utf8"));
|
|
603
|
+
const deps = {
|
|
604
|
+
...pkg.dependencies,
|
|
605
|
+
...pkg.devDependencies
|
|
606
|
+
};
|
|
607
|
+
return BUNDLERS.find((name) => deps[name]);
|
|
608
|
+
} catch {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
function configSource(options, typescript) {
|
|
613
|
+
const fields = [` sourceLocale: '${options.sourceLocale ?? "en"}',`];
|
|
614
|
+
if (options.locales?.length) fields.push(` locales: [${options.locales.map((l) => `'${l}'`).join(", ")}],`);
|
|
615
|
+
if (options.dir) fields.push(` dir: '${options.dir}',`);
|
|
616
|
+
const body = `export default {\n${fields.join("\n")}\n}`;
|
|
617
|
+
if (typescript) return `import type { VerbalyConfig } from '@verbaly/compiler';\n\n${body} satisfies VerbalyConfig;\n`;
|
|
618
|
+
return `/** @type {import('@verbaly/compiler').VerbalyConfig} */\n${body};\n`;
|
|
619
|
+
}
|
|
620
|
+
function init(options = {}) {
|
|
621
|
+
const root = options.root ?? process.cwd();
|
|
622
|
+
const created = [];
|
|
623
|
+
const skipped = [];
|
|
624
|
+
const existing = findConfigFile(root);
|
|
625
|
+
const typescript = existsSync(join(root, "tsconfig.json"));
|
|
626
|
+
const configFile = existing ?? (typescript ? "verbaly.config.ts" : "verbaly.config.mjs");
|
|
627
|
+
if (existing) skipped.push(existing);
|
|
628
|
+
else {
|
|
629
|
+
writeFileSync(join(root, configFile), configSource(options, typescript));
|
|
630
|
+
created.push(configFile);
|
|
631
|
+
}
|
|
632
|
+
const dir = join(root, options.dir ?? "locales");
|
|
633
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
634
|
+
for (const locale of /* @__PURE__ */ new Set([options.sourceLocale ?? "en", ...options.locales ?? []])) {
|
|
635
|
+
const file = join(dir, `${locale}.json`);
|
|
636
|
+
const label = relative(root, file).replaceAll("\\", "/");
|
|
637
|
+
if (existsSync(file)) skipped.push(label);
|
|
638
|
+
else {
|
|
639
|
+
writeFileSync(file, "{}\n");
|
|
640
|
+
created.push(label);
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
const bundler = detectBundler(root);
|
|
644
|
+
const next = [];
|
|
645
|
+
if (bundler === "vite") next.push("install the plugin: pnpm add -D @verbaly/vite", "add verbaly() to the plugins in vite.config");
|
|
646
|
+
else if (bundler) next.push("install the plugin: pnpm add -D @verbaly/unplugin", `add the verbaly ${bundler} plugin to your build config`);
|
|
647
|
+
else next.push("run \"verbaly extract\" after writing your first t`…` message");
|
|
648
|
+
return {
|
|
649
|
+
created,
|
|
650
|
+
skipped,
|
|
651
|
+
bundler,
|
|
652
|
+
configFile,
|
|
653
|
+
next
|
|
654
|
+
};
|
|
655
|
+
}
|
|
656
|
+
//#endregion
|
|
657
|
+
//#region src/doctor.ts
|
|
658
|
+
const PREVIEW = 5;
|
|
659
|
+
async function doctor(cfg) {
|
|
660
|
+
const entries = [];
|
|
661
|
+
const ok = (check, message) => entries.push({
|
|
662
|
+
level: "ok",
|
|
663
|
+
check,
|
|
664
|
+
message
|
|
665
|
+
});
|
|
666
|
+
const warn = (check, message, fix) => entries.push({
|
|
667
|
+
level: "warn",
|
|
668
|
+
check,
|
|
669
|
+
message,
|
|
670
|
+
fix
|
|
671
|
+
});
|
|
672
|
+
const error = (check, message, fix) => entries.push({
|
|
673
|
+
level: "error",
|
|
674
|
+
check,
|
|
675
|
+
message,
|
|
676
|
+
fix
|
|
677
|
+
});
|
|
678
|
+
const rel = (path) => relative(cfg.root, path).replaceAll("\\", "/");
|
|
679
|
+
const configFile = findConfigFile(cfg.root);
|
|
680
|
+
if (configFile) ok("config", `${configFile} found`);
|
|
681
|
+
else warn("config", "no config file — running on defaults", "run `npx verbaly init`");
|
|
682
|
+
const catalogs = {};
|
|
683
|
+
let catalogsHealthy = true;
|
|
684
|
+
if (!existsSync(cfg.dir)) {
|
|
685
|
+
catalogsHealthy = false;
|
|
686
|
+
error("catalogs", `catalogs directory ${rel(cfg.dir)}/ does not exist`, "run `npx verbaly init` to scaffold it");
|
|
687
|
+
} else {
|
|
688
|
+
for (const locale of cfg.locales) {
|
|
689
|
+
const file = join(cfg.dir, `${locale}.json`);
|
|
690
|
+
if (!existsSync(file)) {
|
|
691
|
+
catalogsHealthy = false;
|
|
692
|
+
error(`locale ${locale}`, `${rel(file)} is missing`, "run `npx verbaly extract` to create it");
|
|
693
|
+
continue;
|
|
694
|
+
}
|
|
695
|
+
try {
|
|
696
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
697
|
+
const bad = Object.entries(parsed).find(([, value]) => typeof value !== "string");
|
|
698
|
+
if (bad) {
|
|
699
|
+
catalogsHealthy = false;
|
|
700
|
+
error(`locale ${locale}`, `${rel(file)} has a non-string value at "${bad[0]}"`, "catalogs are flat key → string JSON; fix the value");
|
|
701
|
+
} else catalogs[locale] = parsed;
|
|
702
|
+
} catch {
|
|
703
|
+
catalogsHealthy = false;
|
|
704
|
+
error(`locale ${locale}`, `${rel(file)} is not valid JSON`, "repair the file (or delete it and run `npx verbaly extract`)");
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
if (catalogsHealthy) ok("catalogs", `${cfg.locales.length} locales (${cfg.locales.join(", ")}) in ${rel(cfg.dir)}/`);
|
|
708
|
+
}
|
|
709
|
+
const source = catalogs[cfg.sourceLocale];
|
|
710
|
+
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`");
|
|
711
|
+
const deps = readDeps(cfg.root);
|
|
712
|
+
const bundler = detectBundler(cfg.root);
|
|
713
|
+
const wired = deps["@verbaly/vite"] || deps["@verbaly/unplugin"];
|
|
714
|
+
if (!bundler) ok("plugin", "no bundler detected — CLI flow (extract/check) applies");
|
|
715
|
+
else if (wired) ok("plugin", `${deps["@verbaly/vite"] ? "@verbaly/vite" : "@verbaly/unplugin"} installed for ${bundler}`);
|
|
716
|
+
else if (bundler === "vite") warn("plugin", "vite detected but @verbaly/vite is not installed", "pnpm add -D @verbaly/vite and add verbaly() to the plugins in vite.config");
|
|
717
|
+
else warn("plugin", `${bundler} detected but @verbaly/unplugin is not installed`, `pnpm add -D @verbaly/unplugin and add the verbaly ${bundler} plugin to your build config`);
|
|
718
|
+
if (source) {
|
|
719
|
+
const dtsPath = join(cfg.root, "verbaly.d.ts");
|
|
720
|
+
if (!existsSync(dtsPath)) warn("types", "verbaly.d.ts has not been generated", "run `npx verbaly extract`");
|
|
721
|
+
else if (readFileSync(dtsPath, "utf8") !== generateDts(new Map(Object.entries(source)))) warn("types", "verbaly.d.ts is stale", "run `npx verbaly extract`");
|
|
722
|
+
else ok("types", "verbaly.d.ts is up to date");
|
|
723
|
+
}
|
|
724
|
+
const registry = await extractProject(cfg);
|
|
725
|
+
if (source) {
|
|
726
|
+
const extracted = registry.messages();
|
|
727
|
+
const used = registry.usedKeys();
|
|
728
|
+
const orphans = Object.keys(source).filter((key) => !extracted.has(key) && !used.has(key));
|
|
729
|
+
if (orphans.length > 0) warn("orphans", `${orphans.length} catalog ${orphans.length === 1 ? "key is" : "keys are"} no longer referenced (${preview(orphans)})`, "run `npx verbaly extract --prune` to drop them");
|
|
730
|
+
else ok("orphans", "no orphan keys");
|
|
731
|
+
}
|
|
732
|
+
if (catalogsHealthy) {
|
|
733
|
+
const result = check(cfg, catalogs, registry);
|
|
734
|
+
if (result.unknown.length > 0) error("keys", `${result.unknown.length} unknown ${result.unknown.length === 1 ? "key" : "keys"} used in code (${preview(result.unknown.map((u) => u.key))})`, "fix the key or add it to the catalogs — `npx verbaly check` for details");
|
|
735
|
+
if (result.missing.length > 0) {
|
|
736
|
+
const locales = [...new Set(result.missing.map((m) => m.locale))];
|
|
737
|
+
warn("translations", `${result.missing.length} missing ${result.missing.length === 1 ? "translation" : "translations"} (${locales.join(", ")})`, "run `npx verbaly translate` or fill the catalogs — `npx verbaly check` for details");
|
|
738
|
+
}
|
|
739
|
+
if (result.ok) ok("translations", "all translations complete");
|
|
740
|
+
}
|
|
741
|
+
return {
|
|
742
|
+
ok: entries.every((entry) => entry.level !== "error"),
|
|
743
|
+
entries
|
|
744
|
+
};
|
|
745
|
+
}
|
|
746
|
+
function preview(keys) {
|
|
747
|
+
const head = keys.slice(0, PREVIEW).join(", ");
|
|
748
|
+
return keys.length > PREVIEW ? `${head}, …` : head;
|
|
749
|
+
}
|
|
750
|
+
function readDeps(root) {
|
|
751
|
+
try {
|
|
752
|
+
const pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
753
|
+
return {
|
|
754
|
+
...pkg.dependencies,
|
|
755
|
+
...pkg.devDependencies
|
|
756
|
+
};
|
|
757
|
+
} catch {
|
|
758
|
+
return {};
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
//#endregion
|
|
762
|
+
//#region src/pseudo.ts
|
|
763
|
+
const PSEUDO_LOCALE = "en-XA";
|
|
764
|
+
const ACCENTS = {
|
|
765
|
+
a: "á",
|
|
766
|
+
b: "ƀ",
|
|
767
|
+
c: "ç",
|
|
768
|
+
d: "đ",
|
|
769
|
+
e: "é",
|
|
770
|
+
f: "ƒ",
|
|
771
|
+
g: "ğ",
|
|
772
|
+
h: "ĥ",
|
|
773
|
+
i: "í",
|
|
774
|
+
j: "ĵ",
|
|
775
|
+
k: "ķ",
|
|
776
|
+
l: "ĺ",
|
|
777
|
+
m: "ɱ",
|
|
778
|
+
n: "ñ",
|
|
779
|
+
o: "ó",
|
|
780
|
+
p: "þ",
|
|
781
|
+
q: "ǫ",
|
|
782
|
+
r: "ŕ",
|
|
783
|
+
s: "š",
|
|
784
|
+
t: "ţ",
|
|
785
|
+
u: "ú",
|
|
786
|
+
v: "ṽ",
|
|
787
|
+
w: "ŵ",
|
|
788
|
+
x: "ẋ",
|
|
789
|
+
y: "ý",
|
|
790
|
+
z: "ž",
|
|
791
|
+
A: "Á",
|
|
792
|
+
B: "Ɓ",
|
|
793
|
+
C: "Ç",
|
|
794
|
+
D: "Đ",
|
|
795
|
+
E: "É",
|
|
796
|
+
F: "Ƒ",
|
|
797
|
+
G: "Ğ",
|
|
798
|
+
H: "Ĥ",
|
|
799
|
+
I: "Í",
|
|
800
|
+
J: "Ĵ",
|
|
801
|
+
K: "Ķ",
|
|
802
|
+
L: "Ĺ",
|
|
803
|
+
M: "Ḿ",
|
|
804
|
+
N: "Ñ",
|
|
805
|
+
O: "Ó",
|
|
806
|
+
P: "Þ",
|
|
807
|
+
Q: "Ǫ",
|
|
808
|
+
R: "Ŕ",
|
|
809
|
+
S: "Š",
|
|
810
|
+
T: "Ţ",
|
|
811
|
+
U: "Ú",
|
|
812
|
+
V: "Ṽ",
|
|
813
|
+
W: "Ŵ",
|
|
814
|
+
X: "Ẍ",
|
|
815
|
+
Y: "Ý",
|
|
816
|
+
Z: "Ž"
|
|
639
817
|
};
|
|
640
|
-
|
|
818
|
+
const TAG_AT = /<\/?[a-zA-Z][\w-]*\/?>/y;
|
|
641
819
|
function pseudoLocalize(message) {
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
const pad = "~".repeat(Math.ceil(letters / 3));
|
|
678
|
-
return `\u27E6${out}${pad ? " " + pad : ""}\u27E7`;
|
|
820
|
+
let out = "";
|
|
821
|
+
let letters = 0;
|
|
822
|
+
let i = 0;
|
|
823
|
+
while (i < message.length) {
|
|
824
|
+
const two = message.slice(i, i + 2);
|
|
825
|
+
if (two === "{{" || two === "}}" || two === "||" || two === "##") {
|
|
826
|
+
out += two;
|
|
827
|
+
i += 2;
|
|
828
|
+
continue;
|
|
829
|
+
}
|
|
830
|
+
const ch = message[i];
|
|
831
|
+
if (ch === "{") {
|
|
832
|
+
const end = matchBrace(message, i);
|
|
833
|
+
out += message.slice(i, end);
|
|
834
|
+
i = end;
|
|
835
|
+
continue;
|
|
836
|
+
}
|
|
837
|
+
if (ch === "<") {
|
|
838
|
+
TAG_AT.lastIndex = i;
|
|
839
|
+
const m = TAG_AT.exec(message);
|
|
840
|
+
if (m) {
|
|
841
|
+
out += m[0];
|
|
842
|
+
i += m[0].length;
|
|
843
|
+
continue;
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
const mapped = ACCENTS[ch];
|
|
847
|
+
if (mapped) {
|
|
848
|
+
out += mapped;
|
|
849
|
+
letters += 1;
|
|
850
|
+
} else out += ch;
|
|
851
|
+
i += 1;
|
|
852
|
+
}
|
|
853
|
+
const pad = "~".repeat(Math.ceil(letters / 3));
|
|
854
|
+
return `⟦${out}${pad ? " " + pad : ""}⟧`;
|
|
679
855
|
}
|
|
680
856
|
function matchBrace(message, start) {
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
857
|
+
let depth = 0;
|
|
858
|
+
for (let i = start; i < message.length; i++) {
|
|
859
|
+
const two = message.slice(i, i + 2);
|
|
860
|
+
if (two === "{{" || two === "}}") {
|
|
861
|
+
i += 1;
|
|
862
|
+
continue;
|
|
863
|
+
}
|
|
864
|
+
const ch = message[i];
|
|
865
|
+
if (ch === "{") depth += 1;
|
|
866
|
+
else if (ch === "}") {
|
|
867
|
+
depth -= 1;
|
|
868
|
+
if (depth === 0) return i + 1;
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
return message.length;
|
|
696
872
|
}
|
|
697
873
|
function pseudoCatalogs(cfg, catalogs, locale = PSEUDO_LOCALE) {
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
catalogs[locale] = target;
|
|
704
|
-
return Object.keys(target).filter((key) => target[key]);
|
|
874
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
875
|
+
const target = {};
|
|
876
|
+
for (const [key, msg] of Object.entries(source)) target[key] = msg ? pseudoLocalize(msg) : "";
|
|
877
|
+
catalogs[locale] = target;
|
|
878
|
+
return Object.keys(target).filter((key) => target[key]);
|
|
705
879
|
}
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
"embed",
|
|
724
|
-
"hr",
|
|
725
|
-
"img",
|
|
726
|
-
"input",
|
|
727
|
-
"link",
|
|
728
|
-
"meta",
|
|
729
|
-
"param",
|
|
730
|
-
"source",
|
|
731
|
-
"track",
|
|
732
|
-
"wbr"
|
|
880
|
+
//#endregion
|
|
881
|
+
//#region src/render.ts
|
|
882
|
+
const VOID_TAGS = /* @__PURE__ */ new Set([
|
|
883
|
+
"area",
|
|
884
|
+
"base",
|
|
885
|
+
"br",
|
|
886
|
+
"col",
|
|
887
|
+
"embed",
|
|
888
|
+
"hr",
|
|
889
|
+
"img",
|
|
890
|
+
"input",
|
|
891
|
+
"link",
|
|
892
|
+
"meta",
|
|
893
|
+
"param",
|
|
894
|
+
"source",
|
|
895
|
+
"track",
|
|
896
|
+
"wbr"
|
|
733
897
|
]);
|
|
734
|
-
|
|
735
|
-
|
|
898
|
+
const START_TAG = /<([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|'[^']*'|[^"'>])*)>/g;
|
|
899
|
+
const ATTR = /([^\s=/"'<>]+)(?:\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+)))?/g;
|
|
736
900
|
function renderHtml(html, options) {
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
901
|
+
const attr = options.attribute ?? "data-verbaly";
|
|
902
|
+
const argsAttr = `${attr}-args`;
|
|
903
|
+
const attrsAttr = `${attr}-attr`;
|
|
904
|
+
const richAttr = `${attr}-rich`;
|
|
905
|
+
const linksAttr = `${attr}-links`;
|
|
906
|
+
const richTags = new Set(options.richTags ?? RICH_TAGS);
|
|
907
|
+
const globalLinks = options.richLinks;
|
|
908
|
+
const sourceLocale = options.sourceLocale ?? "en";
|
|
909
|
+
const messages = {};
|
|
910
|
+
for (const [locale, catalog] of Object.entries(options.catalogs)) {
|
|
911
|
+
const clean = {};
|
|
912
|
+
for (const [key, msg] of Object.entries(catalog)) if (msg) clean[key] = msg;
|
|
913
|
+
messages[locale] = clean;
|
|
914
|
+
}
|
|
915
|
+
const v = createVerbaly({
|
|
916
|
+
locale: options.locale,
|
|
917
|
+
fallback: sourceLocale,
|
|
918
|
+
messages
|
|
919
|
+
});
|
|
920
|
+
const t = v.t;
|
|
921
|
+
const ms = new MagicString(html);
|
|
922
|
+
const missing = /* @__PURE__ */ new Set();
|
|
923
|
+
const skip = protectedRanges(html);
|
|
924
|
+
const inSkip = (index) => skip.some(([from, to]) => index >= from && index < to);
|
|
925
|
+
START_TAG.lastIndex = 0;
|
|
926
|
+
let m;
|
|
927
|
+
while ((m = START_TAG.exec(html)) !== null) {
|
|
928
|
+
if (inSkip(m.index)) continue;
|
|
929
|
+
const [full, rawName, attrChunk] = m;
|
|
930
|
+
const tagName = rawName.toLowerCase();
|
|
931
|
+
const openEnd = m.index + full.length;
|
|
932
|
+
const chunkStart = m.index + 1 + rawName.length;
|
|
933
|
+
if (tagName === "html" && options.setLang !== false) {
|
|
934
|
+
setAttribute(ms, html, chunkStart, openEnd, attrChunk, "lang", options.locale);
|
|
935
|
+
continue;
|
|
936
|
+
}
|
|
937
|
+
const attrs = parseAttrs(attrChunk);
|
|
938
|
+
const key = attrs.get(attr);
|
|
939
|
+
const attrMapRaw = attrs.get(attrsAttr);
|
|
940
|
+
if (key === void 0 && attrMapRaw === void 0) continue;
|
|
941
|
+
const args = parseArgs$1(attrs.get(argsAttr));
|
|
942
|
+
if (key) {
|
|
943
|
+
if (!v.has(key)) missing.add(key);
|
|
944
|
+
else if (!VOID_TAGS.has(tagName) && !attrChunk.trimEnd().endsWith("/")) {
|
|
945
|
+
const close = findClose(html, tagName, openEnd, inSkip);
|
|
946
|
+
if (close) {
|
|
947
|
+
const text = t(key, args);
|
|
948
|
+
const own = parseArgs$1(attrs.get(linksAttr));
|
|
949
|
+
const links = own ? globalLinks ? {
|
|
950
|
+
...globalLinks,
|
|
951
|
+
...own
|
|
952
|
+
} : own : globalLinks;
|
|
953
|
+
const content = attrs.has(richAttr) ? richToHtml(parseTags(text), richTags, links) : escapeHtml(text);
|
|
954
|
+
if (html.slice(openEnd, close.contentEnd) !== content) if (openEnd === close.contentEnd) ms.appendLeft(openEnd, content);
|
|
955
|
+
else ms.overwrite(openEnd, close.contentEnd, content);
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
if (attrMapRaw !== void 0) {
|
|
960
|
+
const map = parseArgs$1(attrMapRaw);
|
|
961
|
+
if (map) for (const [name, attrKey] of Object.entries(map)) {
|
|
962
|
+
if (typeof attrKey !== "string" || name.toLowerCase().startsWith("on")) continue;
|
|
963
|
+
if (!v.has(attrKey)) {
|
|
964
|
+
missing.add(attrKey);
|
|
965
|
+
continue;
|
|
966
|
+
}
|
|
967
|
+
setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, t(attrKey, args));
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
return {
|
|
972
|
+
html: ms.toString(),
|
|
973
|
+
missing: [...missing]
|
|
974
|
+
};
|
|
806
975
|
}
|
|
807
976
|
async function renderSite(cfg, options = {}) {
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
977
|
+
const site = join(cfg.root, options.site ?? "dist");
|
|
978
|
+
const locales = options.locales ?? cfg.locales;
|
|
979
|
+
const catalogs = loadCatalogs(cfg);
|
|
980
|
+
const files = await glob("**/*.html", {
|
|
981
|
+
cwd: site,
|
|
982
|
+
absolute: true,
|
|
983
|
+
ignore: locales.map((locale) => `${locale}/**`)
|
|
984
|
+
});
|
|
985
|
+
const missing = {};
|
|
986
|
+
for (const file of files) {
|
|
987
|
+
const html = readFileSync(file, "utf8");
|
|
988
|
+
const rel = relative(site, file);
|
|
989
|
+
for (const locale of locales) {
|
|
990
|
+
const result = renderHtml(html, {
|
|
991
|
+
locale,
|
|
992
|
+
catalogs,
|
|
993
|
+
sourceLocale: cfg.sourceLocale,
|
|
994
|
+
attribute: options.attribute,
|
|
995
|
+
richTags: options.richTags,
|
|
996
|
+
richLinks: options.richLinks ?? cfg.render.links
|
|
997
|
+
});
|
|
998
|
+
for (const key of result.missing) {
|
|
999
|
+
const list = missing[locale] ??= [];
|
|
1000
|
+
if (!list.includes(key)) list.push(key);
|
|
1001
|
+
}
|
|
1002
|
+
const out = locale === cfg.sourceLocale ? file : join(site, locale, rel);
|
|
1003
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
1004
|
+
writeFileSync(out, result.html);
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
return {
|
|
1008
|
+
files: files.length,
|
|
1009
|
+
locales: [...locales],
|
|
1010
|
+
missing
|
|
1011
|
+
};
|
|
839
1012
|
}
|
|
840
1013
|
function parseAttrs(chunk) {
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
return void 0;
|
|
856
|
-
}
|
|
1014
|
+
const attrs = /* @__PURE__ */ new Map();
|
|
1015
|
+
ATTR.lastIndex = 0;
|
|
1016
|
+
let m;
|
|
1017
|
+
while ((m = ATTR.exec(chunk)) !== null) attrs.set(m[1].toLowerCase(), m[2] ?? m[3] ?? m[4] ?? "");
|
|
1018
|
+
return attrs;
|
|
1019
|
+
}
|
|
1020
|
+
function parseArgs$1(raw) {
|
|
1021
|
+
if (!raw) return void 0;
|
|
1022
|
+
try {
|
|
1023
|
+
return JSON.parse(decodeEntities(raw));
|
|
1024
|
+
} catch {
|
|
1025
|
+
console.warn(`[verbaly] invalid args JSON: ${raw}`);
|
|
1026
|
+
return;
|
|
1027
|
+
}
|
|
857
1028
|
}
|
|
858
1029
|
function protectedRanges(html) {
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
1030
|
+
const ranges = [];
|
|
1031
|
+
const re = /<!--[\s\S]*?-->|<script\b[\s\S]*?<\/script\s*>|<style\b[\s\S]*?<\/style\s*>/gi;
|
|
1032
|
+
let m;
|
|
1033
|
+
while ((m = re.exec(html)) !== null) {
|
|
1034
|
+
const openEnd = m[0].startsWith("<!--") ? m.index : html.indexOf(">", m.index) + 1;
|
|
1035
|
+
ranges.push([openEnd, m.index + m[0].length]);
|
|
1036
|
+
}
|
|
1037
|
+
return ranges;
|
|
867
1038
|
}
|
|
868
1039
|
function findClose(html, tagName, from, inSkip) {
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
} else if (!m[0].endsWith("/>")) {
|
|
882
|
-
depth += 1;
|
|
883
|
-
}
|
|
884
|
-
}
|
|
885
|
-
return null;
|
|
1040
|
+
const re = new RegExp(`<${tagName}(?=[\\s/>])(?:"[^"]*"|'[^']*'|[^"'>])*>|</${tagName}\\s*>`, "gi");
|
|
1041
|
+
re.lastIndex = from;
|
|
1042
|
+
let depth = 1;
|
|
1043
|
+
let m;
|
|
1044
|
+
while ((m = re.exec(html)) !== null) {
|
|
1045
|
+
if (inSkip(m.index)) continue;
|
|
1046
|
+
if (m[0][1] === "/") {
|
|
1047
|
+
depth -= 1;
|
|
1048
|
+
if (depth === 0) return { contentEnd: m.index };
|
|
1049
|
+
} else if (!m[0].endsWith("/>")) depth += 1;
|
|
1050
|
+
}
|
|
1051
|
+
return null;
|
|
886
1052
|
}
|
|
887
1053
|
function setAttribute(ms, html, chunkStart, openEnd, attrChunk, name, value) {
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
return;
|
|
899
|
-
}
|
|
900
|
-
const selfClosing = html.slice(openEnd - 2, openEnd) === "/>";
|
|
901
|
-
const insertAt = openEnd - (selfClosing ? 2 : 1);
|
|
902
|
-
ms.appendLeft(insertAt, ` ${name}="${escaped}"`);
|
|
1054
|
+
const escaped = escapeAttr(value);
|
|
1055
|
+
const existing = new RegExp(`(\\s${name}\\s*=\\s*)("[^"]*"|'[^']*'|[^\\s>]+)`, "i").exec(attrChunk);
|
|
1056
|
+
if (existing) {
|
|
1057
|
+
const valueStart = chunkStart + existing.index + existing[1].length;
|
|
1058
|
+
const valueEnd = valueStart + existing[2].length;
|
|
1059
|
+
if (html.slice(valueStart, valueEnd) !== `"${escaped}"`) ms.overwrite(valueStart, valueEnd, `"${escaped}"`);
|
|
1060
|
+
return;
|
|
1061
|
+
}
|
|
1062
|
+
const insertAt = openEnd - (html.slice(openEnd - 2, openEnd) === "/>" ? 2 : 1);
|
|
1063
|
+
ms.appendLeft(insertAt, ` ${name}="${escaped}"`);
|
|
903
1064
|
}
|
|
904
1065
|
function richToHtml(nodes, allowed, links) {
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
out += `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
|
|
919
|
-
} else {
|
|
920
|
-
out += richToHtml(node.children, allowed, links);
|
|
921
|
-
}
|
|
922
|
-
}
|
|
923
|
-
return out;
|
|
1066
|
+
let out = "";
|
|
1067
|
+
for (const node of nodes) if (typeof node === "string") out += escapeHtml(node);
|
|
1068
|
+
else if (links?.[node.name] !== void 0) {
|
|
1069
|
+
const link = links[node.name];
|
|
1070
|
+
const { href, target, rel } = typeof link === "string" ? { href: link } : link;
|
|
1071
|
+
const safe = safeHref(href);
|
|
1072
|
+
let attrs = safe !== void 0 ? ` href="${escapeAttr(safe)}"` : "";
|
|
1073
|
+
if (target) attrs += ` target="${escapeAttr(target)}"`;
|
|
1074
|
+
if (rel) attrs += ` rel="${escapeAttr(rel)}"`;
|
|
1075
|
+
out += `<a${attrs}>${richToHtml(node.children, allowed, links)}</a>`;
|
|
1076
|
+
} else if (allowed.has(node.name)) out += `<${node.name}>${richToHtml(node.children, allowed, links)}</${node.name}>`;
|
|
1077
|
+
else out += richToHtml(node.children, allowed, links);
|
|
1078
|
+
return out;
|
|
924
1079
|
}
|
|
925
1080
|
function escapeHtml(text) {
|
|
926
|
-
|
|
1081
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
927
1082
|
}
|
|
928
1083
|
function escapeAttr(text) {
|
|
929
|
-
|
|
1084
|
+
return escapeHtml(text).replace(/"/g, """);
|
|
930
1085
|
}
|
|
931
1086
|
function decodeEntities(text) {
|
|
932
|
-
|
|
1087
|
+
return text.replace(/"/g, "\"").replace(/'/g, "'").replace(/</g, "<").replace(/>/g, ">").replace(/&/g, "&");
|
|
933
1088
|
}
|
|
934
|
-
|
|
935
|
-
|
|
1089
|
+
//#endregion
|
|
1090
|
+
//#region src/translate.ts
|
|
936
1091
|
async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
1092
|
+
const batchSize = options.batchSize ?? 20;
|
|
1093
|
+
const targets = (options.locales ?? cfg.locales).filter((locale) => locale !== cfg.sourceLocale && cfg.locales.includes(locale));
|
|
1094
|
+
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
1095
|
+
const result = {
|
|
1096
|
+
translated: {},
|
|
1097
|
+
invalid: {},
|
|
1098
|
+
pending: {}
|
|
1099
|
+
};
|
|
1100
|
+
for (const locale of targets) {
|
|
1101
|
+
const catalog = catalogs[locale] ??= {};
|
|
1102
|
+
const missing = Object.keys(source).filter((key) => source[key] && !catalog[key]);
|
|
1103
|
+
if (missing.length === 0) continue;
|
|
1104
|
+
if (options.dryRun) {
|
|
1105
|
+
result.pending[locale] = missing;
|
|
1106
|
+
continue;
|
|
1107
|
+
}
|
|
1108
|
+
for (let i = 0; i < missing.length; i += batchSize) {
|
|
1109
|
+
const keys = missing.slice(i, i + batchSize);
|
|
1110
|
+
const messages = Object.fromEntries(keys.map((key) => [key, source[key]]));
|
|
1111
|
+
const out = await provider({
|
|
1112
|
+
sourceLocale: cfg.sourceLocale,
|
|
1113
|
+
targetLocale: locale,
|
|
1114
|
+
messages
|
|
1115
|
+
});
|
|
1116
|
+
for (const key of keys) {
|
|
1117
|
+
const text = out[key];
|
|
1118
|
+
if (typeof text === "string" && text.trim() && structureMatches(source[key], text)) {
|
|
1119
|
+
catalog[key] = text;
|
|
1120
|
+
(result.translated[locale] ??= []).push(key);
|
|
1121
|
+
} else (result.invalid[locale] ??= []).push(key);
|
|
1122
|
+
}
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
return result;
|
|
971
1126
|
}
|
|
972
1127
|
function structureMatches(source, translated) {
|
|
973
|
-
|
|
1128
|
+
return sameMembers(paramNames(source), paramNames(translated)) && sameMembers(tagTokens(source), tagTokens(translated));
|
|
974
1129
|
}
|
|
975
1130
|
function paramNames(message) {
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
1131
|
+
try {
|
|
1132
|
+
return [...collectParams(message).keys()].sort();
|
|
1133
|
+
} catch {
|
|
1134
|
+
return ["\0invalid"];
|
|
1135
|
+
}
|
|
981
1136
|
}
|
|
982
|
-
|
|
1137
|
+
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
983
1138
|
function tagTokens(message) {
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
}
|
|
988
|
-
return out.sort();
|
|
1139
|
+
const out = [];
|
|
1140
|
+
for (const match of message.matchAll(TAG)) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
1141
|
+
return out.sort();
|
|
989
1142
|
}
|
|
990
1143
|
function sameMembers(a, b) {
|
|
991
|
-
|
|
1144
|
+
return a.length === b.length && a.every((value, i) => value === b[i]);
|
|
992
1145
|
}
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
1146
|
+
//#endregion
|
|
1147
|
+
//#region src/cli.ts
|
|
1148
|
+
const HELP = `verbaly — i18n compiler
|
|
996
1149
|
|
|
997
1150
|
Usage:
|
|
1151
|
+
verbaly init scaffold config + locale catalogs (detects your bundler)
|
|
1152
|
+
verbaly doctor diagnose the setup (config, catalogs, plugin, types, keys)
|
|
998
1153
|
verbaly extract scan sources, update catalogs and types
|
|
999
1154
|
verbaly check verify translations are complete (CI)
|
|
1000
1155
|
verbaly translate fill missing translations via a provider (default: claude)
|
|
@@ -1016,131 +1171,151 @@ Config file: verbaly.config.{js,mjs,ts,mts,json} at root (flags win).
|
|
|
1016
1171
|
The claude provider needs @anthropic-ai/sdk installed and ANTHROPIC_API_KEY set.
|
|
1017
1172
|
`;
|
|
1018
1173
|
async function main() {
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
}
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
${
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1174
|
+
const { positionals, values } = parseArgs({
|
|
1175
|
+
allowPositionals: true,
|
|
1176
|
+
options: {
|
|
1177
|
+
root: { type: "string" },
|
|
1178
|
+
dir: { type: "string" },
|
|
1179
|
+
source: { type: "string" },
|
|
1180
|
+
locales: { type: "string" },
|
|
1181
|
+
prune: { type: "boolean" },
|
|
1182
|
+
model: { type: "string" },
|
|
1183
|
+
locale: { type: "string" },
|
|
1184
|
+
site: { type: "string" },
|
|
1185
|
+
"dry-run": { type: "boolean" },
|
|
1186
|
+
help: {
|
|
1187
|
+
type: "boolean",
|
|
1188
|
+
short: "h"
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
});
|
|
1192
|
+
const command = positionals[0];
|
|
1193
|
+
if (values.help || !command) {
|
|
1194
|
+
console.log(HELP);
|
|
1195
|
+
process.exitCode = command ? 0 : 1;
|
|
1196
|
+
return;
|
|
1197
|
+
}
|
|
1198
|
+
if (command === "init") {
|
|
1199
|
+
const result = init({
|
|
1200
|
+
root: values.root,
|
|
1201
|
+
dir: values.dir,
|
|
1202
|
+
sourceLocale: values.source,
|
|
1203
|
+
locales: values.locales?.split(",")
|
|
1204
|
+
});
|
|
1205
|
+
if (result.created.length) console.log(`[verbaly] created: ${result.created.join(", ")}`);
|
|
1206
|
+
if (result.skipped.length) console.log(` kept (already there): ${result.skipped.join(", ")}`);
|
|
1207
|
+
if (result.bundler) console.log(` detected bundler: ${result.bundler}`);
|
|
1208
|
+
console.log([" next steps:", ...result.next.map((step, i) => ` ${i + 1}. ${step}`)].join("\n"));
|
|
1209
|
+
return;
|
|
1210
|
+
}
|
|
1211
|
+
const cfg = await loadConfig(values.root ?? process.cwd(), {
|
|
1212
|
+
dir: values.dir,
|
|
1213
|
+
sourceLocale: values.source,
|
|
1214
|
+
locales: values.locales?.split(",")
|
|
1215
|
+
});
|
|
1216
|
+
if (command === "doctor") {
|
|
1217
|
+
const result = await doctor(cfg);
|
|
1218
|
+
const icon = {
|
|
1219
|
+
ok: "✓",
|
|
1220
|
+
warn: "⚠",
|
|
1221
|
+
error: "✗"
|
|
1222
|
+
};
|
|
1223
|
+
console.log(`[verbaly] doctor — ${result.entries.length} checks`);
|
|
1224
|
+
for (const entry of result.entries) {
|
|
1225
|
+
const line = ` ${icon[entry.level]} ${entry.check}: ${entry.message}`;
|
|
1226
|
+
if (entry.level === "error") console.error(line);
|
|
1227
|
+
else if (entry.level === "warn") console.warn(line);
|
|
1228
|
+
else console.log(line);
|
|
1229
|
+
if (entry.fix) console.log(` fix: ${entry.fix}`);
|
|
1230
|
+
}
|
|
1231
|
+
if (result.ok) console.log("[verbaly] setup looks healthy ✓");
|
|
1232
|
+
else {
|
|
1233
|
+
console.error("[verbaly] doctor found problems");
|
|
1234
|
+
process.exitCode = 1;
|
|
1235
|
+
}
|
|
1236
|
+
return;
|
|
1237
|
+
}
|
|
1238
|
+
if (command === "extract") {
|
|
1239
|
+
const registry = await extractProject(cfg);
|
|
1240
|
+
const catalogs = loadCatalogs(cfg);
|
|
1241
|
+
if (values.prune) {
|
|
1242
|
+
const removed = pruneCatalogs(cfg, catalogs, registry);
|
|
1243
|
+
for (const [locale, keys] of Object.entries(removed)) console.log(` ${locale}: -${keys.length} pruned`);
|
|
1244
|
+
}
|
|
1245
|
+
const { added } = syncCatalogs(cfg, catalogs, registry);
|
|
1246
|
+
for (const locale of cfg.locales) writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
1247
|
+
writeDts(cfg, new Map(Object.entries(catalogs[cfg.sourceLocale] ?? {})));
|
|
1248
|
+
const total = registry.messages().size;
|
|
1249
|
+
console.log(`[verbaly] ${total} messages · locales: ${cfg.locales.join(", ")}`);
|
|
1250
|
+
for (const [locale, keys] of Object.entries(added)) console.log(` ${locale}: +${keys.length}`);
|
|
1251
|
+
return;
|
|
1252
|
+
}
|
|
1253
|
+
if (command === "check") {
|
|
1254
|
+
const registry = await extractProject(cfg);
|
|
1255
|
+
const result = check(cfg, loadCatalogs(cfg), registry);
|
|
1256
|
+
if (result.ok) {
|
|
1257
|
+
console.log("[verbaly] all translations complete ✓");
|
|
1258
|
+
return;
|
|
1259
|
+
}
|
|
1260
|
+
console.error(`[verbaly] check failed\n${formatCheckResult(result)}`);
|
|
1261
|
+
process.exitCode = 1;
|
|
1262
|
+
return;
|
|
1263
|
+
}
|
|
1264
|
+
if (command === "translate") {
|
|
1265
|
+
const catalogs = loadCatalogs(cfg);
|
|
1266
|
+
const result = await translateCatalogs(cfg, catalogs, await resolveProvider(cfg, values.model), {
|
|
1267
|
+
locales: values.locales?.split(","),
|
|
1268
|
+
batchSize: cfg.translate.batchSize,
|
|
1269
|
+
dryRun: values["dry-run"]
|
|
1270
|
+
});
|
|
1271
|
+
if (values["dry-run"]) {
|
|
1272
|
+
const entries = Object.entries(result.pending);
|
|
1273
|
+
if (entries.length === 0) {
|
|
1274
|
+
console.log("[verbaly] nothing to translate ✓");
|
|
1275
|
+
return;
|
|
1276
|
+
}
|
|
1277
|
+
for (const [locale, keys] of entries) console.log(` ${locale}: ${keys.length} missing — ${keys.join(", ")}`);
|
|
1278
|
+
return;
|
|
1279
|
+
}
|
|
1280
|
+
for (const locale of Object.keys(result.translated)) {
|
|
1281
|
+
writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
1282
|
+
console.log(` ${locale}: +${result.translated[locale].length} translated`);
|
|
1283
|
+
}
|
|
1284
|
+
for (const [locale, keys] of Object.entries(result.invalid)) console.warn(` ${locale}: ${keys.length} rejected (params/tags not preserved): ${keys.join(", ")}`);
|
|
1285
|
+
if (Object.keys(result.translated).length === 0 && Object.keys(result.invalid).length === 0) console.log("[verbaly] nothing to translate ✓");
|
|
1286
|
+
return;
|
|
1287
|
+
}
|
|
1288
|
+
if (command === "render") {
|
|
1289
|
+
const result = await renderSite(cfg, {
|
|
1290
|
+
site: values.site,
|
|
1291
|
+
locales: values.locales?.split(",")
|
|
1292
|
+
});
|
|
1293
|
+
console.log(`[verbaly] ${result.files} pages × ${result.locales.length} locales (${result.locales.join(", ")})`);
|
|
1294
|
+
for (const [locale, keys] of Object.entries(result.missing)) console.warn(` ${locale}: ${keys.length} keys not pre-filled — ${keys.join(", ")}`);
|
|
1295
|
+
return;
|
|
1296
|
+
}
|
|
1297
|
+
if (command === "pseudo") {
|
|
1298
|
+
const catalogs = loadCatalogs(cfg);
|
|
1299
|
+
const locale = values.locale ?? "en-XA";
|
|
1300
|
+
const keys = pseudoCatalogs(cfg, catalogs, locale);
|
|
1301
|
+
writeCatalog(cfg, locale, catalogs[locale] ?? {});
|
|
1302
|
+
console.log(`[verbaly] ${keys.length} messages pseudo-localized → ${locale}`);
|
|
1303
|
+
return;
|
|
1304
|
+
}
|
|
1305
|
+
console.error(`[verbaly] unknown command "${command}"\n${HELP}`);
|
|
1306
|
+
process.exitCode = 1;
|
|
1135
1307
|
}
|
|
1136
1308
|
async function resolveProvider(cfg, model) {
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1309
|
+
const configured = cfg.translate.provider;
|
|
1310
|
+
if (typeof configured === "function") return configured;
|
|
1311
|
+
const { claudeProvider } = await import("./claude-Bi_Ex2hm.js");
|
|
1312
|
+
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1141
1313
|
}
|
|
1142
1314
|
main().catch((error) => {
|
|
1143
|
-
|
|
1144
|
-
|
|
1315
|
+
console.error("[verbaly]", error instanceof Error ? error.message : error);
|
|
1316
|
+
process.exitCode = 1;
|
|
1145
1317
|
});
|
|
1318
|
+
//#endregion
|
|
1319
|
+
export {};
|
|
1320
|
+
|
|
1146
1321
|
//# sourceMappingURL=cli.js.map
|