@langlink/l10n-mcp 0.2.0 → 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";
@@ -9,6 +163,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
9
163
 
10
164
  // packages/l10n-mcp/src/http.ts
11
165
  import { createServer } from "node:http";
166
+ import { timingSafeEqual } from "node:crypto";
12
167
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
13
168
  var MAX_BODY_BYTES = 10 * 1024 * 1024;
14
169
  function sendJson(response, status, value) {
@@ -32,9 +187,20 @@ async function readBody(request) {
32
187
  if (chunks.length === 0) return void 0;
33
188
  return JSON.parse(Buffer.concat(chunks).toString("utf8"));
34
189
  }
190
+ function timingSafeEqualString(a, b) {
191
+ const aBuf = Buffer.from(a);
192
+ const bBuf = Buffer.from(b);
193
+ if (aBuf.length !== bBuf.length) {
194
+ Buffer.compare(bBuf, bBuf);
195
+ return false;
196
+ }
197
+ return timingSafeEqual(aBuf, bBuf);
198
+ }
35
199
  function authorized(request, token) {
36
200
  if (!token) return true;
37
- return request.headers.authorization === `Bearer ${token}`;
201
+ const header = request.headers.authorization;
202
+ if (typeof header !== "string" || !header.startsWith("Bearer ")) return false;
203
+ return timingSafeEqualString(header.slice("Bearer ".length), token);
38
204
  }
39
205
  function createHttpMcpServer(options) {
40
206
  return createServer(async (request, response) => {
@@ -91,9 +257,18 @@ function createHttpMcpServer(options) {
91
257
  }
92
258
  });
93
259
  }
260
+ function isLoopbackHost(host) {
261
+ const h = host.toLowerCase();
262
+ return h === "127.0.0.1" || h === "::1" || h === "localhost";
263
+ }
94
264
  function listenHttpMcp(options) {
95
265
  const host = options.host ?? "127.0.0.1";
96
266
  const port = options.port ?? 8765;
267
+ if (!options.token && !isLoopbackHost(host)) {
268
+ return Promise.reject(
269
+ new Error("L10N MCP HTTP token is required when binding a non-loopback host")
270
+ );
271
+ }
97
272
  const server = createHttpMcpServer(options);
98
273
  return new Promise((resolvePromise, reject) => {
99
274
  server.once("error", reject);
@@ -261,7 +436,53 @@ async function writeRecipe(recipe) {
261
436
 
262
437
  // packages/l10n-mcp/src/server.ts
263
438
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
264
- 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";
265
486
 
266
487
  // packages/l10n-core/src/xml.ts
267
488
  import { Parser } from "htmlparser2";
@@ -287,6 +508,7 @@ function parseXml(xml) {
287
508
  children: []
288
509
  };
289
510
  const stack = [root];
511
+ let cdata = false;
290
512
  const parser = new Parser(
291
513
  {
292
514
  onopentag(name, attribs) {
@@ -294,11 +516,19 @@ function parseXml(xml) {
294
516
  if (!parent) return;
295
517
  const start = parser.startIndex;
296
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(":")) : "";
297
525
  const node = {
298
526
  type: "element",
299
527
  name,
300
528
  local: localName(name),
301
529
  attribs: { ...attribs },
530
+ namespaceUri: namespaces[prefix] ?? "",
531
+ namespaces,
302
532
  start,
303
533
  openEnd,
304
534
  innerStart: openEnd,
@@ -322,9 +552,16 @@ function parseXml(xml) {
322
552
  parent.children.push({
323
553
  type: "text",
324
554
  text,
555
+ ...cdata ? { cdata: true } : {},
325
556
  start: parser.startIndex,
326
557
  end: parser.endIndex + 1
327
558
  });
559
+ },
560
+ oncdatastart() {
561
+ cdata = true;
562
+ },
563
+ oncdataend() {
564
+ cdata = false;
328
565
  }
329
566
  },
330
567
  {
@@ -339,8 +576,8 @@ function parseXml(xml) {
339
576
  parser.end();
340
577
  return root;
341
578
  }
342
- function validateXml(xml) {
343
- const parser = new SaxesParser({ xmlns: false });
579
+ function validateXml(xml, namespaces = false) {
580
+ const parser = new SaxesParser({ xmlns: namespaces });
344
581
  let error;
345
582
  parser.on("error", (value) => {
346
583
  error = value;
@@ -393,7 +630,7 @@ function decodeXmlEntities(value) {
393
630
  function textOf(node) {
394
631
  let value = "";
395
632
  for (const child of node.children) {
396
- value += child.type === "text" ? decodeXmlEntities(child.text) : textOf(child);
633
+ value += child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : textOf(child);
397
634
  }
398
635
  return value;
399
636
  }
@@ -401,7 +638,167 @@ function elementSignature(node) {
401
638
  return descendants(node).map((element) => element.local);
402
639
  }
403
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
+
404
800
  // packages/l10n-core/src/formats/xliff12.ts
801
+ var SDL_NAMESPACE = "http://sdl.com/FileTypes/SdlXliff/1.0";
405
802
  var SDL_DATATYPE = "x-sdlfilterframework2";
406
803
  function attr(node, name) {
407
804
  return node.attribs[name];
@@ -409,12 +806,12 @@ function attr(node, name) {
409
806
  function isSegMrk(node) {
410
807
  return node.local === "mrk" && node.attribs.mtype === "seg";
411
808
  }
412
- function sdlElement(segDefs) {
413
- if (!segDefs) return void 0;
414
- 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;
415
813
  }
416
- function sdlState(segDefs) {
417
- const seg = sdlElement(segDefs);
814
+ function sdlState(seg) {
418
815
  if (!seg) return void 0;
419
816
  const state = {};
420
817
  const mapping = [
@@ -431,138 +828,152 @@ function sdlState(segDefs) {
431
828
  }
432
829
  return Object.keys(state).length > 0 ? state : void 0;
433
830
  }
434
- function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
831
+ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
435
832
  const sourceTags = sourceContainer ? elementSignature(sourceContainer) : [];
436
833
  const targetTags = targetContainer ? elementSignature(targetContainer) : [];
437
834
  const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
438
835
  if (!targetContainer) warnings.push(`${id}: target missing`);
439
- if (targetContainer && sourceTags.length > 0 && sourceTags.join("|") !== targetTags.join("|")) {
836
+ if (sourceContainer && targetContainer && JSON.stringify(inlineCodeSignatures(sourceContainer).sort()) !== JSON.stringify(inlineCodeSignatures(targetContainer).sort())) {
440
837
  warnings.push(`${id}: source/target inline tag signature differs`);
441
838
  }
442
839
  return {
443
840
  id,
444
- index: segmentIndex,
841
+ index: ordinal,
445
842
  location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
446
843
  sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
447
844
  targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
448
- sourceText: sourceContainer ? textOf(sourceContainer) : "",
449
- targetText: targetContainer ? textOf(targetContainer) : "",
845
+ sourceText: sourceContainer ? inlineText(sourceContainer) : "",
846
+ targetText: targetContainer ? inlineText(targetContainer) : "",
450
847
  sourceTags,
451
848
  targetTags,
452
849
  targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
850
+ ...sourceContainer === void 0 ? {} : { sourceNode: sourceContainer },
453
851
  ...targetContainer === void 0 ? {} : { targetNode: targetContainer },
454
852
  ...sdlNode === void 0 ? {} : { sdlNode },
455
853
  ...sdl === void 0 ? {} : { sdl }
456
854
  };
457
855
  }
458
856
  function parseXliff(xml, options = {}) {
459
- validateXml(xml);
857
+ validateXml(xml, true);
460
858
  const root = elementChildren(parseXml(xml))[0];
461
- if (!root) throw new Error("No root element");
462
- 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");
463
860
  const version = attr(root, "version") ?? "";
464
861
  const isXliff2 = version === "2.0" || version === "2.1";
465
- if (version !== "1.2" && !isXliff2) {
466
- throw new Error(
467
- `Unsupported XLIFF version: ${version || "(missing)"}; supported: 1.2 (SDLXLIFF/MQXLIFF), 2.0 and 2.1`
468
- );
469
- }
470
- const files = [];
471
- const segments = [];
472
- const warnings = [];
473
- const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
474
- const allElements = descendants(root);
475
- const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
476
- 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 = [];
477
869
  const filename = options.filename?.toLowerCase() ?? "";
478
- let format = version === "2.1" ? "xliff21" : version === "2.0" ? "xliff20" : filename.endsWith(".sdlxliff") ? "sdlxliff" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdlElement ? "sdlxliff" : "xliff12";
479
- 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) => {
480
874
  const datatype = attr(fileNode, "datatype");
481
- if (datatype === SDL_DATATYPE) format = "sdlxliff";
482
- const file = {
875
+ if (!isXliff2 && datatype === SDL_DATATYPE) format = "sdlxliff";
876
+ files.push({
483
877
  index: fileIndex,
484
- sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(fileNode, "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
485
- 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") ?? "",
486
880
  ...datatype === void 0 ? {} : { datatype }
487
- };
488
- files.push(file);
489
- const units = descendants(
490
- fileNode,
491
- (candidate) => candidate.local === "trans-unit" || candidate.local === "unit"
492
- );
493
- units.forEach((unit, unitIndex) => {
494
- const unitId = attr(unit, "id") ?? `unit-${unitIndex}`;
495
- if (isXliff2) {
496
- const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
497
- if (segmentNodes.length === 0) {
498
- warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
499
- 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;
500
889
  }
501
- segmentNodes.forEach((segmentNode, segmentIndex) => {
502
- const segmentChildren = elementChildren(segmentNode);
503
- const source = segmentChildren.find((child) => child.local === "source");
504
- const target = segmentChildren.find((child) => child.local === "target");
505
- const segmentId = attr(segmentNode, "id") ?? `seg-${segmentIndex}`;
506
- segments.push(
507
- makeSegment(
508
- fileIndex,
509
- unitId,
510
- segmentIndex,
511
- source,
512
- target,
513
- segmentId,
514
- void 0,
515
- void 0,
516
- xml,
517
- warnings
518
- )
519
- );
520
- });
521
- return;
522
- }
523
- const children = elementChildren(unit);
524
- const sourceElement = children.find((child) => child.local === "source");
525
- const targetElement = children.find((child) => child.local === "target");
526
- const segSourceElement = children.find((child) => child.local === "seg-source");
527
- const segDefsElement = children.find((child) => child.local === "seg-defs");
528
- const sdl = sdlState(segDefsElement);
529
- const sdlNode = sdlElement(segDefsElement);
530
- if (segSourceElement) {
531
- const sourceMrks = descendants(segSourceElement, isSegMrk);
532
- const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
533
- sourceMrks.forEach((sourceMrk, segmentIndex) => {
534
- const mid = attr(sourceMrk, "mid");
535
- const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
536
- if (targetMrk === void 0 && targetMrks.length > 0) {
537
- 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);
538
917
  }
539
- segments.push(
540
- makeSegment(
541
- fileIndex,
542
- unitId,
543
- segmentIndex,
544
- sourceMrk,
545
- targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
546
- mid,
547
- sdl,
548
- sdlNode,
549
- xml,
550
- warnings
551
- )
552
- );
553
- });
554
- return;
555
- }
556
- if (sourceElement || targetElement) {
557
- segments.push(
558
- makeSegment(fileIndex, unitId, 0, 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
559
961
  );
560
- } else {
561
- warnings.push(`${fileIndex}:${unitId}: no source/target`);
962
+ else warnings.push(fileIndex + ":" + unitId + ": no source/target");
562
963
  }
563
- });
964
+ };
965
+ if (isXliff2) walk(fileNode, inherited(fileNode, true));
966
+ else for (const body of children(fileNode, "body")) walk(body, true);
564
967
  });
565
- 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
+ }
566
977
  return { format, version, files, segments, warnings, xml };
567
978
  }
568
979
 
@@ -668,8 +1079,8 @@ function readProperties(props) {
668
1079
  function parseTmx(xml, options = {}) {
669
1080
  const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
670
1081
  validateXml(source);
671
- const document = parseXml(source);
672
- const root = elementChildren(document).find((child) => child.local === "tmx");
1082
+ const document2 = parseXml(source);
1083
+ const root = elementChildren(document2).find((child) => child.local === "tmx");
673
1084
  if (!root) throw new Error("Not a TMX document: missing <tmx> root element");
674
1085
  const warnings = [];
675
1086
  const header = findChild(root, "header");
@@ -728,322 +1139,69 @@ function parseTmx(xml, options = {}) {
728
1139
  return { entries, warnings };
729
1140
  }
730
1141
 
731
- // packages/l10n-core/src/assets/glossary.ts
732
- var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
733
- var TARGET_HEADERS = /* @__PURE__ */ new Set(["target", "target term", "translation", "\u8BD1\u6587", "\u76EE\u6807", "\u76EE\u6807\u8BED"]);
734
- var FORBIDDEN_HEADERS = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
735
- function stripBom(text) {
736
- 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" };
737
1155
  }
738
- function detectDelimiter(text) {
739
- let commas = 0;
740
- let tabs = 0;
741
- let inQuotes = false;
742
- for (let index = 0; index < text.length; index += 1) {
743
- const char = text[index];
744
- if (char === '"') {
745
- if (inQuotes && text[index + 1] === '"') index += 1;
746
- else inQuotes = !inQuotes;
747
- } else if (!inQuotes) {
748
- if (char === "\n") break;
749
- else if (char === ",") commas += 1;
750
- 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;
751
1166
  }
752
- }
753
- return tabs > commas ? " " : ",";
754
- }
755
- function parseCsvRows(text, delimiter = ",") {
756
- const rows = [];
757
- let row = [];
758
- let field = "";
759
- let inQuotes = false;
760
- const pushRow = () => {
761
- row.push(field);
762
- field = "";
763
- if (row.some((cell) => cell.trim() !== "")) rows.push(row);
764
- row = [];
765
- };
766
- for (let index = 0; index < text.length; index += 1) {
767
- const char = text[index];
768
- if (inQuotes) {
769
- if (char === '"') {
770
- if (text[index + 1] === '"') {
771
- field += '"';
772
- index += 1;
773
- } else {
774
- inQuotes = false;
775
- }
776
- } else if (char === "\r") {
777
- field += "\n";
778
- if (text[index + 1] === "\n") index += 1;
779
- } else {
780
- field += char;
781
- }
1167
+ if (!WORD_CHAR.test(first.value)) {
1168
+ index = first.end;
782
1169
  continue;
783
1170
  }
784
- if (char === '"') {
785
- inQuotes = true;
786
- } else if (char === delimiter) {
787
- row.push(field);
788
- field = "";
789
- } else if (char === "\n") {
790
- pushRow();
791
- } else if (char === "\r") {
792
- if (text[index + 1] !== "\n") pushRow();
793
- } else {
794
- 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;
795
1177
  }
1178
+ tokens.push({ value: text.slice(index, end), start: index, end, kind: "word" });
1179
+ index = end;
796
1180
  }
797
- if (field !== "" || row.length > 0) pushRow();
798
- return rows;
1181
+ return tokens;
799
1182
  }
800
- function headerIndex(row, aliases) {
801
- return row.findIndex((cell) => aliases.has(cell.trim().toLowerCase()));
1183
+ function codePointLength(value) {
1184
+ return [...value].length;
802
1185
  }
803
- function isTruthyFlag(value) {
804
- 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";
805
1190
  }
806
- function normalizedHeader(row) {
807
- 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;
808
1203
  }
809
- function parseGlossaryCsv(text, options = {}) {
810
- const content = stripBom(text);
811
- const delimiter = detectDelimiter(content);
812
- const rows = parseCsvRows(content, delimiter);
813
- const warnings = [];
814
- if (rows.length === 0) return { entries: [], warnings: ["glossary: no rows found"] };
815
- let sourceColumn = 0;
816
- let targetColumn = 1;
817
- let forbiddenColumn = 2;
818
- let dataRows = rows;
819
- const first = rows[0];
820
- if (first) {
821
- const header = normalizedHeader(first);
822
- const sourceIndex = headerIndex(header, SOURCE_HEADERS);
823
- const targetIndex = headerIndex(header, TARGET_HEADERS);
824
- if (sourceIndex >= 0 && targetIndex >= 0) {
825
- sourceColumn = sourceIndex;
826
- targetColumn = targetIndex;
827
- const forbiddenIndex = headerIndex(header, FORBIDDEN_HEADERS);
828
- forbiddenColumn = forbiddenIndex >= 0 ? forbiddenIndex : void 0;
829
- const known = /* @__PURE__ */ new Set([sourceIndex, targetIndex]);
830
- if (forbiddenIndex >= 0) known.add(forbiddenIndex);
831
- header.forEach((cell, index) => {
832
- if (cell !== "" && !known.has(index)) {
833
- warnings.push(`glossary: ignored column "${first[index]}"`);
834
- }
835
- });
836
- dataRows = rows.slice(1);
837
- } else if (sourceIndex >= 0 || targetIndex >= 0) {
838
- warnings.push("glossary: header is missing a source or target column; using positional columns");
839
- sourceColumn = sourceIndex >= 0 ? sourceIndex : 0;
840
- targetColumn = targetIndex >= 0 ? targetIndex : sourceColumn === 0 ? 1 : 0;
841
- forbiddenColumn = void 0;
842
- dataRows = rows.slice(1);
843
- }
844
- }
845
- const entries = [];
846
- dataRows.forEach((row, rowIndex) => {
847
- const position = rowIndex + 1;
848
- const source = (row[sourceColumn] ?? "").trim();
849
- const target = (row[targetColumn] ?? "").trim();
850
- if (source === "" && target === "") return;
851
- if (source === "" || target === "") {
852
- warnings.push(`glossary: row ${position} is missing ${source === "" ? "source" : "target"}; skipped`);
853
- return;
854
- }
855
- const forbiddenCell = forbiddenColumn === void 0 ? "" : row[forbiddenColumn] ?? "";
856
- const forbidden = isTruthyFlag(forbiddenCell);
857
- entries.push({
858
- id: `glossary-${entries.length + 1}`,
859
- source,
860
- target,
861
- ...options.sourceLanguage === void 0 ? {} : { sourceLanguage: options.sourceLanguage },
862
- ...options.targetLanguage === void 0 ? {} : { targetLanguage: options.targetLanguage },
863
- ...forbidden ? { forbidden: true } : {}
864
- });
865
- });
866
- return { entries, warnings };
867
- }
868
-
869
- // packages/l10n-core/src/assets/tm-matcher.ts
870
- var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
871
- var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
872
- var DOUBLE_QUOTES = /[\u201c\u201d\u201e\u201f\u2033]/gu;
873
- var DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
874
- var TERMINAL_PUNCTUATION = /[.!?\u2026;:\u3002\uff01\uff1f\uff1b\uff1a]$/u;
875
- var CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
876
- var WORD_CHAR = /[\p{L}\p{N}_]/u;
877
- var COMBINING_MARK = new RegExp("\\p{M}", "u");
878
- var TAG_TOKEN = /^<\/?[A-Za-z_][\w.:-]*(?:\s+[^<>]*?)?\s*\/?>/u;
879
- var PLACEHOLDER_TOKEN = /^(?:\$\{[^{}]*\}|%[0-9]+\$[sdifuxX]|%[-+ 0#]*[0-9]*(?:\.[0-9]+)?[sdifuxXc]|\{[^{}]*\})/u;
880
- var URL_TOKEN = /^(?:(?:https?|ftp):\/\/|www\.)[^\s<>"'{}]+/u;
881
- var EMAIL_TOKEN = /^[\w.+-]+@[\w-]+(?:\.[\w-]+)+/u;
882
- var PATH_TOKEN = /^(?:(?:[A-Za-z]:[\\/]|\\\\|~[\\/])[^\s<>"'{}]+|\/(?:[^\s<>"'{}]*\/)[^\s<>"'{}]*)/u;
883
- var NUMBER_TOKEN = new RegExp("^\\p{Nd}+(?:[.,]\\p{Nd}+)?", "u");
884
- var SIMILARITY_TRUNCATE = 200;
885
- var TRUNCATION_PENALTY = 7;
886
- var DEFAULT_THRESHOLD = 50;
887
- var DEFAULT_MAX_MATCHES = 3;
888
- function normalizeText(text) {
889
- return text.normalize("NFKC").normalize("NFC").replace(ZERO_WIDTH, "").replace(QUOTES, "'").replace(DOUBLE_QUOTES, '"').replace(DASHES, "-").replace(/\u2026/gu, "...").replace(/\s+/gu, " ").trim().toLowerCase();
890
- }
891
- function matchAtomic(rest) {
892
- const tag = TAG_TOKEN.exec(rest);
893
- if (tag) return { kind: "tag", raw: tag[0], skeleton: "<TAG>" };
894
- const placeholder = PLACEHOLDER_TOKEN.exec(rest);
895
- if (placeholder) return { kind: "placeholder", raw: placeholder[0], skeleton: "<PH>" };
896
- const url = URL_TOKEN.exec(rest);
897
- if (url) return { kind: "atomic", raw: url[0], skeleton: url[0] };
898
- const email = EMAIL_TOKEN.exec(rest);
899
- if (email) return { kind: "atomic", raw: email[0], skeleton: email[0] };
900
- const path = PATH_TOKEN.exec(rest);
901
- if (path) return { kind: "atomic", raw: path[0], skeleton: path[0] };
902
- return void 0;
903
- }
904
- function tokenize(text) {
905
- const norm = normalizeText(text);
906
- const tokens = [];
907
- let index = 0;
908
- while (index < norm.length) {
909
- const rest = norm.slice(index);
910
- const atomic = matchAtomic(rest);
911
- if (atomic) {
912
- tokens.push(atomic);
913
- index += atomic.raw.length;
914
- continue;
915
- }
916
- const char = norm[index];
917
- if (CJK_CHAR.test(char)) {
918
- tokens.push({ kind: "cjk", raw: char, skeleton: char });
919
- index += 1;
920
- continue;
921
- }
922
- const number2 = NUMBER_TOKEN.exec(rest);
923
- if (number2) {
924
- tokens.push({ kind: "number", raw: number2[0], skeleton: "<NUM>" });
925
- index += number2[0].length;
926
- continue;
927
- }
928
- if (WORD_CHAR.test(char)) {
929
- let end = index + 1;
930
- while (end < norm.length) {
931
- const next = norm[end];
932
- if (CJK_CHAR.test(next)) break;
933
- if (WORD_CHAR.test(next) || COMBINING_MARK.test(next)) {
934
- end += 1;
935
- continue;
936
- }
937
- break;
938
- }
939
- const raw = norm.slice(index, end);
940
- tokens.push({ kind: "word", raw, skeleton: raw });
941
- index = end;
942
- continue;
943
- }
944
- index += 1;
945
- }
946
- return {
947
- tokens,
948
- skeleton: tokens.map((token) => token.skeleton),
949
- numbers: tokens.filter((token) => token.kind === "number").map((token) => token.raw),
950
- atoms: tokens.filter((token) => token.kind === "tag" || token.kind === "placeholder").map((token) => `${token.kind}:${token.raw}`),
951
- norm,
952
- terminalPunct: TERMINAL_PUNCTUATION.test(norm)
953
- };
954
- }
955
- function arraysEqual(a, b) {
956
- if (a.length !== b.length) return false;
957
- return a.every((value, index) => value === b[index]);
958
- }
959
- function tokensEqual(a, b) {
960
- if (a.length !== b.length) return false;
961
- return a.every(
962
- (token, index) => token.kind === b[index].kind && token.raw === b[index].raw
963
- );
964
- }
965
- function firstDifference(a, b) {
966
- for (let index = 0; index < Math.min(a.length, b.length); index += 1) {
967
- if (a[index] !== b[index]) return `${a[index]} vs ${b[index]}`;
968
- }
969
- return `${a.length} vs ${b.length}`;
970
- }
971
- function compareStructure(source, entry, tmEntry) {
972
- const skeletonEqual = arraysEqual(source.skeleton, entry.skeleton);
973
- const numbersEqual = arraysEqual(source.numbers, entry.numbers);
974
- const atomsEqual = arraysEqual(source.atoms, entry.atoms);
975
- const numberDiff = !numbersEqual;
976
- const tagDiff = !atomsEqual;
977
- const punctDiff = source.terminalPunct !== entry.terminalPunct || tokensEqual(source.tokens, entry.tokens) && source.norm !== entry.norm;
978
- const penalties = [];
979
- if (numberDiff) {
980
- penalties.push({
981
- kind: "number",
982
- points: 2,
983
- detail: `${source.numbers.join(", ") || "none"} vs ${entry.numbers.join(", ") || "none"}`
984
- });
985
- }
986
- if (tagDiff) {
987
- if (source.atoms.length !== entry.atoms.length) {
988
- penalties.push({
989
- kind: "tag_count",
990
- points: 10,
991
- detail: `${source.atoms.length} vs ${entry.atoms.length} tags/placeholders`
992
- });
993
- } else {
994
- penalties.push({
995
- kind: "tag_id",
996
- points: 3,
997
- detail: firstDifference(source.atoms, entry.atoms)
998
- });
999
- }
1000
- }
1001
- if (punctDiff) {
1002
- penalties.push({
1003
- kind: "punctuation",
1004
- points: 1,
1005
- detail: "difference is limited to punctuation/whitespace"
1006
- });
1007
- }
1008
- if (tmEntry.fuzzy === true) {
1009
- penalties.push({ kind: "fuzzy", points: 5, detail: "TM entry is flagged as fuzzy" });
1010
- }
1011
- if (tmEntry.origin === "alignment") {
1012
- penalties.push({
1013
- kind: "unaligned",
1014
- points: 2,
1015
- detail: "alignment entry (default unaligned penalty)"
1016
- });
1017
- }
1018
- return { skeletonEqual, numbersEqual, atomsEqual, numberDiff, tagDiff, punctDiff, penalties };
1019
- }
1020
- function sumPenalties(penalties) {
1021
- return penalties.reduce((total, penalty) => total + penalty.points, 0);
1022
- }
1023
- function osaDistance(a, b) {
1024
- const rows = a.length + 1;
1025
- const cols = b.length + 1;
1026
- const matrix = Array.from(
1027
- { length: rows },
1028
- (_, row) => Array.from({ length: cols }, (_2, column) => row === 0 ? column : column === 0 ? row : 0)
1029
- );
1030
- for (let i = 1; i < rows; i += 1) {
1031
- for (let j = 1; j < cols; j += 1) {
1032
- const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1033
- let value = Math.min(
1034
- matrix[i - 1][j] + 1,
1035
- matrix[i][j - 1] + 1,
1036
- matrix[i - 1][j - 1] + cost
1037
- );
1038
- if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
1039
- value = Math.min(value, matrix[i - 2][j - 2] + 1);
1040
- }
1041
- matrix[i][j] = value;
1042
- }
1043
- }
1044
- return matrix[rows - 1][cols - 1];
1045
- }
1046
- function levenshteinDistance(a, b) {
1204
+ function levenshtein(a, b) {
1047
1205
  if (a === b) return 0;
1048
1206
  if (a.length === 0) return b.length;
1049
1207
  if (b.length === 0) return a.length;
@@ -1059,208 +1217,14 @@ function levenshteinDistance(a, b) {
1059
1217
  }
1060
1218
  return previous[b.length];
1061
1219
  }
1062
- function round1(value) {
1063
- return Math.round(value * 10) / 10;
1064
- }
1065
- function similarityOf(source, entry) {
1066
- if (arraysEqual(source.skeleton, entry.skeleton)) return { similarity: 100, truncated: false };
1067
- if (source.skeleton.length >= 2 && entry.skeleton.length >= 2) {
1068
- const distance2 = osaDistance(source.skeleton, entry.skeleton);
1069
- const denominator2 = Math.max(source.skeleton.length, entry.skeleton.length, 3);
1070
- return { similarity: round1(100 * (1 - distance2 / denominator2)), truncated: false };
1071
- }
1072
- const truncated = source.norm.length > SIMILARITY_TRUNCATE || entry.norm.length > SIMILARITY_TRUNCATE;
1073
- const a = source.norm.slice(0, SIMILARITY_TRUNCATE);
1074
- const b = entry.norm.slice(0, SIMILARITY_TRUNCATE);
1075
- const denominator = Math.max(a.length, b.length, 3);
1076
- const distance = levenshteinDistance(a, b);
1077
- return { similarity: round1(100 * (1 - distance / denominator)), truncated };
1078
- }
1079
- function matchTypeFor(rate) {
1080
- if (rate >= 95) return "near_exact";
1081
- if (rate >= 85) return "high_fuzzy";
1082
- if (rate >= 75) return "medium_fuzzy";
1083
- return "low_fuzzy";
1084
- }
1085
- function compareContext(provided, stored) {
1086
- if (!provided || !stored) return { matched: false, both: false };
1087
- const previousMatches = provided.previousSource !== void 0 && stored.previousSource !== void 0 && normalizeText(provided.previousSource) === normalizeText(stored.previousSource);
1088
- const nextMatches = provided.nextSource !== void 0 && stored.nextSource !== void 0 && normalizeText(provided.nextSource) === normalizeText(stored.nextSource);
1089
- return { matched: previousMatches || nextMatches, both: previousMatches && nextMatches };
1090
- }
1091
- function compareLastModified(a, b) {
1092
- const left = a.lastModified ?? "";
1093
- const right = b.lastModified ?? "";
1094
- if (left === right) return 0;
1095
- if (left === "") return 1;
1096
- if (right === "") return -1;
1097
- 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;
1098
1224
  }
1099
1225
  function languageAllowed(entryLanguage, wanted) {
1100
1226
  if (wanted === void 0) return true;
1101
- if (entryLanguage === "") return true;
1102
- return languageMatches(entryLanguage, wanted);
1103
- }
1104
- function matchTm(entries, sourceText, options = {}) {
1105
- const threshold = options.threshold ?? DEFAULT_THRESHOLD;
1106
- const maxMatches = options.maxMatches ?? DEFAULT_MAX_MATCHES;
1107
- if (maxMatches <= 0) return [];
1108
- const source = tokenize(sourceText);
1109
- const scored = [];
1110
- entries.forEach((entry, index) => {
1111
- if (!languageAllowed(entry.sourceLanguage, options.sourceLanguage)) return;
1112
- if (!languageAllowed(entry.targetLanguage, options.targetLanguage)) return;
1113
- const target = tokenize(entry.source);
1114
- const structure = compareStructure(source, target, entry);
1115
- const context = compareContext(options.context, entry.context);
1116
- const truncation = structure.skeletonEqual ? { similarity: 100, truncated: false } : similarityOf(source, target);
1117
- let matchRate;
1118
- let matchType;
1119
- if (structure.skeletonEqual && structure.numbersEqual && structure.atomsEqual && !structure.punctDiff) {
1120
- structure.penalties.length = 0;
1121
- if (context.both) {
1122
- matchRate = 102;
1123
- matchType = "double_context";
1124
- } else if (context.matched) {
1125
- matchRate = 101;
1126
- matchType = "context";
1127
- } else {
1128
- matchRate = 100;
1129
- matchType = "exact";
1130
- }
1131
- } else if (structure.skeletonEqual) {
1132
- const penalties = sumPenalties(structure.penalties);
1133
- const candidate = Math.min(99, Math.round(100 - penalties));
1134
- if (candidate < 50) return;
1135
- matchRate = candidate;
1136
- matchType = matchTypeFor(candidate);
1137
- } else {
1138
- const truncationPenalty = truncation.truncated ? TRUNCATION_PENALTY : 0;
1139
- const adjusted = truncation.similarity - sumPenalties(structure.penalties) - truncationPenalty;
1140
- if (adjusted < 50) return;
1141
- const candidate = Math.min(99, Math.round(adjusted));
1142
- if (candidate < 50) return;
1143
- matchRate = candidate;
1144
- matchType = matchTypeFor(candidate);
1145
- }
1146
- if (matchRate < threshold) return;
1147
- scored.push({
1148
- index,
1149
- match: {
1150
- entry,
1151
- similarity: truncation.similarity,
1152
- matchRate,
1153
- matchType,
1154
- penalties: structure.penalties,
1155
- contextMatched: context.matched,
1156
- flags: {
1157
- numberDiff: structure.numberDiff,
1158
- tagDiff: structure.tagDiff,
1159
- punctDiff: structure.punctDiff,
1160
- truncated: truncation.truncated
1161
- }
1162
- }
1163
- });
1164
- });
1165
- scored.sort((left, right) => {
1166
- const a = left.match;
1167
- const b = right.match;
1168
- if (b.matchRate !== a.matchRate) return b.matchRate - a.matchRate;
1169
- if (b.similarity !== a.similarity) return b.similarity - a.similarity;
1170
- const modified = compareLastModified(a.entry, b.entry);
1171
- if (modified !== 0) return modified;
1172
- if (a.entry.id !== b.entry.id) return a.entry.id < b.entry.id ? -1 : 1;
1173
- return left.index - right.index;
1174
- });
1175
- return scored.slice(0, maxMatches).map((item) => item.match);
1176
- }
1177
-
1178
- // packages/l10n-core/src/assets/term-matcher.ts
1179
- var CJK_CHAR2 = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
1180
- var WORD_CHAR2 = /[\p{L}\p{N}_]/u;
1181
- var COMBINING_MARK2 = new RegExp("\\p{M}", "u");
1182
- var MAX_TERM_TOKENS = 8;
1183
- var FUZZY_THRESHOLD = 0.8;
1184
- function readToken(text, index) {
1185
- const codePoint = text.codePointAt(index);
1186
- if (codePoint === void 0) return void 0;
1187
- const char = String.fromCodePoint(codePoint);
1188
- const end = index + char.length;
1189
- if (CJK_CHAR2.test(char)) return { value: char, start: index, end, kind: "cjk" };
1190
- return { value: char, start: index, end, kind: "word" };
1191
- }
1192
- function tokenizeText(text) {
1193
- const tokens = [];
1194
- let index = 0;
1195
- while (index < text.length) {
1196
- const first = readToken(text, index);
1197
- if (!first) break;
1198
- if (first.kind === "cjk") {
1199
- tokens.push(first);
1200
- index = first.end;
1201
- continue;
1202
- }
1203
- if (!WORD_CHAR2.test(first.value)) {
1204
- index = first.end;
1205
- continue;
1206
- }
1207
- let end = first.end;
1208
- while (end < text.length) {
1209
- const next = readToken(text, end);
1210
- if (!next || next.kind === "cjk") break;
1211
- if (!WORD_CHAR2.test(next.value) && !COMBINING_MARK2.test(next.value)) break;
1212
- end = next.end;
1213
- }
1214
- tokens.push({ value: text.slice(index, end), start: index, end, kind: "word" });
1215
- index = end;
1216
- }
1217
- return tokens;
1218
- }
1219
- function codePointLength(value) {
1220
- return [...value].length;
1221
- }
1222
- function caseModeOf(entry) {
1223
- if (entry.caseSensitive === true) return "yes";
1224
- if (entry.caseSensitive === false) return "no";
1225
- return "permissive";
1226
- }
1227
- function caseMatches(term, text, mode) {
1228
- if (term.length !== text.length) return false;
1229
- if (mode === "yes") return term === text;
1230
- if (mode === "no") return term.toLowerCase() === text.toLowerCase();
1231
- for (let index = 0; index < term.length; index += 1) {
1232
- const termChar = term[index];
1233
- const textChar = text[index];
1234
- if (termChar === textChar) continue;
1235
- if (termChar.toLowerCase() !== textChar.toLowerCase()) return false;
1236
- if (termChar !== termChar.toLowerCase()) return false;
1237
- }
1238
- return true;
1239
- }
1240
- function levenshtein(a, b) {
1241
- if (a === b) return 0;
1242
- if (a.length === 0) return b.length;
1243
- if (b.length === 0) return a.length;
1244
- let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
1245
- for (let i = 1; i <= a.length; i += 1) {
1246
- const current = new Array(b.length + 1).fill(0);
1247
- current[0] = i;
1248
- for (let j = 1; j <= b.length; j += 1) {
1249
- const cost = a[i - 1] === b[j - 1] ? 0 : 1;
1250
- current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
1251
- }
1252
- previous = current;
1253
- }
1254
- return previous[b.length];
1255
- }
1256
- function similarity(a, b) {
1257
- const longest = Math.max(a.length, b.length);
1258
- if (longest === 0) return 1;
1259
- return 1 - levenshtein(a, b) / longest;
1260
- }
1261
- function languageAllowed2(entryLanguage, wanted) {
1262
- if (wanted === void 0) return true;
1263
- if (entryLanguage === void 0 || entryLanguage === "") return true;
1227
+ if (entryLanguage === void 0 || entryLanguage === "") return true;
1264
1228
  return languageMatches(entryLanguage, wanted);
1265
1229
  }
1266
1230
  function scanExactOrPrefix(tokens, termTokens, caseMode, options) {
@@ -1340,8 +1304,8 @@ function matchTerms(text, entries, options = {}) {
1340
1304
  warnings.push(message);
1341
1305
  };
1342
1306
  for (const entry of entries) {
1343
- if (!languageAllowed2(entry.sourceLanguage, options.sourceLanguage)) continue;
1344
- if (!languageAllowed2(entry.targetLanguage, options.targetLanguage)) continue;
1307
+ if (!languageAllowed(entry.sourceLanguage, options.sourceLanguage)) continue;
1308
+ if (!languageAllowed(entry.targetLanguage, options.targetLanguage)) continue;
1345
1309
  const caseMode = caseModeOf(entry);
1346
1310
  const mode = entry.matching ?? "prefix50";
1347
1311
  const forms = /* @__PURE__ */ new Set();
@@ -1421,7 +1385,7 @@ function matchTerms(text, entries, options = {}) {
1421
1385
  return { hits, issues: [], warnings };
1422
1386
  }
1423
1387
 
1424
- // packages/l10n-core/src/qa/rules.ts
1388
+ // packages/l10n-core/src/qa/catalog.ts
1425
1389
  var RULES = [
1426
1390
  // structural class
1427
1391
  { id: "EMPTY_TARGET", severity: "critical" },
@@ -1436,8 +1400,11 @@ var RULES = [
1436
1400
  // content class
1437
1401
  { id: "NUMBER_MISMATCH", severity: "major" },
1438
1402
  { id: "DATE_MISMATCH", severity: "major" },
1403
+ { id: "DATE_FORMAT", severity: "minor", requiresLocale: true },
1439
1404
  { id: "DUPLICATE_WORD", severity: "major" },
1440
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" },
1441
1408
  { id: "SOURCE_LEFTOVER", severity: "major" },
1442
1409
  { id: "PUNCT_DUP", severity: "major" },
1443
1410
  { id: "TRAILING_COLON", severity: "minor" },
@@ -1447,9 +1414,24 @@ var RULES = [
1447
1414
  { id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
1448
1415
  { id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
1449
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
1450
1428
  var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
1451
1429
  var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
1452
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
+ );
1453
1435
  var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
1454
1436
  function normalizeForCompare(value) {
1455
1437
  return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
@@ -1563,15 +1545,16 @@ function checkTags(context) {
1563
1545
  const sourceTags = context.segment.sourceTags;
1564
1546
  const targetTags = context.segment.targetTags;
1565
1547
  if (sourceTags.length === 0 && targetTags.length === 0) return;
1566
- const sourceSignature = sourceTags.join("|");
1567
- const targetSignature = targetTags.join("|");
1568
- if (sourceSignature === targetSignature) return;
1569
- 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;
1570
1553
  draft(context, {
1571
1554
  ruleId: "TAG_MISMATCH",
1572
- 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(", ")}]`,
1573
- evidence: `source tags: [${sourceTags.join(", ")}], target tags: [${targetTags.join(", ")}]`,
1574
- 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"
1575
1558
  });
1576
1559
  }
1577
1560
  var URL_PATTERNS = [
@@ -1637,7 +1620,7 @@ var MONTHS = new Map(
1637
1620
  );
1638
1621
  var MONTH_ALIASES = new Map([
1639
1622
  ...MONTHS,
1640
- ...Array.from(MONTHS, ([month, number2]) => [month.slice(0, 3), number2]),
1623
+ ...Array.from(MONTHS, ([month, number3]) => [month.slice(0, 3), number3]),
1641
1624
  ["sept", 9]
1642
1625
  ]);
1643
1626
  var CJK_DIGITS = /* @__PURE__ */ new Map([
@@ -1662,9 +1645,10 @@ function validMonthDay(month, day) {
1662
1645
  var DATE_PATTERNS = [
1663
1646
  /\b(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})\b/gu,
1664
1647
  /\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b/gu,
1665
- /(\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
1666
1649
  ];
1667
1650
  function extractDates(value) {
1651
+ value = normalizeNumericText(value);
1668
1652
  const tokens = [];
1669
1653
  const push = (key, text, start) => {
1670
1654
  tokens.push({ key, text, span: { start, end: start + text.length } });
@@ -1762,7 +1746,7 @@ function extractDates(value) {
1762
1746
  }
1763
1747
  function maskSpans(value, spans) {
1764
1748
  if (spans.length === 0) return value;
1765
- const chars = Array.from(value);
1749
+ const chars = value.split("");
1766
1750
  for (const span of spans) {
1767
1751
  for (let index = span.start; index < span.end && index < chars.length; index += 1) chars[index] = " ";
1768
1752
  }
@@ -1782,10 +1766,14 @@ function normalizeNumber(raw) {
1782
1766
  if (/^\d+\.0+$/u.test(value)) value = value.replace(/\.0+$/u, "");
1783
1767
  return `${sign}${value}`;
1784
1768
  }
1769
+ function normalizeNumericText(value) {
1770
+ return value.replace(/[0-9.,+-]/gu, (character) => character.normalize("NFKC"));
1771
+ }
1785
1772
  function extractNumbers(value) {
1773
+ const normalized = normalizeNumericText(value);
1786
1774
  const masked = maskSpans(
1787
- value,
1788
- extractDates(value).map((token) => token.span)
1775
+ normalized,
1776
+ [...extractDates(normalized).map((token) => token.span), ...collectPlaceholders(normalized)]
1789
1777
  );
1790
1778
  const results = [];
1791
1779
  for (const match of masked.matchAll(NUMBER_RE)) results.push(normalizeNumber(match[0]));
@@ -1824,6 +1812,47 @@ function checkDates(context) {
1824
1812
  ...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
1825
1813
  });
1826
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
+ }
1827
1856
  var BRACKET_PAIRS = /* @__PURE__ */ new Map([
1828
1857
  ["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
1829
1858
  ["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
@@ -2189,18 +2218,23 @@ function checkWhitespace(context) {
2189
2218
  function checkFullwidthHalfwidth(context) {
2190
2219
  const target = context.target;
2191
2220
  if (target.trim().length === 0) return;
2192
- const fullwidth = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/u.exec(target);
2193
- if (fullwidth && fullwidth.index !== void 0) {
2221
+ const exempt = new Set(context.locale?.fullwidthExemptChars ?? []);
2222
+ const fullwidthPattern = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/gu;
2223
+ for (const match of target.matchAll(fullwidthPattern)) {
2224
+ const char = match[0];
2225
+ if (exempt.has(char)) continue;
2226
+ const index = match.index ?? 0;
2194
2227
  draft(context, {
2195
2228
  ruleId: "FULLWIDTH_HALFWIDTH",
2196
2229
  message: "\u8BD1\u6587\u4E2D\u5B57\u6BCD/\u6570\u5B57\u4F7F\u7528\u5168\u89D2\u5F62\u5F0F",
2197
- evidence: `found '${fullwidth[0]}'`,
2230
+ evidence: `found '${char}'`,
2198
2231
  suggestion: "\u5B57\u6BCD\u4E0E\u6570\u5B57\u901A\u5E38\u4F7F\u7528\u534A\u89D2\u5F62\u5F0F",
2199
- targetRange: { start: fullwidth.index, end: fullwidth.index + 1 }
2232
+ targetRange: { start: index, end: index + 1 }
2200
2233
  });
2201
2234
  return;
2202
2235
  }
2203
- if (!context.targetIsCjk) return;
2236
+ const expectFullwidthPunctuation = context.locale?.fullwidthPunctuation ?? context.targetIsCjk;
2237
+ if (!expectFullwidthPunctuation) return;
2204
2238
  const mixed = /[\u4e00-\u9fff][,;:!?]|[,;:!?][\u4e00-\u9fff]/u.exec(target);
2205
2239
  if (mixed === null || mixed.index === void 0) return;
2206
2240
  draft(context, {
@@ -2304,6 +2338,91 @@ function checkTermRules(context) {
2304
2338
  });
2305
2339
  }
2306
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
+ }
2307
2426
  var CHECK_GROUPS = [
2308
2427
  { ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
2309
2428
  { ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
@@ -2314,6 +2433,7 @@ var CHECK_GROUPS = [
2314
2433
  { ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
2315
2434
  { ids: ["NUMBER_MISMATCH"], run: checkNumbers },
2316
2435
  { ids: ["DATE_MISMATCH"], run: checkDates },
2436
+ { ids: ["DATE_FORMAT"], run: checkDateFormat },
2317
2437
  { ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
2318
2438
  { ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
2319
2439
  { ids: ["PUNCT_DUP"], run: checkPunctDup },
@@ -2328,20 +2448,31 @@ function checkEmptyTargetGroup(context) {
2328
2448
  if (source.trim().length === 0) return;
2329
2449
  const targetMissing = context.segment.targetRange === null && context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2330
2450
  const empty = context.target.trim().length === 0 && context.targetInner.trim().length === 0;
2331
- 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;
2332
2455
  draft(context, {
2333
2456
  ruleId: "EMPTY_TARGET",
2334
- 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",
2335
2458
  evidence: `source: ${truncate(source)}`
2336
2459
  });
2337
2460
  }
2338
2461
  function checkUntranslatedCopyGroup(context) {
2339
2462
  const { source, target } = context;
2340
2463
  if (source.trim().length === 0 || target.trim().length === 0) return;
2341
- if (normalizeForCompare(source) !== normalizeForCompare(target)) return;
2464
+ const normalizedSource = normalizeForCompare(source);
2465
+ if (normalizedSource !== normalizeForCompare(target)) return;
2466
+ const policy = context.untranslatedCopy;
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;
2342
2469
  const tokens = wordTokens(source);
2343
2470
  const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
2344
- if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= 4) return;
2471
+ const maxShort = policy?.shortSegmentMaxLength ?? 4;
2472
+ if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= maxShort) return;
2473
+ if (tokens.length <= 1 && policy?.singleTokenExemptions?.some((entry) => normalizeForCompare(entry) === normalizedSource)) {
2474
+ return;
2475
+ }
2345
2476
  draft(context, {
2346
2477
  ruleId: "UNTRANSLATED_COPY",
2347
2478
  message: "\u8BD1\u6587\u4E0E\u6E90\u6587\u5B8C\u5168\u76F8\u540C\uFF08\u672A\u7FFB\u8BD1\uFF09",
@@ -2369,6 +2500,7 @@ function isApplicable(ruleId, segment) {
2369
2500
  case "UNPAIRED_BRACKET":
2370
2501
  case "NUMBER_MISMATCH":
2371
2502
  case "DATE_MISMATCH":
2503
+ case "DATE_FORMAT":
2372
2504
  case "DUPLICATE_WORD":
2373
2505
  case "TERM_MISSING":
2374
2506
  case "SOURCE_LEFTOVER":
@@ -2395,6 +2527,7 @@ function resolveEnabledRules(options) {
2395
2527
  return enabled2;
2396
2528
  }
2397
2529
  const enabled = new Set(DEFAULT_ENABLED);
2530
+ if (options.locale?.targetLocale === void 0) for (const ruleId of LOCALE_RULE_IDS) enabled.delete(ruleId);
2398
2531
  if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
2399
2532
  return enabled;
2400
2533
  }
@@ -2419,6 +2552,8 @@ function buildContext(segment, options, glossary) {
2419
2552
  targetIsCjk: isCjkHeavy(segment.targetText),
2420
2553
  glossary,
2421
2554
  drafts: [],
2555
+ ...options.locale === void 0 ? {} : { locale: options.locale },
2556
+ ...options.untranslatedCopy === void 0 ? {} : { untranslatedCopy: options.untranslatedCopy },
2422
2557
  ...termHits === void 0 ? {} : { termHits }
2423
2558
  };
2424
2559
  }
@@ -2454,6 +2589,14 @@ function runDeterministicChecks(segments, options = {}) {
2454
2589
  const state = /* @__PURE__ */ new Map();
2455
2590
  for (const rule of RULES) {
2456
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
+ }
2457
2600
  if (rule.requiresTerms === true && !termAvailable) {
2458
2601
  state.set(rule.id, {
2459
2602
  status: "skipped",
@@ -2470,6 +2613,7 @@ function runDeterministicChecks(segments, options = {}) {
2470
2613
  const applicable = /* @__PURE__ */ new Set();
2471
2614
  for (const ruleId of enabled) {
2472
2615
  if (state.get(ruleId)?.status !== "executed") continue;
2616
+ if (DOCUMENT_RULE_IDS.has(ruleId)) continue;
2473
2617
  if (isApplicable(ruleId, segment)) applicable.add(ruleId);
2474
2618
  }
2475
2619
  if (applicable.size === 0) continue;
@@ -2508,103 +2652,1125 @@ function runDeterministicChecks(segments, options = {}) {
2508
2652
  issues.push(toIssue(draft2, segment, draft2.ruleId));
2509
2653
  }
2510
2654
  }
2511
- const rules = [];
2512
- for (const rule of RULES) {
2513
- const execution = state.get(rule.id);
2514
- if (execution === void 0) continue;
2515
- const status = execution.status === "executed" && execution.checkedSegments === 0 ? "not_applicable" : execution.status;
2516
- rules.push({
2517
- ruleId: rule.id,
2518
- status,
2519
- reason: status === "not_applicable" ? "no applicable segments" : execution.reason,
2520
- 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"
2521
3597
  });
2522
3598
  }
2523
- issues.sort(compareIssues);
2524
- const coverageIncomplete = rules.some((rule) => rule.status === "skipped" || rule.status === "failed");
2525
- const anyExecuted = rules.some(
2526
- (rule) => rule.status === "executed" || rule.status === "not_applicable"
2527
- );
2528
- const checkedSegments = anyExecuted ? segments.length : 0;
2529
- return {
2530
- issues,
2531
- totalSegments: segments.length,
2532
- checkedSegments,
2533
- coverage: coverageIncomplete ? "incomplete" : "complete",
2534
- rules
2535
- };
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 };
2536
3610
  }
2537
-
2538
- // packages/l10n-core/src/qa/report.ts
2539
- function renderJson(result) {
2540
- const issues = result.issues.map((issue) => ({
2541
- ruleId: issue.ruleId,
2542
- severity: issue.severity,
2543
- segmentId: issue.segmentId,
2544
- segmentIndex: issue.segmentIndex,
2545
- message: issue.message,
2546
- evidence: issue.evidence,
2547
- ...issue.suggestion === void 0 ? {} : { suggestion: issue.suggestion },
2548
- ...issue.sourceRange === void 0 ? {} : { sourceRange: { start: issue.sourceRange.start, end: issue.sourceRange.end } },
2549
- ...issue.targetRange === void 0 ? {} : { targetRange: { start: issue.targetRange.start, end: issue.targetRange.end } },
2550
- ...issue.termId === void 0 ? {} : { termId: issue.termId },
2551
- origin: issue.origin
2552
- }));
2553
- return JSON.stringify(
2554
- {
2555
- totalSegments: result.totalSegments,
2556
- checkedSegments: result.checkedSegments,
2557
- coverage: result.coverage,
2558
- issueCount: issues.length,
2559
- issues,
2560
- rules: result.rules.map((rule) => ({
2561
- ruleId: rule.ruleId,
2562
- status: rule.status,
2563
- reason: rule.reason,
2564
- checkedSegments: rule.checkedSegments
2565
- }))
2566
- },
2567
- null,
2568
- 2
2569
- );
3611
+ function sumPenalties(penalties) {
3612
+ return penalties.reduce((total, penalty) => total + penalty.points, 0);
2570
3613
  }
2571
-
2572
- // packages/l10n-core/src/workflows/check.ts
2573
- function termMatchOptions(document, segment) {
2574
- const file = document.files[segment.location.fileIndex];
2575
- const options = {};
2576
- if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
2577
- if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
2578
- 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];
2579
3636
  }
2580
- function checkDocument(document, options = {}) {
2581
- const glossary = options.glossary ?? [];
2582
- const segmentTermHits = /* @__PURE__ */ new Map();
2583
- if (glossary.length > 0) {
2584
- for (const segment of document.segments) {
2585
- const termOptions = termMatchOptions(document, segment);
2586
- const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
2587
- const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
2588
- segmentTermHits.set(segment.id, {
2589
- source: sourceMatches.hits,
2590
- target: targetMatches.hits
2591
- });
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);
2592
3648
  }
3649
+ previous = current;
2593
3650
  }
2594
- const qaOptions = {
2595
- ...options.glossary === void 0 ? {} : { glossary: options.glossary },
2596
- ...segmentTermHits.size === 0 ? {} : { segmentTermHits },
2597
- ...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules }
2598
- };
2599
- const qa = runDeterministicChecks(document.segments, qaOptions);
2600
- 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);
2601
3767
  }
2602
3768
 
2603
3769
  // packages/l10n-core/src/formats/bilingual.ts
2604
- import ExcelJS from "exceljs";
3770
+ import ExcelJS2 from "exceljs";
2605
3771
 
2606
3772
  // packages/l10n-core/src/formats/ooxml.ts
2607
- import { unzipSync, zipSync } from "fflate";
3773
+ import { unzipSync as unzipSync2, zipSync } from "fflate";
2608
3774
 
2609
3775
  // packages/l10n-mcp/src/server.ts
2610
3776
  var MAX_BYTES = 200 * 1024 * 1024;
@@ -2620,30 +3786,59 @@ function loadDocument(xml, path) {
2620
3786
  function textResult(value) {
2621
3787
  return { content: [{ type: "text", text: typeof value === "string" ? value : JSON.stringify(value) }] };
2622
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
+ }
2623
3812
  function createL10nMcpServer(options = {}) {
2624
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
+ }
2625
3820
  server.registerTool(
2626
3821
  "l10n_inspect",
2627
3822
  {
2628
3823
  description: "Inspect a local XLIFF 1.2/2.0/2.1 or SDLXLIFF file (no source/target text returned).",
2629
- inputSchema: { path: z.string().describe("XLIFF/SDLXLIFF file path") }
3824
+ inputSchema: { path: z2.string().describe("XLIFF/SDLXLIFF file path") }
2630
3825
  },
2631
3826
  async ({ path }) => {
2632
- const document = loadDocument(await readUtf8(path), path);
3827
+ const document2 = loadDocument(await readUtf8(path), path);
2633
3828
  let missingTarget = 0;
2634
3829
  let tagMismatch = 0;
2635
- for (const segment of document.segments) {
3830
+ for (const segment of document2.segments) {
2636
3831
  if (!segment.targetRange) missingTarget += 1;
2637
3832
  if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
2638
3833
  }
2639
3834
  return textResult({
2640
- format: document.format,
2641
- version: document.version,
2642
- segments: document.segments.length,
3835
+ format: document2.format,
3836
+ version: document2.version,
3837
+ segments: document2.segments.length,
2643
3838
  missingTarget,
2644
3839
  tagMismatch,
2645
- warnings: document.warnings,
2646
- languages: document.files.map((file) => `${file.sourceLanguage}->${file.targetLanguage}`)
3840
+ warnings: document2.warnings,
3841
+ languages: document2.files.map((file) => `${file.sourceLanguage}->${file.targetLanguage}`)
2647
3842
  });
2648
3843
  }
2649
3844
  );
@@ -2652,19 +3847,19 @@ function createL10nMcpServer(options = {}) {
2652
3847
  {
2653
3848
  description: "Match a local XLIFF/SDLXLIFF against a TMX 1.4b file; returns per-segment match metadata.",
2654
3849
  inputSchema: {
2655
- path: z.string(),
2656
- tm: z.string(),
2657
- threshold: z.number().int().min(0).max(100).optional(),
2658
- 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()
2659
3854
  }
2660
3855
  },
2661
3856
  async ({ path, tm, threshold, maxSegments }) => {
2662
- const document = loadDocument(await readUtf8(path), path);
3857
+ const document2 = loadDocument(await readUtf8(path), path);
2663
3858
  const entries = parseTmx(await readUtf8(tm)).entries;
2664
3859
  if (entries.length === 0) throw new Error("TMX contains no usable entries");
2665
3860
  const limit = maxSegments ?? 50;
2666
- const matches = document.segments.slice(0, limit).map((segment) => {
2667
- 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];
2668
3863
  const results = matchTm(entries, segment.sourceText, {
2669
3864
  threshold: threshold ?? 75,
2670
3865
  maxMatches: 1,
@@ -2678,7 +3873,7 @@ function createL10nMcpServer(options = {}) {
2678
3873
  matchType: results[0]?.matchType ?? "no_match"
2679
3874
  };
2680
3875
  });
2681
- return textResult({ segments: document.segments.length, checked: matches.length, matches });
3876
+ return textResult({ segments: document2.segments.length, checked: matches.length, matches });
2682
3877
  }
2683
3878
  );
2684
3879
  server.registerTool(
@@ -2686,19 +3881,19 @@ function createL10nMcpServer(options = {}) {
2686
3881
  {
2687
3882
  description: "Match a local XLIFF/SDLXLIFF against a CSV glossary; returns term hits and forbidden counts.",
2688
3883
  inputSchema: {
2689
- path: z.string(),
2690
- glossary: z.string(),
2691
- 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()
2692
3887
  }
2693
3888
  },
2694
3889
  async ({ path, glossary, maxSegments }) => {
2695
- const document = loadDocument(await readUtf8(path), path);
3890
+ const document2 = loadDocument(await readUtf8(path), path);
2696
3891
  const entries = parseGlossaryCsv(await readUtf8(glossary)).entries;
2697
3892
  const limit = maxSegments ?? 50;
2698
3893
  const hits = [];
2699
3894
  let forbidden = 0;
2700
- for (const segment of document.segments.slice(0, limit)) {
2701
- 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];
2702
3897
  for (const side of ["source", "target"]) {
2703
3898
  const result = matchTerms(segment[`${side}Text`], entries, {
2704
3899
  ...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
@@ -2710,7 +3905,7 @@ function createL10nMcpServer(options = {}) {
2710
3905
  }
2711
3906
  }
2712
3907
  }
2713
- 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) });
2714
3909
  }
2715
3910
  );
2716
3911
  server.registerTool(
@@ -2718,17 +3913,17 @@ function createL10nMcpServer(options = {}) {
2718
3913
  {
2719
3914
  description: "Run deterministic QA on a local XLIFF/SDLXLIFF, optionally with TMX and CSV glossary.",
2720
3915
  inputSchema: {
2721
- path: z.string(),
2722
- tm: z.string().optional(),
2723
- glossary: z.string().optional(),
2724
- 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()
2725
3920
  }
2726
3921
  },
2727
3922
  async ({ path, tm, glossary, maxIssues }) => {
2728
- const document = loadDocument(await readUtf8(path), path);
3923
+ const document2 = loadDocument(await readUtf8(path), path);
2729
3924
  const tmEntries = tm ? parseTmx(await readUtf8(tm)).entries : [];
2730
3925
  const glossaryEntries = glossary ? parseGlossaryCsv(await readUtf8(glossary)).entries : [];
2731
- const result = checkDocument(document, { tmEntries, glossary: glossaryEntries });
3926
+ const result = checkDocument(document2, { tmEntries, glossary: glossaryEntries });
2732
3927
  const limited = maxIssues ?? 100;
2733
3928
  const bySeverity = {};
2734
3929
  for (const issue of result.qa.issues) bySeverity[issue.severity] = (bySeverity[issue.severity] ?? 0) + 1;
@@ -2745,6 +3940,7 @@ function createL10nMcpServer(options = {}) {
2745
3940
  }
2746
3941
  );
2747
3942
  if (options.assetBaseUrl) {
3943
+ const assetBaseUrl = parseAssetBaseUrl(options.assetBaseUrl);
2748
3944
  server.registerTool(
2749
3945
  "l10n_asset_info",
2750
3946
  {
@@ -2752,7 +3948,7 @@ function createL10nMcpServer(options = {}) {
2752
3948
  inputSchema: {}
2753
3949
  },
2754
3950
  async () => {
2755
- const response = await fetch(`${options.assetBaseUrl.replace(/\/$/u, "")}/health`);
3951
+ const response = await fetch(new URL("health", assetBaseUrl), { redirect: "error" });
2756
3952
  if (!response.ok) throw new Error(`asset service returned ${response.status}`);
2757
3953
  return textResult(await response.json());
2758
3954
  }
@@ -2811,6 +4007,15 @@ async function main() {
2811
4007
  console.log("Usage: langlink-l10n-mcp [config ...] | [--http [--host 127.0.0.1] [--port 8765] [--token-env L10N_MCP_HTTP_TOKEN]]");
2812
4008
  return;
2813
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
+ }
2814
4019
  const http = command === "serve" || command === "--http" || process.argv.includes("--http");
2815
4020
  if (http) {
2816
4021
  const portIndex = process.argv.indexOf("--port");
@@ -2832,6 +4037,7 @@ async function main() {
2832
4037
  return;
2833
4038
  }
2834
4039
  const server = createL10nMcpServer({
4040
+ localQaReview: true,
2835
4041
  ...process.env.L10N_ASSET_BASE_URL ? { assetBaseUrl: process.env.L10N_ASSET_BASE_URL } : {}
2836
4042
  });
2837
4043
  await server.connect(new StdioServerTransport());
@@ -2844,6 +4050,7 @@ if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.ar
2844
4050
  });
2845
4051
  }
2846
4052
  export {
2847
- createL10nMcpServer
4053
+ createL10nMcpServer,
4054
+ parseAssetBaseUrl
2848
4055
  };
2849
4056
  //# sourceMappingURL=index.js.map