@verbaly/compiler 0.28.0 → 0.30.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/dist/cli.js +0 -0
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +22 -1
- package/dist/index.js +290 -40
- package/dist/index.js.map +1 -1
- package/package.json +4 -4
package/dist/index.d.ts
CHANGED
|
@@ -63,6 +63,7 @@ interface TranslateResult {
|
|
|
63
63
|
pending: Record<string, string[]>;
|
|
64
64
|
}
|
|
65
65
|
declare function translateCatalogs(cfg: ResolvedConfig, catalogs: Catalogs, provider: TranslateProvider, options?: TranslateOptions): Promise<TranslateResult>;
|
|
66
|
+
declare function resolveProvider(cfg: ResolvedConfig, model?: string): Promise<TranslateProvider>;
|
|
66
67
|
declare function structureMatches(source: string, translated: string): boolean;
|
|
67
68
|
//#endregion
|
|
68
69
|
//#region src/config.d.ts
|
|
@@ -127,6 +128,16 @@ declare class MessageRegistry {
|
|
|
127
128
|
origins(): Map<string, string[]>;
|
|
128
129
|
}
|
|
129
130
|
//#endregion
|
|
131
|
+
//#region src/validate.d.ts
|
|
132
|
+
declare const PLURAL_CATEGORIES: Set<string>;
|
|
133
|
+
type IssueSeverity = 'error' | 'warning';
|
|
134
|
+
interface StructureIssue {
|
|
135
|
+
severity: IssueSeverity;
|
|
136
|
+
message: string;
|
|
137
|
+
}
|
|
138
|
+
declare function validateMessage(message: string, locale?: string): StructureIssue[];
|
|
139
|
+
declare function validatePair(source: string, translated: string): StructureIssue[];
|
|
140
|
+
//#endregion
|
|
130
141
|
//#region src/check.d.ts
|
|
131
142
|
interface MissingEntry {
|
|
132
143
|
locale: string;
|
|
@@ -137,13 +148,21 @@ interface UnknownEntry {
|
|
|
137
148
|
key: string;
|
|
138
149
|
files: string[];
|
|
139
150
|
}
|
|
151
|
+
interface BrokenEntry {
|
|
152
|
+
locale: string;
|
|
153
|
+
key: string;
|
|
154
|
+
severity: IssueSeverity;
|
|
155
|
+
issue: string;
|
|
156
|
+
}
|
|
140
157
|
interface CheckResult {
|
|
141
158
|
ok: boolean;
|
|
142
159
|
missing: MissingEntry[];
|
|
143
160
|
unknown: UnknownEntry[];
|
|
161
|
+
broken: BrokenEntry[];
|
|
144
162
|
}
|
|
145
163
|
declare function check(cfg: ResolvedConfig, catalogs: Catalogs, registry: MessageRegistry): CheckResult;
|
|
146
164
|
declare function formatCheckResult(result: CheckResult): string;
|
|
165
|
+
declare function formatCheckWarnings(result: CheckResult): string;
|
|
147
166
|
declare function githubCheckAnnotations(result: CheckResult, registry: MessageRegistry, root: string): string[];
|
|
148
167
|
//#endregion
|
|
149
168
|
//#region src/codegen.d.ts
|
|
@@ -247,6 +266,7 @@ declare function parseExchangeFile(file: string, localeOverride?: string): Parse
|
|
|
247
266
|
//#endregion
|
|
248
267
|
//#region src/extract.d.ts
|
|
249
268
|
declare function extractProject(cfg: ResolvedConfig): Promise<MessageRegistry>;
|
|
269
|
+
declare function collectOrigins(cfg: ResolvedConfig): Promise<Record<string, string[]>>;
|
|
250
270
|
interface SyncResult {
|
|
251
271
|
added: Record<string, string[]>;
|
|
252
272
|
}
|
|
@@ -335,6 +355,7 @@ interface LocaleStatus {
|
|
|
335
355
|
translated: number;
|
|
336
356
|
total: number;
|
|
337
357
|
drafts: number;
|
|
358
|
+
broken: number;
|
|
338
359
|
}
|
|
339
360
|
interface StatusResult {
|
|
340
361
|
messages: number;
|
|
@@ -381,5 +402,5 @@ interface WrapCodeResult {
|
|
|
381
402
|
}
|
|
382
403
|
declare function wrapCode(code: string, file: string): WrapCodeResult;
|
|
383
404
|
//#endregion
|
|
384
|
-
export { type Alternate, type Analysis, type AnalyzeOptions, type Catalog, type Catalogs, type CheckResult, type ClaudeProviderOptions, DRAFTS_FILE, type DoctorEntry, type DoctorResult, type Drafts, type ExchangeFormat, type ExportFormat, type ExportOptions, type ExportResult, type ExportedFile, type ImportOptions, type ImportResult, type InitOptions, type InitResult, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type MobileFormat, PSEUDO_LOCALE, type ParamType, type PluginOptions, RESOLVED_VIRTUAL_ID, type RenderConfig, type RenderHtmlOptions, type RenderHtmlResult, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SFC_FILE_RE, SOURCE_FILE_RE, type StatusResult, type SyncResult, type TaggedMessage, type TaggedParam, type TransComponent, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type UsedKey, VIRTUAL_ID, type VerbalyConfig, type WatchProjectOptions, type WrapCodeResult, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
405
|
+
export { type Alternate, type Analysis, type AnalyzeOptions, type BrokenEntry, type Catalog, type Catalogs, type CheckResult, type ClaudeProviderOptions, DRAFTS_FILE, type DoctorEntry, type DoctorResult, type Drafts, type ExchangeFormat, type ExportFormat, type ExportOptions, type ExportResult, type ExportedFile, type ImportOptions, type ImportResult, type InitOptions, type InitResult, type IssueSeverity, LOCALE_MODULE_PREFIX, type LocaleStatus, MessageRegistry, type MissingEntry, type MobileFormat, PLURAL_CATEGORIES, PSEUDO_LOCALE, type ParamType, type PluginOptions, RESOLVED_VIRTUAL_ID, type RenderConfig, type RenderHtmlOptions, type RenderHtmlResult, type RenderSiteOptions, type RenderSiteResult, type ResolvedConfig, type RuntimeModuleOptions, SFC_FILE_RE, SOURCE_FILE_RE, type StatusResult, type StructureIssue, type SyncResult, type TaggedMessage, type TaggedParam, type TransComponent, type TransformResult, type TranslateConfig, type TranslateOptions, type TranslateProvider, type TranslateRequest, type TranslateResult, type UnknownEntry, type UsedKey, VIRTUAL_ID, type VerbalyConfig, type WatchProjectOptions, type WrapCodeResult, type WrapEntry, type WrapOptions, type WrapResult, type WrapSkip, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatCheckWarnings, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, validateMessage, validatePair, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
385
406
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -2,11 +2,23 @@ import { parse } from "@babel/parser";
|
|
|
2
2
|
import { createHash } from "node:crypto";
|
|
3
3
|
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, watch, writeFileSync } from "node:fs";
|
|
4
4
|
import { basename, dirname, join, relative, resolve } from "node:path";
|
|
5
|
-
import { RICH_TAGS, createVerbaly, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
|
|
5
|
+
import { RICH_TAGS, createVerbaly, flatten, localeDirection, normalizeLink, parse as parse$1, parseTags, safeAttribute } from "verbaly";
|
|
6
6
|
import { pathToFileURL } from "node:url";
|
|
7
7
|
import picomatch from "picomatch";
|
|
8
8
|
import MagicString from "magic-string";
|
|
9
9
|
import { glob } from "tinyglobby";
|
|
10
|
+
//#region \0rolldown/runtime.js
|
|
11
|
+
var __defProp = Object.defineProperty;
|
|
12
|
+
var __exportAll = (all, no_symbols) => {
|
|
13
|
+
let target = {};
|
|
14
|
+
for (var name in all) __defProp(target, name, {
|
|
15
|
+
get: all[name],
|
|
16
|
+
enumerable: true
|
|
17
|
+
});
|
|
18
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
19
|
+
return target;
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
10
22
|
//#region src/key.ts
|
|
11
23
|
function stableKey(message) {
|
|
12
24
|
return createHash("sha256").update(message).digest("base64url").slice(0, 8);
|
|
@@ -30,7 +42,7 @@ function analyze(code, file, options = {}) {
|
|
|
30
42
|
});
|
|
31
43
|
const tagged = [];
|
|
32
44
|
const usedKeys = [];
|
|
33
|
-
walk(ast.program, (node) => {
|
|
45
|
+
walk$1(ast.program, (node) => {
|
|
34
46
|
if (node.type === "TaggedTemplateExpression") {
|
|
35
47
|
const tag = node.tag;
|
|
36
48
|
const explicit = explicitId(tag, names);
|
|
@@ -194,7 +206,7 @@ function selfClosedSource(code, opening) {
|
|
|
194
206
|
}
|
|
195
207
|
function containsTaggedT(node, names) {
|
|
196
208
|
let found = false;
|
|
197
|
-
walk(node, (n) => {
|
|
209
|
+
walk$1(node, (n) => {
|
|
198
210
|
if (n.type !== "TaggedTemplateExpression") return;
|
|
199
211
|
const tag = n.tag;
|
|
200
212
|
if (isTReference(tag, names) || explicitId(tag, names)) found = true;
|
|
@@ -259,14 +271,14 @@ function uniqueName(base, source, taken) {
|
|
|
259
271
|
taken.set(name, source);
|
|
260
272
|
return name;
|
|
261
273
|
}
|
|
262
|
-
function walk(node, visit) {
|
|
274
|
+
function walk$1(node, visit) {
|
|
263
275
|
visit(node);
|
|
264
276
|
for (const key in node) {
|
|
265
277
|
if (SKIP_KEYS.has(key)) continue;
|
|
266
278
|
const value = node[key];
|
|
267
279
|
if (Array.isArray(value)) {
|
|
268
|
-
for (const item of value) if (isNode(item)) walk(item, visit);
|
|
269
|
-
} else if (isNode(value)) walk(value, visit);
|
|
280
|
+
for (const item of value) if (isNode(item)) walk$1(item, visit);
|
|
281
|
+
} else if (isNode(value)) walk$1(value, visit);
|
|
270
282
|
}
|
|
271
283
|
}
|
|
272
284
|
function isNode(value) {
|
|
@@ -284,6 +296,8 @@ const COMMENT_RE = /<!--[\s\S]*?-->/g;
|
|
|
284
296
|
const FRONTMATTER_RE = /^(\uFEFF?\s*---\r?\n)([\s\S]*?)\r?\n---(?=\r?\n|$)/;
|
|
285
297
|
const CANDIDATE_SVELTE = /(?<![\w$])\$?t(?=[`(]|\.id\()/g;
|
|
286
298
|
const CANDIDATE_VUE = /(?<![\w$])t(?=[`(]|\.id\()/g;
|
|
299
|
+
const MUSTACHE_RE = /\{\{[\s\S]*?\}\}/g;
|
|
300
|
+
const DIRECTIVE_RE = /\s(?:v-[\w-]+(?::[^\s=>/]+)?|[:@][^\s=>/]+)\s*=\s*("[^"]*"|'[^']*')/g;
|
|
287
301
|
function analyzeSfc(code, file) {
|
|
288
302
|
const svelte = file.endsWith(".svelte");
|
|
289
303
|
const options = svelte ? { tNames: ["t", "$t"] } : void 0;
|
|
@@ -298,10 +312,14 @@ function analyzeSfc(code, file) {
|
|
|
298
312
|
}
|
|
299
313
|
const markup = blank(blank(blank(frontmatter ? " ".repeat(frontmatter[0].length) + code.slice(frontmatter[0].length) : code, SCRIPT_RE), STYLE_RE), COMMENT_RE);
|
|
300
314
|
const candidates = svelte ? CANDIDATE_SVELTE : CANDIDATE_VUE;
|
|
315
|
+
const ranges = file.endsWith(".vue") ? vueExpressionRanges(markup) : braceRanges(markup);
|
|
316
|
+
let range = 0;
|
|
301
317
|
let consumedTo = 0;
|
|
302
318
|
for (const match of markup.matchAll(candidates)) {
|
|
303
319
|
const start = match.index;
|
|
304
320
|
if (start < consumedTo) continue;
|
|
321
|
+
while (range < ranges.length && ranges[range][1] <= start) range += 1;
|
|
322
|
+
if (range >= ranges.length || start < ranges[range][0]) continue;
|
|
305
323
|
const end = expressionExtent(markup, start);
|
|
306
324
|
if (end === void 0) continue;
|
|
307
325
|
merge(analyzeSegment(code.slice(start, end), file, options), start, true);
|
|
@@ -339,6 +357,31 @@ function analyzeSegment(code, file, options) {
|
|
|
339
357
|
function blank(source, re) {
|
|
340
358
|
return source.replace(re, (m) => " ".repeat(m.length));
|
|
341
359
|
}
|
|
360
|
+
function braceRanges(markup) {
|
|
361
|
+
const ranges = [];
|
|
362
|
+
let i = 0;
|
|
363
|
+
while (i < markup.length) {
|
|
364
|
+
if (markup[i] === "{") {
|
|
365
|
+
const end = balancedEnd(markup, i);
|
|
366
|
+
if (end !== void 0) {
|
|
367
|
+
ranges.push([i + 1, end - 1]);
|
|
368
|
+
i = end;
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
i += 1;
|
|
373
|
+
}
|
|
374
|
+
return ranges;
|
|
375
|
+
}
|
|
376
|
+
function vueExpressionRanges(markup) {
|
|
377
|
+
const ranges = [];
|
|
378
|
+
for (const match of markup.matchAll(MUSTACHE_RE)) ranges.push([match.index + 2, match.index + match[0].length - 2]);
|
|
379
|
+
for (const match of markup.matchAll(DIRECTIVE_RE)) {
|
|
380
|
+
const valueStart = match.index + match[0].length - match[1].length + 1;
|
|
381
|
+
ranges.push([valueStart, valueStart + match[1].length - 2]);
|
|
382
|
+
}
|
|
383
|
+
return ranges.sort((a, b) => a[0] - b[0]);
|
|
384
|
+
}
|
|
342
385
|
function expressionExtent(code, start) {
|
|
343
386
|
let i = start;
|
|
344
387
|
if (code[i] === "$") i += 1;
|
|
@@ -423,6 +466,158 @@ function writeCatalog(cfg, locale, catalog) {
|
|
|
423
466
|
return serialized;
|
|
424
467
|
}
|
|
425
468
|
//#endregion
|
|
469
|
+
//#region src/validate.ts
|
|
470
|
+
const PLURAL_CATEGORIES = /* @__PURE__ */ new Set([
|
|
471
|
+
"zero",
|
|
472
|
+
"one",
|
|
473
|
+
"two",
|
|
474
|
+
"few",
|
|
475
|
+
"many"
|
|
476
|
+
]);
|
|
477
|
+
const EXACT = /^=\d+$/;
|
|
478
|
+
function shapes(message) {
|
|
479
|
+
const out = /* @__PURE__ */ new Map();
|
|
480
|
+
walk(parse$1(message), out);
|
|
481
|
+
return out;
|
|
482
|
+
}
|
|
483
|
+
function walk(nodes, out) {
|
|
484
|
+
for (const node of nodes) {
|
|
485
|
+
if (node.kind !== "param" || !node.variants) continue;
|
|
486
|
+
const selectors = node.variants.map(([key]) => key);
|
|
487
|
+
if (!out.has(node.name)) out.set(node.name, {
|
|
488
|
+
selectors,
|
|
489
|
+
plural: selectors.some((key) => PLURAL_CATEGORIES.has(key)),
|
|
490
|
+
ordinal: node.ordinal === true
|
|
491
|
+
});
|
|
492
|
+
for (const [, body] of node.variants) walk(body, out);
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
function paramNames(message) {
|
|
496
|
+
const names = /* @__PURE__ */ new Set();
|
|
497
|
+
collect(parse$1(message), names);
|
|
498
|
+
return [...names].sort();
|
|
499
|
+
}
|
|
500
|
+
function collect(nodes, out) {
|
|
501
|
+
for (const node of nodes) {
|
|
502
|
+
if (node.kind !== "param") continue;
|
|
503
|
+
out.add(node.name);
|
|
504
|
+
if (node.variants) for (const [, body] of node.variants) collect(body, out);
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
508
|
+
function tagTokens(message) {
|
|
509
|
+
const out = [];
|
|
510
|
+
for (const match of message.matchAll(TAG)) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
511
|
+
return out.sort();
|
|
512
|
+
}
|
|
513
|
+
function surplus(a, b) {
|
|
514
|
+
const rest = [...b];
|
|
515
|
+
const out = [];
|
|
516
|
+
for (const item of a) {
|
|
517
|
+
const at = rest.indexOf(item);
|
|
518
|
+
if (at < 0) out.push(item);
|
|
519
|
+
else rest.splice(at, 1);
|
|
520
|
+
}
|
|
521
|
+
return out;
|
|
522
|
+
}
|
|
523
|
+
function list(items) {
|
|
524
|
+
return items.length > 1 ? `${items.slice(0, -1).join(", ")} and ${items[items.length - 1]}` : items[0];
|
|
525
|
+
}
|
|
526
|
+
function validateMessage(message, locale) {
|
|
527
|
+
const issues = [];
|
|
528
|
+
for (const [name, shape] of shapes(message)) {
|
|
529
|
+
if (!shape.plural) continue;
|
|
530
|
+
if (!shape.selectors.includes("other")) {
|
|
531
|
+
issues.push({
|
|
532
|
+
severity: "error",
|
|
533
|
+
message: `the plural block for {${name}} has no "other" case, so any count it does not list renders empty`
|
|
534
|
+
});
|
|
535
|
+
continue;
|
|
536
|
+
}
|
|
537
|
+
if (!locale) continue;
|
|
538
|
+
const missing = reachableCategories(locale, shape.ordinal).filter((category) => category !== "other" && !shape.selectors.includes(category));
|
|
539
|
+
if (missing.length > 0) issues.push({
|
|
540
|
+
severity: "warning",
|
|
541
|
+
message: `${locale} also needs the ${list(missing)} plural ${missing.length > 1 ? "forms" : "form"} for {${name}}`
|
|
542
|
+
});
|
|
543
|
+
}
|
|
544
|
+
return issues;
|
|
545
|
+
}
|
|
546
|
+
function validatePair(source, translated) {
|
|
547
|
+
const issues = [];
|
|
548
|
+
const lostParams = surplus(paramNames(source), paramNames(translated));
|
|
549
|
+
const newParams = surplus(paramNames(translated), paramNames(source));
|
|
550
|
+
if (lostParams.length > 0) issues.push({
|
|
551
|
+
severity: "error",
|
|
552
|
+
message: `${list(lostParams.map((name) => `{${name}}`))} ${lostParams.length > 1 ? "are" : "is"} missing, so ${lostParams.length > 1 ? "their values" : "its value"} never reaches the text`
|
|
553
|
+
});
|
|
554
|
+
if (newParams.length > 0) issues.push({
|
|
555
|
+
severity: "error",
|
|
556
|
+
message: `${list(newParams.map((name) => `{${name}}`))} ${newParams.length > 1 ? "are" : "is"} not in the source message, so ${newParams.length > 1 ? "they render" : "it renders"} as literal text`
|
|
557
|
+
});
|
|
558
|
+
const lostTags = tagReport(surplus(tagTokens(source), tagTokens(translated)));
|
|
559
|
+
const newTags = tagReport(surplus(tagTokens(translated), tagTokens(source)));
|
|
560
|
+
if (lostTags.length > 0) issues.push({
|
|
561
|
+
severity: "error",
|
|
562
|
+
message: `${list(lostTags)} ${lostTags.length > 1 ? "are" : "is"} missing from the translation`
|
|
563
|
+
});
|
|
564
|
+
if (newTags.length > 0) issues.push({
|
|
565
|
+
severity: "error",
|
|
566
|
+
message: `${list(newTags)} ${newTags.length > 1 ? "are" : "is"} not in the source message`
|
|
567
|
+
});
|
|
568
|
+
const target = shapes(translated);
|
|
569
|
+
for (const [name, shape] of shapes(source)) {
|
|
570
|
+
const other = target.get(name);
|
|
571
|
+
if (!other) {
|
|
572
|
+
issues.push({
|
|
573
|
+
severity: "error",
|
|
574
|
+
message: `the ${shape.plural ? "plural" : "select"} block for {${name}} was flattened into plain text`
|
|
575
|
+
});
|
|
576
|
+
continue;
|
|
577
|
+
}
|
|
578
|
+
if (shape.selectors.includes("other") && !other.selectors.includes("other") && !other.plural) issues.push({
|
|
579
|
+
severity: "error",
|
|
580
|
+
message: `the "other" case for {${name}} is missing, so unlisted values render empty`
|
|
581
|
+
});
|
|
582
|
+
const lostExact = shape.selectors.filter((key) => EXACT.test(key)).filter((key) => !other.selectors.includes(key));
|
|
583
|
+
if (lostExact.length > 0) issues.push({
|
|
584
|
+
severity: "warning",
|
|
585
|
+
message: `the ${list(lostExact)} ${lostExact.length > 1 ? "cases" : "case"} for {${name}} ${lostExact.length > 1 ? "are" : "is"} missing, so ${lostExact.length > 1 ? "those counts" : "that count"} falls back to "other"`
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
return issues;
|
|
589
|
+
}
|
|
590
|
+
function tagReport(tokens) {
|
|
591
|
+
const out = [];
|
|
592
|
+
const seen = /* @__PURE__ */ new Set();
|
|
593
|
+
for (const token of tokens) {
|
|
594
|
+
const name = token.replaceAll("/", "");
|
|
595
|
+
if (seen.has(name)) continue;
|
|
596
|
+
seen.add(name);
|
|
597
|
+
out.push(token.endsWith("/") ? `<${name}/>` : `<${name}>`);
|
|
598
|
+
}
|
|
599
|
+
return out;
|
|
600
|
+
}
|
|
601
|
+
const COUNT_PROBE = 200;
|
|
602
|
+
const reachable = /* @__PURE__ */ new Map();
|
|
603
|
+
function reachableCategories(locale, ordinal) {
|
|
604
|
+
const type = ordinal ? "ordinal" : "cardinal";
|
|
605
|
+
const cacheKey = `${locale}${type}`;
|
|
606
|
+
const hit = reachable.get(cacheKey);
|
|
607
|
+
if (hit) return hit;
|
|
608
|
+
let out;
|
|
609
|
+
try {
|
|
610
|
+
const rules = new Intl.PluralRules(locale, { type });
|
|
611
|
+
const seen = /* @__PURE__ */ new Set();
|
|
612
|
+
for (let n = 0; n <= COUNT_PROBE; n++) seen.add(rules.select(n));
|
|
613
|
+
out = [...seen];
|
|
614
|
+
} catch {
|
|
615
|
+
out = [];
|
|
616
|
+
}
|
|
617
|
+
reachable.set(cacheKey, out);
|
|
618
|
+
return out;
|
|
619
|
+
}
|
|
620
|
+
//#endregion
|
|
426
621
|
//#region src/check.ts
|
|
427
622
|
function check(cfg, catalogs, registry) {
|
|
428
623
|
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
@@ -447,10 +642,35 @@ function check(cfg, catalogs, registry) {
|
|
|
447
642
|
source: source[key] ?? extracted.get(key)?.message
|
|
448
643
|
});
|
|
449
644
|
}
|
|
645
|
+
const broken = [];
|
|
646
|
+
const add = (locale, key, issues) => {
|
|
647
|
+
for (const issue of issues) broken.push({
|
|
648
|
+
locale,
|
|
649
|
+
key,
|
|
650
|
+
severity: issue.severity,
|
|
651
|
+
issue: issue.message
|
|
652
|
+
});
|
|
653
|
+
};
|
|
654
|
+
const sourceFlat = flatten(source);
|
|
655
|
+
for (const key of needed) {
|
|
656
|
+
const text = sourceFlat[key] ?? extracted.get(key)?.message;
|
|
657
|
+
if (text) sourceFlat[key] = text;
|
|
658
|
+
}
|
|
659
|
+
for (const [key, text] of Object.entries(sourceFlat)) add(cfg.sourceLocale, key, validateMessage(text, cfg.sourceLocale));
|
|
660
|
+
for (const locale of cfg.locales) {
|
|
661
|
+
if (locale === cfg.sourceLocale) continue;
|
|
662
|
+
for (const [key, translated] of Object.entries(flatten(catalogs[locale] ?? {}))) {
|
|
663
|
+
if (!translated) continue;
|
|
664
|
+
add(locale, key, validateMessage(translated, locale));
|
|
665
|
+
const text = sourceFlat[key];
|
|
666
|
+
if (text) add(locale, key, validatePair(text, translated));
|
|
667
|
+
}
|
|
668
|
+
}
|
|
450
669
|
return {
|
|
451
|
-
ok: missing.length === 0 && unknown.length === 0,
|
|
670
|
+
ok: missing.length === 0 && unknown.length === 0 && !broken.some((entry) => entry.severity === "error"),
|
|
452
671
|
missing,
|
|
453
|
-
unknown
|
|
672
|
+
unknown,
|
|
673
|
+
broken
|
|
454
674
|
};
|
|
455
675
|
}
|
|
456
676
|
function formatCheckResult(result) {
|
|
@@ -466,8 +686,16 @@ function formatCheckResult(result) {
|
|
|
466
686
|
lines.push("unknown keys (not in any catalog):");
|
|
467
687
|
for (const entry of result.unknown) lines.push(` ${entry.key} (used in ${entry.files.join(", ")})`);
|
|
468
688
|
}
|
|
689
|
+
const errors = result.broken.filter((entry) => entry.severity === "error");
|
|
690
|
+
if (errors.length > 0) {
|
|
691
|
+
lines.push("broken translations:");
|
|
692
|
+
for (const entry of errors) lines.push(` [${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
693
|
+
}
|
|
469
694
|
return lines.join("\n");
|
|
470
695
|
}
|
|
696
|
+
function formatCheckWarnings(result) {
|
|
697
|
+
return result.broken.filter((entry) => entry.severity === "warning").map((entry) => ` [${entry.locale}] ${entry.key}: ${entry.issue}`).join("\n");
|
|
698
|
+
}
|
|
471
699
|
function truncate(text, max) {
|
|
472
700
|
return text.length > max ? text.slice(0, max - 1) + "…" : text;
|
|
473
701
|
}
|
|
@@ -508,6 +736,19 @@ function githubCheckAnnotations(result, registry, root) {
|
|
|
508
736
|
const file = entry.files[0];
|
|
509
737
|
lines.push(file ? `::error file=${escapeProperty(relative(root, file).replaceAll("\\", "/"))}::${text}` : `::error::${text}`);
|
|
510
738
|
}
|
|
739
|
+
for (const entry of result.broken) {
|
|
740
|
+
const origin = messages.get(entry.key);
|
|
741
|
+
const text = escapeData(`[${entry.locale}] ${entry.key}: ${entry.issue}`);
|
|
742
|
+
const command = entry.severity === "error" ? "error" : "warning";
|
|
743
|
+
if (!origin) {
|
|
744
|
+
lines.push(`::${command}::${text}`);
|
|
745
|
+
continue;
|
|
746
|
+
}
|
|
747
|
+
const file = relative(root, origin.file).replaceAll("\\", "/");
|
|
748
|
+
const content = readSource(origin.file);
|
|
749
|
+
const line = content === void 0 ? void 0 : lineAt(content, origin.start);
|
|
750
|
+
lines.push(`::${command} file=${escapeProperty(file)}${line ? `,line=${line}` : ""}::${text}`);
|
|
751
|
+
}
|
|
511
752
|
return lines;
|
|
512
753
|
}
|
|
513
754
|
function lineAt(content, offset) {
|
|
@@ -524,13 +765,6 @@ function escapeProperty(text) {
|
|
|
524
765
|
}
|
|
525
766
|
//#endregion
|
|
526
767
|
//#region src/params.ts
|
|
527
|
-
const PLURAL_KEYS = /* @__PURE__ */ new Set([
|
|
528
|
-
"zero",
|
|
529
|
-
"one",
|
|
530
|
-
"two",
|
|
531
|
-
"few",
|
|
532
|
-
"many"
|
|
533
|
-
]);
|
|
534
768
|
const NUMBER_FORMATS = /* @__PURE__ */ new Set([
|
|
535
769
|
"number",
|
|
536
770
|
"integer",
|
|
@@ -550,7 +784,7 @@ function visit(nodes, out) {
|
|
|
550
784
|
out.set(node.name, types);
|
|
551
785
|
if (node.variants) {
|
|
552
786
|
for (const [key, body] of node.variants) {
|
|
553
|
-
if (
|
|
787
|
+
if (PLURAL_CATEGORIES.has(key) || /^=\d+$/.test(key)) types.add("number");
|
|
554
788
|
else if (key !== "other") types.add("string");
|
|
555
789
|
visit(body, out);
|
|
556
790
|
}
|
|
@@ -1005,6 +1239,12 @@ async function extractProject(cfg) {
|
|
|
1005
1239
|
for (const file of files) registry.update(file, analyzeFile(readFileSync(file, "utf8"), file));
|
|
1006
1240
|
return registry;
|
|
1007
1241
|
}
|
|
1242
|
+
async function collectOrigins(cfg) {
|
|
1243
|
+
const registry = await extractProject(cfg);
|
|
1244
|
+
const origins = {};
|
|
1245
|
+
for (const [key, files] of registry.origins()) origins[key] = files.map((file) => relative(cfg.root, file).replaceAll("\\", "/"));
|
|
1246
|
+
return origins;
|
|
1247
|
+
}
|
|
1008
1248
|
function syncCatalogs(cfg, catalogs, registry) {
|
|
1009
1249
|
const added = {};
|
|
1010
1250
|
const source = catalogs[cfg.sourceLocale] ??= {};
|
|
@@ -1656,25 +1896,19 @@ async function translateCatalogs(cfg, catalogs, provider, options = {}) {
|
|
|
1656
1896
|
}
|
|
1657
1897
|
return result;
|
|
1658
1898
|
}
|
|
1659
|
-
function
|
|
1660
|
-
|
|
1899
|
+
async function resolveProvider(cfg, model) {
|
|
1900
|
+
const configured = cfg.translate.provider;
|
|
1901
|
+
if (typeof configured === "function") return configured;
|
|
1902
|
+
const { claudeProvider } = await Promise.resolve().then(() => claude_exports);
|
|
1903
|
+
return claudeProvider({ model: model ?? cfg.translate.model });
|
|
1661
1904
|
}
|
|
1662
|
-
function
|
|
1905
|
+
function structureMatches(source, translated) {
|
|
1663
1906
|
try {
|
|
1664
|
-
return [...
|
|
1907
|
+
return ![...validateMessage(translated), ...validatePair(source, translated)].some((issue) => issue.severity === "error");
|
|
1665
1908
|
} catch {
|
|
1666
|
-
return
|
|
1909
|
+
return false;
|
|
1667
1910
|
}
|
|
1668
1911
|
}
|
|
1669
|
-
const TAG = /<(\/?)([a-zA-Z][\w-]*)(\/?)>/g;
|
|
1670
|
-
function tagTokens(message) {
|
|
1671
|
-
const out = [];
|
|
1672
|
-
for (const match of message.matchAll(TAG)) out.push(`${match[1]}${match[2]}${match[3]}`);
|
|
1673
|
-
return out.sort();
|
|
1674
|
-
}
|
|
1675
|
-
function sameMembers(a, b) {
|
|
1676
|
-
return a.length === b.length && a.every((value, i) => value === b[i]);
|
|
1677
|
-
}
|
|
1678
1912
|
//#endregion
|
|
1679
1913
|
//#region src/exchange.ts
|
|
1680
1914
|
function isMobileFormat(format) {
|
|
@@ -1890,6 +2124,12 @@ function csvRows(content) {
|
|
|
1890
2124
|
}
|
|
1891
2125
|
//#endregion
|
|
1892
2126
|
//#region src/providers/claude.ts
|
|
2127
|
+
var claude_exports = /* @__PURE__ */ __exportAll({
|
|
2128
|
+
batchFormat: () => batchFormat,
|
|
2129
|
+
buildPrompt: () => buildPrompt,
|
|
2130
|
+
claudeProvider: () => claudeProvider,
|
|
2131
|
+
loadSdk: () => loadSdk
|
|
2132
|
+
});
|
|
1893
2133
|
const DEFAULT_MODEL = "claude-sonnet-5";
|
|
1894
2134
|
const SYSTEM = `You translate UI strings for the Verbaly i18n library.
|
|
1895
2135
|
Rules:
|
|
@@ -2214,16 +2454,25 @@ function status(cfg, catalogs, registry, drafts = {}) {
|
|
|
2214
2454
|
const source = catalogs[cfg.sourceLocale] ?? {};
|
|
2215
2455
|
const needed = /* @__PURE__ */ new Set([...registry.messages().keys(), ...Object.keys(source)]);
|
|
2216
2456
|
const live = effectiveDrafts(drafts, catalogs);
|
|
2457
|
+
const extracted = registry.messages();
|
|
2217
2458
|
const locales = [];
|
|
2218
2459
|
for (const locale of cfg.locales) {
|
|
2219
2460
|
if (locale === cfg.sourceLocale) continue;
|
|
2220
2461
|
let translated = 0;
|
|
2221
2462
|
for (const key of needed) if (catalogs[locale]?.[key]) translated += 1;
|
|
2463
|
+
let broken = 0;
|
|
2464
|
+
const flatSource = flatten(source ?? {});
|
|
2465
|
+
for (const [key, text] of Object.entries(flatten(catalogs[locale] ?? {}))) {
|
|
2466
|
+
if (!text) continue;
|
|
2467
|
+
const from = flatSource[key] ?? extracted.get(key)?.message;
|
|
2468
|
+
if ([...validateMessage(text, locale), ...from ? validatePair(from, text) : []].some((issue) => issue.severity === "error")) broken += 1;
|
|
2469
|
+
}
|
|
2222
2470
|
locales.push({
|
|
2223
2471
|
locale,
|
|
2224
2472
|
translated,
|
|
2225
2473
|
total: needed.size,
|
|
2226
|
-
drafts: live[locale]?.length ?? 0
|
|
2474
|
+
drafts: live[locale]?.length ?? 0,
|
|
2475
|
+
broken
|
|
2227
2476
|
});
|
|
2228
2477
|
}
|
|
2229
2478
|
return {
|
|
@@ -2238,11 +2487,12 @@ function formatStatusResult(result) {
|
|
|
2238
2487
|
lines.push(" no target locales (add locales to your config)");
|
|
2239
2488
|
return lines.join("\n");
|
|
2240
2489
|
}
|
|
2241
|
-
for (const { locale, translated, total, drafts } of result.locales) {
|
|
2490
|
+
for (const { locale, translated, total, drafts, broken } of result.locales) {
|
|
2242
2491
|
const pct = total === 0 ? 100 : Math.floor(translated / total * 100);
|
|
2243
|
-
const mark = translated === total ? " ✓" : "";
|
|
2244
|
-
const
|
|
2245
|
-
|
|
2492
|
+
const mark = translated === total && broken === 0 ? " ✓" : "";
|
|
2493
|
+
const notes = [drafts > 0 ? `${drafts} unreviewed` : "", broken > 0 ? `${broken} broken` : ""].filter(Boolean);
|
|
2494
|
+
const note = notes.length > 0 ? `, ${notes.join(", ")}` : "";
|
|
2495
|
+
lines.push(` ${locale}: ${translated}/${total} translated (${pct}%${note})${mark}`);
|
|
2246
2496
|
}
|
|
2247
2497
|
return lines.join("\n");
|
|
2248
2498
|
}
|
|
@@ -2341,7 +2591,7 @@ function wrapCode(code, file) {
|
|
|
2341
2591
|
const s = new MagicString(code);
|
|
2342
2592
|
const visited = /* @__PURE__ */ new Set();
|
|
2343
2593
|
let changed = false;
|
|
2344
|
-
walk(ast, (node) => {
|
|
2594
|
+
walk$1(ast, (node) => {
|
|
2345
2595
|
if (node.type !== "JSXElement" && node.type !== "JSXFragment") return;
|
|
2346
2596
|
if (visited.has(node)) return;
|
|
2347
2597
|
processElement(node);
|
|
@@ -2460,7 +2710,7 @@ function wrapCode(code, file) {
|
|
|
2460
2710
|
return text.length > 60 ? text.slice(0, 59) + "…" : text;
|
|
2461
2711
|
}
|
|
2462
2712
|
function markSubtree(node) {
|
|
2463
|
-
walk(node, (n) => {
|
|
2713
|
+
walk$1(node, (n) => {
|
|
2464
2714
|
if (n.type === "JSXElement" || n.type === "JSXFragment") visited.add(n);
|
|
2465
2715
|
});
|
|
2466
2716
|
}
|
|
@@ -2474,7 +2724,7 @@ function hasVerbalyAttr(opening) {
|
|
|
2474
2724
|
}
|
|
2475
2725
|
function usesT(node) {
|
|
2476
2726
|
let found = false;
|
|
2477
|
-
walk(node, (n) => {
|
|
2727
|
+
walk$1(node, (n) => {
|
|
2478
2728
|
if (n.type === "TaggedTemplateExpression" && isTReference(n.tag, T_NAMES)) found = true;
|
|
2479
2729
|
if (n.type === "CallExpression" && isTReference(n.callee, T_NAMES)) found = true;
|
|
2480
2730
|
});
|
|
@@ -2482,7 +2732,7 @@ function usesT(node) {
|
|
|
2482
2732
|
}
|
|
2483
2733
|
function containsJsx(node) {
|
|
2484
2734
|
let found = false;
|
|
2485
|
-
walk(node, (n) => {
|
|
2735
|
+
walk$1(node, (n) => {
|
|
2486
2736
|
if (n.type === "JSXElement" || n.type === "JSXFragment") found = true;
|
|
2487
2737
|
});
|
|
2488
2738
|
return found;
|
|
@@ -2500,6 +2750,6 @@ function line(node) {
|
|
|
2500
2750
|
return node.loc?.start?.line ?? 1;
|
|
2501
2751
|
}
|
|
2502
2752
|
//#endregion
|
|
2503
|
-
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, PSEUDO_LOCALE, RESOLVED_VIRTUAL_ID, SFC_FILE_RE, SOURCE_FILE_RE, VIRTUAL_ID, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
2753
|
+
export { DRAFTS_FILE, LOCALE_MODULE_PREFIX, MessageRegistry, PLURAL_CATEGORIES, PSEUDO_LOCALE, RESOLVED_VIRTUAL_ID, SFC_FILE_RE, SOURCE_FILE_RE, VIRTUAL_ID, analyze, analyzeFile, analyzeSfc, catalogPath, check, claudeProvider, clearDrafts, collectOrigins, collectParams, createSourceFilter, detectBundler, doctor, effectiveDrafts, exportCatalogs, extractProject, findConfigFile, formatCheckResult, formatCheckWarnings, formatStatusResult, generateDts, generateLocaleModule, generateRuntimeModule, githubCheckAnnotations, importCatalogs, init, isMobileFormat, isTransformTarget, loadCatalogs, loadConfig, loadConfigFile, loadDrafts, loadVirtualModule, markDrafts, parseExchangeFile, pruneCatalogs, pseudoCatalogs, pseudoLocalize, readCatalog, renderHtml, renderParamType, renderSite, resolveConfig, resolveProvider, resolveVirtualId, runBuildGate, saveDrafts, serializeCatalog, stableKey, status, structureMatches, syncCatalogs, targetLocales, transformCode, transformSource, translateCatalogs, validateMessage, validatePair, watchProject, wrapCode, wrapProject, writeCatalog, writeDts };
|
|
2504
2754
|
|
|
2505
2755
|
//# sourceMappingURL=index.js.map
|