@langlink/l10n-mcp 0.2.1 → 0.3.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/index.js CHANGED
@@ -1,5 +1,159 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ // packages/l10n-mcp/src/platform.ts
4
+ import * as z from "zod/v4";
5
+
6
+ // packages/workbench-sdk/src/index.ts
7
+ var PlatformApiError = class extends Error {
8
+ status;
9
+ code;
10
+ requestId;
11
+ constructor(status, body, requestId) {
12
+ super(typeof body.message === "string" ? body.message : typeof body.detail === "string" ? body.detail : `Platform request failed (${status})`);
13
+ this.name = "PlatformApiError";
14
+ this.status = status;
15
+ this.code = typeof body.code === "string" ? body.code : typeof body.error === "string" ? body.error : "request_failed";
16
+ this.requestId = typeof body.requestId === "string" ? body.requestId : requestId;
17
+ }
18
+ };
19
+ function createPlatformClient(options) {
20
+ const parsed = new URL(options.baseUrl);
21
+ const loopback = ["127.0.0.1", "localhost", "[::1]"].includes(parsed.hostname);
22
+ if (parsed.protocol !== "https:" && !(loopback && parsed.protocol === "http:") || parsed.username || parsed.password || parsed.search || parsed.hash || parsed.pathname !== "/") throw new Error("platform baseUrl must be an HTTPS origin (HTTP only on loopback)");
23
+ const base = parsed.origin;
24
+ const fetcher = options.fetcher ?? fetch;
25
+ const timeoutMs = options.timeoutMs ?? 3e4;
26
+ if (!Number.isInteger(timeoutMs) || timeoutMs < 100 || timeoutMs > 12e4) throw new Error("timeoutMs must be 100..120000");
27
+ const path = (...parts) => `/api/v1/${parts.map(encodeURIComponent).join("/")}`;
28
+ async function response(method, endpoint, body, request = {}) {
29
+ const timeout = AbortSignal.timeout(timeoutMs);
30
+ const signal = request.signal ? AbortSignal.any([request.signal, timeout]) : timeout;
31
+ signal.throwIfAborted();
32
+ const provider = options.token;
33
+ const token = typeof provider === "string" ? provider : await new Promise((resolve2, reject) => {
34
+ const aborted = () => {
35
+ signal.removeEventListener("abort", aborted);
36
+ reject(signal.reason);
37
+ };
38
+ signal.addEventListener("abort", aborted, { once: true });
39
+ Promise.resolve().then(() => {
40
+ signal.throwIfAborted();
41
+ return provider();
42
+ }).then(
43
+ (value) => {
44
+ signal.removeEventListener("abort", aborted);
45
+ resolve2(value);
46
+ },
47
+ (error) => {
48
+ signal.removeEventListener("abort", aborted);
49
+ reject(error);
50
+ }
51
+ );
52
+ if (signal.aborted) aborted();
53
+ });
54
+ signal.throwIfAborted();
55
+ if (!token || /[\r\n]/u.test(token)) throw new Error("a valid platform Token is required");
56
+ const headers = { authorization: `Bearer ${token}`, accept: "application/json" };
57
+ if (body !== void 0) headers["content-type"] = "application/json";
58
+ if (request.idempotencyKey) headers["idempotency-key"] = request.idempotencyKey;
59
+ const result = await fetcher(base + endpoint, { method, headers, redirect: "error", credentials: "omit", ...body === void 0 ? {} : { body: JSON.stringify(body) }, signal });
60
+ if (!result.ok) {
61
+ const parsed2 = await result.json().catch(() => ({}));
62
+ throw new PlatformApiError(result.status, parsed2 && typeof parsed2 === "object" && !Array.isArray(parsed2) ? parsed2 : {}, result.headers.get("x-request-id") ?? void 0);
63
+ }
64
+ return result;
65
+ }
66
+ async function json3(method, endpoint, body, request) {
67
+ return await (await response(method, endpoint, body, request)).json();
68
+ }
69
+ return {
70
+ qaRules: () => json3("GET", path("qa-rules")),
71
+ configurations: (resource, offset = 0) => json3("GET", `${path(resource)}?offset=${offset}`),
72
+ configurationVersion: (resource, id, version) => json3("GET", `${path(resource, id)}?version=${version}`),
73
+ documentConfiguration: (id) => json3("GET", path("documents", id, "configuration")),
74
+ setDocumentConfiguration: (id, input) => json3("PATCH", path("documents", id, "configuration"), input),
75
+ me: () => json3("GET", path("me")),
76
+ plugins: () => json3("GET", path("plugins")),
77
+ models: () => json3("GET", path("models")),
78
+ createDocument: (input, request) => json3("POST", path("documents"), input, request),
79
+ documents: (offset = 0, limit = 50) => json3("GET", `${path("documents")}?offset=${offset}&limit=${limit}`),
80
+ documentChanges: (id, after) => json3("GET", `${path("documents", id, "changes")}?after=${encodeURIComponent(after)}`),
81
+ document: (id, offset = 0, limit = 50, request) => json3("GET", `${path("documents", id)}?segments.offset=${offset}&segments.limit=${limit}`, void 0, request),
82
+ segment: (id, segmentId) => json3("GET", path("documents", id, "segments", segmentId)),
83
+ edit: (id, segmentId, input, request) => json3("PATCH", path("documents", id, "segments", segmentId), input, request),
84
+ attachAssets: (id, input, request) => json3("POST", path("documents", id, "assets"), input, request),
85
+ segmentAssets: (id, segmentId) => json3("GET", path("documents", id, "segments", segmentId, "assets")),
86
+ comments: (id, segmentId) => json3("GET", path("documents", id, "comments") + (segmentId ? `?segmentId=${encodeURIComponent(segmentId)}` : "")),
87
+ addComment: (id, segmentId, body) => json3("POST", path("documents", id, "comments"), { segmentId, body }),
88
+ setStyle: (id, style, customStyleInstruction) => json3("PATCH", path("documents", id, "style"), { style, ...customStyleInstruction === void 0 ? {} : { customStyleInstruction } }),
89
+ setModels: (id, input) => json3("PATCH", path("documents", id, "models"), input),
90
+ createJob: (id, input, request) => json3("POST", path("documents", id, "jobs"), input, request),
91
+ job: (id, request) => json3("GET", path("jobs", id), void 0, request),
92
+ cancelJob: (id) => json3("POST", path("jobs", id, "cancel"), {}),
93
+ retryJob: (id, request) => json3("POST", path("jobs", id, "retry"), {}, request),
94
+ undoJob: (id) => json3("POST", path("jobs", id, "undo"), {}),
95
+ jobResults: (id) => json3("GET", path("jobs", id, "results")),
96
+ updateSuggestion: (id, segmentId, input) => json3("PATCH", path("documents", id, "segments", segmentId, "suggestion"), input),
97
+ discardSuggestion: (id, segmentId, input) => json3("DELETE", path("documents", id, "segments", segmentId, "suggestion"), input),
98
+ applySuggestion: (id, segmentId, input) => json3("POST", path("documents", id, "segments", segmentId, "apply-suggestion"), input),
99
+ exportDocument: async (id, request) => {
100
+ const result = await response("POST", path("documents", id, "export"), {}, request);
101
+ const body = await result.json();
102
+ return body;
103
+ },
104
+ async waitForJob(id, settings = {}) {
105
+ const duration = settings.timeoutMs ?? 12e4;
106
+ const deadline = Date.now() + duration;
107
+ const interval = settings.intervalMs ?? 500;
108
+ if (!Number.isInteger(duration) || duration < 1 || duration > 36e5 || !Number.isInteger(interval) || interval < 50 || interval > 5e3) throw new Error("invalid polling bounds");
109
+ const deadlineSignal = AbortSignal.timeout(duration);
110
+ const signal = settings.signal ? AbortSignal.any([settings.signal, deadlineSignal]) : deadlineSignal;
111
+ for (; ; ) {
112
+ settings.signal?.throwIfAborted();
113
+ if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
114
+ let result;
115
+ try {
116
+ result = await json3("GET", path("jobs", id), void 0, { signal });
117
+ } catch (error) {
118
+ if (deadlineSignal.aborted && !settings.signal?.aborted) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
119
+ throw error;
120
+ }
121
+ if (!["queued", "running"].includes(result.status)) return result;
122
+ if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
123
+ await new Promise((resolve2, reject) => {
124
+ const done = () => {
125
+ settings.signal?.removeEventListener("abort", abort);
126
+ resolve2();
127
+ };
128
+ const timer = setTimeout(done, Math.min(interval, Math.max(0, deadline - Date.now())));
129
+ const abort = () => {
130
+ clearTimeout(timer);
131
+ settings.signal?.removeEventListener("abort", abort);
132
+ reject(settings.signal?.reason);
133
+ };
134
+ settings.signal?.addEventListener("abort", abort, { once: true });
135
+ if (settings.signal?.aborted) abort();
136
+ });
137
+ }
138
+ }
139
+ };
140
+ }
141
+
142
+ // packages/l10n-mcp/src/platform.ts
143
+ function registerPlatformTools(server, options) {
144
+ const client = createPlatformClient(options);
145
+ const result = (value) => ({ content: [{ type: "text", text: JSON.stringify(value) }] });
146
+ server.registerTool("cat_models", { description: "List all gateway models, configured capabilities and explicitly selectable unverified trials. Directory membership does not prove task compatibility. Requires platform read scope.", inputSchema: {} }, async () => result(await client.models()));
147
+ server.registerTool("cat_documents", { description: "List only explicitly authorized platform documents.", inputSchema: { offset: z.number().int().min(0).optional() } }, async ({ offset }) => result(await client.documents(offset)));
148
+ server.registerTool("cat_document", { description: "Read one authorized document page.", inputSchema: { id: z.string(), offset: z.number().int().min(0).optional() } }, async ({ id, offset }) => result(await client.document(id, offset)));
149
+ server.registerTool("cat_create_document", { description: "Upload XML to a platform-owned resource. Requires explicit write/create grant.", inputSchema: { filename: z.string(), xml: z.string() } }, async (input) => result(await client.createDocument(input)));
150
+ server.registerTool("cat_create_job", { description: "Generate suggestions or run QA; never auto-applies a suggestion. Reuse the same idempotencyKey for the same request.", inputSchema: { documentId: z.string(), type: z.enum(["translate", "polish", "aiqa", "check"]), segmentIds: z.array(z.string()).min(1).max(5e3), model: z.string().optional(), instruction: z.string().max(2e3).optional(), idempotencyKey: z.string().max(128) } }, async ({ documentId, idempotencyKey, type, segmentIds, model, instruction }) => result(await client.createJob(documentId, { type, segmentIds, ...model ? { model } : {}, ...instruction ? { instruction } : {} }, { idempotencyKey })));
151
+ server.registerTool("cat_job", { description: "Read a platform job. Does not wait or retry.", inputSchema: { id: z.string() } }, async ({ id }) => result(await client.job(id)));
152
+ server.registerTool("cat_cancel_job", { description: "Request cancellation at segment boundaries.", inputSchema: { id: z.string() } }, async ({ id }) => result(await client.cancelJob(id)));
153
+ server.registerTool("cat_apply_suggestion", { description: "Explicitly apply a selected suggestion with revision protection; a write operation.", inputSchema: { documentId: z.string(), segmentId: z.string(), baseRevision: z.number().int().min(0) } }, async ({ documentId, segmentId, baseRevision }) => result(await client.applySuggestion(documentId, segmentId, { baseRevision })));
154
+ server.registerTool("cat_export", { description: "Export an authorized document as base64; no local file is written.", inputSchema: { id: z.string() } }, async ({ id }) => result(await client.exportDocument(id)));
155
+ }
156
+
3
157
  // packages/l10n-mcp/src/server.ts
4
158
  import { realpathSync } from "node:fs";
5
159
  import { readFile as readFile2 } from "node:fs/promises";
@@ -282,7 +436,53 @@ async function writeRecipe(recipe) {
282
436
 
283
437
  // packages/l10n-mcp/src/server.ts
284
438
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
285
- import * as z from "zod/v4";
439
+ import * as z2 from "zod/v4";
440
+
441
+ // packages/l10n-core/src/qa/review-server.ts
442
+ import { randomBytes } from "node:crypto";
443
+ import { createServer as createServer2 } from "node:http";
444
+ import ExcelJS from "exceljs";
445
+
446
+ // packages/l10n-core/src/qa/report.ts
447
+ function escapeCsvField(value) {
448
+ if (!/[",\r\n]/u.test(value)) return value;
449
+ return `"${value.replaceAll('"', '""')}"`;
450
+ }
451
+ function renderJson(result) {
452
+ const issues = result.issues.map((issue) => ({
453
+ ruleId: issue.ruleId,
454
+ severity: issue.severity,
455
+ segmentId: issue.segmentId,
456
+ segmentIndex: issue.segmentIndex,
457
+ message: issue.message,
458
+ evidence: issue.evidence,
459
+ ...issue.suggestion === void 0 ? {} : { suggestion: issue.suggestion },
460
+ ...issue.sourceRange === void 0 ? {} : { sourceRange: { start: issue.sourceRange.start, end: issue.sourceRange.end } },
461
+ ...issue.targetRange === void 0 ? {} : { targetRange: { start: issue.targetRange.start, end: issue.targetRange.end } },
462
+ ...issue.termId === void 0 ? {} : { termId: issue.termId },
463
+ origin: issue.origin
464
+ }));
465
+ return JSON.stringify(
466
+ {
467
+ totalSegments: result.totalSegments,
468
+ checkedSegments: result.checkedSegments,
469
+ coverage: result.coverage,
470
+ issueCount: issues.length,
471
+ issues,
472
+ rules: result.rules.map((rule) => ({
473
+ ruleId: rule.ruleId,
474
+ status: rule.status,
475
+ reason: rule.reason,
476
+ checkedSegments: rule.checkedSegments
477
+ }))
478
+ },
479
+ null,
480
+ 2
481
+ );
482
+ }
483
+
484
+ // packages/l10n-core/src/qa/review-data.ts
485
+ import { unzipSync } from "fflate";
286
486
 
287
487
  // packages/l10n-core/src/xml.ts
288
488
  import { Parser } from "htmlparser2";
@@ -308,6 +508,7 @@ function parseXml(xml) {
308
508
  children: []
309
509
  };
310
510
  const stack = [root];
511
+ let cdata = false;
311
512
  const parser = new Parser(
312
513
  {
313
514
  onopentag(name, attribs) {
@@ -315,11 +516,19 @@ function parseXml(xml) {
315
516
  if (!parent) return;
316
517
  const start = parser.startIndex;
317
518
  const openEnd = parser.endIndex + 1;
519
+ const namespaces = { xml: "http://www.w3.org/XML/1998/namespace", ...parent.namespaces };
520
+ for (const [key, value] of Object.entries(attribs)) {
521
+ if (key === "xmlns") namespaces[""] = decodeXmlEntities(value);
522
+ else if (key.startsWith("xmlns:")) namespaces[key.slice(6)] = decodeXmlEntities(value);
523
+ }
524
+ const prefix = name.includes(":") ? name.slice(0, name.indexOf(":")) : "";
318
525
  const node = {
319
526
  type: "element",
320
527
  name,
321
528
  local: localName(name),
322
529
  attribs: { ...attribs },
530
+ namespaceUri: namespaces[prefix] ?? "",
531
+ namespaces,
323
532
  start,
324
533
  openEnd,
325
534
  innerStart: openEnd,
@@ -343,9 +552,16 @@ function parseXml(xml) {
343
552
  parent.children.push({
344
553
  type: "text",
345
554
  text,
555
+ ...cdata ? { cdata: true } : {},
346
556
  start: parser.startIndex,
347
557
  end: parser.endIndex + 1
348
558
  });
559
+ },
560
+ oncdatastart() {
561
+ cdata = true;
562
+ },
563
+ oncdataend() {
564
+ cdata = false;
349
565
  }
350
566
  },
351
567
  {
@@ -360,8 +576,8 @@ function parseXml(xml) {
360
576
  parser.end();
361
577
  return root;
362
578
  }
363
- function validateXml(xml) {
364
- const parser = new SaxesParser({ xmlns: false });
579
+ function validateXml(xml, namespaces = false) {
580
+ const parser = new SaxesParser({ xmlns: namespaces });
365
581
  let error;
366
582
  parser.on("error", (value) => {
367
583
  error = value;
@@ -414,7 +630,7 @@ function decodeXmlEntities(value) {
414
630
  function textOf(node) {
415
631
  let value = "";
416
632
  for (const child of node.children) {
417
- value += child.type === "text" ? decodeXmlEntities(child.text) : textOf(child);
633
+ value += child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : textOf(child);
418
634
  }
419
635
  return value;
420
636
  }
@@ -422,7 +638,167 @@ function elementSignature(node) {
422
638
  return descendants(node).map((element) => element.local);
423
639
  }
424
640
 
641
+ // packages/l10n-core/src/formats/inline.ts
642
+ function container(node, namespace) {
643
+ return (node.namespaceUri ?? "") === namespace && ["g", "pc", "mrk"].includes(node.local) && node.attribs.translate !== "no" && node.attribs.mtype !== "protected";
644
+ }
645
+ function inlineCharacter(node) {
646
+ const read = (candidate) => {
647
+ if (candidate.local !== "ch" || !["MQXliff", "http://www.memoq.com/namespace"].includes(candidate.namespaceUri ?? "")) return void 0;
648
+ const value = candidate.attribs.val;
649
+ return value === void 0 ? void 0 : decodeXmlEntities(value);
650
+ };
651
+ const direct = read(node);
652
+ if (direct !== void 0) return direct;
653
+ if (node.local !== "ph" || !["", "urn:oasis:names:tc:xliff:document:1.2", "urn:oasis:names:tc:xliff:document:2.0"].includes(node.namespaceUri ?? "")) return void 0;
654
+ const elements = elementChildren(node);
655
+ if (elements.length === 1 && node.children.every((child) => child.type === "element" || !child.text.trim())) return read(elements[0]);
656
+ if (elements.length) return void 0;
657
+ const payload = textOf(node);
658
+ if (payload.length > 4096 || !payload.trim().startsWith("<")) return void 0;
659
+ try {
660
+ const context = { ...node, namespaces: { mq: "http://www.memoq.com/namespace", ...node.namespaces } };
661
+ const root = fragment(payload, context).root;
662
+ const codes2 = elementChildren(root);
663
+ if (codes2.length !== 1 || !root.children.every((child) => child.type === "element" || !child.text.trim())) return void 0;
664
+ return read(codes2[0]);
665
+ } catch {
666
+ return void 0;
667
+ }
668
+ }
669
+ function inlineText(root) {
670
+ const namespace = root.namespaceUri ?? "";
671
+ const walk = (node) => node.children.map((child) => child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : container(child, namespace) ? walk(child) : inlineCharacter(child) ?? "").join("");
672
+ return walk(root);
673
+ }
674
+ function codeIdentity(node, native = true, namespace = node.namespaceUri ?? "") {
675
+ const identity = ["id", "rid", "startRef", "dataRef", "dataRefStart", "dataRefEnd", "pos"].map((key) => [key, node.attribs[key] ?? null]);
676
+ return JSON.stringify([
677
+ node.namespaceUri ?? "",
678
+ node.local,
679
+ identity,
680
+ native && !container(node, namespace) ? inlineCharacter(node) ?? textOf(node) : ""
681
+ ]);
682
+ }
683
+ function inlineCodeSignatures(root) {
684
+ const result = [], namespace = root.namespaceUri ?? "";
685
+ const walk = (parent) => {
686
+ for (const node of elementChildren(parent)) {
687
+ result.push(codeIdentity(node, true, namespace));
688
+ if (container(node, namespace)) walk(node);
689
+ }
690
+ };
691
+ walk(root);
692
+ return result;
693
+ }
694
+ function fragment(content, context) {
695
+ const escape = (value) => value.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;");
696
+ const ns = Object.entries(context.namespaces ?? {}).filter(([prefix2]) => prefix2 !== "xml").map(([prefix2, value]) => ` xmlns${prefix2 ? `:${prefix2}` : ""}="${escape(value)}"`).join("");
697
+ let prefix = "workbench";
698
+ while (Object.hasOwn(context.namespaces ?? {}, prefix)) prefix += "_";
699
+ const uri = context.namespaceUri ?? "";
700
+ const name = uri ? `${prefix}:wrapper` : "wrapper";
701
+ const binding = uri ? ` xmlns:${prefix}="${escape(uri)}"` : "";
702
+ const xml = `<${name}${ns}${binding}>${content}</${name}>`;
703
+ validateXml(xml, true);
704
+ return { root: elementChildren(parseXml(xml))[0], xml };
705
+ }
706
+ function codes(root, xml) {
707
+ const result = [];
708
+ const namespace = root.namespaceUri ?? "";
709
+ const walk = (parent) => {
710
+ for (const child of elementChildren(parent)) {
711
+ if (container(child, namespace) && child.openEnd !== child.end) {
712
+ result.push({ raw: xml.slice(child.start, child.openEnd), node: child, close: false });
713
+ walk(child);
714
+ result.push({ raw: xml.slice(child.innerEnd, child.end), node: child, close: true });
715
+ } else result.push({ raw: xml.slice(child.start, child.end), node: child, close: false });
716
+ }
717
+ };
718
+ walk(root);
719
+ return result;
720
+ }
721
+ function inlineCodeOrderError(source, target) {
722
+ const sourceCodes = codes(source, ""), targetCodes = codes(target, "");
723
+ const namespace = source.namespaceUri ?? "";
724
+ const positions = /* @__PURE__ */ new Map();
725
+ const key = (code) => (code.close ? "/" : "") + codeIdentity(code.node, true, namespace);
726
+ sourceCodes.forEach((code, index) => {
727
+ const identity = key(code), group = positions.get(identity) ?? [];
728
+ group.push(index);
729
+ positions.set(identity, group);
730
+ });
731
+ const order = targetCodes.map((code) => positions.get(key(code))?.shift() ?? -1);
732
+ if (sourceCodes.length !== order.length || order.includes(-1)) return void 0;
733
+ try {
734
+ validateCodeOrder(sourceCodes, order, sourceCodes.filter((code) => !code.close), namespace);
735
+ return void 0;
736
+ } catch (error) {
737
+ return error instanceof Error ? error.message : "\u6807\u7B7E\u987A\u5E8F\u65E0\u6548";
738
+ }
739
+ }
740
+ function validateCodeOrder(original, order, sourceCodes, namespace) {
741
+ if (order.every((value, i) => value === i)) return;
742
+ const hintFor = (code) => {
743
+ if ((code.node.namespaceUri ?? "") !== namespace) return void 0;
744
+ const matches = sourceCodes.filter((item) => codeIdentity(item.node, false) === codeIdentity(code.node, false));
745
+ const sourceHint = matches.find((item) => ["no", "firstNo"].includes(item.node.attribs.canReorder ?? ""))?.node.attribs.canReorder;
746
+ if (sourceHint && matches.length !== 1) throw new Error("ambiguous source editing hints require original code order");
747
+ return code.node.attribs.canReorder === "no" || sourceHint === "no" ? "no" : code.node.attribs.canReorder === "firstNo" || sourceHint === "firstNo" ? "firstNo" : void 0;
748
+ };
749
+ const position = new Map(order.map((value, i) => [value, i]));
750
+ const stack = [], pairs = [];
751
+ let fixedOrder = false;
752
+ for (let i = 0; i < original.length; i++) {
753
+ const code = original[i], node = code.node;
754
+ if (code.close || (node.namespaceUri ?? "") !== namespace) continue;
755
+ const name = node.local;
756
+ if (node.attribs.isolated === "yes" || node.attribs.isolated === "true") fixedOrder = true;
757
+ if (["bpt", "bx", "sc", "sm"].includes(name)) stack.push(i);
758
+ if (["ept", "ex", "ec", "em"].includes(name)) {
759
+ const start = stack.pop(), open = start === void 0 ? void 0 : original[start].node;
760
+ const opener = { ept: "bpt", ex: "bx", ec: "sc", em: "sm" }[name];
761
+ const matches = open && open.local === opener && (["ec", "em"].includes(name) ? node.attribs.startRef === open.attribs.id && node.attribs.startRef !== void 0 : node.attribs.rid !== void 0 || open.attribs.rid !== void 0 ? node.attribs.rid === open.attribs.rid : true);
762
+ if (!matches || start === void 0) fixedOrder = true;
763
+ else pairs.push([start, i]);
764
+ }
765
+ }
766
+ if (stack.length) fixedOrder = true;
767
+ if (fixedOrder) throw new Error("isolated, overlapping or incomplete codes must retain their original order");
768
+ const active = [];
769
+ const ends = new Map(pairs.map(([start, end]) => [end, start]));
770
+ const starts = new Set(pairs.map(([start]) => start));
771
+ for (const index of order) {
772
+ if (starts.has(index)) active.push(index);
773
+ if (ends.has(index) && active.pop() !== ends.get(index)) throw new Error("inline code pairs are crossed or reversed");
774
+ }
775
+ const logical = original.map((code, index) => ({ code, index })).filter(({ code }) => !code.close);
776
+ let group = [];
777
+ const groups = [];
778
+ for (const { code, index } of logical) {
779
+ const hint = hintFor(code);
780
+ if (hint === "firstNo") {
781
+ group = [index];
782
+ groups.push(group);
783
+ } else if (hint === "no") {
784
+ if (!group.length) throw new Error("non-reorderable codes must retain their original order");
785
+ group.push(index);
786
+ } else group = [];
787
+ }
788
+ const logicalOrder = order.filter((index) => !original[index].close);
789
+ for (const members of groups) {
790
+ const start = logicalOrder.indexOf(members[0]);
791
+ if (members.some((index, offset) => logicalOrder[start + offset] !== index)) throw new Error("non-reorderable code sequence was split or reordered");
792
+ }
793
+ for (const { code, index } of logical) {
794
+ if (hintFor(code) === "no" && ["g", "pc", "mrk"].includes(code.node.local)) {
795
+ if (position.get(index) !== index) throw new Error("non-reorderable container moved");
796
+ }
797
+ }
798
+ }
799
+
425
800
  // packages/l10n-core/src/formats/xliff12.ts
801
+ var SDL_NAMESPACE = "http://sdl.com/FileTypes/SdlXliff/1.0";
426
802
  var SDL_DATATYPE = "x-sdlfilterframework2";
427
803
  function attr(node, name) {
428
804
  return node.attribs[name];
@@ -430,12 +806,12 @@ function attr(node, name) {
430
806
  function isSegMrk(node) {
431
807
  return node.local === "mrk" && node.attribs.mtype === "seg";
432
808
  }
433
- function sdlElement(segDefs) {
434
- if (!segDefs) return void 0;
435
- return elementChildren(segDefs).find((child) => child.local === "seg");
809
+ function sdlElement(segDefs, mid) {
810
+ const nodes = segDefs ? elementChildren(segDefs).filter((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) : [];
811
+ const matches = mid === void 0 ? nodes : nodes.filter((node) => node.attribs.id === mid);
812
+ return matches.length === 1 ? matches[0] : void 0;
436
813
  }
437
- function sdlState(segDefs) {
438
- const seg = sdlElement(segDefs);
814
+ function sdlState(seg) {
439
815
  if (!seg) return void 0;
440
816
  const state = {};
441
817
  const mapping = [
@@ -457,7 +833,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
457
833
  const targetTags = targetContainer ? elementSignature(targetContainer) : [];
458
834
  const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
459
835
  if (!targetContainer) warnings.push(`${id}: target missing`);
460
- if (targetContainer && sourceTags.length > 0 && sourceTags.join("|") !== targetTags.join("|")) {
836
+ if (sourceContainer && targetContainer && JSON.stringify(inlineCodeSignatures(sourceContainer).sort()) !== JSON.stringify(inlineCodeSignatures(targetContainer).sort())) {
461
837
  warnings.push(`${id}: source/target inline tag signature differs`);
462
838
  }
463
839
  return {
@@ -466,8 +842,8 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
466
842
  location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
467
843
  sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
468
844
  targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
469
- sourceText: sourceContainer ? textOf(sourceContainer) : "",
470
- targetText: targetContainer ? textOf(targetContainer) : "",
845
+ sourceText: sourceContainer ? inlineText(sourceContainer) : "",
846
+ targetText: targetContainer ? inlineText(targetContainer) : "",
471
847
  sourceTags,
472
848
  targetTags,
473
849
  targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
@@ -478,115 +854,126 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
478
854
  };
479
855
  }
480
856
  function parseXliff(xml, options = {}) {
481
- validateXml(xml);
857
+ validateXml(xml, true);
482
858
  const root = elementChildren(parseXml(xml))[0];
483
- if (!root) throw new Error("No root element");
484
- if (root.local !== "xliff") throw new Error(`Not an XLIFF document: <${root.name}>`);
859
+ if (!root || root.local !== "xliff") throw new Error("Not an XLIFF document");
485
860
  const version = attr(root, "version") ?? "";
486
861
  const isXliff2 = version === "2.0" || version === "2.1";
487
- if (version !== "1.2" && !isXliff2) {
488
- throw new Error(
489
- `Unsupported XLIFF version: ${version || "(missing)"}; supported: 1.2 (SDLXLIFF/MQXLIFF), 2.0 and 2.1`
490
- );
491
- }
492
- const files = [];
493
- const segments = [];
494
- const warnings = [];
495
- const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
496
- const allElements = descendants(root);
497
- const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
498
- const fileNodes = allElements.filter((element) => element.local === "file");
862
+ if (version !== "1.2" && !isXliff2) throw new Error("Unsupported XLIFF version: " + (version || "(missing)") + "; supported: 1.2, 2.0 and 2.1");
863
+ const namespace = root.namespaceUri ?? "";
864
+ const expected = isXliff2 ? "urn:oasis:names:tc:xliff:document:2.0" : "urn:oasis:names:tc:xliff:document:1.2";
865
+ if (namespace !== "" && namespace !== expected) throw new Error("Unsupported XLIFF namespace: " + namespace);
866
+ const core = (node, local) => node.local === local && (node.namespaceUri ?? "") === namespace;
867
+ const children = (node, local) => elementChildren(node).filter((child) => core(child, local));
868
+ const files = [], segments = [], warnings = [];
499
869
  const filename = options.filename?.toLowerCase() ?? "";
500
- let format = version === "2.1" ? "xliff21" : version === "2.0" ? "xliff20" : filename.endsWith(".sdlxliff") ? "sdlxliff" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdlElement ? "sdlxliff" : "xliff12";
501
- fileNodes.forEach((fileNode, fileIndex) => {
870
+ const hasSdl = descendants(root).some((node) => node.namespaceUri === SDL_NAMESPACE);
871
+ let format = version === "2.1" ? "xliff21" : isXliff2 ? "xliff20" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdl || filename.endsWith(".sdlxliff") ? "sdlxliff" : "xliff12";
872
+ const ids = /* @__PURE__ */ new Map();
873
+ children(root, "file").forEach((fileNode, fileIndex) => {
502
874
  const datatype = attr(fileNode, "datatype");
503
- if (datatype === SDL_DATATYPE) format = "sdlxliff";
504
- const file = {
875
+ if (!isXliff2 && datatype === SDL_DATATYPE) format = "sdlxliff";
876
+ files.push({
505
877
  index: fileIndex,
506
- sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(fileNode, "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
507
- targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(fileNode, "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
878
+ sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
879
+ targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
508
880
  ...datatype === void 0 ? {} : { datatype }
509
- };
510
- files.push(file);
511
- const units = descendants(
512
- fileNode,
513
- (candidate) => candidate.local === "trans-unit" || candidate.local === "unit"
514
- );
515
- units.forEach((unit, unitIndex) => {
516
- const unitId = attr(unit, "id") ?? `unit-${unitIndex}`;
517
- if (isXliff2) {
518
- const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
519
- if (segmentNodes.length === 0) {
520
- warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
521
- return;
881
+ });
882
+ let unitIndex = 0;
883
+ const inherited = (node, parent) => node.attribs.translate === "no" ? false : node.attribs.translate === "yes" ? true : parent;
884
+ const walk = (parent, translate) => {
885
+ for (const unit of elementChildren(parent)) {
886
+ if (core(unit, "group") || core(unit, "body")) {
887
+ walk(unit, core(unit, "group") ? inherited(unit, translate) : translate);
888
+ continue;
522
889
  }
523
- segmentNodes.forEach((segmentNode, segmentIndex) => {
524
- const segmentChildren = elementChildren(segmentNode);
525
- const source = segmentChildren.find((child) => child.local === "source");
526
- const target = segmentChildren.find((child) => child.local === "target");
527
- const segmentId = attr(segmentNode, "id") ?? `seg-${segmentIndex}`;
528
- segments.push(
529
- makeSegment(
530
- fileIndex,
531
- unitId,
532
- segmentIndex,
533
- segments.length,
534
- source,
535
- target,
536
- segmentId,
537
- void 0,
538
- void 0,
539
- xml,
540
- warnings
541
- )
542
- );
543
- });
544
- return;
545
- }
546
- const children = elementChildren(unit);
547
- const sourceElement = children.find((child) => child.local === "source");
548
- const targetElement = children.find((child) => child.local === "target");
549
- const segSourceElement = children.find((child) => child.local === "seg-source");
550
- const segDefsElement = children.find((child) => child.local === "seg-defs");
551
- const sdl = sdlState(segDefsElement);
552
- const sdlNode = sdlElement(segDefsElement);
553
- if (segSourceElement) {
554
- const sourceMrks = descendants(segSourceElement, isSegMrk);
555
- const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
556
- sourceMrks.forEach((sourceMrk, segmentIndex) => {
557
- const mid = attr(sourceMrk, "mid");
558
- const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
559
- if (targetMrk === void 0 && targetMrks.length > 0) {
560
- warnings.push(`${fileIndex}:${unitId}:${mid ?? segmentIndex}: target mrk missing`);
890
+ if (!core(unit, isXliff2 ? "unit" : "trans-unit")) continue;
891
+ const unitId = attr(unit, "id") ?? "unit-" + unitIndex;
892
+ unitIndex += 1;
893
+ const mayTranslate = inherited(unit, translate);
894
+ const data = /* @__PURE__ */ Object.create(null);
895
+ const duplicateData = /* @__PURE__ */ new Set();
896
+ for (const original of children(unit, "originalData")) for (const node of children(original, "data")) {
897
+ const id = node.attribs.id;
898
+ if (!id) continue;
899
+ if (Object.hasOwn(data, id)) duplicateData.add(id);
900
+ else data[id] = textOf(node);
901
+ }
902
+ for (const id of duplicateData) {
903
+ delete data[id];
904
+ warnings.push(fileIndex + ":" + unitId + ": ambiguous originalData reference " + id);
905
+ }
906
+ const segDefs = elementChildren(unit).find((node) => node.local === "seg-defs" && node.namespaceUri === SDL_NAMESPACE);
907
+ const push = (source2, target2, mid, index, reason) => {
908
+ const sdlNode = sdlElement(segDefs, mid), sdl = sdlState(sdlNode);
909
+ const segment = makeSegment(fileIndex, unitId, index, segments.length, source2, target2, mid, sdl, sdlNode, xml, warnings);
910
+ if (Object.keys(data).length) segment.originalData = data;
911
+ const markup = target2 && /<!\[CDATA\[|<!--|<\?/.test(segment.targetInner);
912
+ const ambiguousSdl = segDefs && elementChildren(segDefs).some((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) && !sdlNode;
913
+ const readOnly = reason ?? (!mayTranslate ? "translate=no" : ambiguousSdl ? "SDL segment state cannot be mapped uniquely" : ["true", "yes", "1"].includes(sdl?.locked ?? "") ? "SDL segment locked" : markup ? "CDATA, comments or processing instructions require lossless read-only handling" : void 0);
914
+ if (readOnly) {
915
+ segment.readOnlyReason = readOnly;
916
+ warnings.push(segment.id + ": read-only: " + readOnly);
561
917
  }
562
- segments.push(
563
- makeSegment(
564
- fileIndex,
565
- unitId,
566
- segmentIndex,
567
- segments.length,
568
- sourceMrk,
569
- targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
570
- mid,
571
- sdl,
572
- sdlNode,
573
- xml,
574
- warnings
575
- )
576
- );
577
- });
578
- return;
579
- }
580
- if (sourceElement || targetElement) {
581
- segments.push(
582
- makeSegment(fileIndex, unitId, 0, segments.length, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
918
+ const existing = ids.get(segment.id);
919
+ if (existing) {
920
+ existing.readOnlyReason = "ambiguous segment identity";
921
+ segment.readOnlyReason = "ambiguous segment identity";
922
+ do {
923
+ segment.id += ":duplicate:" + segment.index;
924
+ } while (ids.has(segment.id));
925
+ warnings.push(existing.id + ": ambiguous segment identity; read-only");
926
+ }
927
+ ids.set(segment.id, segment);
928
+ segments.push(segment);
929
+ };
930
+ if (isXliff2) {
931
+ const nodes = children(unit, "segment");
932
+ if (!nodes.length) warnings.push(fileIndex + ":" + unitId + ": unit has no supported segment");
933
+ nodes.forEach((node, index) => push(
934
+ children(node, "source")[0],
935
+ children(node, "target")[0],
936
+ attr(node, "id") ?? "seg-" + index,
937
+ index,
938
+ children(node, "source").length > 1 || children(node, "target").length > 1 ? "ambiguous source/target containers" : void 0
939
+ ));
940
+ continue;
941
+ }
942
+ const source = children(unit, "source")[0], target = children(unit, "target")[0], segSource = children(unit, "seg-source")[0];
943
+ const sourceMrks = segSource ? descendants(segSource, (node) => core(node, "mrk") && isSegMrk(node)) : [];
944
+ if (sourceMrks.length) {
945
+ const targetMrks = target ? descendants(target, (node) => core(node, "mrk") && isSegMrk(node)) : [];
946
+ sourceMrks.forEach((mrk, index) => {
947
+ const mid = attr(mrk, "mid");
948
+ const sourceMatches = sourceMrks.filter((node) => node.attribs.mid === mid);
949
+ const matches = mid ? targetMrks.filter((node) => node.attribs.mid === mid) : [];
950
+ const whole = sourceMrks.length === 1 && !targetMrks.length ? target : void 0;
951
+ const unique = mid !== void 0 && sourceMatches.length === 1 && matches.length === 1;
952
+ const mapped = unique ? matches[0] : whole;
953
+ push(mrk, mapped, mid, index, !mapped || sourceMrks.length > 1 && (!mid || sourceMatches.length !== 1) ? "segmented target cannot be mapped uniquely by mid" : void 0);
954
+ });
955
+ } else if (source || target || segSource) push(
956
+ source ?? segSource,
957
+ target,
958
+ void 0,
959
+ 0,
960
+ children(unit, "source").length > 1 || children(unit, "target").length > 1 ? "ambiguous source/target containers" : void 0
583
961
  );
584
- } else {
585
- warnings.push(`${fileIndex}:${unitId}: no source/target`);
962
+ else warnings.push(fileIndex + ":" + unitId + ": no source/target");
586
963
  }
587
- });
964
+ };
965
+ if (isXliff2) walk(fileNode, inherited(fileNode, true));
966
+ else for (const body of children(fileNode, "body")) walk(body, true);
588
967
  });
589
- if (hasSdlNamespace || hasSdlElement) format = "sdlxliff";
968
+ const ranged = segments.filter((segment) => segment.targetRange).sort((a, b) => a.targetRange.start - b.targetRange.start || b.targetRange.end - a.targetRange.end);
969
+ let previous;
970
+ for (const segment of ranged) {
971
+ if (previous && (segment.targetRange.start < previous.targetRange.end || segment.targetRange.start === previous.targetRange.start)) {
972
+ previous.readOnlyReason = segment.readOnlyReason = "overlapping target containers";
973
+ warnings.push(segment.id + ": overlapping target containers; read-only");
974
+ }
975
+ if (!previous || segment.targetRange.end > previous.targetRange.end) previous = segment;
976
+ }
590
977
  return { format, version, files, segments, warnings, xml };
591
978
  }
592
979
 
@@ -692,8 +1079,8 @@ function readProperties(props) {
692
1079
  function parseTmx(xml, options = {}) {
693
1080
  const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
694
1081
  validateXml(source);
695
- const document = parseXml(source);
696
- const root = elementChildren(document).find((child) => child.local === "tmx");
1082
+ const document2 = parseXml(source);
1083
+ const root = elementChildren(document2).find((child) => child.local === "tmx");
697
1084
  if (!root) throw new Error("Not a TMX document: missing <tmx> root element");
698
1085
  const warnings = [];
699
1086
  const header = findChild(root, "header");
@@ -752,322 +1139,69 @@ function parseTmx(xml, options = {}) {
752
1139
  return { entries, warnings };
753
1140
  }
754
1141
 
755
- // packages/l10n-core/src/assets/glossary.ts
756
- var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
757
- var TARGET_HEADERS = /* @__PURE__ */ new Set(["target", "target term", "translation", "\u8BD1\u6587", "\u76EE\u6807", "\u76EE\u6807\u8BED"]);
758
- var FORBIDDEN_HEADERS = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
759
- function stripBom(text) {
760
- return text.charCodeAt(0) === 65279 ? text.slice(1) : text;
1142
+ // packages/l10n-core/src/assets/term-matcher.ts
1143
+ var CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
1144
+ var WORD_CHAR = /[\p{L}\p{N}_]/u;
1145
+ var COMBINING_MARK = new RegExp("\\p{M}", "u");
1146
+ var MAX_TERM_TOKENS = 8;
1147
+ var FUZZY_THRESHOLD = 0.8;
1148
+ function readToken(text, index) {
1149
+ const codePoint = text.codePointAt(index);
1150
+ if (codePoint === void 0) return void 0;
1151
+ const char = String.fromCodePoint(codePoint);
1152
+ const end = index + char.length;
1153
+ if (CJK_CHAR.test(char)) return { value: char, start: index, end, kind: "cjk" };
1154
+ return { value: char, start: index, end, kind: "word" };
761
1155
  }
762
- function detectDelimiter(text) {
763
- let commas = 0;
764
- let tabs = 0;
765
- let inQuotes = false;
766
- for (let index = 0; index < text.length; index += 1) {
767
- const char = text[index];
768
- if (char === '"') {
769
- if (inQuotes && text[index + 1] === '"') index += 1;
770
- else inQuotes = !inQuotes;
771
- } else if (!inQuotes) {
772
- if (char === "\n") break;
773
- else if (char === ",") commas += 1;
774
- else if (char === " ") tabs += 1;
1156
+ function tokenizeText(text) {
1157
+ const tokens = [];
1158
+ let index = 0;
1159
+ while (index < text.length) {
1160
+ const first = readToken(text, index);
1161
+ if (!first) break;
1162
+ if (first.kind === "cjk") {
1163
+ tokens.push(first);
1164
+ index = first.end;
1165
+ continue;
775
1166
  }
776
- }
777
- return tabs > commas ? " " : ",";
778
- }
779
- function parseCsvRows(text, delimiter = ",") {
780
- const rows = [];
781
- let row = [];
782
- let field = "";
783
- let inQuotes = false;
784
- const pushRow = () => {
785
- row.push(field);
786
- field = "";
787
- if (row.some((cell) => cell.trim() !== "")) rows.push(row);
788
- row = [];
789
- };
790
- for (let index = 0; index < text.length; index += 1) {
791
- const char = text[index];
792
- if (inQuotes) {
793
- if (char === '"') {
794
- if (text[index + 1] === '"') {
795
- field += '"';
796
- index += 1;
797
- } else {
798
- inQuotes = false;
799
- }
800
- } else if (char === "\r") {
801
- field += "\n";
802
- if (text[index + 1] === "\n") index += 1;
803
- } else {
804
- field += char;
805
- }
1167
+ if (!WORD_CHAR.test(first.value)) {
1168
+ index = first.end;
806
1169
  continue;
807
1170
  }
808
- if (char === '"') {
809
- inQuotes = true;
810
- } else if (char === delimiter) {
811
- row.push(field);
812
- field = "";
813
- } else if (char === "\n") {
814
- pushRow();
815
- } else if (char === "\r") {
816
- if (text[index + 1] !== "\n") pushRow();
817
- } else {
818
- field += char;
1171
+ let end = first.end;
1172
+ while (end < text.length) {
1173
+ const next = readToken(text, end);
1174
+ if (!next || next.kind === "cjk") break;
1175
+ if (!WORD_CHAR.test(next.value) && !COMBINING_MARK.test(next.value)) break;
1176
+ end = next.end;
819
1177
  }
1178
+ tokens.push({ value: text.slice(index, end), start: index, end, kind: "word" });
1179
+ index = end;
820
1180
  }
821
- if (field !== "" || row.length > 0) pushRow();
822
- return rows;
1181
+ return tokens;
823
1182
  }
824
- function headerIndex(row, aliases) {
825
- return row.findIndex((cell) => aliases.has(cell.trim().toLowerCase()));
1183
+ function codePointLength(value) {
1184
+ return [...value].length;
826
1185
  }
827
- function isTruthyFlag(value) {
828
- return /^(?:true|1|yes|y|是|禁用)$/i.test(value.trim());
1186
+ function caseModeOf(entry) {
1187
+ if (entry.caseSensitive === true) return "yes";
1188
+ if (entry.caseSensitive === false) return "no";
1189
+ return "permissive";
829
1190
  }
830
- function normalizedHeader(row) {
831
- return row.map((cell) => cell.trim().toLowerCase());
1191
+ function caseMatches(term, text, mode) {
1192
+ if (term.length !== text.length) return false;
1193
+ if (mode === "yes") return term === text;
1194
+ if (mode === "no") return term.toLowerCase() === text.toLowerCase();
1195
+ for (let index = 0; index < term.length; index += 1) {
1196
+ const termChar = term[index];
1197
+ const textChar = text[index];
1198
+ if (termChar === textChar) continue;
1199
+ if (termChar.toLowerCase() !== textChar.toLowerCase()) return false;
1200
+ if (termChar !== termChar.toLowerCase()) return false;
1201
+ }
1202
+ return true;
832
1203
  }
833
- function parseGlossaryCsv(text, options = {}) {
834
- const content = stripBom(text);
835
- const delimiter = detectDelimiter(content);
836
- const rows = parseCsvRows(content, delimiter);
837
- const warnings = [];
838
- if (rows.length === 0) return { entries: [], warnings: ["glossary: no rows found"] };
839
- let sourceColumn = 0;
840
- let targetColumn = 1;
841
- let forbiddenColumn = 2;
842
- let dataRows = rows;
843
- const first = rows[0];
844
- if (first) {
845
- const header = normalizedHeader(first);
846
- const sourceIndex = headerIndex(header, SOURCE_HEADERS);
847
- const targetIndex = headerIndex(header, TARGET_HEADERS);
848
- if (sourceIndex >= 0 && targetIndex >= 0) {
849
- sourceColumn = sourceIndex;
850
- targetColumn = targetIndex;
851
- const forbiddenIndex = headerIndex(header, FORBIDDEN_HEADERS);
852
- forbiddenColumn = forbiddenIndex >= 0 ? forbiddenIndex : void 0;
853
- const known = /* @__PURE__ */ new Set([sourceIndex, targetIndex]);
854
- if (forbiddenIndex >= 0) known.add(forbiddenIndex);
855
- header.forEach((cell, index) => {
856
- if (cell !== "" && !known.has(index)) {
857
- warnings.push(`glossary: ignored column "${first[index]}"`);
858
- }
859
- });
860
- dataRows = rows.slice(1);
861
- } else if (sourceIndex >= 0 || targetIndex >= 0) {
862
- warnings.push("glossary: header is missing a source or target column; using positional columns");
863
- sourceColumn = sourceIndex >= 0 ? sourceIndex : 0;
864
- targetColumn = targetIndex >= 0 ? targetIndex : sourceColumn === 0 ? 1 : 0;
865
- forbiddenColumn = void 0;
866
- dataRows = rows.slice(1);
867
- }
868
- }
869
- const entries = [];
870
- dataRows.forEach((row, rowIndex) => {
871
- const position = rowIndex + 1;
872
- const source = (row[sourceColumn] ?? "").trim();
873
- const target = (row[targetColumn] ?? "").trim();
874
- if (source === "" && target === "") return;
875
- if (source === "" || target === "") {
876
- warnings.push(`glossary: row ${position} is missing ${source === "" ? "source" : "target"}; skipped`);
877
- return;
878
- }
879
- const forbiddenCell = forbiddenColumn === void 0 ? "" : row[forbiddenColumn] ?? "";
880
- const forbidden = isTruthyFlag(forbiddenCell);
881
- entries.push({
882
- id: `glossary-${entries.length + 1}`,
883
- source,
884
- target,
885
- ...options.sourceLanguage === void 0 ? {} : { sourceLanguage: options.sourceLanguage },
886
- ...options.targetLanguage === void 0 ? {} : { targetLanguage: options.targetLanguage },
887
- ...forbidden ? { forbidden: true } : {}
888
- });
889
- });
890
- return { entries, warnings };
891
- }
892
-
893
- // packages/l10n-core/src/assets/tm-matcher.ts
894
- var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
895
- var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
896
- var DOUBLE_QUOTES = /[\u201c\u201d\u201e\u201f\u2033]/gu;
897
- var DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
898
- var TERMINAL_PUNCTUATION = /[.!?\u2026;:\u3002\uff01\uff1f\uff1b\uff1a]$/u;
899
- var CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
900
- var WORD_CHAR = /[\p{L}\p{N}_]/u;
901
- var COMBINING_MARK = new RegExp("\\p{M}", "u");
902
- var TAG_TOKEN = /^<\/?[A-Za-z_][\w.:-]*(?:\s+[^<>]*?)?\s*\/?>/u;
903
- var PLACEHOLDER_TOKEN = /^(?:\$\{[^{}]*\}|%[0-9]+\$[sdifuxX]|%[-+ 0#]*[0-9]*(?:\.[0-9]+)?[sdifuxXc]|\{[^{}]*\})/u;
904
- var URL_TOKEN = /^(?:(?:https?|ftp):\/\/|www\.)[^\s<>"'{}]+/u;
905
- var EMAIL_TOKEN = /^[\w.+-]+@[\w-]+(?:\.[\w-]+)+/u;
906
- var PATH_TOKEN = /^(?:(?:[A-Za-z]:[\\/]|\\\\|~[\\/])[^\s<>"'{}]+|\/(?:[^\s<>"'{}]*\/)[^\s<>"'{}]*)/u;
907
- var NUMBER_TOKEN = new RegExp("^\\p{Nd}+(?:[.,]\\p{Nd}+)?", "u");
908
- var SIMILARITY_TRUNCATE = 200;
909
- var TRUNCATION_PENALTY = 7;
910
- var DEFAULT_THRESHOLD = 50;
911
- var DEFAULT_MAX_MATCHES = 3;
912
- function normalizeText(text) {
913
- return text.normalize("NFKC").normalize("NFC").replace(ZERO_WIDTH, "").replace(QUOTES, "'").replace(DOUBLE_QUOTES, '"').replace(DASHES, "-").replace(/\u2026/gu, "...").replace(/\s+/gu, " ").trim().toLowerCase();
914
- }
915
- function matchAtomic(rest) {
916
- const tag = TAG_TOKEN.exec(rest);
917
- if (tag) return { kind: "tag", raw: tag[0], skeleton: "<TAG>" };
918
- const placeholder = PLACEHOLDER_TOKEN.exec(rest);
919
- if (placeholder) return { kind: "placeholder", raw: placeholder[0], skeleton: "<PH>" };
920
- const url = URL_TOKEN.exec(rest);
921
- if (url) return { kind: "atomic", raw: url[0], skeleton: url[0] };
922
- const email = EMAIL_TOKEN.exec(rest);
923
- if (email) return { kind: "atomic", raw: email[0], skeleton: email[0] };
924
- const path = PATH_TOKEN.exec(rest);
925
- if (path) return { kind: "atomic", raw: path[0], skeleton: path[0] };
926
- return void 0;
927
- }
928
- function tokenize(text) {
929
- const norm = normalizeText(text);
930
- const tokens = [];
931
- let index = 0;
932
- while (index < norm.length) {
933
- const rest = norm.slice(index);
934
- const atomic = matchAtomic(rest);
935
- if (atomic) {
936
- tokens.push(atomic);
937
- index += atomic.raw.length;
938
- continue;
939
- }
940
- const char = norm[index];
941
- if (CJK_CHAR.test(char)) {
942
- tokens.push({ kind: "cjk", raw: char, skeleton: char });
943
- index += 1;
944
- continue;
945
- }
946
- const number2 = NUMBER_TOKEN.exec(rest);
947
- if (number2) {
948
- tokens.push({ kind: "number", raw: number2[0], skeleton: "<NUM>" });
949
- index += number2[0].length;
950
- continue;
951
- }
952
- if (WORD_CHAR.test(char)) {
953
- let end = index + 1;
954
- while (end < norm.length) {
955
- const next = norm[end];
956
- if (CJK_CHAR.test(next)) break;
957
- if (WORD_CHAR.test(next) || COMBINING_MARK.test(next)) {
958
- end += 1;
959
- continue;
960
- }
961
- break;
962
- }
963
- const raw = norm.slice(index, end);
964
- tokens.push({ kind: "word", raw, skeleton: raw });
965
- index = end;
966
- continue;
967
- }
968
- index += 1;
969
- }
970
- return {
971
- tokens,
972
- skeleton: tokens.map((token) => token.skeleton),
973
- numbers: tokens.filter((token) => token.kind === "number").map((token) => token.raw),
974
- atoms: tokens.filter((token) => token.kind === "tag" || token.kind === "placeholder").map((token) => `${token.kind}:${token.raw}`),
975
- norm,
976
- terminalPunct: TERMINAL_PUNCTUATION.test(norm)
977
- };
978
- }
979
- function arraysEqual(a, b) {
980
- if (a.length !== b.length) return false;
981
- return a.every((value, index) => value === b[index]);
982
- }
983
- function tokensEqual(a, b) {
984
- if (a.length !== b.length) return false;
985
- return a.every(
986
- (token, index) => token.kind === b[index].kind && token.raw === b[index].raw
987
- );
988
- }
989
- function firstDifference(a, b) {
990
- for (let index = 0; index < Math.min(a.length, b.length); index += 1) {
991
- if (a[index] !== b[index]) return `${a[index]} vs ${b[index]}`;
992
- }
993
- return `${a.length} vs ${b.length}`;
994
- }
995
- function compareStructure(source, entry, tmEntry) {
996
- const skeletonEqual = arraysEqual(source.skeleton, entry.skeleton);
997
- const numbersEqual = arraysEqual(source.numbers, entry.numbers);
998
- const atomsEqual = arraysEqual(source.atoms, entry.atoms);
999
- const numberDiff = !numbersEqual;
1000
- const tagDiff = !atomsEqual;
1001
- const punctDiff = source.terminalPunct !== entry.terminalPunct || tokensEqual(source.tokens, entry.tokens) && source.norm !== entry.norm;
1002
- const penalties = [];
1003
- if (numberDiff) {
1004
- penalties.push({
1005
- kind: "number",
1006
- points: 2,
1007
- detail: `${source.numbers.join(", ") || "none"} vs ${entry.numbers.join(", ") || "none"}`
1008
- });
1009
- }
1010
- if (tagDiff) {
1011
- if (source.atoms.length !== entry.atoms.length) {
1012
- penalties.push({
1013
- kind: "tag_count",
1014
- points: 10,
1015
- detail: `${source.atoms.length} vs ${entry.atoms.length} tags/placeholders`
1016
- });
1017
- } else {
1018
- penalties.push({
1019
- kind: "tag_id",
1020
- points: 3,
1021
- detail: firstDifference(source.atoms, entry.atoms)
1022
- });
1023
- }
1024
- }
1025
- if (punctDiff) {
1026
- penalties.push({
1027
- kind: "punctuation",
1028
- points: 1,
1029
- detail: "difference is limited to punctuation/whitespace"
1030
- });
1031
- }
1032
- if (tmEntry.fuzzy === true) {
1033
- penalties.push({ kind: "fuzzy", points: 5, detail: "TM entry is flagged as fuzzy" });
1034
- }
1035
- if (tmEntry.origin === "alignment") {
1036
- penalties.push({
1037
- kind: "unaligned",
1038
- points: 2,
1039
- detail: "alignment entry (default unaligned penalty)"
1040
- });
1041
- }
1042
- return { skeletonEqual, numbersEqual, atomsEqual, numberDiff, tagDiff, punctDiff, penalties };
1043
- }
1044
- function sumPenalties(penalties) {
1045
- return penalties.reduce((total, penalty) => total + penalty.points, 0);
1046
- }
1047
- function osaDistance(a, b) {
1048
- const rows = a.length + 1;
1049
- const cols = b.length + 1;
1050
- const matrix = Array.from(
1051
- { length: rows },
1052
- (_, row) => Array.from({ length: cols }, (_2, column) => row === 0 ? column : column === 0 ? row : 0)
1053
- );
1054
- for (let i = 1; i < rows; i += 1) {
1055
- for (let j = 1; j < cols; j += 1) {
1056
- const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1057
- let value = Math.min(
1058
- matrix[i - 1][j] + 1,
1059
- matrix[i][j - 1] + 1,
1060
- matrix[i - 1][j - 1] + cost
1061
- );
1062
- if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
1063
- value = Math.min(value, matrix[i - 2][j - 2] + 1);
1064
- }
1065
- matrix[i][j] = value;
1066
- }
1067
- }
1068
- return matrix[rows - 1][cols - 1];
1069
- }
1070
- function levenshteinDistance(a, b) {
1204
+ function levenshtein(a, b) {
1071
1205
  if (a === b) return 0;
1072
1206
  if (a.length === 0) return b.length;
1073
1207
  if (b.length === 0) return a.length;
@@ -1083,208 +1217,14 @@ function levenshteinDistance(a, b) {
1083
1217
  }
1084
1218
  return previous[b.length];
1085
1219
  }
1086
- function round1(value) {
1087
- return Math.round(value * 10) / 10;
1088
- }
1089
- function similarityOf(source, entry) {
1090
- if (arraysEqual(source.skeleton, entry.skeleton)) return { similarity: 100, truncated: false };
1091
- if (source.skeleton.length >= 2 && entry.skeleton.length >= 2) {
1092
- const distance2 = osaDistance(source.skeleton, entry.skeleton);
1093
- const denominator2 = Math.max(source.skeleton.length, entry.skeleton.length, 3);
1094
- return { similarity: round1(100 * (1 - distance2 / denominator2)), truncated: false };
1095
- }
1096
- const truncated = source.norm.length > SIMILARITY_TRUNCATE || entry.norm.length > SIMILARITY_TRUNCATE;
1097
- const a = source.norm.slice(0, SIMILARITY_TRUNCATE);
1098
- const b = entry.norm.slice(0, SIMILARITY_TRUNCATE);
1099
- const denominator = Math.max(a.length, b.length, 3);
1100
- const distance = levenshteinDistance(a, b);
1101
- return { similarity: round1(100 * (1 - distance / denominator)), truncated };
1102
- }
1103
- function matchTypeFor(rate) {
1104
- if (rate >= 95) return "near_exact";
1105
- if (rate >= 85) return "high_fuzzy";
1106
- if (rate >= 75) return "medium_fuzzy";
1107
- return "low_fuzzy";
1108
- }
1109
- function compareContext(provided, stored) {
1110
- if (!provided || !stored) return { matched: false, both: false };
1111
- const previousMatches = provided.previousSource !== void 0 && stored.previousSource !== void 0 && normalizeText(provided.previousSource) === normalizeText(stored.previousSource);
1112
- const nextMatches = provided.nextSource !== void 0 && stored.nextSource !== void 0 && normalizeText(provided.nextSource) === normalizeText(stored.nextSource);
1113
- return { matched: previousMatches || nextMatches, both: previousMatches && nextMatches };
1114
- }
1115
- function compareLastModified(a, b) {
1116
- const left = a.lastModified ?? "";
1117
- const right = b.lastModified ?? "";
1118
- if (left === right) return 0;
1119
- if (left === "") return 1;
1120
- if (right === "") return -1;
1121
- return left < right ? 1 : -1;
1220
+ function similarity(a, b) {
1221
+ const longest = Math.max(a.length, b.length);
1222
+ if (longest === 0) return 1;
1223
+ return 1 - levenshtein(a, b) / longest;
1122
1224
  }
1123
1225
  function languageAllowed(entryLanguage, wanted) {
1124
1226
  if (wanted === void 0) return true;
1125
- if (entryLanguage === "") return true;
1126
- return languageMatches(entryLanguage, wanted);
1127
- }
1128
- function matchTm(entries, sourceText, options = {}) {
1129
- const threshold = options.threshold ?? DEFAULT_THRESHOLD;
1130
- const maxMatches = options.maxMatches ?? DEFAULT_MAX_MATCHES;
1131
- if (maxMatches <= 0) return [];
1132
- const source = tokenize(sourceText);
1133
- const scored = [];
1134
- entries.forEach((entry, index) => {
1135
- if (!languageAllowed(entry.sourceLanguage, options.sourceLanguage)) return;
1136
- if (!languageAllowed(entry.targetLanguage, options.targetLanguage)) return;
1137
- const target = tokenize(entry.source);
1138
- const structure = compareStructure(source, target, entry);
1139
- const context = compareContext(options.context, entry.context);
1140
- const truncation = structure.skeletonEqual ? { similarity: 100, truncated: false } : similarityOf(source, target);
1141
- let matchRate;
1142
- let matchType;
1143
- if (structure.skeletonEqual && structure.numbersEqual && structure.atomsEqual && !structure.punctDiff) {
1144
- structure.penalties.length = 0;
1145
- if (context.both) {
1146
- matchRate = 102;
1147
- matchType = "double_context";
1148
- } else if (context.matched) {
1149
- matchRate = 101;
1150
- matchType = "context";
1151
- } else {
1152
- matchRate = 100;
1153
- matchType = "exact";
1154
- }
1155
- } else if (structure.skeletonEqual) {
1156
- const penalties = sumPenalties(structure.penalties);
1157
- const candidate = Math.min(99, Math.round(100 - penalties));
1158
- if (candidate < 50) return;
1159
- matchRate = candidate;
1160
- matchType = matchTypeFor(candidate);
1161
- } else {
1162
- const truncationPenalty = truncation.truncated ? TRUNCATION_PENALTY : 0;
1163
- const adjusted = truncation.similarity - sumPenalties(structure.penalties) - truncationPenalty;
1164
- if (adjusted < 50) return;
1165
- const candidate = Math.min(99, Math.round(adjusted));
1166
- if (candidate < 50) return;
1167
- matchRate = candidate;
1168
- matchType = matchTypeFor(candidate);
1169
- }
1170
- if (matchRate < threshold) return;
1171
- scored.push({
1172
- index,
1173
- match: {
1174
- entry,
1175
- similarity: truncation.similarity,
1176
- matchRate,
1177
- matchType,
1178
- penalties: structure.penalties,
1179
- contextMatched: context.matched,
1180
- flags: {
1181
- numberDiff: structure.numberDiff,
1182
- tagDiff: structure.tagDiff,
1183
- punctDiff: structure.punctDiff,
1184
- truncated: truncation.truncated
1185
- }
1186
- }
1187
- });
1188
- });
1189
- scored.sort((left, right) => {
1190
- const a = left.match;
1191
- const b = right.match;
1192
- if (b.matchRate !== a.matchRate) return b.matchRate - a.matchRate;
1193
- if (b.similarity !== a.similarity) return b.similarity - a.similarity;
1194
- const modified = compareLastModified(a.entry, b.entry);
1195
- if (modified !== 0) return modified;
1196
- if (a.entry.id !== b.entry.id) return a.entry.id < b.entry.id ? -1 : 1;
1197
- return left.index - right.index;
1198
- });
1199
- return scored.slice(0, maxMatches).map((item) => item.match);
1200
- }
1201
-
1202
- // packages/l10n-core/src/assets/term-matcher.ts
1203
- var CJK_CHAR2 = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
1204
- var WORD_CHAR2 = /[\p{L}\p{N}_]/u;
1205
- var COMBINING_MARK2 = new RegExp("\\p{M}", "u");
1206
- var MAX_TERM_TOKENS = 8;
1207
- var FUZZY_THRESHOLD = 0.8;
1208
- function readToken(text, index) {
1209
- const codePoint = text.codePointAt(index);
1210
- if (codePoint === void 0) return void 0;
1211
- const char = String.fromCodePoint(codePoint);
1212
- const end = index + char.length;
1213
- if (CJK_CHAR2.test(char)) return { value: char, start: index, end, kind: "cjk" };
1214
- return { value: char, start: index, end, kind: "word" };
1215
- }
1216
- function tokenizeText(text) {
1217
- const tokens = [];
1218
- let index = 0;
1219
- while (index < text.length) {
1220
- const first = readToken(text, index);
1221
- if (!first) break;
1222
- if (first.kind === "cjk") {
1223
- tokens.push(first);
1224
- index = first.end;
1225
- continue;
1226
- }
1227
- if (!WORD_CHAR2.test(first.value)) {
1228
- index = first.end;
1229
- continue;
1230
- }
1231
- let end = first.end;
1232
- while (end < text.length) {
1233
- const next = readToken(text, end);
1234
- if (!next || next.kind === "cjk") break;
1235
- if (!WORD_CHAR2.test(next.value) && !COMBINING_MARK2.test(next.value)) break;
1236
- end = next.end;
1237
- }
1238
- tokens.push({ value: text.slice(index, end), start: index, end, kind: "word" });
1239
- index = end;
1240
- }
1241
- return tokens;
1242
- }
1243
- function codePointLength(value) {
1244
- return [...value].length;
1245
- }
1246
- function caseModeOf(entry) {
1247
- if (entry.caseSensitive === true) return "yes";
1248
- if (entry.caseSensitive === false) return "no";
1249
- return "permissive";
1250
- }
1251
- function caseMatches(term, text, mode) {
1252
- if (term.length !== text.length) return false;
1253
- if (mode === "yes") return term === text;
1254
- if (mode === "no") return term.toLowerCase() === text.toLowerCase();
1255
- for (let index = 0; index < term.length; index += 1) {
1256
- const termChar = term[index];
1257
- const textChar = text[index];
1258
- if (termChar === textChar) continue;
1259
- if (termChar.toLowerCase() !== textChar.toLowerCase()) return false;
1260
- if (termChar !== termChar.toLowerCase()) return false;
1261
- }
1262
- return true;
1263
- }
1264
- function levenshtein(a, b) {
1265
- if (a === b) return 0;
1266
- if (a.length === 0) return b.length;
1267
- if (b.length === 0) return a.length;
1268
- let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
1269
- for (let i = 1; i <= a.length; i += 1) {
1270
- const current = new Array(b.length + 1).fill(0);
1271
- current[0] = i;
1272
- for (let j = 1; j <= b.length; j += 1) {
1273
- const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1274
- current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
1275
- }
1276
- previous = current;
1277
- }
1278
- return previous[b.length];
1279
- }
1280
- function similarity(a, b) {
1281
- const longest = Math.max(a.length, b.length);
1282
- if (longest === 0) return 1;
1283
- return 1 - levenshtein(a, b) / longest;
1284
- }
1285
- function languageAllowed2(entryLanguage, wanted) {
1286
- if (wanted === void 0) return true;
1287
- if (entryLanguage === void 0 || entryLanguage === "") return true;
1227
+ if (entryLanguage === void 0 || entryLanguage === "") return true;
1288
1228
  return languageMatches(entryLanguage, wanted);
1289
1229
  }
1290
1230
  function scanExactOrPrefix(tokens, termTokens, caseMode, options) {
@@ -1364,8 +1304,8 @@ function matchTerms(text, entries, options = {}) {
1364
1304
  warnings.push(message);
1365
1305
  };
1366
1306
  for (const entry of entries) {
1367
- if (!languageAllowed2(entry.sourceLanguage, options.sourceLanguage)) continue;
1368
- if (!languageAllowed2(entry.targetLanguage, options.targetLanguage)) continue;
1307
+ if (!languageAllowed(entry.sourceLanguage, options.sourceLanguage)) continue;
1308
+ if (!languageAllowed(entry.targetLanguage, options.targetLanguage)) continue;
1369
1309
  const caseMode = caseModeOf(entry);
1370
1310
  const mode = entry.matching ?? "prefix50";
1371
1311
  const forms = /* @__PURE__ */ new Set();
@@ -1445,7 +1385,7 @@ function matchTerms(text, entries, options = {}) {
1445
1385
  return { hits, issues: [], warnings };
1446
1386
  }
1447
1387
 
1448
- // packages/l10n-core/src/qa/rules.ts
1388
+ // packages/l10n-core/src/qa/catalog.ts
1449
1389
  var RULES = [
1450
1390
  // structural class
1451
1391
  { id: "EMPTY_TARGET", severity: "critical" },
@@ -1460,8 +1400,11 @@ var RULES = [
1460
1400
  // content class
1461
1401
  { id: "NUMBER_MISMATCH", severity: "major" },
1462
1402
  { id: "DATE_MISMATCH", severity: "major" },
1403
+ { id: "DATE_FORMAT", severity: "minor", requiresLocale: true },
1463
1404
  { id: "DUPLICATE_WORD", severity: "major" },
1464
1405
  { id: "TERM_MISSING", severity: "major", requiresTerms: true },
1406
+ { id: "TERMINOLOGY_INCONSISTENT", severity: "major", requiresTerms: true, scope: "document" },
1407
+ { id: "SOURCE_TARGET_INCONSISTENT", severity: "major", scope: "document" },
1465
1408
  { id: "SOURCE_LEFTOVER", severity: "major" },
1466
1409
  { id: "PUNCT_DUP", severity: "major" },
1467
1410
  { id: "TRAILING_COLON", severity: "minor" },
@@ -1471,9 +1414,24 @@ var RULES = [
1471
1414
  { id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
1472
1415
  { id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
1473
1416
  ];
1417
+ var QA_RULE_CATALOG = Object.freeze(RULES.map((rule, index) => Object.freeze({
1418
+ ruleId: rule.id,
1419
+ code: `QA-${String(index + 1).padStart(3, "0")}`,
1420
+ severity: rule.severity,
1421
+ defaultEnabled: rule.defaultOff !== true,
1422
+ scope: rule.scope ?? "segment",
1423
+ requiresTerms: rule.requiresTerms === true,
1424
+ requiresLocale: rule.requiresLocale === true
1425
+ })));
1426
+
1427
+ // packages/l10n-core/src/qa/rules.ts
1474
1428
  var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
1475
1429
  var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
1476
1430
  var TERM_RULE_IDS = RULES.filter((rule) => rule.requiresTerms).map((rule) => rule.id);
1431
+ var LOCALE_RULE_IDS = RULES.filter((rule) => rule.requiresLocale).map((rule) => rule.id);
1432
+ var DOCUMENT_RULE_IDS = new Set(
1433
+ RULES.filter((rule) => rule.scope === "document").map((rule) => rule.id)
1434
+ );
1477
1435
  var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
1478
1436
  function normalizeForCompare(value) {
1479
1437
  return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
@@ -1587,15 +1545,16 @@ function checkTags(context) {
1587
1545
  const sourceTags = context.segment.sourceTags;
1588
1546
  const targetTags = context.segment.targetTags;
1589
1547
  if (sourceTags.length === 0 && targetTags.length === 0) return;
1590
- const sourceSignature = sourceTags.join("|");
1591
- const targetSignature = targetTags.join("|");
1592
- if (sourceSignature === targetSignature) return;
1593
- const sameMultiset = sourceTags.length === targetTags.length && JSON.stringify([...sourceTags].sort()) === JSON.stringify([...targetTags].sort());
1548
+ const source = context.segment.sourceNode ? inlineCodeSignatures(context.segment.sourceNode) : sourceTags;
1549
+ const target = context.segment.targetNode ? inlineCodeSignatures(context.segment.targetNode) : targetTags;
1550
+ const sameInventory = JSON.stringify([...source].sort()) === JSON.stringify([...target].sort());
1551
+ const orderError = sameInventory && context.segment.sourceNode && context.segment.targetNode ? inlineCodeOrderError(context.segment.sourceNode, context.segment.targetNode) : void 0;
1552
+ if (sameInventory && !orderError) return;
1594
1553
  draft(context, {
1595
1554
  ruleId: "TAG_MISMATCH",
1596
- message: sameMultiset ? "inline tag \u987A\u5E8F\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4" : `inline tag \u4E0D\u4E00\u81F4: \u6E90\u6587 [${sourceTags.join(", ")}] / \u8BD1\u6587 [${targetTags.join(", ")}]`,
1597
- evidence: `source tags: [${sourceTags.join(", ")}], target tags: [${targetTags.join(", ")}]`,
1598
- suggestion: "\u4FDD\u6301 inline tag \u6570\u91CF\u4E0E\u987A\u5E8F\u548C\u6E90\u6587\u4E00\u81F4"
1555
+ message: orderError ? "\u6807\u7B7E\u7684\u914D\u5BF9\u6216\u987A\u5E8F\u4E0E\u6E90\u6587\u7EA6\u675F\u4E0D\u4E00\u81F4" : "\u6807\u7B7E\u7684\u6570\u91CF\u3001\u6807\u8BC6\u6216\u539F\u751F\u4EE3\u7801\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4",
1556
+ evidence: JSON.stringify({ source, target, ...orderError ? { orderError } : {} }),
1557
+ suggestion: "\u6838\u5BF9\u6807\u7B7E\u7684 ID\u3001\u914D\u5BF9\u5F15\u7528\u548C\u539F\u751F\u4EE3\u7801\uFF0C\u4FDD\u7559\u6E90\u6587\u4E2D\u7684\u6807\u7B7E"
1599
1558
  });
1600
1559
  }
1601
1560
  var URL_PATTERNS = [
@@ -1661,7 +1620,7 @@ var MONTHS = new Map(
1661
1620
  );
1662
1621
  var MONTH_ALIASES = new Map([
1663
1622
  ...MONTHS,
1664
- ...Array.from(MONTHS, ([month, number2]) => [month.slice(0, 3), number2]),
1623
+ ...Array.from(MONTHS, ([month, number3]) => [month.slice(0, 3), number3]),
1665
1624
  ["sept", 9]
1666
1625
  ]);
1667
1626
  var CJK_DIGITS = /* @__PURE__ */ new Map([
@@ -1686,9 +1645,10 @@ function validMonthDay(month, day) {
1686
1645
  var DATE_PATTERNS = [
1687
1646
  /\b(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})\b/gu,
1688
1647
  /\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b/gu,
1689
- /(\d{4}|[〇零一二三四五六七八九]{4})年(\d{1,2})月(?:(\d{1,2})日)?/gu
1648
+ /(\d{4}|[〇零一二三四五六七八九]{4})\s*年\s*(\d{1,2})\s*月(?:\s*(\d{1,2})\s*日)?/gu
1690
1649
  ];
1691
1650
  function extractDates(value) {
1651
+ value = normalizeNumericText(value);
1692
1652
  const tokens = [];
1693
1653
  const push = (key, text, start) => {
1694
1654
  tokens.push({ key, text, span: { start, end: start + text.length } });
@@ -1786,7 +1746,7 @@ function extractDates(value) {
1786
1746
  }
1787
1747
  function maskSpans(value, spans) {
1788
1748
  if (spans.length === 0) return value;
1789
- const chars = Array.from(value);
1749
+ const chars = value.split("");
1790
1750
  for (const span of spans) {
1791
1751
  for (let index = span.start; index < span.end && index < chars.length; index += 1) chars[index] = " ";
1792
1752
  }
@@ -1806,10 +1766,14 @@ function normalizeNumber(raw) {
1806
1766
  if (/^\d+\.0+$/u.test(value)) value = value.replace(/\.0+$/u, "");
1807
1767
  return `${sign}${value}`;
1808
1768
  }
1769
+ function normalizeNumericText(value) {
1770
+ return value.replace(/[0-9.,+-]/gu, (character) => character.normalize("NFKC"));
1771
+ }
1809
1772
  function extractNumbers(value) {
1773
+ const normalized = normalizeNumericText(value);
1810
1774
  const masked = maskSpans(
1811
- value,
1812
- extractDates(value).map((token) => token.span)
1775
+ normalized,
1776
+ [...extractDates(normalized).map((token) => token.span), ...collectPlaceholders(normalized)]
1813
1777
  );
1814
1778
  const results = [];
1815
1779
  for (const match of masked.matchAll(NUMBER_RE)) results.push(normalizeNumber(match[0]));
@@ -1848,6 +1812,47 @@ function checkDates(context) {
1848
1812
  ...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
1849
1813
  });
1850
1814
  }
1815
+ function localeDateOrder(locale) {
1816
+ const normalized = locale.trim().toLowerCase().replace(/_/gu, "-");
1817
+ if (/^(?:zh|ja|ko)(?:-|$)/u.test(normalized)) return "ymd";
1818
+ if (/^en-us(?:-|$)/u.test(normalized)) return "mdy";
1819
+ if (/^(?:en-gb|en-au|en-nz|en-ie|de|fr|es|it|pt|nl|ru|pl|tr|sv|da|fi|nb|cs|hu|ro|bg|hr|sk|sl|uk|el)(?:-|$)/u.test(
1820
+ normalized
1821
+ )) {
1822
+ return "dmy";
1823
+ }
1824
+ return void 0;
1825
+ }
1826
+ function numericDateOrder(text) {
1827
+ if (/^(?:\d{4}|[〇零一二三四五六七八九]{4})年/u.test(text)) return "ymd";
1828
+ if (/^\d{4}[./-]\d{1,2}[./-]\d{1,2}$/u.test(text)) return "ymd";
1829
+ const match = /^(\d{1,2})[./-](\d{1,2})[./-]\d{2,4}$/u.exec(text);
1830
+ if (!match) return void 0;
1831
+ const first = Number(match[1]);
1832
+ const second = Number(match[2]);
1833
+ if (first > 12 && second <= 12) return "dmy";
1834
+ if (second > 12 && first <= 12) return "mdy";
1835
+ return void 0;
1836
+ }
1837
+ function checkDateFormat(context) {
1838
+ const targetLocale = context.locale?.targetLocale;
1839
+ if (targetLocale === void 0) return;
1840
+ const expected = localeDateOrder(targetLocale);
1841
+ if (expected === void 0) return;
1842
+ const dates = extractDates(context.target);
1843
+ if (dates.length === 0) return;
1844
+ for (const token of dates) {
1845
+ const actual = numericDateOrder(token.text);
1846
+ if (actual === void 0 || actual === expected) continue;
1847
+ draft(context, {
1848
+ ruleId: "DATE_FORMAT",
1849
+ message: `\u65E5\u671F\u683C\u5F0F\u4E0E\u76EE\u6807\u8BED\u8A00\u4E60\u60EF\u4E0D\u4E00\u81F4: '${token.text}' \u4E3A ${actual.toUpperCase()}\uFF0C${targetLocale} \u671F\u671B ${expected.toUpperCase()}`,
1850
+ evidence: `target date: "${token.text}"`,
1851
+ suggestion: expected === "ymd" ? "\u6539\u4E3A \u5E74-\u6708-\u65E5 \u987A\u5E8F" : expected === "dmy" ? "\u6539\u4E3A \u65E5/\u6708/\u5E74 \u987A\u5E8F" : "\u6539\u4E3A \u6708/\u65E5/\u5E74 \u987A\u5E8F",
1852
+ targetRange: { start: token.span.start, end: token.span.end }
1853
+ });
1854
+ }
1855
+ }
1851
1856
  var BRACKET_PAIRS = /* @__PURE__ */ new Map([
1852
1857
  ["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
1853
1858
  ["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
@@ -2333,6 +2338,91 @@ function checkTermRules(context) {
2333
2338
  });
2334
2339
  }
2335
2340
  }
2341
+ function sourceKey(value) {
2342
+ return value.normalize("NFKC").replace(/\s+/gu, " ").trim();
2343
+ }
2344
+ function sourceTargetInconsistencies(contexts) {
2345
+ const groups = /* @__PURE__ */ new Map();
2346
+ for (const context of contexts) {
2347
+ if (context.source.trim().length === 0 || context.target.trim().length === 0) continue;
2348
+ const key = sourceKey(context.source);
2349
+ const group = groups.get(key);
2350
+ if (group === void 0) groups.set(key, [context]);
2351
+ else group.push(context);
2352
+ }
2353
+ const drafts = [];
2354
+ let checkedSegments = 0;
2355
+ let emitted = 0;
2356
+ for (const group of groups.values()) {
2357
+ if (group.length < 2) continue;
2358
+ checkedSegments += group.length;
2359
+ const targets = group.map((context) => normalizeForCompare(context.target));
2360
+ const variants = Array.from(new Set(targets));
2361
+ if (variants.length < 2) continue;
2362
+ const reference = variants[0];
2363
+ for (let index = 1; index < group.length && emitted < 50; index += 1) {
2364
+ if (targets[index] === reference) continue;
2365
+ const context = group[index];
2366
+ const segment = context?.segment;
2367
+ if (context === void 0 || segment === void 0) continue;
2368
+ drafts.push({
2369
+ segment,
2370
+ draft: {
2371
+ ruleId: "SOURCE_TARGET_INCONSISTENT",
2372
+ message: `\u540C\u4E00\u6E90\u6587\u5B58\u5728\u4E0D\u540C\u8BD1\u6CD5: '${truncate(context.source)}'`,
2373
+ evidence: `variants: [${variants.slice(0, 3).map((value) => truncate(value, 40)).join(" | ")}]`,
2374
+ suggestion: "\u7EDF\u4E00\u8BE5\u53E5\u6BB5\u7684\u8BD1\u6CD5"
2375
+ }
2376
+ });
2377
+ emitted += 1;
2378
+ }
2379
+ }
2380
+ return { drafts, checkedSegments };
2381
+ }
2382
+ function terminologyInconsistencies(contexts) {
2383
+ const perTerm = /* @__PURE__ */ new Map();
2384
+ let checkedSegments = 0;
2385
+ for (const context of contexts) {
2386
+ if (context.termHits === void 0 || context.target.trim().length === 0) continue;
2387
+ const sourceHits = longestHits(context.termHits.source);
2388
+ if (sourceHits.length === 0) continue;
2389
+ checkedSegments += 1;
2390
+ for (const hit of sourceHits) {
2391
+ const entry = hit.entry;
2392
+ if (entry.forbidden === true || !isApproved(entry) || (entry.target ?? "").trim().length === 0) continue;
2393
+ const bucket = perTerm.get(entry.id) ?? { entry, items: [] };
2394
+ if (!bucket.items.some((item) => item.context === context)) {
2395
+ bucket.items.push({
2396
+ context,
2397
+ mentioned: targetMentionsTerm(entry, context.target, context.termHits.target)
2398
+ });
2399
+ }
2400
+ perTerm.set(entry.id, bucket);
2401
+ }
2402
+ }
2403
+ const drafts = [];
2404
+ let emitted = 0;
2405
+ for (const { entry, items } of perTerm.values()) {
2406
+ const mentioned = items.filter((item) => item.mentioned);
2407
+ const missing = items.filter((item) => !item.mentioned);
2408
+ if (mentioned.length === 0 || missing.length === 0) continue;
2409
+ for (const item of missing) {
2410
+ if (emitted >= 25) break;
2411
+ drafts.push({
2412
+ segment: item.context.segment,
2413
+ draft: {
2414
+ ruleId: "TERMINOLOGY_INCONSISTENT",
2415
+ message: `\u672F\u8BED '${entry.source}' \u2192 '${entry.target}' \u5728\u90E8\u5206\u53E5\u6BB5\u5DF2\u4F7F\u7528\u3001\u90E8\u5206\u672A\u4F7F\u7528\uFF08\u4E0D\u4E00\u81F4\uFF09`,
2416
+ evidence: `used in ${mentioned.length} segment(s), missing in ${missing.length}`,
2417
+ suggestion: entry.target,
2418
+ termId: entry.id
2419
+ }
2420
+ });
2421
+ emitted += 1;
2422
+ }
2423
+ }
2424
+ return { drafts, checkedSegments };
2425
+ }
2336
2426
  var CHECK_GROUPS = [
2337
2427
  { ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
2338
2428
  { ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
@@ -2343,6 +2433,7 @@ var CHECK_GROUPS = [
2343
2433
  { ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
2344
2434
  { ids: ["NUMBER_MISMATCH"], run: checkNumbers },
2345
2435
  { ids: ["DATE_MISMATCH"], run: checkDates },
2436
+ { ids: ["DATE_FORMAT"], run: checkDateFormat },
2346
2437
  { ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
2347
2438
  { ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
2348
2439
  { ids: ["PUNCT_DUP"], run: checkPunctDup },
@@ -2357,10 +2448,13 @@ function checkEmptyTargetGroup(context) {
2357
2448
  if (source.trim().length === 0) return;
2358
2449
  const targetMissing = context.segment.targetRange === null && context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2359
2450
  const empty = context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2360
- if (!targetMissing && !empty) return;
2451
+ const targetPlaceholders = collectPlaceholders(context.target);
2452
+ const hasVisibleBody = /[\p{L}\p{N}]/u.test(maskSpans(source, collectPlaceholders(source)));
2453
+ const missingBody = hasVisibleBody && (context.target.trim() === "" || targetPlaceholders.length > 0 && !/[\p{L}\p{N}]/u.test(maskSpans(context.target, targetPlaceholders)));
2454
+ if (!targetMissing && !empty && !missingBody) return;
2361
2455
  draft(context, {
2362
2456
  ruleId: "EMPTY_TARGET",
2363
- message: targetMissing ? "\u76EE\u6807\u7F3A\u5931\uFF08target \u5143\u7D20\u4E0D\u5B58\u5728\uFF09" : "\u8BD1\u6587\u4E3A\u7A7A\uFF08\u6E90\u6587\u975E\u7A7A\uFF09",
2457
+ message: targetMissing ? "\u76EE\u6807\u7F3A\u5931\uFF08target \u5143\u7D20\u4E0D\u5B58\u5728\uFF09" : empty ? "\u8BD1\u6587\u4E3A\u7A7A\uFF08\u6E90\u6587\u975E\u7A7A\uFF09" : "\u8BD1\u6587\u7F3A\u5C11\u6B63\u6587",
2364
2458
  evidence: `source: ${truncate(source)}`
2365
2459
  });
2366
2460
  }
@@ -2371,6 +2465,7 @@ function checkUntranslatedCopyGroup(context) {
2371
2465
  if (normalizedSource !== normalizeForCompare(target)) return;
2372
2466
  const policy = context.untranslatedCopy;
2373
2467
  if (policy?.whitelist?.some((entry) => normalizeForCompare(entry) === normalizedSource)) return;
2468
+ if (policy?.ignoreIdentifiers && /^(?=.*[0-9])[a-z0-9][a-z0-9._/+:%-]*$/u.test(normalizedSource)) return;
2374
2469
  const tokens = wordTokens(source);
2375
2470
  const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
2376
2471
  const maxShort = policy?.shortSegmentMaxLength ?? 4;
@@ -2405,6 +2500,7 @@ function isApplicable(ruleId, segment) {
2405
2500
  case "UNPAIRED_BRACKET":
2406
2501
  case "NUMBER_MISMATCH":
2407
2502
  case "DATE_MISMATCH":
2503
+ case "DATE_FORMAT":
2408
2504
  case "DUPLICATE_WORD":
2409
2505
  case "TERM_MISSING":
2410
2506
  case "SOURCE_LEFTOVER":
@@ -2431,6 +2527,7 @@ function resolveEnabledRules(options) {
2431
2527
  return enabled2;
2432
2528
  }
2433
2529
  const enabled = new Set(DEFAULT_ENABLED);
2530
+ if (options.locale?.targetLocale === void 0) for (const ruleId of LOCALE_RULE_IDS) enabled.delete(ruleId);
2434
2531
  if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
2435
2532
  return enabled;
2436
2533
  }
@@ -2492,6 +2589,14 @@ function runDeterministicChecks(segments, options = {}) {
2492
2589
  const state = /* @__PURE__ */ new Map();
2493
2590
  for (const rule of RULES) {
2494
2591
  if (!enabled.has(rule.id)) continue;
2592
+ if (rule.requiresLocale === true && options.locale?.targetLocale === void 0) {
2593
+ state.set(rule.id, {
2594
+ status: "skipped",
2595
+ reason: "no locale.targetLocale provided; locale rule was not executed",
2596
+ checkedSegments: 0
2597
+ });
2598
+ continue;
2599
+ }
2495
2600
  if (rule.requiresTerms === true && !termAvailable) {
2496
2601
  state.set(rule.id, {
2497
2602
  status: "skipped",
@@ -2508,6 +2613,7 @@ function runDeterministicChecks(segments, options = {}) {
2508
2613
  const applicable = /* @__PURE__ */ new Set();
2509
2614
  for (const ruleId of enabled) {
2510
2615
  if (state.get(ruleId)?.status !== "executed") continue;
2616
+ if (DOCUMENT_RULE_IDS.has(ruleId)) continue;
2511
2617
  if (isApplicable(ruleId, segment)) applicable.add(ruleId);
2512
2618
  }
2513
2619
  if (applicable.size === 0) continue;
@@ -2546,105 +2652,1125 @@ function runDeterministicChecks(segments, options = {}) {
2546
2652
  issues.push(toIssue(draft2, segment, draft2.ruleId));
2547
2653
  }
2548
2654
  }
2549
- const rules = [];
2550
- for (const rule of RULES) {
2551
- const execution = state.get(rule.id);
2552
- if (execution === void 0) continue;
2553
- const status = execution.status === "executed" && execution.checkedSegments === 0 ? "not_applicable" : execution.status;
2554
- rules.push({
2555
- ruleId: rule.id,
2556
- status,
2557
- reason: status === "not_applicable" ? "no applicable segments" : execution.reason,
2558
- checkedSegments: execution.checkedSegments
2655
+ const documentRules = RULES.filter(
2656
+ (rule) => rule.scope === "document" && state.get(rule.id)?.status === "executed"
2657
+ );
2658
+ if (documentRules.length > 0) {
2659
+ const contexts = segments.map((segment) => buildContext(segment, options, glossary));
2660
+ for (const rule of documentRules) {
2661
+ const result = rule.id === "TERMINOLOGY_INCONSISTENT" ? terminologyInconsistencies(contexts) : sourceTargetInconsistencies(contexts);
2662
+ for (const item of result.drafts) issues.push(toIssue(item.draft, item.segment, rule.id));
2663
+ const execution = state.get(rule.id);
2664
+ if (execution !== void 0) execution.checkedSegments = result.checkedSegments;
2665
+ }
2666
+ }
2667
+ const rules = [];
2668
+ for (const rule of RULES) {
2669
+ const execution = state.get(rule.id);
2670
+ if (execution === void 0) continue;
2671
+ const status = execution.status === "executed" && execution.checkedSegments === 0 ? "not_applicable" : execution.status;
2672
+ rules.push({
2673
+ ruleId: rule.id,
2674
+ status,
2675
+ reason: status === "not_applicable" ? "no applicable segments" : execution.reason,
2676
+ checkedSegments: execution.checkedSegments
2677
+ });
2678
+ }
2679
+ issues.sort(compareIssues);
2680
+ const coverageIncomplete = rules.some((rule) => rule.status === "skipped" || rule.status === "failed");
2681
+ const anyExecuted = rules.some(
2682
+ (rule) => rule.status === "executed" || rule.status === "not_applicable"
2683
+ );
2684
+ const checkedSegments = anyExecuted ? segments.length : 0;
2685
+ return {
2686
+ issues,
2687
+ totalSegments: segments.length,
2688
+ checkedSegments,
2689
+ coverage: coverageIncomplete ? "incomplete" : "complete",
2690
+ rules
2691
+ };
2692
+ }
2693
+
2694
+ // packages/l10n-core/src/workflows/check.ts
2695
+ function termMatchOptions(document2, segment) {
2696
+ const file = document2.files[segment.location.fileIndex];
2697
+ const options = {};
2698
+ if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
2699
+ if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
2700
+ return options;
2701
+ }
2702
+ function checkDocument(document2, options = {}) {
2703
+ const glossary = options.glossary ?? [];
2704
+ const segmentTermHits = /* @__PURE__ */ new Map();
2705
+ if (glossary.length > 0) {
2706
+ for (const segment of document2.segments) {
2707
+ const termOptions = termMatchOptions(document2, segment);
2708
+ const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
2709
+ const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
2710
+ segmentTermHits.set(segment.id, {
2711
+ source: sourceMatches.hits,
2712
+ target: targetMatches.hits
2713
+ });
2714
+ }
2715
+ }
2716
+ const qaOptions = {
2717
+ ...options.glossary === void 0 ? {} : { glossary: options.glossary },
2718
+ ...segmentTermHits.size === 0 ? {} : { segmentTermHits },
2719
+ ...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules },
2720
+ ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
2721
+ ...options.locale === void 0 ? {} : { locale: options.locale }
2722
+ };
2723
+ const qa = runDeterministicChecks(document2.segments, qaOptions);
2724
+ return { qa, segmentTermHits };
2725
+ }
2726
+
2727
+ // packages/l10n-core/src/qa/review-data.ts
2728
+ var REVIEW_GROUPS = [
2729
+ { label: "\u6807\u70B9\u4E0E\u7A7A\u683C", rules: ["UNPAIRED_BRACKET", "PUNCT_DUP", "WHITESPACE"] },
2730
+ { label: "Tag \u4E0E\u53D8\u91CF", rules: ["TAG_MISMATCH", "PLACEHOLDER_MISSING", "PLACEHOLDER_EXTRA"] },
2731
+ { label: "\u6570\u5B57\u4E0E\u65E5\u671F", rules: ["NUMBER_MISMATCH", "DATE_MISMATCH"] },
2732
+ { label: "\u7A7A\u8BD1\u4E0E\u6B63\u6587\u7F3A\u5931", rules: ["EMPTY_TARGET"] },
2733
+ { label: "\u539F\u8BD1\u76F8\u540C", rules: ["UNTRANSLATED_COPY"] }
2734
+ ];
2735
+ var REVIEW_FILE_BYTES = 12 * 1024 * 1024;
2736
+ var REVIEW_FILE_LIMIT = 20;
2737
+ var FILE_EXTENSION = /\.(?:xlf|xliff|sdlxliff|mqxliff|mqxlz)$/iu;
2738
+ var categories = new Map(
2739
+ REVIEW_GROUPS.flatMap((group) => group.rules.map((rule) => [rule, group.label]))
2740
+ );
2741
+ var REVIEW_COLUMNS = {
2742
+ file: "\u6587\u4EF6",
2743
+ segment: "\u6BB5\u53F7",
2744
+ source: "\u539F\u6587",
2745
+ target: "\u8BD1\u6587",
2746
+ category: "\u5206\u7C7B",
2747
+ severity: "\u4E25\u91CD\u7A0B\u5EA6",
2748
+ rule: "\u68C0\u67E5\u9879",
2749
+ message: "\u95EE\u9898",
2750
+ evidence: "\u4F9D\u636E"
2751
+ };
2752
+ function reviewOptions(value) {
2753
+ if (typeof value !== "object" || value === null) throw new Error("\u68C0\u67E5\u8BBE\u7F6E\u65E0\u6548\u3002");
2754
+ const { rules, identicalWhitelist, customRules } = value;
2755
+ if (!Array.isArray(rules) || rules.some((rule) => typeof rule !== "string" || !categories.has(rule))) {
2756
+ throw new Error("\u68C0\u67E5\u8BBE\u7F6E\u5305\u542B\u672A\u77E5\u89C4\u5219\u3002");
2757
+ }
2758
+ if (!Array.isArray(identicalWhitelist) || identicalWhitelist.length > 100 || identicalWhitelist.some((term) => typeof term !== "string" || term.length > 300)) {
2759
+ throw new Error("\u539F\u8BD1\u76F8\u540C\u653E\u884C\u540D\u5355\u6700\u591A 100 \u6761\uFF0C\u6BCF\u6761\u4E0D\u8D85\u8FC7 300 \u5B57\u3002");
2760
+ }
2761
+ if (!Array.isArray(customRules) || customRules.length > 20) throw new Error("\u6700\u591A\u6DFB\u52A0 20 \u6761\u81EA\u5B9A\u4E49\u68C0\u67E5\u3002");
2762
+ for (const rule of customRules) {
2763
+ if (typeof rule !== "object" || rule === null || typeof rule.name !== "string" || !rule.name.trim() || rule.name.length > 80 || typeof rule.sourceContains !== "string" || rule.sourceContains.length > 300 || typeof rule.value !== "string" || !rule.value.trim() || rule.value.length > 300 || !["required", "forbidden", "maxLength"].includes(rule.kind) || rule.kind === "maxLength" && !/^[1-9]\d{0,4}$/u.test(rule.value)) {
2764
+ throw new Error("\u8BF7\u68C0\u67E5\u81EA\u5B9A\u4E49\u9879\u7684\u540D\u79F0\u3001\u68C0\u6D4B\u7C7B\u578B\u548C\u5339\u914D\u503C\u3002\u957F\u5EA6\u4E0A\u9650\u987B\u4E3A\u6B63\u6574\u6570\u3002");
2765
+ }
2766
+ }
2767
+ if (rules.length === 0 && customRules.length === 0) throw new Error("\u8BF7\u9009\u62E9\u81F3\u5C11\u4E00\u9879\u68C0\u67E5\u3002");
2768
+ return { rules, identicalWhitelist, customRules };
2769
+ }
2770
+ var decoder = new TextDecoder("utf-8", { fatal: true });
2771
+ function parseReviewFile(name, bytes) {
2772
+ if (!FILE_EXTENSION.test(name)) throw new Error("\u8BF7\u9009\u62E9 XLIFF\u3001SDLXLIFF\u3001MQXLIFF \u6216 MQXLZ \u6587\u4EF6\u3002");
2773
+ if (bytes.byteLength > REVIEW_FILE_BYTES) throw new Error("\u6587\u4EF6\u8D85\u8FC7 12 MB\u3002");
2774
+ if (!name.toLowerCase().endsWith(".mqxlz")) {
2775
+ return [{ label: name, document: parseXliff(decoder.decode(bytes), { filename: name }) }];
2776
+ }
2777
+ let size = 0;
2778
+ const entries = unzipSync(bytes, {
2779
+ filter(entry) {
2780
+ if (!entry.name.toLowerCase().endsWith(".mqxliff")) return false;
2781
+ size += entry.originalSize;
2782
+ if (size > REVIEW_FILE_BYTES) throw new Error("MQXLZ \u4E2D\u7684\u53CC\u8BED\u5185\u5BB9\u89E3\u538B\u540E\u8D85\u8FC7 12 MB\u3002");
2783
+ return true;
2784
+ }
2785
+ });
2786
+ if (Object.keys(entries).length === 0) throw new Error("MQXLZ \u4E2D\u672A\u627E\u5230 MQXLIFF \u6587\u4EF6\u3002");
2787
+ return Object.entries(entries).map(([filename, content]) => ({
2788
+ label: `${name} / ${filename}`,
2789
+ document: parseXliff(decoder.decode(content), { filename })
2790
+ }));
2791
+ }
2792
+ function rowLocation(file, document2, segment) {
2793
+ const { fileIndex, unitId, mid } = segment.location;
2794
+ return {
2795
+ file: document2.files.length === 1 ? file : `${file} / \u6587\u6863 ${fileIndex + 1}`,
2796
+ segment: mid === void 0 || mid === unitId ? unitId : `${unitId}/${mid}`,
2797
+ source: segment.sourceTags.length ? segment.sourceInner : segment.sourceText,
2798
+ target: segment.targetTags.length ? segment.targetInner : segment.targetText
2799
+ };
2800
+ }
2801
+ function customFinding(rule, segment) {
2802
+ if (!segment.sourceText.trim() || !segment.targetText.trim() || !segment.sourceText.includes(rule.sourceContains)) return void 0;
2803
+ const length = Array.from(segment.targetText).length;
2804
+ if (rule.kind === "required" && !segment.targetText.includes(rule.value)) return `\u8BD1\u6587\u7F3A\u5C11\u300C${rule.value}\u300D\u3002`;
2805
+ if (rule.kind === "forbidden" && segment.targetText.includes(rule.value)) return `\u8BD1\u6587\u5305\u542B\u7981\u7528\u5185\u5BB9\u300C${rule.value}\u300D\u3002`;
2806
+ if (rule.kind === "maxLength" && length > Number(rule.value)) return `\u8BD1\u6587\u957F ${length} \u5B57\uFF0C\u8D85\u8FC7\u4E0A\u9650 ${rule.value}\u3002`;
2807
+ return void 0;
2808
+ }
2809
+ function reviewDocument(file, document2, options) {
2810
+ const segments = document2.segments.filter((segment) => segment.readOnlyReason !== "translate=no");
2811
+ const { qa } = checkDocument({ ...document2, segments }, {
2812
+ enabledRules: options.rules,
2813
+ untranslatedCopy: { shortSegmentMaxLength: 0, ignoreIdentifiers: true, whitelist: options.identicalWhitelist }
2814
+ });
2815
+ const indexed = new Map(segments.map((segment) => [segment.id, segment]));
2816
+ const rows = qa.issues.map((issue) => {
2817
+ const candidate = issue.ruleId === "UNTRANSLATED_COPY";
2818
+ const segment = indexed.get(issue.segmentId);
2819
+ const same = segment.sourceText === segment.targetText ? "\u539F\u8BD1\u5B8C\u5168\u76F8\u540C" : "\u539F\u8BD1\u7ECF\u5F52\u4E00\u5316\u540E\u76F8\u540C";
2820
+ return {
2821
+ ...rowLocation(file, document2, segment),
2822
+ category: categories.get(issue.ruleId),
2823
+ severity: candidate ? "neutral" : issue.severity,
2824
+ rule: issue.ruleId,
2825
+ message: candidate ? `${same}\uFF0C\u8BF7\u786E\u8BA4\u662F\u5426\u9700\u8981\u7FFB\u8BD1\u3002` : issue.message,
2826
+ evidence: issue.evidence
2827
+ };
2828
+ });
2829
+ for (const segment of segments) {
2830
+ for (const rule of options.customRules) {
2831
+ const finding = customFinding(rule, segment);
2832
+ if (finding !== void 0) {
2833
+ rows.push({
2834
+ ...rowLocation(file, document2, segment),
2835
+ category: "\u81EA\u5B9A\u4E49",
2836
+ severity: "major",
2837
+ rule: rule.name,
2838
+ message: finding,
2839
+ evidence: rule.sourceContains ? `\u539F\u6587\u5305\u542B\u300C${rule.sourceContains}\u300D` : ""
2840
+ });
2841
+ }
2842
+ }
2843
+ }
2844
+ return { rows, segments: segments.length, incomplete: qa.rules.some((rule) => rule.status === "failed" || rule.status === "skipped") };
2845
+ }
2846
+ async function runQaReview(files, options) {
2847
+ if (files.length === 0 || files.length > REVIEW_FILE_LIMIT) throw new Error("\u8BF7\u9009\u62E9 1\u201320 \u4E2A\u53CC\u8BED\u6587\u4EF6\u3002");
2848
+ const report = { rows: [], files: [] };
2849
+ for (const file of files) {
2850
+ try {
2851
+ const parsed = parseReviewFile(file.name, new Uint8Array(await file.arrayBuffer()));
2852
+ let segments = 0;
2853
+ let incomplete = false;
2854
+ for (const { label, document: document2 } of parsed) {
2855
+ const result = reviewDocument(label, document2, options);
2856
+ for (const row of result.rows) report.rows.push(row);
2857
+ segments += result.segments;
2858
+ incomplete ||= result.incomplete;
2859
+ }
2860
+ if (segments === 0) throw new Error("\u672A\u627E\u5230\u53EF\u68C0\u67E5\u7684\u6BB5\u843D\u3002");
2861
+ report.files.push({ name: file.name, segments, incomplete });
2862
+ } catch (error) {
2863
+ report.files.push({
2864
+ name: file.name,
2865
+ segments: 0,
2866
+ incomplete: true,
2867
+ error: error instanceof Error ? error.message : "\u6587\u4EF6\u5904\u7406\u5931\u8D25\u3002"
2868
+ });
2869
+ }
2870
+ }
2871
+ return report;
2872
+ }
2873
+
2874
+ // packages/l10n-core/src/qa/review-page.ts
2875
+ var styles = `
2876
+ :root { color-scheme: light; --paper: #f4f6f9; --ink: #192438; --muted: #4b5a6f;
2877
+ --border: #d9e0ea; --blue: #164aa4; --surface: #fff; font: 15px/1.5 system-ui, sans-serif; }
2878
+ * { box-sizing: border-box; }
2879
+ body { margin: 0; color: var(--ink); background: var(--paper); }
2880
+ main { width: min(1480px, 100%); margin: auto; padding: 28px clamp(14px, 3vw, 36px) 44px; }
2881
+ .brand { color: var(--blue); font-weight: 700; letter-spacing: .04em; }
2882
+ h1 { font-size: clamp(26px, 4vw, 35px); margin: 4px 0 22px; letter-spacing: -.03em; }
2883
+ h2 { font-size: 18px; margin: 0 0 14px; }
2884
+ p { margin: 0; } small, .muted { color: var(--muted); }
2885
+ .layout { display: grid; grid-template-columns: minmax(250px, 285px) minmax(0, 1fr); gap: 18px; align-items: start; }
2886
+ .layout.offline { grid-template-columns: minmax(0, 1fr); }
2887
+ .panel { min-width: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 18px; }
2888
+ label, legend { font-weight: 600; }
2889
+ .field { display: grid; gap: 5px; }
2890
+ .field + .field { margin-top: 12px; }
2891
+ input, button, select, textarea { font: inherit; color: inherit; }
2892
+ input:not([type=checkbox]), select, textarea { background: #fff; width: 100%; min-width: 0;
2893
+ border: 1px solid #aab6c7; padding: 6px 8px; border-radius: 6px; }
2894
+ input[type=file] { padding: 7px 4px; font-size: 12px; }
2895
+ input::file-selector-button { font: inherit; margin-right: 6px; }
2896
+ fieldset { margin: 14px 0; padding: 8px 10px 10px; border: 1px solid var(--border); border-radius: 8px; }
2897
+ .checkbox { display: flex; align-items: center; gap: 6px; font-weight: 450; min-height: 30px; }
2898
+ input[type=checkbox] { accent-color: var(--blue); }
2899
+ summary { cursor: pointer; font-weight: 600; }
2900
+ .custom { display: grid; gap: 9px; margin: 10px 0; }
2901
+ .custom .field { margin: 0; }
2902
+ .custom input, .custom select { font-size: 13px; }
2903
+ .run, .filters, .exports, .pager { display: flex; flex-wrap: wrap; align-items: end; gap: 8px; }
2904
+ .run { margin-top: 18px; }
2905
+ button { background: #fff; border: 1px solid #aab6c7; border-radius: 6px; min-height: 34px; padding: 5px 11px; cursor: pointer; }
2906
+ button.primary { color: white; background: var(--blue); border-color: var(--blue); font-weight: 650; }
2907
+ button:disabled { opacity: .55; cursor: default; }
2908
+ :focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }
2909
+ #status { min-height: 22px; margin-bottom: 10px; }
2910
+ #error, .fail { color: #a22628; }
2911
+ #files { margin: 4px 0 12px; padding-left: 20px; overflow-wrap: anywhere; }
2912
+ .filters { margin: 8px 0 12px; }
2913
+ .filters .field, .exports .field { flex: 1 1 125px; margin: 0; font-size: 13px; }
2914
+ .filters .search { flex-basis: 180px; }
2915
+ .exports { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 14px; align-items: end; }
2916
+ .exports .field { flex: 0 1 150px; }
2917
+ .table { overflow: auto; max-height: 64vh; border: 1px solid var(--border); border-radius: 8px; }
2918
+ table { width: 100%; min-width: 680px; border-collapse: collapse; table-layout: fixed; text-align: left; font-size: 13px; }
2919
+ th, td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: top; overflow-wrap: anywhere; white-space: pre-wrap; }
2920
+ th { position: sticky; top: 0; background: #edf2f8; }
2921
+ th:nth-child(1) { width: 17%; } th:nth-child(2), th:nth-child(3) { width: 23%; } th:nth-child(4) { width: 14%; }
2922
+ tr:last-child td { border-bottom: 0; }
2923
+ td small { display: block; }
2924
+ td details { margin-top: 5px; }
2925
+ .pager { justify-content: space-between; align-items: center; margin-top: 10px; }
2926
+ [hidden] { display: none !important; }
2927
+ @media (max-width: 950px) { .layout { grid-template-columns: minmax(0, 1fr); } }
2928
+ `;
2929
+ function reviewClient() {
2930
+ const get = (id) => document.getElementById(id);
2931
+ const form = document.getElementById("settings");
2932
+ const results = get("results");
2933
+ const reportData = document.getElementById("report-data");
2934
+ const search = get("search");
2935
+ const file = get("file-filter");
2936
+ const category = get("category");
2937
+ const severity = get("severity");
2938
+ const order = get("order");
2939
+ const pagerText = get("pager-text");
2940
+ const previous = get("previous");
2941
+ const next = get("next");
2942
+ const table = get("rows");
2943
+ const status = get("status");
2944
+ const error = get("error");
2945
+ const severityLabel = { critical: "\u4E25\u91CD", major: "\u4E3B\u8981", minor: "\u8F7B\u5FAE", neutral: "\u5F85\u6838\u67E5" };
2946
+ const severityRank = { critical: 0, major: 1, minor: 2, neutral: 3 };
2947
+ const collator = new Intl.Collator(void 0, { numeric: true });
2948
+ let report = { rows: [], files: [] };
2949
+ let filtered = [];
2950
+ let groups = [];
2951
+ let page = 0;
2952
+ const pageSize = 50;
2953
+ function fillOptions(select, values) {
2954
+ select.options.length = 1;
2955
+ for (const value of [...new Set(values)].sort(collator.compare)) select.add(new Option(value, value));
2956
+ }
2957
+ function cell(row, text) {
2958
+ const td = row.insertCell();
2959
+ td.textContent = text;
2960
+ return td;
2961
+ }
2962
+ function render() {
2963
+ const maxPage = Math.max(0, Math.ceil(groups.length / pageSize) - 1);
2964
+ page = Math.min(page, maxPage);
2965
+ table.replaceChildren();
2966
+ for (const group of groups.slice(page * pageSize, (page + 1) * pageSize)) {
2967
+ const issue = group[0];
2968
+ const row = document.createElement("tr");
2969
+ const location = cell(row, issue.file);
2970
+ const segment = document.createElement("small");
2971
+ segment.textContent = "\u6BB5 " + issue.segment;
2972
+ location.append(segment);
2973
+ cell(row, issue.source || "\u2014");
2974
+ cell(row, issue.target || "\u2014");
2975
+ const kind = cell(row, [...new Set(group.map((entry) => entry.category))].join("\uFF0F"));
2976
+ const level = document.createElement("small");
2977
+ const strongest = group.reduce((left, right) => (severityRank[left.severity] ?? 4) <= (severityRank[right.severity] ?? 4) ? left : right);
2978
+ level.textContent = severityLabel[strongest.severity] || strongest.severity;
2979
+ kind.append(level);
2980
+ const problem = cell(row, "");
2981
+ for (const finding of group) {
2982
+ const details = document.createElement("details");
2983
+ const summary = document.createElement("summary");
2984
+ summary.textContent = finding.message;
2985
+ const evidence = document.createElement("p");
2986
+ evidence.textContent = [finding.rule, finding.evidence].filter(Boolean).join(" \xB7 ");
2987
+ details.append(summary, evidence);
2988
+ problem.append(details);
2989
+ }
2990
+ table.append(row);
2991
+ }
2992
+ get("no-results").hidden = filtered.length > 0;
2993
+ get("table").hidden = filtered.length === 0;
2994
+ pagerText.textContent = groups.length + " \u4E2A\u6BB5 \xB7 " + filtered.length + " / " + report.rows.length + " \u6761\u95EE\u9898 \xB7 \u7B2C " + (page + 1) + " / " + (maxPage + 1) + " \u9875";
2995
+ previous.disabled = page === 0;
2996
+ next.disabled = page === maxPage;
2997
+ }
2998
+ function filter() {
2999
+ const query = search.value.trim().toLocaleLowerCase();
3000
+ filtered = report.rows.filter(
3001
+ (row) => (!file.value || row.file === file.value) && (!category.value || row.category === category.value) && (!severity.value || row.severity === severity.value) && (!query || Object.values(row).some((value) => value.toLocaleLowerCase().includes(query)))
3002
+ );
3003
+ filtered.sort((left, right) => {
3004
+ const byLocation = collator.compare(left.file, right.file) || collator.compare(left.segment, right.segment);
3005
+ if (order.value === "severity") return (severityRank[left.severity] ?? 4) - (severityRank[right.severity] ?? 4) || byLocation;
3006
+ if (order.value === "category") return collator.compare(left.category, right.category) || byLocation;
3007
+ return order.value === "desc" ? -byLocation : byLocation;
3008
+ });
3009
+ const grouped = /* @__PURE__ */ new Map();
3010
+ for (const issue of filtered) {
3011
+ const key = JSON.stringify([issue.file, issue.segment]);
3012
+ const existing = grouped.get(key);
3013
+ if (existing) existing.push(issue);
3014
+ else grouped.set(key, [issue]);
3015
+ }
3016
+ groups = [...grouped.values()];
3017
+ page = 0;
3018
+ render();
3019
+ }
3020
+ function show(data) {
3021
+ report = data;
3022
+ const failures = data.files.filter((entry) => entry.incomplete).length;
3023
+ const segments = data.files.reduce((total, entry) => total + entry.segments, 0);
3024
+ status.textContent = (failures ? "\u68C0\u67E5\u672A\u5B8C\u6574\u5B8C\u6210" : "\u68C0\u67E5\u5B8C\u6210") + " \xB7 " + data.files.length + " \u4E2A\u6587\u4EF6 \xB7 " + segments + " \u6BB5 \xB7 " + data.rows.length + " \u6761\u95EE\u9898" + (failures ? " \xB7 " + failures + " \u4E2A\u6587\u4EF6\u672A\u5B8C\u6574\u68C0\u67E5" : "");
3025
+ const fileList = get("files");
3026
+ fileList.replaceChildren();
3027
+ for (const entry of data.files.filter((item) => item.incomplete)) {
3028
+ const li = document.createElement("li");
3029
+ li.className = "fail";
3030
+ li.textContent = entry.name + " \xB7 " + (entry.error || "\u90E8\u5206\u89C4\u5219\u672A\u5B8C\u6210");
3031
+ fileList.append(li);
3032
+ }
3033
+ fileList.hidden = failures === 0;
3034
+ search.value = "";
3035
+ severity.value = "";
3036
+ order.value = "file";
3037
+ fillOptions(file, data.rows.map((row) => row.file));
3038
+ fillOptions(category, data.rows.map((row) => row.category));
3039
+ results.hidden = false;
3040
+ filter();
3041
+ }
3042
+ search.addEventListener("input", filter);
3043
+ for (const select of [file, category, severity, order]) select.addEventListener("change", filter);
3044
+ previous.addEventListener("click", () => {
3045
+ page -= 1;
3046
+ render();
3047
+ });
3048
+ next.addEventListener("click", () => {
3049
+ page += 1;
3050
+ render();
3051
+ });
3052
+ if (reportData) show(JSON.parse(reportData.textContent ?? "{}"));
3053
+ if (!form) return;
3054
+ const add = get("add-rule");
3055
+ const ruleList = get("custom-rules");
3056
+ add.addEventListener("click", () => {
3057
+ const rule = get("rule-template").content.cloneNode(true);
3058
+ const row = rule.querySelector("fieldset");
3059
+ row.querySelector("button").addEventListener("click", () => {
3060
+ row.remove();
3061
+ add.disabled = false;
3062
+ add.focus();
3063
+ });
3064
+ ruleList.append(rule);
3065
+ add.disabled = ruleList.children.length >= 20;
3066
+ row.querySelector("input").focus();
3067
+ });
3068
+ form.addEventListener("submit", async (event) => {
3069
+ event.preventDefault();
3070
+ const run = get("run");
3071
+ if (run.disabled) return;
3072
+ const body = new FormData();
3073
+ for (const upload of get("uploads").files ?? []) body.append("file", upload);
3074
+ const rules = Array.from(form.querySelectorAll("input[data-rules]:checked")).flatMap((item) => (item.dataset.rules ?? "").split(","));
3075
+ const identicalWhitelist = get("whitelist").value.split("\n").map((text) => text.trim()).filter(Boolean);
3076
+ const customRules = Array.from(ruleList.querySelectorAll("fieldset")).map((row) => ({
3077
+ name: row.querySelector("[data-name]").value.trim(),
3078
+ sourceContains: row.querySelector("[data-source]").value,
3079
+ kind: row.querySelector("select").value,
3080
+ value: row.querySelector("[data-value]").value.trim()
3081
+ }));
3082
+ body.append("options", JSON.stringify({ rules, identicalWhitelist, customRules }));
3083
+ run.disabled = true;
3084
+ error.textContent = "";
3085
+ status.textContent = "\u6B63\u5728\u68C0\u67E5\u6587\u4EF6\u2026";
3086
+ results.hidden = true;
3087
+ try {
3088
+ const response = await fetch("/check", { method: "POST", body });
3089
+ const data = await response.json();
3090
+ if (!response.ok) throw new Error(data.error || "\u68C0\u67E5\u5931\u8D25\u3002");
3091
+ show(data);
3092
+ } catch (cause) {
3093
+ status.textContent = "\u68C0\u67E5\u5931\u8D25";
3094
+ error.textContent = cause instanceof Error ? cause.message : "\u8BF7\u91CD\u8BD5\u3002";
3095
+ } finally {
3096
+ run.disabled = false;
3097
+ }
3098
+ });
3099
+ for (const button of document.querySelectorAll("button[data-export]")) {
3100
+ button.addEventListener("click", async () => {
3101
+ const format = button.dataset.export;
3102
+ if (button.disabled) return;
3103
+ button.disabled = true;
3104
+ error.textContent = "";
3105
+ try {
3106
+ const chosen = get("export-scope").value === "all" ? report.rows : filtered;
3107
+ const response = await fetch("/export/" + format, {
3108
+ method: "POST",
3109
+ headers: { "Content-Type": "application/json" },
3110
+ body: JSON.stringify({ rows: chosen, files: report.files })
3111
+ });
3112
+ if (!response.ok) throw new Error((await response.json()).error || "\u5BFC\u51FA\u5931\u8D25\u3002");
3113
+ const url = URL.createObjectURL(await response.blob());
3114
+ const anchor = document.createElement("a");
3115
+ anchor.href = url;
3116
+ anchor.download = "langlink-qa." + format;
3117
+ anchor.click();
3118
+ setTimeout(() => URL.revokeObjectURL(url), 1e3);
3119
+ } catch (cause) {
3120
+ error.textContent = cause instanceof Error ? cause.message : "\u5BFC\u51FA\u5931\u8D25\u3002";
3121
+ } finally {
3122
+ button.disabled = false;
3123
+ }
3124
+ });
3125
+ }
3126
+ }
3127
+ function renderReviewPage(report, nonce = "") {
3128
+ const offline = report !== void 0;
3129
+ const title = offline ? "\u53CC\u8BED QA \u62A5\u544A" : "\u53CC\u8BED\u6587\u4EF6\u68C0\u67E5";
3130
+ const settings = offline ? "" : `<form class="panel" id="settings">
3131
+ <h2>\u672C\u6B21\u68C0\u67E5</h2>
3132
+ <label class="field">\u53CC\u8BED\u6587\u4EF6<input type="file" id="uploads" multiple required accept=".xlf,.xliff,.mqxliff,.mqxlz,.sdlxliff">
3133
+ <small>\u6700\u591A 20 \u4E2A\u6587\u4EF6\uFF0C\u5355\u4E2A 12 MB\uFF0C\u5408\u8BA1 36 MB\u3002</small></label>
3134
+ <fieldset><legend>\u68C0\u67E5\u9879</legend>
3135
+ ${REVIEW_GROUPS.map((group) => `<label class="checkbox"><input type="checkbox" checked data-rules="${group.rules.join(",")}">${group.label}</label>`).join("")}
3136
+ </fieldset>
3137
+ <details><summary>\u5141\u8BB8\u539F\u8BD1\u76F8\u540C\u7684\u5185\u5BB9</summary>
3138
+ <label class="field"><small>\u54C1\u724C\u540D\u6216\u5176\u4ED6\u65E0\u9700\u7FFB\u8BD1\u7684\u6587\u672C\uFF0C\u6BCF\u884C\u4E00\u6761\u3002</small><textarea id="whitelist" rows="3"></textarea></label>
3139
+ </details>
3140
+ <fieldset><legend>\u81EA\u5B9A\u4E49\u68C0\u67E5</legend><div id="custom-rules"></div>
3141
+ <button type="button" id="add-rule">\u6DFB\u52A0\u68C0\u67E5\u9879</button>
3142
+ </fieldset>
3143
+ <div class="run"><button class="primary" id="run" type="submit">\u8FD0\u884C\u68C0\u67E5</button></div>
3144
+ <template id="rule-template"><fieldset class="custom"><legend>\u68C0\u67E5\u9879</legend>
3145
+ <label class="field">\u540D\u79F0<input data-name required maxlength="80"></label>
3146
+ <label class="field">\u539F\u6587\u5305\u542B\uFF08\u53EF\u9009\uFF09<input data-source maxlength="300"></label>
3147
+ <label class="field">\u8981\u6C42<select><option value="required">\u8BD1\u6587\u5FC5\u542B</option><option value="forbidden">\u8BD1\u6587\u7981\u7528</option><option value="maxLength">\u8BD1\u6587\u957F\u5EA6\u4E0A\u9650</option></select></label>
3148
+ <label class="field">\u8BCD\u8BED\u6216\u957F\u5EA6<input data-value required maxlength="300"></label>
3149
+ <button type="button">\u79FB\u9664\u68C0\u67E5\u9879</button>
3150
+ </fieldset></template>
3151
+ </form>`;
3152
+ const exports = offline ? "" : `<div class="exports">
3153
+ <label class="field">\u5BFC\u51FA\u8303\u56F4<select id="export-scope"><option value="filtered">\u5F53\u524D\u7B5B\u9009</option><option value="all">\u5168\u90E8\u7ED3\u679C</option></select></label>
3154
+ <button type="button" data-export="xlsx">Excel</button><button type="button" data-export="csv">CSV</button><button type="button" data-export="html">HTML</button></div>`;
3155
+ const data = offline ? `<script type="application/json" id="report-data">${JSON.stringify(report).replaceAll("<", "\\u003c")}</script>` : "";
3156
+ return `<!doctype html><html lang="zh-CN"><head><meta charset="utf-8">
3157
+ <meta name="viewport" content="width=device-width, initial-scale=1"><title>${title} \xB7 LangLink</title>
3158
+ <style>${styles}</style></head><body><main><p class="brand">LangLink</p><h1>${title}</h1>
3159
+ <div class="layout${offline ? " offline" : ""}">${settings}
3160
+ <section class="panel" aria-labelledby="results-title"><h2 id="results-title">\u68C0\u67E5\u7ED3\u679C</h2>
3161
+ <p class="muted" id="status" role="status">${offline ? "" : "\u9009\u62E9\u6587\u4EF6\u548C\u68C0\u67E5\u9879\uFF0C\u7136\u540E\u8FD0\u884C\u68C0\u67E5\u3002"}</p>
3162
+ <p id="error" role="alert"></p><ul id="files" hidden></ul>
3163
+ <div id="results" hidden><div class="filters">
3164
+ <label class="field search">\u641C\u7D22<input type="search" id="search" placeholder="\u539F\u6587\u3001\u8BD1\u6587\u3001\u6BB5\u53F7\u6216\u89C4\u5219"></label>
3165
+ <label class="field">\u6587\u4EF6<select id="file-filter"><option value="">\u5168\u90E8\u6587\u4EF6</option></select></label>
3166
+ <div class="field"><label for="category">\u5206\u7C7B</label><select id="category"><option value="">\u5168\u90E8\u5206\u7C7B</option></select></div>
3167
+ <label class="field">\u4E25\u91CD\u7A0B\u5EA6<select id="severity"><option value="">\u5168\u90E8</option><option value="critical">\u4E25\u91CD</option><option value="major">\u4E3B\u8981</option><option value="minor">\u8F7B\u5FAE</option><option value="neutral">\u5F85\u6838\u67E5</option></select></label>
3168
+ <label class="field">\u6392\u5E8F<select id="order"><option value="file">\u6587\u4EF6\u548C\u6BB5\u53F7</option><option value="desc">\u6587\u4EF6\u548C\u6BB5\u53F7\u5012\u5E8F</option><option value="severity">\u4E25\u91CD\u7A0B\u5EA6</option><option value="category">\u95EE\u9898\u5206\u7C7B</option></select></label>
3169
+ </div><p id="no-results" hidden>\u5F53\u524D\u7B5B\u9009\u6CA1\u6709\u95EE\u9898\u8BB0\u5F55\u3002</p>
3170
+ <div class="table" id="table" tabindex="0" role="region" aria-label="\u95EE\u9898\u660E\u7EC6"><table>
3171
+ <thead><tr><th scope="col">\u6587\u4EF6\uFF0F\u6BB5\u53F7</th><th scope="col">\u539F\u6587</th><th scope="col">\u8BD1\u6587</th><th scope="col">\u5206\u7C7B</th><th scope="col">\u95EE\u9898</th></tr></thead>
3172
+ <tbody id="rows"></tbody></table></div>
3173
+ <div class="pager"><span id="pager-text" aria-live="polite"></span><div><button type="button" id="previous">\u4E0A\u4E00\u9875</button> <button type="button" id="next">\u4E0B\u4E00\u9875</button></div></div>
3174
+ ${exports}</div></section></div></main>${data}
3175
+ <script${nonce ? ` nonce="${nonce}"` : ""}>(${reviewClient.toString()})();</script></body></html>`;
3176
+ }
3177
+
3178
+ // packages/l10n-core/src/qa/review-server.ts
3179
+ var HOST = "127.0.0.1";
3180
+ var MAX_BODY = 40 * 1024 * 1024;
3181
+ var IDLE_TIMEOUT_MS = 30 * 60 * 1e3;
3182
+ var columnKeys = Object.keys(REVIEW_COLUMNS);
3183
+ var RequestTooLarge = class extends Error {
3184
+ };
3185
+ async function readBody2(request) {
3186
+ const chunks = [];
3187
+ let size = 0;
3188
+ for await (const chunk of request.iterator({ destroyOnReturn: false })) {
3189
+ size += chunk.byteLength;
3190
+ if (size > MAX_BODY) throw new RequestTooLarge("\u672C\u6B21\u8BF7\u6C42\u8D85\u8FC7\u5927\u5C0F\u9650\u5236\u3002\u8BF7\u51CF\u5C11\u6587\u4EF6\u6216\u7F29\u5C0F\u5BFC\u51FA\u8303\u56F4\u3002");
3191
+ chunks.push(chunk);
3192
+ }
3193
+ return new Uint8Array(Buffer.concat(chunks, size));
3194
+ }
3195
+ function reply(response, status, type, body) {
3196
+ response.writeHead(status, { "Content-Type": type, "Cache-Control": "no-store", "X-Content-Type-Options": "nosniff" });
3197
+ response.end(body);
3198
+ }
3199
+ function json2(response, status, value) {
3200
+ reply(response, status, "application/json; charset=utf-8", JSON.stringify(value));
3201
+ }
3202
+ function readReport(bytes) {
3203
+ const report = JSON.parse(new TextDecoder().decode(bytes));
3204
+ if (!report || !Array.isArray(report.rows) || !Array.isArray(report.files) || report.rows.some((row) => !row || columnKeys.some((key) => typeof row[key] !== "string")) || report.files.some((file) => !file || typeof file.name !== "string" || !Number.isSafeInteger(file.segments) || typeof file.incomplete !== "boolean" || file.error !== void 0 && typeof file.error !== "string")) {
3205
+ throw new Error("\u5BFC\u51FA\u6570\u636E\u65E0\u6548\u3002");
3206
+ }
3207
+ return report;
3208
+ }
3209
+ function csvCell(value) {
3210
+ return escapeCsvField(/^[\s\uFEFF]*[=+@-]|^[\t\r\n]/u.test(value) ? "'" + value : value);
3211
+ }
3212
+ async function exportReport(response, format, report) {
3213
+ response.setHeader("Content-Disposition", `attachment; filename="langlink-qa.${format}"`);
3214
+ if (format === "html") {
3215
+ reply(response, 200, "text/html; charset=utf-8", renderReviewPage(report));
3216
+ return;
3217
+ }
3218
+ const headers = Object.values(REVIEW_COLUMNS);
3219
+ if (format === "csv") {
3220
+ const lines = [headers.join(","), ...report.rows.map((row) => columnKeys.map((key) => csvCell(row[key])).join(","))];
3221
+ reply(response, 200, "text/csv; charset=utf-8", "\uFEFF" + lines.join("\r\n") + "\r\n");
3222
+ return;
3223
+ }
3224
+ if (report.rows.length > 1048575 || report.rows.some((row) => columnKeys.some((key) => row[key].length > 32767))) {
3225
+ throw new Error("\u7ED3\u679C\u8D85\u8FC7 Excel \u7684\u884C\u6570\u6216\u5355\u5143\u683C\u957F\u5EA6\u9650\u5236\uFF0C\u8BF7\u4F7F\u7528 CSV \u6216 HTML\u3002");
3226
+ }
3227
+ const workbook = new ExcelJS.Workbook();
3228
+ const sheet = workbook.addWorksheet("QA", { views: [{ state: "frozen", ySplit: 1 }] });
3229
+ const widths = [32, 14, 48, 48, 19, 14, 27, 46, 60];
3230
+ sheet.columns = headers.map((header, index) => ({ header, width: widths[index] ?? 24 }));
3231
+ for (const row of report.rows) sheet.addRow(columnKeys.map((key) => row[key]));
3232
+ sheet.getRow(1).font = { bold: true };
3233
+ sheet.autoFilter = { from: { row: 1, column: 1 }, to: { row: Math.max(2, sheet.rowCount), column: headers.length } };
3234
+ const bytes = new Uint8Array(await workbook.xlsx.writeBuffer());
3235
+ reply(response, 200, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", bytes);
3236
+ }
3237
+ async function listenQaReview(onClose) {
3238
+ let origin;
3239
+ let authority;
3240
+ let expiry;
3241
+ const server = createServer2((request, response) => {
3242
+ if (request.headers.host !== authority) {
3243
+ json2(response, 403, { error: "\u8BF7\u4F7F\u7528\u672C\u673A\u68C0\u67E5\u9875\u9762\u3002" });
3244
+ return;
3245
+ }
3246
+ response.setHeader("Referrer-Policy", "no-referrer");
3247
+ response.setHeader("X-Frame-Options", "DENY");
3248
+ if (request.method === "GET" && request.url === "/") {
3249
+ expiry.refresh();
3250
+ const nonce = randomBytes(16).toString("base64");
3251
+ response.setHeader(
3252
+ "Content-Security-Policy",
3253
+ `default-src 'none'; script-src 'nonce-${nonce}'; style-src 'unsafe-inline'; connect-src 'self'; frame-ancestors 'none'; base-uri 'none'; form-action 'self'`
3254
+ );
3255
+ reply(response, 200, "text/html; charset=utf-8", renderReviewPage(void 0, nonce));
3256
+ return;
3257
+ }
3258
+ if (request.method !== "POST" || request.headers.origin !== origin) {
3259
+ json2(response, 403, { error: "\u8BF7\u4ECE\u672C\u673A\u68C0\u67E5\u9875\u9762\u53D1\u8D77\u64CD\u4F5C\u3002" });
3260
+ return;
3261
+ }
3262
+ expiry.refresh();
3263
+ void (async () => {
3264
+ if (request.url === "/check") {
3265
+ const type = request.headers["content-type"] ?? "";
3266
+ if (!type.startsWith("multipart/form-data;")) throw new Error("\u8BF7\u9009\u62E9\u53CC\u8BED\u6587\u4EF6\u3002");
3267
+ const data = await new Response(await readBody2(request), { headers: { "Content-Type": type } }).formData();
3268
+ const options = data.get("options");
3269
+ const files = data.getAll("file");
3270
+ if (typeof options !== "string" || !files.every((file) => file instanceof File)) {
3271
+ throw new Error("\u7F3A\u5C11\u6587\u4EF6\u6216\u68C0\u67E5\u8BBE\u7F6E\u3002");
3272
+ }
3273
+ if (files.reduce((size, file) => size + file.size, 0) > 36 * 1024 * 1024) throw new RequestTooLarge("\u6587\u4EF6\u5408\u8BA1\u8D85\u8FC7 36 MB\u3002");
3274
+ json2(response, 200, await runQaReview(files, reviewOptions(JSON.parse(options))));
3275
+ return;
3276
+ }
3277
+ const format = /^\/export\/(xlsx|csv|html)$/u.exec(request.url ?? "")?.[1];
3278
+ if (format === void 0) {
3279
+ json2(response, 404, { error: "\u627E\u4E0D\u5230\u6B64\u64CD\u4F5C\u3002" });
3280
+ return;
3281
+ }
3282
+ await exportReport(response, format, readReport(await readBody2(request)));
3283
+ })().catch((cause) => {
3284
+ response.removeHeader("Content-Disposition");
3285
+ json2(response, cause instanceof RequestTooLarge ? 413 : 400, {
3286
+ error: cause instanceof Error ? cause.message : "\u64CD\u4F5C\u5931\u8D25\u3002"
3287
+ });
3288
+ });
3289
+ });
3290
+ await new Promise((resolve2, reject) => {
3291
+ server.once("error", reject);
3292
+ server.listen(0, HOST, () => {
3293
+ server.off("error", reject);
3294
+ resolve2();
3295
+ });
3296
+ });
3297
+ const address = server.address();
3298
+ if (!address || typeof address === "string") throw new Error("\u65E0\u6CD5\u542F\u52A8\u672C\u673A\u68C0\u67E5\u9875\u9762\u3002");
3299
+ authority = `${HOST}:${address.port}`;
3300
+ origin = `http://${authority}`;
3301
+ expiry = setTimeout(() => server.close(), IDLE_TIMEOUT_MS);
3302
+ expiry.unref();
3303
+ server.unref();
3304
+ server.once("close", () => {
3305
+ clearTimeout(expiry);
3306
+ onClose?.();
3307
+ });
3308
+ return { url: origin + "/", close: () => new Promise((resolve2) => server.close(() => resolve2())) };
3309
+ }
3310
+ var activeReview;
3311
+ async function openQaReview() {
3312
+ activeReview ??= listenQaReview(() => {
3313
+ activeReview = void 0;
3314
+ }).catch((cause) => {
3315
+ activeReview = void 0;
3316
+ throw cause;
3317
+ });
3318
+ const { url } = await activeReview;
3319
+ return { url, message: `[\u6253\u5F00\u53CC\u8BED\u6587\u4EF6\u68C0\u67E5](${url})\uFF0C\u9009\u62E9\u6587\u4EF6\u548C\u89C4\u5219\u540E\u70B9\u51FB\u300C\u8FD0\u884C\u68C0\u67E5\u300D\u3002` };
3320
+ }
3321
+
3322
+ // packages/l10n-core/src/assets/glossary.ts
3323
+ var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
3324
+ var TARGET_HEADERS = /* @__PURE__ */ new Set(["target", "target term", "translation", "\u8BD1\u6587", "\u76EE\u6807", "\u76EE\u6807\u8BED"]);
3325
+ var FORBIDDEN_HEADERS = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
3326
+ function stripBom(text) {
3327
+ return text.charCodeAt(0) === 65279 ? text.slice(1) : text;
3328
+ }
3329
+ function detectDelimiter(text) {
3330
+ let commas = 0;
3331
+ let tabs = 0;
3332
+ let inQuotes = false;
3333
+ for (let index = 0; index < text.length; index += 1) {
3334
+ const char = text[index];
3335
+ if (char === '"') {
3336
+ if (inQuotes && text[index + 1] === '"') index += 1;
3337
+ else inQuotes = !inQuotes;
3338
+ } else if (!inQuotes) {
3339
+ if (char === "\n") break;
3340
+ else if (char === ",") commas += 1;
3341
+ else if (char === " ") tabs += 1;
3342
+ }
3343
+ }
3344
+ return tabs > commas ? " " : ",";
3345
+ }
3346
+ function parseCsvRows(text, delimiter = ",") {
3347
+ const rows = [];
3348
+ let row = [];
3349
+ let field = "";
3350
+ let inQuotes = false;
3351
+ const pushRow = () => {
3352
+ row.push(field);
3353
+ field = "";
3354
+ if (row.some((cell) => cell.trim() !== "")) rows.push(row);
3355
+ row = [];
3356
+ };
3357
+ for (let index = 0; index < text.length; index += 1) {
3358
+ const char = text[index];
3359
+ if (inQuotes) {
3360
+ if (char === '"') {
3361
+ if (text[index + 1] === '"') {
3362
+ field += '"';
3363
+ index += 1;
3364
+ } else {
3365
+ inQuotes = false;
3366
+ }
3367
+ } else if (char === "\r") {
3368
+ field += "\n";
3369
+ if (text[index + 1] === "\n") index += 1;
3370
+ } else {
3371
+ field += char;
3372
+ }
3373
+ continue;
3374
+ }
3375
+ if (char === '"') {
3376
+ inQuotes = true;
3377
+ } else if (char === delimiter) {
3378
+ row.push(field);
3379
+ field = "";
3380
+ } else if (char === "\n") {
3381
+ pushRow();
3382
+ } else if (char === "\r") {
3383
+ if (text[index + 1] !== "\n") pushRow();
3384
+ } else {
3385
+ field += char;
3386
+ }
3387
+ }
3388
+ if (field !== "" || row.length > 0) pushRow();
3389
+ return rows;
3390
+ }
3391
+ function headerIndex(row, aliases) {
3392
+ return row.findIndex((cell) => aliases.has(cell.trim().toLowerCase()));
3393
+ }
3394
+ function isTruthyFlag(value) {
3395
+ return /^(?:true|1|yes|y|是|禁用)$/i.test(value.trim());
3396
+ }
3397
+ function normalizedHeader(row) {
3398
+ return row.map((cell) => cell.trim().toLowerCase());
3399
+ }
3400
+ function parseGlossaryCsv(text, options = {}) {
3401
+ const content = stripBom(text);
3402
+ const delimiter = detectDelimiter(content);
3403
+ const rows = parseCsvRows(content, delimiter);
3404
+ const warnings = [];
3405
+ if (rows.length === 0) return { entries: [], warnings: ["glossary: no rows found"] };
3406
+ let sourceColumn = 0;
3407
+ let targetColumn = 1;
3408
+ let forbiddenColumn = 2;
3409
+ let dataRows = rows;
3410
+ const first = rows[0];
3411
+ if (first) {
3412
+ const header = normalizedHeader(first);
3413
+ const sourceIndex = headerIndex(header, SOURCE_HEADERS);
3414
+ const targetIndex = headerIndex(header, TARGET_HEADERS);
3415
+ if (sourceIndex >= 0 && targetIndex >= 0) {
3416
+ sourceColumn = sourceIndex;
3417
+ targetColumn = targetIndex;
3418
+ const forbiddenIndex = headerIndex(header, FORBIDDEN_HEADERS);
3419
+ forbiddenColumn = forbiddenIndex >= 0 ? forbiddenIndex : void 0;
3420
+ const known = /* @__PURE__ */ new Set([sourceIndex, targetIndex]);
3421
+ if (forbiddenIndex >= 0) known.add(forbiddenIndex);
3422
+ header.forEach((cell, index) => {
3423
+ if (cell !== "" && !known.has(index)) {
3424
+ warnings.push(`glossary: ignored column "${first[index]}"`);
3425
+ }
3426
+ });
3427
+ dataRows = rows.slice(1);
3428
+ } else if (sourceIndex >= 0 || targetIndex >= 0) {
3429
+ warnings.push("glossary: header is missing a source or target column; using positional columns");
3430
+ sourceColumn = sourceIndex >= 0 ? sourceIndex : 0;
3431
+ targetColumn = targetIndex >= 0 ? targetIndex : sourceColumn === 0 ? 1 : 0;
3432
+ forbiddenColumn = void 0;
3433
+ dataRows = rows.slice(1);
3434
+ }
3435
+ }
3436
+ const entries = [];
3437
+ dataRows.forEach((row, rowIndex) => {
3438
+ const position = rowIndex + 1;
3439
+ const source = (row[sourceColumn] ?? "").trim();
3440
+ const target = (row[targetColumn] ?? "").trim();
3441
+ if (source === "" && target === "") return;
3442
+ if (source === "" || target === "") {
3443
+ warnings.push(`glossary: row ${position} is missing ${source === "" ? "source" : "target"}; skipped`);
3444
+ return;
3445
+ }
3446
+ const forbiddenCell = forbiddenColumn === void 0 ? "" : row[forbiddenColumn] ?? "";
3447
+ const forbidden = isTruthyFlag(forbiddenCell);
3448
+ entries.push({
3449
+ id: `glossary-${entries.length + 1}`,
3450
+ source,
3451
+ target,
3452
+ ...options.sourceLanguage === void 0 ? {} : { sourceLanguage: options.sourceLanguage },
3453
+ ...options.targetLanguage === void 0 ? {} : { targetLanguage: options.targetLanguage },
3454
+ ...forbidden ? { forbidden: true } : {}
3455
+ });
3456
+ });
3457
+ return { entries, warnings };
3458
+ }
3459
+
3460
+ // packages/l10n-core/src/assets/tm-matcher.ts
3461
+ var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
3462
+ var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
3463
+ var DOUBLE_QUOTES = /[\u201c\u201d\u201e\u201f\u2033]/gu;
3464
+ var DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
3465
+ var TERMINAL_PUNCTUATION = /[.!?\u2026;:\u3002\uff01\uff1f\uff1b\uff1a]$/u;
3466
+ var CJK_CHAR2 = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
3467
+ var WORD_CHAR2 = /[\p{L}\p{N}_]/u;
3468
+ var COMBINING_MARK2 = new RegExp("\\p{M}", "u");
3469
+ var TAG_TOKEN = /^<\/?[A-Za-z_][\w.:-]*(?:\s+[^<>]*?)?\s*\/?>/u;
3470
+ var PLACEHOLDER_TOKEN = /^(?:\$\{[^{}]*\}|%[0-9]+\$[sdifuxX]|%[-+ 0#]*[0-9]*(?:\.[0-9]+)?[sdifuxXc]|\{[^{}]*\})/u;
3471
+ var URL_TOKEN = /^(?:(?:https?|ftp):\/\/|www\.)[^\s<>"'{}]+/u;
3472
+ var EMAIL_TOKEN = /^[\w.+-]+@[\w-]+(?:\.[\w-]+)+/u;
3473
+ var PATH_TOKEN = /^(?:(?:[A-Za-z]:[\\/]|\\\\|~[\\/])[^\s<>"'{}]+|\/(?:[^\s<>"'{}]*\/)[^\s<>"'{}]*)/u;
3474
+ var NUMBER_TOKEN = new RegExp("^\\p{Nd}+(?:[.,]\\p{Nd}+)?", "u");
3475
+ var SIMILARITY_TRUNCATE = 200;
3476
+ var TRUNCATION_PENALTY = 7;
3477
+ var DEFAULT_THRESHOLD = 50;
3478
+ var DEFAULT_MAX_MATCHES = 3;
3479
+ function normalizeText(text) {
3480
+ return text.normalize("NFKC").normalize("NFC").replace(ZERO_WIDTH, "").replace(QUOTES, "'").replace(DOUBLE_QUOTES, '"').replace(DASHES, "-").replace(/\u2026/gu, "...").replace(/\s+/gu, " ").trim().toLowerCase();
3481
+ }
3482
+ function matchAtomic(rest) {
3483
+ const tag = TAG_TOKEN.exec(rest);
3484
+ if (tag) return { kind: "tag", raw: tag[0], skeleton: "<TAG>" };
3485
+ const placeholder = PLACEHOLDER_TOKEN.exec(rest);
3486
+ if (placeholder) return { kind: "placeholder", raw: placeholder[0], skeleton: "<PH>" };
3487
+ const url = URL_TOKEN.exec(rest);
3488
+ if (url) return { kind: "atomic", raw: url[0], skeleton: url[0] };
3489
+ const email = EMAIL_TOKEN.exec(rest);
3490
+ if (email) return { kind: "atomic", raw: email[0], skeleton: email[0] };
3491
+ const path = PATH_TOKEN.exec(rest);
3492
+ if (path) return { kind: "atomic", raw: path[0], skeleton: path[0] };
3493
+ return void 0;
3494
+ }
3495
+ function tokenize(text) {
3496
+ const norm = normalizeText(text);
3497
+ const tokens = [];
3498
+ let index = 0;
3499
+ while (index < norm.length) {
3500
+ const rest = norm.slice(index);
3501
+ const atomic = matchAtomic(rest);
3502
+ if (atomic) {
3503
+ tokens.push(atomic);
3504
+ index += atomic.raw.length;
3505
+ continue;
3506
+ }
3507
+ const char = norm[index];
3508
+ if (CJK_CHAR2.test(char)) {
3509
+ tokens.push({ kind: "cjk", raw: char, skeleton: char });
3510
+ index += 1;
3511
+ continue;
3512
+ }
3513
+ const number3 = NUMBER_TOKEN.exec(rest);
3514
+ if (number3) {
3515
+ tokens.push({ kind: "number", raw: number3[0], skeleton: "<NUM>" });
3516
+ index += number3[0].length;
3517
+ continue;
3518
+ }
3519
+ if (WORD_CHAR2.test(char)) {
3520
+ let end = index + 1;
3521
+ while (end < norm.length) {
3522
+ const next = norm[end];
3523
+ if (CJK_CHAR2.test(next)) break;
3524
+ if (WORD_CHAR2.test(next) || COMBINING_MARK2.test(next)) {
3525
+ end += 1;
3526
+ continue;
3527
+ }
3528
+ break;
3529
+ }
3530
+ const raw = norm.slice(index, end);
3531
+ tokens.push({ kind: "word", raw, skeleton: raw });
3532
+ index = end;
3533
+ continue;
3534
+ }
3535
+ index += 1;
3536
+ }
3537
+ return {
3538
+ tokens,
3539
+ skeleton: tokens.map((token) => token.skeleton),
3540
+ numbers: tokens.filter((token) => token.kind === "number").map((token) => token.raw),
3541
+ atoms: tokens.filter((token) => token.kind === "tag" || token.kind === "placeholder").map((token) => `${token.kind}:${token.raw}`),
3542
+ norm,
3543
+ terminalPunct: TERMINAL_PUNCTUATION.test(norm)
3544
+ };
3545
+ }
3546
+ function arraysEqual(a, b) {
3547
+ if (a.length !== b.length) return false;
3548
+ return a.every((value, index) => value === b[index]);
3549
+ }
3550
+ function tokensEqual(a, b) {
3551
+ if (a.length !== b.length) return false;
3552
+ return a.every(
3553
+ (token, index) => token.kind === b[index].kind && token.raw === b[index].raw
3554
+ );
3555
+ }
3556
+ function firstDifference(a, b) {
3557
+ for (let index = 0; index < Math.min(a.length, b.length); index += 1) {
3558
+ if (a[index] !== b[index]) return `${a[index]} vs ${b[index]}`;
3559
+ }
3560
+ return `${a.length} vs ${b.length}`;
3561
+ }
3562
+ function compareStructure(source, entry, tmEntry) {
3563
+ const skeletonEqual = arraysEqual(source.skeleton, entry.skeleton);
3564
+ const numbersEqual = arraysEqual(source.numbers, entry.numbers);
3565
+ const atomsEqual = arraysEqual(source.atoms, entry.atoms);
3566
+ const numberDiff = !numbersEqual;
3567
+ const tagDiff = !atomsEqual;
3568
+ const punctDiff = source.terminalPunct !== entry.terminalPunct || tokensEqual(source.tokens, entry.tokens) && source.norm !== entry.norm;
3569
+ const penalties = [];
3570
+ if (numberDiff) {
3571
+ penalties.push({
3572
+ kind: "number",
3573
+ points: 2,
3574
+ detail: `${source.numbers.join(", ") || "none"} vs ${entry.numbers.join(", ") || "none"}`
3575
+ });
3576
+ }
3577
+ if (tagDiff) {
3578
+ if (source.atoms.length !== entry.atoms.length) {
3579
+ penalties.push({
3580
+ kind: "tag_count",
3581
+ points: 10,
3582
+ detail: `${source.atoms.length} vs ${entry.atoms.length} tags/placeholders`
3583
+ });
3584
+ } else {
3585
+ penalties.push({
3586
+ kind: "tag_id",
3587
+ points: 3,
3588
+ detail: firstDifference(source.atoms, entry.atoms)
3589
+ });
3590
+ }
3591
+ }
3592
+ if (punctDiff) {
3593
+ penalties.push({
3594
+ kind: "punctuation",
3595
+ points: 1,
3596
+ detail: "difference is limited to punctuation/whitespace"
2559
3597
  });
2560
3598
  }
2561
- issues.sort(compareIssues);
2562
- const coverageIncomplete = rules.some((rule) => rule.status === "skipped" || rule.status === "failed");
2563
- const anyExecuted = rules.some(
2564
- (rule) => rule.status === "executed" || rule.status === "not_applicable"
2565
- );
2566
- const checkedSegments = anyExecuted ? segments.length : 0;
2567
- return {
2568
- issues,
2569
- totalSegments: segments.length,
2570
- checkedSegments,
2571
- coverage: coverageIncomplete ? "incomplete" : "complete",
2572
- rules
2573
- };
3599
+ if (tmEntry.fuzzy === true) {
3600
+ penalties.push({ kind: "fuzzy", points: 5, detail: "TM entry is flagged as fuzzy" });
3601
+ }
3602
+ if (tmEntry.origin === "alignment") {
3603
+ penalties.push({
3604
+ kind: "unaligned",
3605
+ points: 2,
3606
+ detail: "alignment entry (default unaligned penalty)"
3607
+ });
3608
+ }
3609
+ return { skeletonEqual, numbersEqual, atomsEqual, numberDiff, tagDiff, punctDiff, penalties };
2574
3610
  }
2575
-
2576
- // packages/l10n-core/src/qa/report.ts
2577
- function renderJson(result) {
2578
- const issues = result.issues.map((issue) => ({
2579
- ruleId: issue.ruleId,
2580
- severity: issue.severity,
2581
- segmentId: issue.segmentId,
2582
- segmentIndex: issue.segmentIndex,
2583
- message: issue.message,
2584
- evidence: issue.evidence,
2585
- ...issue.suggestion === void 0 ? {} : { suggestion: issue.suggestion },
2586
- ...issue.sourceRange === void 0 ? {} : { sourceRange: { start: issue.sourceRange.start, end: issue.sourceRange.end } },
2587
- ...issue.targetRange === void 0 ? {} : { targetRange: { start: issue.targetRange.start, end: issue.targetRange.end } },
2588
- ...issue.termId === void 0 ? {} : { termId: issue.termId },
2589
- origin: issue.origin
2590
- }));
2591
- return JSON.stringify(
2592
- {
2593
- totalSegments: result.totalSegments,
2594
- checkedSegments: result.checkedSegments,
2595
- coverage: result.coverage,
2596
- issueCount: issues.length,
2597
- issues,
2598
- rules: result.rules.map((rule) => ({
2599
- ruleId: rule.ruleId,
2600
- status: rule.status,
2601
- reason: rule.reason,
2602
- checkedSegments: rule.checkedSegments
2603
- }))
2604
- },
2605
- null,
2606
- 2
2607
- );
3611
+ function sumPenalties(penalties) {
3612
+ return penalties.reduce((total, penalty) => total + penalty.points, 0);
2608
3613
  }
2609
-
2610
- // packages/l10n-core/src/workflows/check.ts
2611
- function termMatchOptions(document, segment) {
2612
- const file = document.files[segment.location.fileIndex];
2613
- const options = {};
2614
- if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
2615
- if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
2616
- return options;
3614
+ function osaDistance(a, b) {
3615
+ const rows = a.length + 1;
3616
+ const cols = b.length + 1;
3617
+ const matrix = Array.from(
3618
+ { length: rows },
3619
+ (_, row) => Array.from({ length: cols }, (_2, column) => row === 0 ? column : column === 0 ? row : 0)
3620
+ );
3621
+ for (let i = 1; i < rows; i += 1) {
3622
+ for (let j = 1; j < cols; j += 1) {
3623
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
3624
+ let value = Math.min(
3625
+ matrix[i - 1][j] + 1,
3626
+ matrix[i][j - 1] + 1,
3627
+ matrix[i - 1][j - 1] + cost
3628
+ );
3629
+ if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
3630
+ value = Math.min(value, matrix[i - 2][j - 2] + 1);
3631
+ }
3632
+ matrix[i][j] = value;
3633
+ }
3634
+ }
3635
+ return matrix[rows - 1][cols - 1];
2617
3636
  }
2618
- function checkDocument(document, options = {}) {
2619
- const glossary = options.glossary ?? [];
2620
- const segmentTermHits = /* @__PURE__ */ new Map();
2621
- if (glossary.length > 0) {
2622
- for (const segment of document.segments) {
2623
- const termOptions = termMatchOptions(document, segment);
2624
- const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
2625
- const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
2626
- segmentTermHits.set(segment.id, {
2627
- source: sourceMatches.hits,
2628
- target: targetMatches.hits
2629
- });
3637
+ function levenshteinDistance(a, b) {
3638
+ if (a === b) return 0;
3639
+ if (a.length === 0) return b.length;
3640
+ if (b.length === 0) return a.length;
3641
+ let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
3642
+ for (let i = 1; i <= a.length; i += 1) {
3643
+ const current = new Array(b.length + 1).fill(0);
3644
+ current[0] = i;
3645
+ for (let j = 1; j <= b.length; j += 1) {
3646
+ const cost = a[i - 1] === b[j - 1] ? 0 : 1;
3647
+ current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
2630
3648
  }
3649
+ previous = current;
2631
3650
  }
2632
- const qaOptions = {
2633
- ...options.glossary === void 0 ? {} : { glossary: options.glossary },
2634
- ...segmentTermHits.size === 0 ? {} : { segmentTermHits },
2635
- ...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules },
2636
- ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
2637
- ...options.locale === void 0 ? {} : { locale: options.locale }
2638
- };
2639
- const qa = runDeterministicChecks(document.segments, qaOptions);
2640
- return { qa, segmentTermHits };
3651
+ return previous[b.length];
3652
+ }
3653
+ function round1(value) {
3654
+ return Math.round(value * 10) / 10;
3655
+ }
3656
+ function similarityOf(source, entry) {
3657
+ if (arraysEqual(source.skeleton, entry.skeleton)) return { similarity: 100, truncated: false };
3658
+ if (source.skeleton.length >= 2 && entry.skeleton.length >= 2) {
3659
+ const distance2 = osaDistance(source.skeleton, entry.skeleton);
3660
+ const denominator2 = Math.max(source.skeleton.length, entry.skeleton.length, 3);
3661
+ return { similarity: round1(100 * (1 - distance2 / denominator2)), truncated: false };
3662
+ }
3663
+ const truncated = source.norm.length > SIMILARITY_TRUNCATE || entry.norm.length > SIMILARITY_TRUNCATE;
3664
+ const a = source.norm.slice(0, SIMILARITY_TRUNCATE);
3665
+ const b = entry.norm.slice(0, SIMILARITY_TRUNCATE);
3666
+ const denominator = Math.max(a.length, b.length, 3);
3667
+ const distance = levenshteinDistance(a, b);
3668
+ return { similarity: round1(100 * (1 - distance / denominator)), truncated };
3669
+ }
3670
+ function matchTypeFor(rate) {
3671
+ if (rate >= 95) return "near_exact";
3672
+ if (rate >= 85) return "high_fuzzy";
3673
+ if (rate >= 75) return "medium_fuzzy";
3674
+ return "low_fuzzy";
3675
+ }
3676
+ function compareContext(provided, stored) {
3677
+ if (!provided || !stored) return { matched: false, both: false };
3678
+ const previousMatches = provided.previousSource !== void 0 && stored.previousSource !== void 0 && normalizeText(provided.previousSource) === normalizeText(stored.previousSource);
3679
+ const nextMatches = provided.nextSource !== void 0 && stored.nextSource !== void 0 && normalizeText(provided.nextSource) === normalizeText(stored.nextSource);
3680
+ return { matched: previousMatches || nextMatches, both: previousMatches && nextMatches };
3681
+ }
3682
+ function compareLastModified(a, b) {
3683
+ const left = a.lastModified ?? "";
3684
+ const right = b.lastModified ?? "";
3685
+ if (left === right) return 0;
3686
+ if (left === "") return 1;
3687
+ if (right === "") return -1;
3688
+ return left < right ? 1 : -1;
3689
+ }
3690
+ function languageAllowed2(entryLanguage, wanted) {
3691
+ if (wanted === void 0) return true;
3692
+ if (entryLanguage === "") return true;
3693
+ return languageMatches(entryLanguage, wanted);
3694
+ }
3695
+ function matchTm(entries, sourceText, options = {}) {
3696
+ const threshold = options.threshold ?? DEFAULT_THRESHOLD;
3697
+ const maxMatches = options.maxMatches ?? DEFAULT_MAX_MATCHES;
3698
+ if (maxMatches <= 0) return [];
3699
+ const source = tokenize(sourceText);
3700
+ const scored = [];
3701
+ entries.forEach((entry, index) => {
3702
+ if (!languageAllowed2(entry.sourceLanguage, options.sourceLanguage)) return;
3703
+ if (!languageAllowed2(entry.targetLanguage, options.targetLanguage)) return;
3704
+ const target = tokenize(entry.source);
3705
+ const structure = compareStructure(source, target, entry);
3706
+ const context = compareContext(options.context, entry.context);
3707
+ const truncation = structure.skeletonEqual ? { similarity: 100, truncated: false } : similarityOf(source, target);
3708
+ let matchRate;
3709
+ let matchType;
3710
+ if (structure.skeletonEqual && structure.numbersEqual && structure.atomsEqual && !structure.punctDiff) {
3711
+ structure.penalties.length = 0;
3712
+ if (context.both) {
3713
+ matchRate = 102;
3714
+ matchType = "double_context";
3715
+ } else if (context.matched) {
3716
+ matchRate = 101;
3717
+ matchType = "context";
3718
+ } else {
3719
+ matchRate = 100;
3720
+ matchType = "exact";
3721
+ }
3722
+ } else if (structure.skeletonEqual) {
3723
+ const penalties = sumPenalties(structure.penalties);
3724
+ const candidate = Math.min(99, Math.round(100 - penalties));
3725
+ if (candidate < 50) return;
3726
+ matchRate = candidate;
3727
+ matchType = matchTypeFor(candidate);
3728
+ } else {
3729
+ const truncationPenalty = truncation.truncated ? TRUNCATION_PENALTY : 0;
3730
+ const adjusted = truncation.similarity - sumPenalties(structure.penalties) - truncationPenalty;
3731
+ if (adjusted < 50) return;
3732
+ const candidate = Math.min(99, Math.round(adjusted));
3733
+ if (candidate < 50) return;
3734
+ matchRate = candidate;
3735
+ matchType = matchTypeFor(candidate);
3736
+ }
3737
+ if (matchRate < threshold) return;
3738
+ scored.push({
3739
+ index,
3740
+ match: {
3741
+ entry,
3742
+ similarity: truncation.similarity,
3743
+ matchRate,
3744
+ matchType,
3745
+ penalties: structure.penalties,
3746
+ contextMatched: context.matched,
3747
+ flags: {
3748
+ numberDiff: structure.numberDiff,
3749
+ tagDiff: structure.tagDiff,
3750
+ punctDiff: structure.punctDiff,
3751
+ truncated: truncation.truncated
3752
+ }
3753
+ }
3754
+ });
3755
+ });
3756
+ scored.sort((left, right) => {
3757
+ const a = left.match;
3758
+ const b = right.match;
3759
+ if (b.matchRate !== a.matchRate) return b.matchRate - a.matchRate;
3760
+ if (b.similarity !== a.similarity) return b.similarity - a.similarity;
3761
+ const modified = compareLastModified(a.entry, b.entry);
3762
+ if (modified !== 0) return modified;
3763
+ if (a.entry.id !== b.entry.id) return a.entry.id < b.entry.id ? -1 : 1;
3764
+ return left.index - right.index;
3765
+ });
3766
+ return scored.slice(0, maxMatches).map((item) => item.match);
2641
3767
  }
2642
3768
 
2643
3769
  // packages/l10n-core/src/formats/bilingual.ts
2644
- import ExcelJS from "exceljs";
3770
+ import ExcelJS2 from "exceljs";
2645
3771
 
2646
3772
  // packages/l10n-core/src/formats/ooxml.ts
2647
- import { unzipSync, zipSync } from "fflate";
3773
+ import { unzipSync as unzipSync2, zipSync } from "fflate";
2648
3774
 
2649
3775
  // packages/l10n-mcp/src/server.ts
2650
3776
  var MAX_BYTES = 200 * 1024 * 1024;
@@ -2660,30 +3786,59 @@ function loadDocument(xml, path) {
2660
3786
  function textResult(value) {
2661
3787
  return { content: [{ type: "text", text: typeof value === "string" ? value : JSON.stringify(value) }] };
2662
3788
  }
3789
+ function parseAssetBaseUrl(raw) {
3790
+ let url;
3791
+ try {
3792
+ url = new URL(raw);
3793
+ } catch {
3794
+ throw new Error("asset base URL is not a valid URL");
3795
+ }
3796
+ if (url.protocol !== "http:" && url.protocol !== "https:") {
3797
+ throw new Error("asset base URL must use http or https");
3798
+ }
3799
+ const hostname = url.hostname.replace(/^\[/u, "").replace(/\]$/u, "").toLowerCase();
3800
+ const loopback = hostname === "127.0.0.1" || hostname === "localhost" || hostname === "::1";
3801
+ if (url.protocol === "http:" && !loopback) {
3802
+ throw new Error("asset base URL must use https unless it points at loopback");
3803
+ }
3804
+ if (url.pathname !== "/" || url.search !== "" || url.hash !== "") {
3805
+ throw new Error("asset base URL must be a bare origin (no path, query, or fragment)");
3806
+ }
3807
+ if (url.username !== "" || url.password !== "") {
3808
+ throw new Error("asset base URL must not embed credentials");
3809
+ }
3810
+ return url;
3811
+ }
2663
3812
  function createL10nMcpServer(options = {}) {
2664
3813
  const server = new McpServer({ name: "langlink-l10n", version: "0.0.0" });
3814
+ if (options.localQaReview) {
3815
+ server.registerTool("l10n_qa_review", {
3816
+ description: "Open the local bilingual QA page. Return its clickable URL, then the user can select files and rules, run checks, and export the results.",
3817
+ inputSchema: {}
3818
+ }, async () => textResult((await openQaReview()).message));
3819
+ }
2665
3820
  server.registerTool(
2666
3821
  "l10n_inspect",
2667
3822
  {
2668
3823
  description: "Inspect a local XLIFF 1.2/2.0/2.1 or SDLXLIFF file (no source/target text returned).",
2669
- inputSchema: { path: z.string().describe("XLIFF/SDLXLIFF file path") }
3824
+ inputSchema: { path: z2.string().describe("XLIFF/SDLXLIFF file path") }
2670
3825
  },
2671
3826
  async ({ path }) => {
2672
- const document = loadDocument(await readUtf8(path), path);
3827
+ const document2 = loadDocument(await readUtf8(path), path);
2673
3828
  let missingTarget = 0;
2674
3829
  let tagMismatch = 0;
2675
- for (const segment of document.segments) {
3830
+ for (const segment of document2.segments) {
2676
3831
  if (!segment.targetRange) missingTarget += 1;
2677
3832
  if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
2678
3833
  }
2679
3834
  return textResult({
2680
- format: document.format,
2681
- version: document.version,
2682
- segments: document.segments.length,
3835
+ format: document2.format,
3836
+ version: document2.version,
3837
+ segments: document2.segments.length,
2683
3838
  missingTarget,
2684
3839
  tagMismatch,
2685
- warnings: document.warnings,
2686
- languages: document.files.map((file) => `${file.sourceLanguage}->${file.targetLanguage}`)
3840
+ warnings: document2.warnings,
3841
+ languages: document2.files.map((file) => `${file.sourceLanguage}->${file.targetLanguage}`)
2687
3842
  });
2688
3843
  }
2689
3844
  );
@@ -2692,19 +3847,19 @@ function createL10nMcpServer(options = {}) {
2692
3847
  {
2693
3848
  description: "Match a local XLIFF/SDLXLIFF against a TMX 1.4b file; returns per-segment match metadata.",
2694
3849
  inputSchema: {
2695
- path: z.string(),
2696
- tm: z.string(),
2697
- threshold: z.number().int().min(0).max(100).optional(),
2698
- maxSegments: z.number().int().min(1).max(500).optional()
3850
+ path: z2.string(),
3851
+ tm: z2.string(),
3852
+ threshold: z2.number().int().min(0).max(100).optional(),
3853
+ maxSegments: z2.number().int().min(1).max(500).optional()
2699
3854
  }
2700
3855
  },
2701
3856
  async ({ path, tm, threshold, maxSegments }) => {
2702
- const document = loadDocument(await readUtf8(path), path);
3857
+ const document2 = loadDocument(await readUtf8(path), path);
2703
3858
  const entries = parseTmx(await readUtf8(tm)).entries;
2704
3859
  if (entries.length === 0) throw new Error("TMX contains no usable entries");
2705
3860
  const limit = maxSegments ?? 50;
2706
- const matches = document.segments.slice(0, limit).map((segment) => {
2707
- const file = document.files[segment.location.fileIndex];
3861
+ const matches = document2.segments.slice(0, limit).map((segment) => {
3862
+ const file = document2.files[segment.location.fileIndex];
2708
3863
  const results = matchTm(entries, segment.sourceText, {
2709
3864
  threshold: threshold ?? 75,
2710
3865
  maxMatches: 1,
@@ -2718,7 +3873,7 @@ function createL10nMcpServer(options = {}) {
2718
3873
  matchType: results[0]?.matchType ?? "no_match"
2719
3874
  };
2720
3875
  });
2721
- return textResult({ segments: document.segments.length, checked: matches.length, matches });
3876
+ return textResult({ segments: document2.segments.length, checked: matches.length, matches });
2722
3877
  }
2723
3878
  );
2724
3879
  server.registerTool(
@@ -2726,19 +3881,19 @@ function createL10nMcpServer(options = {}) {
2726
3881
  {
2727
3882
  description: "Match a local XLIFF/SDLXLIFF against a CSV glossary; returns term hits and forbidden counts.",
2728
3883
  inputSchema: {
2729
- path: z.string(),
2730
- glossary: z.string(),
2731
- maxSegments: z.number().int().min(1).max(500).optional()
3884
+ path: z2.string(),
3885
+ glossary: z2.string(),
3886
+ maxSegments: z2.number().int().min(1).max(500).optional()
2732
3887
  }
2733
3888
  },
2734
3889
  async ({ path, glossary, maxSegments }) => {
2735
- const document = loadDocument(await readUtf8(path), path);
3890
+ const document2 = loadDocument(await readUtf8(path), path);
2736
3891
  const entries = parseGlossaryCsv(await readUtf8(glossary)).entries;
2737
3892
  const limit = maxSegments ?? 50;
2738
3893
  const hits = [];
2739
3894
  let forbidden = 0;
2740
- for (const segment of document.segments.slice(0, limit)) {
2741
- const file = document.files[segment.location.fileIndex];
3895
+ for (const segment of document2.segments.slice(0, limit)) {
3896
+ const file = document2.files[segment.location.fileIndex];
2742
3897
  for (const side of ["source", "target"]) {
2743
3898
  const result = matchTerms(segment[`${side}Text`], entries, {
2744
3899
  ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
@@ -2750,7 +3905,7 @@ function createL10nMcpServer(options = {}) {
2750
3905
  }
2751
3906
  }
2752
3907
  }
2753
- return textResult({ segments: document.segments.length, entries: entries.length, hitCount: hits.length, forbiddenCount: forbidden, hits: hits.slice(0, 200) });
3908
+ return textResult({ segments: document2.segments.length, entries: entries.length, hitCount: hits.length, forbiddenCount: forbidden, hits: hits.slice(0, 200) });
2754
3909
  }
2755
3910
  );
2756
3911
  server.registerTool(
@@ -2758,17 +3913,17 @@ function createL10nMcpServer(options = {}) {
2758
3913
  {
2759
3914
  description: "Run deterministic QA on a local XLIFF/SDLXLIFF, optionally with TMX and CSV glossary.",
2760
3915
  inputSchema: {
2761
- path: z.string(),
2762
- tm: z.string().optional(),
2763
- glossary: z.string().optional(),
2764
- maxIssues: z.number().int().min(1).max(500).optional()
3916
+ path: z2.string(),
3917
+ tm: z2.string().optional(),
3918
+ glossary: z2.string().optional(),
3919
+ maxIssues: z2.number().int().min(1).max(500).optional()
2765
3920
  }
2766
3921
  },
2767
3922
  async ({ path, tm, glossary, maxIssues }) => {
2768
- const document = loadDocument(await readUtf8(path), path);
3923
+ const document2 = loadDocument(await readUtf8(path), path);
2769
3924
  const tmEntries = tm ? parseTmx(await readUtf8(tm)).entries : [];
2770
3925
  const glossaryEntries = glossary ? parseGlossaryCsv(await readUtf8(glossary)).entries : [];
2771
- const result = checkDocument(document, { tmEntries, glossary: glossaryEntries });
3926
+ const result = checkDocument(document2, { tmEntries, glossary: glossaryEntries });
2772
3927
  const limited = maxIssues ?? 100;
2773
3928
  const bySeverity = {};
2774
3929
  for (const issue of result.qa.issues) bySeverity[issue.severity] = (bySeverity[issue.severity] ?? 0) + 1;
@@ -2785,6 +3940,7 @@ function createL10nMcpServer(options = {}) {
2785
3940
  }
2786
3941
  );
2787
3942
  if (options.assetBaseUrl) {
3943
+ const assetBaseUrl = parseAssetBaseUrl(options.assetBaseUrl);
2788
3944
  server.registerTool(
2789
3945
  "l10n_asset_info",
2790
3946
  {
@@ -2792,7 +3948,7 @@ function createL10nMcpServer(options = {}) {
2792
3948
  inputSchema: {}
2793
3949
  },
2794
3950
  async () => {
2795
- const response = await fetch(`${options.assetBaseUrl.replace(/\/$/u, "")}/health`);
3951
+ const response = await fetch(new URL("health", assetBaseUrl), { redirect: "error" });
2796
3952
  if (!response.ok) throw new Error(`asset service returned ${response.status}`);
2797
3953
  return textResult(await response.json());
2798
3954
  }
@@ -2851,6 +4007,15 @@ async function main() {
2851
4007
  console.log("Usage: langlink-l10n-mcp [config ...] | [--http [--host 127.0.0.1] [--port 8765] [--token-env L10N_MCP_HTTP_TOKEN]]");
2852
4008
  return;
2853
4009
  }
4010
+ if (command === "--platform") {
4011
+ const baseUrl = process.env.WORKBENCH_PLATFORM_URL, token = process.env.WORKBENCH_PLATFORM_TOKEN;
4012
+ if (!baseUrl || !token) throw new Error("platform mode requires WORKBENCH_PLATFORM_URL and WORKBENCH_PLATFORM_TOKEN");
4013
+ if (process.argv.includes("--http")) throw new Error("platform mode is stdio-only; never expose one user's platform credential through an HTTP proxy");
4014
+ const server2 = new McpServer({ name: "langlink-cat-platform", version: "1.0.0" });
4015
+ registerPlatformTools(server2, { baseUrl, token });
4016
+ await server2.connect(new StdioServerTransport());
4017
+ return;
4018
+ }
2854
4019
  const http = command === "serve" || command === "--http" || process.argv.includes("--http");
2855
4020
  if (http) {
2856
4021
  const portIndex = process.argv.indexOf("--port");
@@ -2872,6 +4037,7 @@ async function main() {
2872
4037
  return;
2873
4038
  }
2874
4039
  const server = createL10nMcpServer({
4040
+ localQaReview: true,
2875
4041
  ...process.env.L10N_ASSET_BASE_URL ? { assetBaseUrl: process.env.L10N_ASSET_BASE_URL } : {}
2876
4042
  });
2877
4043
  await server.connect(new StdioServerTransport());
@@ -2884,6 +4050,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.ar
2884
4050
  });
2885
4051
  }
2886
4052
  export {
2887
- createL10nMcpServer
4053
+ createL10nMcpServer,
4054
+ parseAssetBaseUrl
2888
4055
  };
2889
4056
  //# sourceMappingURL=index.js.map