@langlink/l10n-mcp 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +35 -2
- package/dist/index.js +2702 -0
- package/dist/index.js.map +7 -0
- package/package.json +35 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,2702 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// packages/l10n-mcp/src/server.ts
|
|
4
|
+
import { realpathSync } from "node:fs";
|
|
5
|
+
import { readFile as readFile2 } from "node:fs/promises";
|
|
6
|
+
import { basename, resolve } from "node:path";
|
|
7
|
+
import { pathToFileURL } from "node:url";
|
|
8
|
+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
|
9
|
+
|
|
10
|
+
// packages/l10n-mcp/src/config.ts
|
|
11
|
+
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
import { dirname, join } from "node:path";
|
|
14
|
+
var SERVER_NAME = "langlink";
|
|
15
|
+
var PACKAGE = "@langlink/l10n-mcp";
|
|
16
|
+
function stdio(name) {
|
|
17
|
+
return { command: "npx", args: ["-y", PACKAGE] };
|
|
18
|
+
}
|
|
19
|
+
function json(value) {
|
|
20
|
+
return `${JSON.stringify(value, null, 2)}
|
|
21
|
+
`;
|
|
22
|
+
}
|
|
23
|
+
function clientRecipe(client, home = homedir()) {
|
|
24
|
+
switch (client) {
|
|
25
|
+
case "codex":
|
|
26
|
+
return {
|
|
27
|
+
client,
|
|
28
|
+
path: join(home, ".codex/config.toml"),
|
|
29
|
+
format: "toml",
|
|
30
|
+
content: `[mcp_servers.${SERVER_NAME}]
|
|
31
|
+
command = "npx"
|
|
32
|
+
args = ["-y", "${PACKAGE}"]
|
|
33
|
+
startup_timeout_sec = 30
|
|
34
|
+
tool_timeout_sec = 120
|
|
35
|
+
`,
|
|
36
|
+
notes: [
|
|
37
|
+
"Codex CLI reads this from ~/.codex/config.toml.",
|
|
38
|
+
"Equivalent command: codex mcp add langlink -- npx -y @langlink/l10n-mcp"
|
|
39
|
+
]
|
|
40
|
+
};
|
|
41
|
+
case "workbuddy":
|
|
42
|
+
return {
|
|
43
|
+
client,
|
|
44
|
+
path: join(home, ".workbuddy/mcp.json"),
|
|
45
|
+
format: "json",
|
|
46
|
+
content: json({ mcpServers: { [SERVER_NAME]: stdio(SERVER_NAME) } }),
|
|
47
|
+
notes: ["WorkBuddy also exposes MCP configuration in its UI."]
|
|
48
|
+
};
|
|
49
|
+
case "zcode":
|
|
50
|
+
return {
|
|
51
|
+
client,
|
|
52
|
+
path: join(home, ".zcode/cli/config.json"),
|
|
53
|
+
format: "json",
|
|
54
|
+
content: json({ mcp: { servers: { [SERVER_NAME]: { ...stdio(SERVER_NAME), env: {} } } } }),
|
|
55
|
+
notes: [
|
|
56
|
+
"ZCode user config is ~/.zcode/cli/config.json with mcp.servers.",
|
|
57
|
+
"Alternatively merge the generic mcpServers shape into ~/.agents/mcp.json."
|
|
58
|
+
]
|
|
59
|
+
};
|
|
60
|
+
case "trae":
|
|
61
|
+
return {
|
|
62
|
+
client,
|
|
63
|
+
path: null,
|
|
64
|
+
format: "json",
|
|
65
|
+
content: json({ mcpServers: { [SERVER_NAME]: stdio(SERVER_NAME) } }),
|
|
66
|
+
notes: [
|
|
67
|
+
"Trae manages MCP servers through its MCP settings UI; paste the JSON snippet there.",
|
|
68
|
+
"No stable user-level config path was verified, so this command never writes Trae config."
|
|
69
|
+
]
|
|
70
|
+
};
|
|
71
|
+
case "agents":
|
|
72
|
+
return {
|
|
73
|
+
client,
|
|
74
|
+
path: join(home, ".agents/mcp.json"),
|
|
75
|
+
format: "json",
|
|
76
|
+
content: json({ mcpServers: { [SERVER_NAME]: stdio(SERVER_NAME) } }),
|
|
77
|
+
notes: ["Shared ~/.agents/mcp.json convention; ZCode can import this file."]
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function objectAt(value, key) {
|
|
82
|
+
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
83
|
+
throw new Error(`${key} must be a JSON object`);
|
|
84
|
+
}
|
|
85
|
+
return value;
|
|
86
|
+
}
|
|
87
|
+
async function mergeJson(recipe, target) {
|
|
88
|
+
let existing = {};
|
|
89
|
+
let exists = false;
|
|
90
|
+
try {
|
|
91
|
+
existing = objectAt(JSON.parse(await readFile(target, "utf8")), target);
|
|
92
|
+
exists = true;
|
|
93
|
+
} catch (error) {
|
|
94
|
+
if (error.code !== "ENOENT") throw error;
|
|
95
|
+
}
|
|
96
|
+
if (exists) {
|
|
97
|
+
const backup = `${target}.bak.${Date.now()}`;
|
|
98
|
+
await copyFile(target, backup);
|
|
99
|
+
if (recipe.client === "zcode" || recipe.client === "workbuddy" || recipe.client === "agents") {
|
|
100
|
+
const path = recipe.client === "zcode" ? ["mcp", "servers"] : ["mcpServers"];
|
|
101
|
+
let node = existing;
|
|
102
|
+
for (const key of path.slice(0, -1)) {
|
|
103
|
+
node = objectAt(node[key] ??= {}, key);
|
|
104
|
+
}
|
|
105
|
+
const servers = objectAt(node[path[path.length - 1]] ??= {}, path[path.length - 1]);
|
|
106
|
+
const next = objectAt(JSON.parse(recipe.content), recipe.client);
|
|
107
|
+
const nextServers = recipe.client === "zcode" ? objectAt(objectAt(next.mcp, "mcp").servers, "servers") : objectAt(next.mcpServers, "mcpServers");
|
|
108
|
+
const candidate = nextServers[SERVER_NAME];
|
|
109
|
+
const current = JSON.stringify(servers[SERVER_NAME]);
|
|
110
|
+
if (current === JSON.stringify(candidate)) {
|
|
111
|
+
return { client: recipe.client, path: target, action: "unchanged" };
|
|
112
|
+
}
|
|
113
|
+
servers[SERVER_NAME] = candidate;
|
|
114
|
+
await writeFile(target, json(existing), "utf8");
|
|
115
|
+
return { client: recipe.client, path: target, action: "merged", backup };
|
|
116
|
+
}
|
|
117
|
+
throw new Error(`refusing to rewrite existing non-JSON config: ${target}`);
|
|
118
|
+
}
|
|
119
|
+
await mkdir(dirname(target), { recursive: true });
|
|
120
|
+
await writeFile(target, recipe.content, "utf8");
|
|
121
|
+
return { client: recipe.client, path: target, action: "created" };
|
|
122
|
+
}
|
|
123
|
+
async function writeRecipe(recipe) {
|
|
124
|
+
if (!recipe.path) {
|
|
125
|
+
return { client: recipe.client, path: "", action: "manual" };
|
|
126
|
+
}
|
|
127
|
+
if (recipe.format === "toml") {
|
|
128
|
+
try {
|
|
129
|
+
await readFile(recipe.path, "utf8");
|
|
130
|
+
return { client: recipe.client, path: recipe.path, action: "manual" };
|
|
131
|
+
} catch (error) {
|
|
132
|
+
if (error.code !== "ENOENT") throw error;
|
|
133
|
+
}
|
|
134
|
+
await mkdir(dirname(recipe.path), { recursive: true });
|
|
135
|
+
await writeFile(recipe.path, recipe.content, "utf8");
|
|
136
|
+
return { client: recipe.client, path: recipe.path, action: "created" };
|
|
137
|
+
}
|
|
138
|
+
return mergeJson(recipe, recipe.path);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// packages/l10n-mcp/src/server.ts
|
|
142
|
+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
|
143
|
+
import * as z from "zod/v4";
|
|
144
|
+
|
|
145
|
+
// packages/l10n-core/src/xml.ts
|
|
146
|
+
import { Parser } from "htmlparser2";
|
|
147
|
+
import { SaxesParser } from "saxes";
|
|
148
|
+
function localName(name) {
|
|
149
|
+
const index = name.lastIndexOf(":");
|
|
150
|
+
return index < 0 ? name : name.slice(index + 1);
|
|
151
|
+
}
|
|
152
|
+
function isElement(node) {
|
|
153
|
+
return node.type === "element";
|
|
154
|
+
}
|
|
155
|
+
function parseXml(xml) {
|
|
156
|
+
const root = {
|
|
157
|
+
type: "element",
|
|
158
|
+
name: "#root",
|
|
159
|
+
local: "#root",
|
|
160
|
+
attribs: {},
|
|
161
|
+
start: 0,
|
|
162
|
+
openEnd: 0,
|
|
163
|
+
innerStart: 0,
|
|
164
|
+
innerEnd: xml.length,
|
|
165
|
+
end: xml.length,
|
|
166
|
+
children: []
|
|
167
|
+
};
|
|
168
|
+
const stack = [root];
|
|
169
|
+
const parser = new Parser(
|
|
170
|
+
{
|
|
171
|
+
onopentag(name, attribs) {
|
|
172
|
+
const parent = stack[stack.length - 1];
|
|
173
|
+
if (!parent) return;
|
|
174
|
+
const start = parser.startIndex;
|
|
175
|
+
const openEnd = parser.endIndex + 1;
|
|
176
|
+
const node = {
|
|
177
|
+
type: "element",
|
|
178
|
+
name,
|
|
179
|
+
local: localName(name),
|
|
180
|
+
attribs: { ...attribs },
|
|
181
|
+
start,
|
|
182
|
+
openEnd,
|
|
183
|
+
innerStart: openEnd,
|
|
184
|
+
innerEnd: openEnd,
|
|
185
|
+
end: openEnd,
|
|
186
|
+
children: []
|
|
187
|
+
};
|
|
188
|
+
parent.children.push(node);
|
|
189
|
+
stack.push(node);
|
|
190
|
+
},
|
|
191
|
+
onclosetag() {
|
|
192
|
+
const node = stack.pop();
|
|
193
|
+
if (!node || node === root) return;
|
|
194
|
+
const closeStart = parser.startIndex;
|
|
195
|
+
node.innerEnd = closeStart > node.openEnd ? closeStart : node.openEnd;
|
|
196
|
+
node.end = Math.max(node.openEnd, parser.endIndex + 1);
|
|
197
|
+
},
|
|
198
|
+
ontext(text) {
|
|
199
|
+
const parent = stack[stack.length - 1];
|
|
200
|
+
if (!parent) return;
|
|
201
|
+
parent.children.push({
|
|
202
|
+
type: "text",
|
|
203
|
+
text,
|
|
204
|
+
start: parser.startIndex,
|
|
205
|
+
end: parser.endIndex + 1
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
xmlMode: true,
|
|
211
|
+
decodeEntities: false,
|
|
212
|
+
lowerCaseTags: false,
|
|
213
|
+
lowerCaseAttributeNames: false,
|
|
214
|
+
recognizeSelfClosing: true
|
|
215
|
+
}
|
|
216
|
+
);
|
|
217
|
+
parser.write(xml);
|
|
218
|
+
parser.end();
|
|
219
|
+
return root;
|
|
220
|
+
}
|
|
221
|
+
function validateXml(xml) {
|
|
222
|
+
const parser = new SaxesParser({ xmlns: false });
|
|
223
|
+
let error;
|
|
224
|
+
parser.on("error", (value) => {
|
|
225
|
+
error = value;
|
|
226
|
+
});
|
|
227
|
+
try {
|
|
228
|
+
parser.write(xml).close();
|
|
229
|
+
} catch (caught) {
|
|
230
|
+
error = caught instanceof Error ? caught : new Error(String(caught));
|
|
231
|
+
}
|
|
232
|
+
if (error) throw new Error(`Invalid XML: ${error.message}`);
|
|
233
|
+
}
|
|
234
|
+
function elementChildren(node) {
|
|
235
|
+
return node.children.filter(isElement);
|
|
236
|
+
}
|
|
237
|
+
function descendants(node, predicate = () => true) {
|
|
238
|
+
const result = [];
|
|
239
|
+
const walk = (parent) => {
|
|
240
|
+
for (const child of parent.children) {
|
|
241
|
+
if (!isElement(child)) continue;
|
|
242
|
+
if (predicate(child)) result.push(child);
|
|
243
|
+
walk(child);
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
walk(node);
|
|
247
|
+
return result;
|
|
248
|
+
}
|
|
249
|
+
function decodeXmlEntities(value) {
|
|
250
|
+
return value.replace(
|
|
251
|
+
/&(?:#x([0-9a-fA-F]+)|#(\d+)|(amp|lt|gt|quot|apos));/g,
|
|
252
|
+
(_match, hex, dec, named) => {
|
|
253
|
+
if (hex !== void 0) return String.fromCodePoint(Number.parseInt(hex, 16));
|
|
254
|
+
if (dec !== void 0) return String.fromCodePoint(Number.parseInt(dec, 10));
|
|
255
|
+
switch (named) {
|
|
256
|
+
case "amp":
|
|
257
|
+
return "&";
|
|
258
|
+
case "lt":
|
|
259
|
+
return "<";
|
|
260
|
+
case "gt":
|
|
261
|
+
return ">";
|
|
262
|
+
case "quot":
|
|
263
|
+
return '"';
|
|
264
|
+
case "apos":
|
|
265
|
+
return "'";
|
|
266
|
+
default:
|
|
267
|
+
return _match;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
);
|
|
271
|
+
}
|
|
272
|
+
function textOf(node) {
|
|
273
|
+
let value = "";
|
|
274
|
+
for (const child of node.children) {
|
|
275
|
+
value += child.type === "text" ? decodeXmlEntities(child.text) : textOf(child);
|
|
276
|
+
}
|
|
277
|
+
return value;
|
|
278
|
+
}
|
|
279
|
+
function elementSignature(node) {
|
|
280
|
+
return descendants(node).map((element) => element.local);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// packages/l10n-core/src/formats/xliff12.ts
|
|
284
|
+
var SDL_DATATYPE = "x-sdlfilterframework2";
|
|
285
|
+
function attr(node, name) {
|
|
286
|
+
return node.attribs[name];
|
|
287
|
+
}
|
|
288
|
+
function isSegMrk(node) {
|
|
289
|
+
return node.local === "mrk" && node.attribs.mtype === "seg";
|
|
290
|
+
}
|
|
291
|
+
function sdlElement(segDefs) {
|
|
292
|
+
if (!segDefs) return void 0;
|
|
293
|
+
return elementChildren(segDefs).find((child) => child.local === "seg");
|
|
294
|
+
}
|
|
295
|
+
function sdlState(segDefs) {
|
|
296
|
+
const seg = sdlElement(segDefs);
|
|
297
|
+
if (!seg) return void 0;
|
|
298
|
+
const state = {};
|
|
299
|
+
const mapping = [
|
|
300
|
+
["id", "id"],
|
|
301
|
+
["conf", "conf"],
|
|
302
|
+
["origin", "origin"],
|
|
303
|
+
["percent", "percent"],
|
|
304
|
+
["textMatch", "text-match"],
|
|
305
|
+
["locked", "locked"]
|
|
306
|
+
];
|
|
307
|
+
for (const [key, source] of mapping) {
|
|
308
|
+
const value = seg.attribs[source];
|
|
309
|
+
if (value !== void 0) state[key] = value;
|
|
310
|
+
}
|
|
311
|
+
return Object.keys(state).length > 0 ? state : void 0;
|
|
312
|
+
}
|
|
313
|
+
function makeSegment(fileIndex, unitId, segmentIndex, sourceContainer, targetContainer, mid, sdl, sdlNode, xml, warnings) {
|
|
314
|
+
const sourceTags = sourceContainer ? elementSignature(sourceContainer) : [];
|
|
315
|
+
const targetTags = targetContainer ? elementSignature(targetContainer) : [];
|
|
316
|
+
const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
|
|
317
|
+
if (!targetContainer) warnings.push(`${id}: target missing`);
|
|
318
|
+
if (targetContainer && sourceTags.length > 0 && sourceTags.join("|") !== targetTags.join("|")) {
|
|
319
|
+
warnings.push(`${id}: source/target inline tag signature differs`);
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
id,
|
|
323
|
+
index: segmentIndex,
|
|
324
|
+
location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
|
|
325
|
+
sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
|
|
326
|
+
targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
|
|
327
|
+
sourceText: sourceContainer ? textOf(sourceContainer) : "",
|
|
328
|
+
targetText: targetContainer ? textOf(targetContainer) : "",
|
|
329
|
+
sourceTags,
|
|
330
|
+
targetTags,
|
|
331
|
+
targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
|
|
332
|
+
...targetContainer === void 0 ? {} : { targetNode: targetContainer },
|
|
333
|
+
...sdlNode === void 0 ? {} : { sdlNode },
|
|
334
|
+
...sdl === void 0 ? {} : { sdl }
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function parseXliff(xml, options = {}) {
|
|
338
|
+
validateXml(xml);
|
|
339
|
+
const root = elementChildren(parseXml(xml))[0];
|
|
340
|
+
if (!root) throw new Error("No root element");
|
|
341
|
+
if (root.local !== "xliff") throw new Error(`Not an XLIFF document: <${root.name}>`);
|
|
342
|
+
const version = attr(root, "version") ?? "";
|
|
343
|
+
const isXliff2 = version === "2.0" || version === "2.1";
|
|
344
|
+
if (version !== "1.2" && !isXliff2) {
|
|
345
|
+
throw new Error(
|
|
346
|
+
`Unsupported XLIFF version: ${version || "(missing)"}; supported: 1.2 (SDLXLIFF/MQXLIFF), 2.0 and 2.1`
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
const files = [];
|
|
350
|
+
const segments = [];
|
|
351
|
+
const warnings = [];
|
|
352
|
+
const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
|
|
353
|
+
const allElements = descendants(root);
|
|
354
|
+
const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
|
|
355
|
+
const fileNodes = allElements.filter((element) => element.local === "file");
|
|
356
|
+
const filename = options.filename?.toLowerCase() ?? "";
|
|
357
|
+
let format = version === "2.1" ? "xliff21" : version === "2.0" ? "xliff20" : filename.endsWith(".sdlxliff") ? "sdlxliff" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdlElement ? "sdlxliff" : "xliff12";
|
|
358
|
+
fileNodes.forEach((fileNode, fileIndex) => {
|
|
359
|
+
const datatype = attr(fileNode, "datatype");
|
|
360
|
+
if (datatype === SDL_DATATYPE) format = "sdlxliff";
|
|
361
|
+
const file = {
|
|
362
|
+
index: fileIndex,
|
|
363
|
+
sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(fileNode, "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
|
|
364
|
+
targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(fileNode, "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
|
|
365
|
+
...datatype === void 0 ? {} : { datatype }
|
|
366
|
+
};
|
|
367
|
+
files.push(file);
|
|
368
|
+
const units = descendants(
|
|
369
|
+
fileNode,
|
|
370
|
+
(candidate) => candidate.local === "trans-unit" || candidate.local === "unit"
|
|
371
|
+
);
|
|
372
|
+
units.forEach((unit, unitIndex) => {
|
|
373
|
+
const unitId = attr(unit, "id") ?? `unit-${unitIndex}`;
|
|
374
|
+
if (isXliff2) {
|
|
375
|
+
const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
|
|
376
|
+
if (segmentNodes.length === 0) {
|
|
377
|
+
warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
segmentNodes.forEach((segmentNode, segmentIndex) => {
|
|
381
|
+
const segmentChildren = elementChildren(segmentNode);
|
|
382
|
+
const source = segmentChildren.find((child) => child.local === "source");
|
|
383
|
+
const target = segmentChildren.find((child) => child.local === "target");
|
|
384
|
+
const segmentId = attr(segmentNode, "id") ?? `seg-${segmentIndex}`;
|
|
385
|
+
segments.push(
|
|
386
|
+
makeSegment(
|
|
387
|
+
fileIndex,
|
|
388
|
+
unitId,
|
|
389
|
+
segmentIndex,
|
|
390
|
+
source,
|
|
391
|
+
target,
|
|
392
|
+
segmentId,
|
|
393
|
+
void 0,
|
|
394
|
+
void 0,
|
|
395
|
+
xml,
|
|
396
|
+
warnings
|
|
397
|
+
)
|
|
398
|
+
);
|
|
399
|
+
});
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
const children = elementChildren(unit);
|
|
403
|
+
const sourceElement = children.find((child) => child.local === "source");
|
|
404
|
+
const targetElement = children.find((child) => child.local === "target");
|
|
405
|
+
const segSourceElement = children.find((child) => child.local === "seg-source");
|
|
406
|
+
const segDefsElement = children.find((child) => child.local === "seg-defs");
|
|
407
|
+
const sdl = sdlState(segDefsElement);
|
|
408
|
+
const sdlNode = sdlElement(segDefsElement);
|
|
409
|
+
if (segSourceElement) {
|
|
410
|
+
const sourceMrks = descendants(segSourceElement, isSegMrk);
|
|
411
|
+
const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
|
|
412
|
+
sourceMrks.forEach((sourceMrk, segmentIndex) => {
|
|
413
|
+
const mid = attr(sourceMrk, "mid");
|
|
414
|
+
const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
|
|
415
|
+
if (targetMrk === void 0 && targetMrks.length > 0) {
|
|
416
|
+
warnings.push(`${fileIndex}:${unitId}:${mid ?? segmentIndex}: target mrk missing`);
|
|
417
|
+
}
|
|
418
|
+
segments.push(
|
|
419
|
+
makeSegment(
|
|
420
|
+
fileIndex,
|
|
421
|
+
unitId,
|
|
422
|
+
segmentIndex,
|
|
423
|
+
sourceMrk,
|
|
424
|
+
targetMrk ?? (targetMrks.length === 0 ? targetElement : void 0),
|
|
425
|
+
mid,
|
|
426
|
+
sdl,
|
|
427
|
+
sdlNode,
|
|
428
|
+
xml,
|
|
429
|
+
warnings
|
|
430
|
+
)
|
|
431
|
+
);
|
|
432
|
+
});
|
|
433
|
+
return;
|
|
434
|
+
}
|
|
435
|
+
if (sourceElement || targetElement) {
|
|
436
|
+
segments.push(
|
|
437
|
+
makeSegment(fileIndex, unitId, 0, sourceElement, targetElement, void 0, sdl, sdlNode, xml, warnings)
|
|
438
|
+
);
|
|
439
|
+
} else {
|
|
440
|
+
warnings.push(`${fileIndex}:${unitId}: no source/target`);
|
|
441
|
+
}
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
if (hasSdlNamespace || hasSdlElement) format = "sdlxliff";
|
|
445
|
+
return { format, version, files, segments, warnings, xml };
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
// packages/l10n-core/src/assets/tmx.ts
|
|
449
|
+
var CONTEXT_PRE = "x-context-pre";
|
|
450
|
+
var CONTEXT_NEXT = "x-context-next";
|
|
451
|
+
function normalizeLanguage(language) {
|
|
452
|
+
return language.trim().replace(/_/g, "-").toLowerCase();
|
|
453
|
+
}
|
|
454
|
+
function languageMatches(actual, wanted) {
|
|
455
|
+
const normalizedActual = normalizeLanguage(actual);
|
|
456
|
+
const normalizedWanted = normalizeLanguage(wanted);
|
|
457
|
+
if (normalizedActual === normalizedWanted) return true;
|
|
458
|
+
return normalizedActual.split("-")[0] === normalizedWanted.split("-")[0];
|
|
459
|
+
}
|
|
460
|
+
function findChild(parent, local) {
|
|
461
|
+
return elementChildren(parent).find((child) => child.local === local);
|
|
462
|
+
}
|
|
463
|
+
function findDescendant(parent, local) {
|
|
464
|
+
for (const child of elementChildren(parent)) {
|
|
465
|
+
if (child.local === local) return child;
|
|
466
|
+
const nested = findDescendant(child, local);
|
|
467
|
+
if (nested) return nested;
|
|
468
|
+
}
|
|
469
|
+
return void 0;
|
|
470
|
+
}
|
|
471
|
+
function tuvLanguage(tuv) {
|
|
472
|
+
return tuv.attribs["xml:lang"] ?? tuv.attribs.lang ?? "";
|
|
473
|
+
}
|
|
474
|
+
function findTuv(tuvs, language, excluded) {
|
|
475
|
+
const wanted = normalizeLanguage(language);
|
|
476
|
+
const candidates = tuvs.filter((tuv) => tuv !== excluded);
|
|
477
|
+
const exact = candidates.find((tuv) => normalizeLanguage(tuvLanguage(tuv)) === wanted);
|
|
478
|
+
if (exact) return exact;
|
|
479
|
+
const wantedBase = wanted.split("-")[0];
|
|
480
|
+
return candidates.find((tuv) => normalizeLanguage(tuvLanguage(tuv)).split("-")[0] === wantedBase);
|
|
481
|
+
}
|
|
482
|
+
function tuvText(tuv) {
|
|
483
|
+
const seg = findDescendant(tuv, "seg");
|
|
484
|
+
return seg ? textOf(seg).trim() : void 0;
|
|
485
|
+
}
|
|
486
|
+
function firstLanguage(tus) {
|
|
487
|
+
for (const tu of tus) {
|
|
488
|
+
for (const tuv of elementChildren(tu).filter((child) => child.local === "tuv")) {
|
|
489
|
+
const language = tuvLanguage(tuv);
|
|
490
|
+
if (language !== "") return language;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
return void 0;
|
|
494
|
+
}
|
|
495
|
+
function inferTargetLanguage(tus, sourceLanguage) {
|
|
496
|
+
for (const tu of tus) {
|
|
497
|
+
for (const tuv of elementChildren(tu).filter((child) => child.local === "tuv")) {
|
|
498
|
+
const language = tuvLanguage(tuv);
|
|
499
|
+
if (language !== "" && !languageMatches(language, sourceLanguage)) return language;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return void 0;
|
|
503
|
+
}
|
|
504
|
+
function readProps(tu) {
|
|
505
|
+
const props = [];
|
|
506
|
+
const walk = (parent) => {
|
|
507
|
+
for (const child of elementChildren(parent)) {
|
|
508
|
+
if (child.local === "prop") props.push(child);
|
|
509
|
+
else walk(child);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
walk(tu);
|
|
513
|
+
return props;
|
|
514
|
+
}
|
|
515
|
+
function parseContext(props) {
|
|
516
|
+
let previousSource;
|
|
517
|
+
let nextSource;
|
|
518
|
+
for (const prop of props) {
|
|
519
|
+
const type = (prop.attribs.type ?? prop.attribs.name ?? "").toLowerCase();
|
|
520
|
+
const value = textOf(prop).trim();
|
|
521
|
+
if (value === "") continue;
|
|
522
|
+
if (type === CONTEXT_PRE) previousSource = value;
|
|
523
|
+
else if (type === CONTEXT_NEXT) nextSource = value;
|
|
524
|
+
}
|
|
525
|
+
if (previousSource === void 0 && nextSource === void 0) return void 0;
|
|
526
|
+
return {
|
|
527
|
+
...previousSource === void 0 ? {} : { previousSource },
|
|
528
|
+
...nextSource === void 0 ? {} : { nextSource }
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
function readProperties(props) {
|
|
532
|
+
const properties = {};
|
|
533
|
+
let count = 0;
|
|
534
|
+
for (const prop of props) {
|
|
535
|
+
const propType = (prop.attribs.type ?? prop.attribs.name ?? "").toLowerCase();
|
|
536
|
+
if (propType === CONTEXT_PRE || propType === CONTEXT_NEXT) continue;
|
|
537
|
+
const baseKey = (prop.attribs.type ?? prop.attribs.name ?? "").trim() || `prop-${count + 1}`;
|
|
538
|
+
const value = textOf(prop).trim();
|
|
539
|
+
if (value === "") continue;
|
|
540
|
+
let key = baseKey;
|
|
541
|
+
for (let suffix = 2; key in properties; suffix += 1) key = `${baseKey}#${suffix}`;
|
|
542
|
+
properties[key] = value;
|
|
543
|
+
count += 1;
|
|
544
|
+
}
|
|
545
|
+
return count === 0 ? void 0 : properties;
|
|
546
|
+
}
|
|
547
|
+
function parseTmx(xml, options = {}) {
|
|
548
|
+
const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
|
|
549
|
+
validateXml(source);
|
|
550
|
+
const document = parseXml(source);
|
|
551
|
+
const root = elementChildren(document).find((child) => child.local === "tmx");
|
|
552
|
+
if (!root) throw new Error("Not a TMX document: missing <tmx> root element");
|
|
553
|
+
const warnings = [];
|
|
554
|
+
const header = findChild(root, "header");
|
|
555
|
+
const body = findChild(root, "body");
|
|
556
|
+
if (!body) {
|
|
557
|
+
warnings.push("TMX: missing <body> element; no entries parsed");
|
|
558
|
+
return { entries: [], warnings };
|
|
559
|
+
}
|
|
560
|
+
const tus = elementChildren(body).filter((child) => child.local === "tu");
|
|
561
|
+
const sourceLanguage = options.sourceLanguage ?? header?.attribs.srclang ?? firstLanguage(tus);
|
|
562
|
+
if (sourceLanguage === void 0) {
|
|
563
|
+
warnings.push("TMX: source language could not be determined; no entries parsed");
|
|
564
|
+
return { entries: [], warnings };
|
|
565
|
+
}
|
|
566
|
+
const targetLanguage = options.targetLanguage ?? inferTargetLanguage(tus, sourceLanguage);
|
|
567
|
+
if (targetLanguage === void 0) {
|
|
568
|
+
warnings.push("TMX: target language could not be determined; no entries parsed");
|
|
569
|
+
return { entries: [], warnings };
|
|
570
|
+
}
|
|
571
|
+
const entries = [];
|
|
572
|
+
for (const [index, tu] of tus.entries()) {
|
|
573
|
+
const label = tu.attribs.tuid ?? tu.attribs.id ?? `#${index + 1}`;
|
|
574
|
+
const tuvs = elementChildren(tu).filter((child) => child.local === "tuv");
|
|
575
|
+
const sourceTuv = findTuv(tuvs, sourceLanguage);
|
|
576
|
+
const targetTuv = findTuv(tuvs, targetLanguage, sourceTuv);
|
|
577
|
+
if (!sourceTuv) {
|
|
578
|
+
warnings.push(`TMX: tu ${label} has no tuv for source language "${sourceLanguage}"; skipped`);
|
|
579
|
+
continue;
|
|
580
|
+
}
|
|
581
|
+
if (!targetTuv) {
|
|
582
|
+
warnings.push(`TMX: tu ${label} has no tuv for target language "${targetLanguage}"; skipped`);
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
const sourceText = tuvText(sourceTuv);
|
|
586
|
+
const targetText = tuvText(targetTuv);
|
|
587
|
+
if (!sourceText || !targetText) {
|
|
588
|
+
warnings.push(`TMX: tu ${label} has no <seg> text; skipped`);
|
|
589
|
+
continue;
|
|
590
|
+
}
|
|
591
|
+
const props = readProps(tu);
|
|
592
|
+
const context = parseContext(props);
|
|
593
|
+
const properties = readProperties(props);
|
|
594
|
+
const lastModified = tu.attribs.creationdate ?? tu.attribs.changedate;
|
|
595
|
+
entries.push({
|
|
596
|
+
id: label,
|
|
597
|
+
source: sourceText,
|
|
598
|
+
target: targetText,
|
|
599
|
+
sourceLanguage,
|
|
600
|
+
targetLanguage,
|
|
601
|
+
origin: "tm",
|
|
602
|
+
...context === void 0 ? {} : { context },
|
|
603
|
+
...lastModified === void 0 ? {} : { lastModified },
|
|
604
|
+
...properties === void 0 ? {} : { properties }
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
return { entries, warnings };
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// packages/l10n-core/src/assets/glossary.ts
|
|
611
|
+
var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
|
|
612
|
+
var TARGET_HEADERS = /* @__PURE__ */ new Set(["target", "target term", "translation", "\u8BD1\u6587", "\u76EE\u6807", "\u76EE\u6807\u8BED"]);
|
|
613
|
+
var FORBIDDEN_HEADERS = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
|
|
614
|
+
function stripBom(text) {
|
|
615
|
+
return text.charCodeAt(0) === 65279 ? text.slice(1) : text;
|
|
616
|
+
}
|
|
617
|
+
function detectDelimiter(text) {
|
|
618
|
+
let commas = 0;
|
|
619
|
+
let tabs = 0;
|
|
620
|
+
let inQuotes = false;
|
|
621
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
622
|
+
const char = text[index];
|
|
623
|
+
if (char === '"') {
|
|
624
|
+
if (inQuotes && text[index + 1] === '"') index += 1;
|
|
625
|
+
else inQuotes = !inQuotes;
|
|
626
|
+
} else if (!inQuotes) {
|
|
627
|
+
if (char === "\n") break;
|
|
628
|
+
else if (char === ",") commas += 1;
|
|
629
|
+
else if (char === " ") tabs += 1;
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
return tabs > commas ? " " : ",";
|
|
633
|
+
}
|
|
634
|
+
function parseCsvRows(text, delimiter = ",") {
|
|
635
|
+
const rows = [];
|
|
636
|
+
let row = [];
|
|
637
|
+
let field = "";
|
|
638
|
+
let inQuotes = false;
|
|
639
|
+
const pushRow = () => {
|
|
640
|
+
row.push(field);
|
|
641
|
+
field = "";
|
|
642
|
+
if (row.some((cell) => cell.trim() !== "")) rows.push(row);
|
|
643
|
+
row = [];
|
|
644
|
+
};
|
|
645
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
646
|
+
const char = text[index];
|
|
647
|
+
if (inQuotes) {
|
|
648
|
+
if (char === '"') {
|
|
649
|
+
if (text[index + 1] === '"') {
|
|
650
|
+
field += '"';
|
|
651
|
+
index += 1;
|
|
652
|
+
} else {
|
|
653
|
+
inQuotes = false;
|
|
654
|
+
}
|
|
655
|
+
} else if (char === "\r") {
|
|
656
|
+
field += "\n";
|
|
657
|
+
if (text[index + 1] === "\n") index += 1;
|
|
658
|
+
} else {
|
|
659
|
+
field += char;
|
|
660
|
+
}
|
|
661
|
+
continue;
|
|
662
|
+
}
|
|
663
|
+
if (char === '"') {
|
|
664
|
+
inQuotes = true;
|
|
665
|
+
} else if (char === delimiter) {
|
|
666
|
+
row.push(field);
|
|
667
|
+
field = "";
|
|
668
|
+
} else if (char === "\n") {
|
|
669
|
+
pushRow();
|
|
670
|
+
} else if (char === "\r") {
|
|
671
|
+
if (text[index + 1] !== "\n") pushRow();
|
|
672
|
+
} else {
|
|
673
|
+
field += char;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
if (field !== "" || row.length > 0) pushRow();
|
|
677
|
+
return rows;
|
|
678
|
+
}
|
|
679
|
+
function headerIndex(row, aliases) {
|
|
680
|
+
return row.findIndex((cell) => aliases.has(cell.trim().toLowerCase()));
|
|
681
|
+
}
|
|
682
|
+
function isTruthyFlag(value) {
|
|
683
|
+
return /^(?:true|1|yes|y|是|禁用)$/i.test(value.trim());
|
|
684
|
+
}
|
|
685
|
+
function normalizedHeader(row) {
|
|
686
|
+
return row.map((cell) => cell.trim().toLowerCase());
|
|
687
|
+
}
|
|
688
|
+
function parseGlossaryCsv(text, options = {}) {
|
|
689
|
+
const content = stripBom(text);
|
|
690
|
+
const delimiter = detectDelimiter(content);
|
|
691
|
+
const rows = parseCsvRows(content, delimiter);
|
|
692
|
+
const warnings = [];
|
|
693
|
+
if (rows.length === 0) return { entries: [], warnings: ["glossary: no rows found"] };
|
|
694
|
+
let sourceColumn = 0;
|
|
695
|
+
let targetColumn = 1;
|
|
696
|
+
let forbiddenColumn = 2;
|
|
697
|
+
let dataRows = rows;
|
|
698
|
+
const first = rows[0];
|
|
699
|
+
if (first) {
|
|
700
|
+
const header = normalizedHeader(first);
|
|
701
|
+
const sourceIndex = headerIndex(header, SOURCE_HEADERS);
|
|
702
|
+
const targetIndex = headerIndex(header, TARGET_HEADERS);
|
|
703
|
+
if (sourceIndex >= 0 && targetIndex >= 0) {
|
|
704
|
+
sourceColumn = sourceIndex;
|
|
705
|
+
targetColumn = targetIndex;
|
|
706
|
+
const forbiddenIndex = headerIndex(header, FORBIDDEN_HEADERS);
|
|
707
|
+
forbiddenColumn = forbiddenIndex >= 0 ? forbiddenIndex : void 0;
|
|
708
|
+
const known = /* @__PURE__ */ new Set([sourceIndex, targetIndex]);
|
|
709
|
+
if (forbiddenIndex >= 0) known.add(forbiddenIndex);
|
|
710
|
+
header.forEach((cell, index) => {
|
|
711
|
+
if (cell !== "" && !known.has(index)) {
|
|
712
|
+
warnings.push(`glossary: ignored column "${first[index]}"`);
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
dataRows = rows.slice(1);
|
|
716
|
+
} else if (sourceIndex >= 0 || targetIndex >= 0) {
|
|
717
|
+
warnings.push("glossary: header is missing a source or target column; using positional columns");
|
|
718
|
+
sourceColumn = sourceIndex >= 0 ? sourceIndex : 0;
|
|
719
|
+
targetColumn = targetIndex >= 0 ? targetIndex : sourceColumn === 0 ? 1 : 0;
|
|
720
|
+
forbiddenColumn = void 0;
|
|
721
|
+
dataRows = rows.slice(1);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
const entries = [];
|
|
725
|
+
dataRows.forEach((row, rowIndex) => {
|
|
726
|
+
const position = rowIndex + 1;
|
|
727
|
+
const source = (row[sourceColumn] ?? "").trim();
|
|
728
|
+
const target = (row[targetColumn] ?? "").trim();
|
|
729
|
+
if (source === "" && target === "") return;
|
|
730
|
+
if (source === "" || target === "") {
|
|
731
|
+
warnings.push(`glossary: row ${position} is missing ${source === "" ? "source" : "target"}; skipped`);
|
|
732
|
+
return;
|
|
733
|
+
}
|
|
734
|
+
const forbiddenCell = forbiddenColumn === void 0 ? "" : row[forbiddenColumn] ?? "";
|
|
735
|
+
const forbidden = isTruthyFlag(forbiddenCell);
|
|
736
|
+
entries.push({
|
|
737
|
+
id: `glossary-${entries.length + 1}`,
|
|
738
|
+
source,
|
|
739
|
+
target,
|
|
740
|
+
...options.sourceLanguage === void 0 ? {} : { sourceLanguage: options.sourceLanguage },
|
|
741
|
+
...options.targetLanguage === void 0 ? {} : { targetLanguage: options.targetLanguage },
|
|
742
|
+
...forbidden ? { forbidden: true } : {}
|
|
743
|
+
});
|
|
744
|
+
});
|
|
745
|
+
return { entries, warnings };
|
|
746
|
+
}
|
|
747
|
+
|
|
748
|
+
// packages/l10n-core/src/assets/tm-matcher.ts
|
|
749
|
+
var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
|
|
750
|
+
var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
|
|
751
|
+
var DOUBLE_QUOTES = /[\u201c\u201d\u201e\u201f\u2033]/gu;
|
|
752
|
+
var DASHES = /[\u2010\u2011\u2012\u2013\u2014\u2015\u2212]/gu;
|
|
753
|
+
var TERMINAL_PUNCTUATION = /[.!?\u2026;:\u3002\uff01\uff1f\uff1b\uff1a]$/u;
|
|
754
|
+
var CJK_CHAR = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
|
|
755
|
+
var WORD_CHAR = /[\p{L}\p{N}_]/u;
|
|
756
|
+
var COMBINING_MARK = new RegExp("\\p{M}", "u");
|
|
757
|
+
var TAG_TOKEN = /^<\/?[A-Za-z_][\w.:-]*(?:\s+[^<>]*?)?\s*\/?>/u;
|
|
758
|
+
var PLACEHOLDER_TOKEN = /^(?:\$\{[^{}]*\}|%[0-9]+\$[sdifuxX]|%[-+ 0#]*[0-9]*(?:\.[0-9]+)?[sdifuxXc]|\{[^{}]*\})/u;
|
|
759
|
+
var URL_TOKEN = /^(?:(?:https?|ftp):\/\/|www\.)[^\s<>"'{}]+/u;
|
|
760
|
+
var EMAIL_TOKEN = /^[\w.+-]+@[\w-]+(?:\.[\w-]+)+/u;
|
|
761
|
+
var PATH_TOKEN = /^(?:(?:[A-Za-z]:[\\/]|\\\\|~[\\/])[^\s<>"'{}]+|\/(?:[^\s<>"'{}]*\/)[^\s<>"'{}]*)/u;
|
|
762
|
+
var NUMBER_TOKEN = new RegExp("^\\p{Nd}+(?:[.,]\\p{Nd}+)?", "u");
|
|
763
|
+
var SIMILARITY_TRUNCATE = 200;
|
|
764
|
+
var TRUNCATION_PENALTY = 7;
|
|
765
|
+
var DEFAULT_THRESHOLD = 50;
|
|
766
|
+
var DEFAULT_MAX_MATCHES = 3;
|
|
767
|
+
function normalizeText(text) {
|
|
768
|
+
return text.normalize("NFKC").normalize("NFC").replace(ZERO_WIDTH, "").replace(QUOTES, "'").replace(DOUBLE_QUOTES, '"').replace(DASHES, "-").replace(/\u2026/gu, "...").replace(/\s+/gu, " ").trim().toLowerCase();
|
|
769
|
+
}
|
|
770
|
+
function matchAtomic(rest) {
|
|
771
|
+
const tag = TAG_TOKEN.exec(rest);
|
|
772
|
+
if (tag) return { kind: "tag", raw: tag[0], skeleton: "<TAG>" };
|
|
773
|
+
const placeholder = PLACEHOLDER_TOKEN.exec(rest);
|
|
774
|
+
if (placeholder) return { kind: "placeholder", raw: placeholder[0], skeleton: "<PH>" };
|
|
775
|
+
const url = URL_TOKEN.exec(rest);
|
|
776
|
+
if (url) return { kind: "atomic", raw: url[0], skeleton: url[0] };
|
|
777
|
+
const email = EMAIL_TOKEN.exec(rest);
|
|
778
|
+
if (email) return { kind: "atomic", raw: email[0], skeleton: email[0] };
|
|
779
|
+
const path = PATH_TOKEN.exec(rest);
|
|
780
|
+
if (path) return { kind: "atomic", raw: path[0], skeleton: path[0] };
|
|
781
|
+
return void 0;
|
|
782
|
+
}
|
|
783
|
+
function tokenize(text) {
|
|
784
|
+
const norm = normalizeText(text);
|
|
785
|
+
const tokens = [];
|
|
786
|
+
let index = 0;
|
|
787
|
+
while (index < norm.length) {
|
|
788
|
+
const rest = norm.slice(index);
|
|
789
|
+
const atomic = matchAtomic(rest);
|
|
790
|
+
if (atomic) {
|
|
791
|
+
tokens.push(atomic);
|
|
792
|
+
index += atomic.raw.length;
|
|
793
|
+
continue;
|
|
794
|
+
}
|
|
795
|
+
const char = norm[index];
|
|
796
|
+
if (CJK_CHAR.test(char)) {
|
|
797
|
+
tokens.push({ kind: "cjk", raw: char, skeleton: char });
|
|
798
|
+
index += 1;
|
|
799
|
+
continue;
|
|
800
|
+
}
|
|
801
|
+
const number2 = NUMBER_TOKEN.exec(rest);
|
|
802
|
+
if (number2) {
|
|
803
|
+
tokens.push({ kind: "number", raw: number2[0], skeleton: "<NUM>" });
|
|
804
|
+
index += number2[0].length;
|
|
805
|
+
continue;
|
|
806
|
+
}
|
|
807
|
+
if (WORD_CHAR.test(char)) {
|
|
808
|
+
let end = index + 1;
|
|
809
|
+
while (end < norm.length) {
|
|
810
|
+
const next = norm[end];
|
|
811
|
+
if (CJK_CHAR.test(next)) break;
|
|
812
|
+
if (WORD_CHAR.test(next) || COMBINING_MARK.test(next)) {
|
|
813
|
+
end += 1;
|
|
814
|
+
continue;
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
const raw = norm.slice(index, end);
|
|
819
|
+
tokens.push({ kind: "word", raw, skeleton: raw });
|
|
820
|
+
index = end;
|
|
821
|
+
continue;
|
|
822
|
+
}
|
|
823
|
+
index += 1;
|
|
824
|
+
}
|
|
825
|
+
return {
|
|
826
|
+
tokens,
|
|
827
|
+
skeleton: tokens.map((token) => token.skeleton),
|
|
828
|
+
numbers: tokens.filter((token) => token.kind === "number").map((token) => token.raw),
|
|
829
|
+
atoms: tokens.filter((token) => token.kind === "tag" || token.kind === "placeholder").map((token) => `${token.kind}:${token.raw}`),
|
|
830
|
+
norm,
|
|
831
|
+
terminalPunct: TERMINAL_PUNCTUATION.test(norm)
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
function arraysEqual(a, b) {
|
|
835
|
+
if (a.length !== b.length) return false;
|
|
836
|
+
return a.every((value, index) => value === b[index]);
|
|
837
|
+
}
|
|
838
|
+
function tokensEqual(a, b) {
|
|
839
|
+
if (a.length !== b.length) return false;
|
|
840
|
+
return a.every(
|
|
841
|
+
(token, index) => token.kind === b[index].kind && token.raw === b[index].raw
|
|
842
|
+
);
|
|
843
|
+
}
|
|
844
|
+
function firstDifference(a, b) {
|
|
845
|
+
for (let index = 0; index < Math.min(a.length, b.length); index += 1) {
|
|
846
|
+
if (a[index] !== b[index]) return `${a[index]} vs ${b[index]}`;
|
|
847
|
+
}
|
|
848
|
+
return `${a.length} vs ${b.length}`;
|
|
849
|
+
}
|
|
850
|
+
function compareStructure(source, entry, tmEntry) {
|
|
851
|
+
const skeletonEqual = arraysEqual(source.skeleton, entry.skeleton);
|
|
852
|
+
const numbersEqual = arraysEqual(source.numbers, entry.numbers);
|
|
853
|
+
const atomsEqual = arraysEqual(source.atoms, entry.atoms);
|
|
854
|
+
const numberDiff = !numbersEqual;
|
|
855
|
+
const tagDiff = !atomsEqual;
|
|
856
|
+
const punctDiff = source.terminalPunct !== entry.terminalPunct || tokensEqual(source.tokens, entry.tokens) && source.norm !== entry.norm;
|
|
857
|
+
const penalties = [];
|
|
858
|
+
if (numberDiff) {
|
|
859
|
+
penalties.push({
|
|
860
|
+
kind: "number",
|
|
861
|
+
points: 2,
|
|
862
|
+
detail: `${source.numbers.join(", ") || "none"} vs ${entry.numbers.join(", ") || "none"}`
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
if (tagDiff) {
|
|
866
|
+
if (source.atoms.length !== entry.atoms.length) {
|
|
867
|
+
penalties.push({
|
|
868
|
+
kind: "tag_count",
|
|
869
|
+
points: 10,
|
|
870
|
+
detail: `${source.atoms.length} vs ${entry.atoms.length} tags/placeholders`
|
|
871
|
+
});
|
|
872
|
+
} else {
|
|
873
|
+
penalties.push({
|
|
874
|
+
kind: "tag_id",
|
|
875
|
+
points: 3,
|
|
876
|
+
detail: firstDifference(source.atoms, entry.atoms)
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
if (punctDiff) {
|
|
881
|
+
penalties.push({
|
|
882
|
+
kind: "punctuation",
|
|
883
|
+
points: 1,
|
|
884
|
+
detail: "difference is limited to punctuation/whitespace"
|
|
885
|
+
});
|
|
886
|
+
}
|
|
887
|
+
if (tmEntry.fuzzy === true) {
|
|
888
|
+
penalties.push({ kind: "fuzzy", points: 5, detail: "TM entry is flagged as fuzzy" });
|
|
889
|
+
}
|
|
890
|
+
if (tmEntry.origin === "alignment") {
|
|
891
|
+
penalties.push({
|
|
892
|
+
kind: "unaligned",
|
|
893
|
+
points: 2,
|
|
894
|
+
detail: "alignment entry (default unaligned penalty)"
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
return { skeletonEqual, numbersEqual, atomsEqual, numberDiff, tagDiff, punctDiff, penalties };
|
|
898
|
+
}
|
|
899
|
+
function sumPenalties(penalties) {
|
|
900
|
+
return penalties.reduce((total, penalty) => total + penalty.points, 0);
|
|
901
|
+
}
|
|
902
|
+
function osaDistance(a, b) {
|
|
903
|
+
const rows = a.length + 1;
|
|
904
|
+
const cols = b.length + 1;
|
|
905
|
+
const matrix = Array.from(
|
|
906
|
+
{ length: rows },
|
|
907
|
+
(_, row) => Array.from({ length: cols }, (_2, column) => row === 0 ? column : column === 0 ? row : 0)
|
|
908
|
+
);
|
|
909
|
+
for (let i = 1; i < rows; i += 1) {
|
|
910
|
+
for (let j = 1; j < cols; j += 1) {
|
|
911
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
912
|
+
let value = Math.min(
|
|
913
|
+
matrix[i - 1][j] + 1,
|
|
914
|
+
matrix[i][j - 1] + 1,
|
|
915
|
+
matrix[i - 1][j - 1] + cost
|
|
916
|
+
);
|
|
917
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) {
|
|
918
|
+
value = Math.min(value, matrix[i - 2][j - 2] + 1);
|
|
919
|
+
}
|
|
920
|
+
matrix[i][j] = value;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
return matrix[rows - 1][cols - 1];
|
|
924
|
+
}
|
|
925
|
+
function levenshteinDistance(a, b) {
|
|
926
|
+
if (a === b) return 0;
|
|
927
|
+
if (a.length === 0) return b.length;
|
|
928
|
+
if (b.length === 0) return a.length;
|
|
929
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
930
|
+
for (let i = 1; i <= a.length; i += 1) {
|
|
931
|
+
const current = new Array(b.length + 1).fill(0);
|
|
932
|
+
current[0] = i;
|
|
933
|
+
for (let j = 1; j <= b.length; j += 1) {
|
|
934
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
935
|
+
current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
|
|
936
|
+
}
|
|
937
|
+
previous = current;
|
|
938
|
+
}
|
|
939
|
+
return previous[b.length];
|
|
940
|
+
}
|
|
941
|
+
function round1(value) {
|
|
942
|
+
return Math.round(value * 10) / 10;
|
|
943
|
+
}
|
|
944
|
+
function similarityOf(source, entry) {
|
|
945
|
+
if (arraysEqual(source.skeleton, entry.skeleton)) return { similarity: 100, truncated: false };
|
|
946
|
+
if (source.skeleton.length >= 2 && entry.skeleton.length >= 2) {
|
|
947
|
+
const distance2 = osaDistance(source.skeleton, entry.skeleton);
|
|
948
|
+
const denominator2 = Math.max(source.skeleton.length, entry.skeleton.length, 3);
|
|
949
|
+
return { similarity: round1(100 * (1 - distance2 / denominator2)), truncated: false };
|
|
950
|
+
}
|
|
951
|
+
const truncated = source.norm.length > SIMILARITY_TRUNCATE || entry.norm.length > SIMILARITY_TRUNCATE;
|
|
952
|
+
const a = source.norm.slice(0, SIMILARITY_TRUNCATE);
|
|
953
|
+
const b = entry.norm.slice(0, SIMILARITY_TRUNCATE);
|
|
954
|
+
const denominator = Math.max(a.length, b.length, 3);
|
|
955
|
+
const distance = levenshteinDistance(a, b);
|
|
956
|
+
return { similarity: round1(100 * (1 - distance / denominator)), truncated };
|
|
957
|
+
}
|
|
958
|
+
function matchTypeFor(rate) {
|
|
959
|
+
if (rate >= 95) return "near_exact";
|
|
960
|
+
if (rate >= 85) return "high_fuzzy";
|
|
961
|
+
if (rate >= 75) return "medium_fuzzy";
|
|
962
|
+
return "low_fuzzy";
|
|
963
|
+
}
|
|
964
|
+
function compareContext(provided, stored) {
|
|
965
|
+
if (!provided || !stored) return { matched: false, both: false };
|
|
966
|
+
const previousMatches = provided.previousSource !== void 0 && stored.previousSource !== void 0 && normalizeText(provided.previousSource) === normalizeText(stored.previousSource);
|
|
967
|
+
const nextMatches = provided.nextSource !== void 0 && stored.nextSource !== void 0 && normalizeText(provided.nextSource) === normalizeText(stored.nextSource);
|
|
968
|
+
return { matched: previousMatches || nextMatches, both: previousMatches && nextMatches };
|
|
969
|
+
}
|
|
970
|
+
function compareLastModified(a, b) {
|
|
971
|
+
const left = a.lastModified ?? "";
|
|
972
|
+
const right = b.lastModified ?? "";
|
|
973
|
+
if (left === right) return 0;
|
|
974
|
+
if (left === "") return 1;
|
|
975
|
+
if (right === "") return -1;
|
|
976
|
+
return left < right ? 1 : -1;
|
|
977
|
+
}
|
|
978
|
+
function languageAllowed(entryLanguage, wanted) {
|
|
979
|
+
if (wanted === void 0) return true;
|
|
980
|
+
if (entryLanguage === "") return true;
|
|
981
|
+
return languageMatches(entryLanguage, wanted);
|
|
982
|
+
}
|
|
983
|
+
function matchTm(entries, sourceText, options = {}) {
|
|
984
|
+
const threshold = options.threshold ?? DEFAULT_THRESHOLD;
|
|
985
|
+
const maxMatches = options.maxMatches ?? DEFAULT_MAX_MATCHES;
|
|
986
|
+
if (maxMatches <= 0) return [];
|
|
987
|
+
const source = tokenize(sourceText);
|
|
988
|
+
const scored = [];
|
|
989
|
+
entries.forEach((entry, index) => {
|
|
990
|
+
if (!languageAllowed(entry.sourceLanguage, options.sourceLanguage)) return;
|
|
991
|
+
if (!languageAllowed(entry.targetLanguage, options.targetLanguage)) return;
|
|
992
|
+
const target = tokenize(entry.source);
|
|
993
|
+
const structure = compareStructure(source, target, entry);
|
|
994
|
+
const context = compareContext(options.context, entry.context);
|
|
995
|
+
const truncation = structure.skeletonEqual ? { similarity: 100, truncated: false } : similarityOf(source, target);
|
|
996
|
+
let matchRate;
|
|
997
|
+
let matchType;
|
|
998
|
+
if (structure.skeletonEqual && structure.numbersEqual && structure.atomsEqual && !structure.punctDiff) {
|
|
999
|
+
structure.penalties.length = 0;
|
|
1000
|
+
if (context.both) {
|
|
1001
|
+
matchRate = 102;
|
|
1002
|
+
matchType = "double_context";
|
|
1003
|
+
} else if (context.matched) {
|
|
1004
|
+
matchRate = 101;
|
|
1005
|
+
matchType = "context";
|
|
1006
|
+
} else {
|
|
1007
|
+
matchRate = 100;
|
|
1008
|
+
matchType = "exact";
|
|
1009
|
+
}
|
|
1010
|
+
} else if (structure.skeletonEqual) {
|
|
1011
|
+
const penalties = sumPenalties(structure.penalties);
|
|
1012
|
+
const candidate = Math.min(99, Math.round(100 - penalties));
|
|
1013
|
+
if (candidate < 50) return;
|
|
1014
|
+
matchRate = candidate;
|
|
1015
|
+
matchType = matchTypeFor(candidate);
|
|
1016
|
+
} else {
|
|
1017
|
+
const truncationPenalty = truncation.truncated ? TRUNCATION_PENALTY : 0;
|
|
1018
|
+
const adjusted = truncation.similarity - sumPenalties(structure.penalties) - truncationPenalty;
|
|
1019
|
+
if (adjusted < 50) return;
|
|
1020
|
+
const candidate = Math.min(99, Math.round(adjusted));
|
|
1021
|
+
if (candidate < 50) return;
|
|
1022
|
+
matchRate = candidate;
|
|
1023
|
+
matchType = matchTypeFor(candidate);
|
|
1024
|
+
}
|
|
1025
|
+
if (matchRate < threshold) return;
|
|
1026
|
+
scored.push({
|
|
1027
|
+
index,
|
|
1028
|
+
match: {
|
|
1029
|
+
entry,
|
|
1030
|
+
similarity: truncation.similarity,
|
|
1031
|
+
matchRate,
|
|
1032
|
+
matchType,
|
|
1033
|
+
penalties: structure.penalties,
|
|
1034
|
+
contextMatched: context.matched,
|
|
1035
|
+
flags: {
|
|
1036
|
+
numberDiff: structure.numberDiff,
|
|
1037
|
+
tagDiff: structure.tagDiff,
|
|
1038
|
+
punctDiff: structure.punctDiff,
|
|
1039
|
+
truncated: truncation.truncated
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
});
|
|
1043
|
+
});
|
|
1044
|
+
scored.sort((left, right) => {
|
|
1045
|
+
const a = left.match;
|
|
1046
|
+
const b = right.match;
|
|
1047
|
+
if (b.matchRate !== a.matchRate) return b.matchRate - a.matchRate;
|
|
1048
|
+
if (b.similarity !== a.similarity) return b.similarity - a.similarity;
|
|
1049
|
+
const modified = compareLastModified(a.entry, b.entry);
|
|
1050
|
+
if (modified !== 0) return modified;
|
|
1051
|
+
if (a.entry.id !== b.entry.id) return a.entry.id < b.entry.id ? -1 : 1;
|
|
1052
|
+
return left.index - right.index;
|
|
1053
|
+
});
|
|
1054
|
+
return scored.slice(0, maxMatches).map((item) => item.match);
|
|
1055
|
+
}
|
|
1056
|
+
|
|
1057
|
+
// packages/l10n-core/src/assets/term-matcher.ts
|
|
1058
|
+
var CJK_CHAR2 = /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}]/u;
|
|
1059
|
+
var WORD_CHAR2 = /[\p{L}\p{N}_]/u;
|
|
1060
|
+
var COMBINING_MARK2 = new RegExp("\\p{M}", "u");
|
|
1061
|
+
var MAX_TERM_TOKENS = 8;
|
|
1062
|
+
var FUZZY_THRESHOLD = 0.8;
|
|
1063
|
+
function readToken(text, index) {
|
|
1064
|
+
const codePoint = text.codePointAt(index);
|
|
1065
|
+
if (codePoint === void 0) return void 0;
|
|
1066
|
+
const char = String.fromCodePoint(codePoint);
|
|
1067
|
+
const end = index + char.length;
|
|
1068
|
+
if (CJK_CHAR2.test(char)) return { value: char, start: index, end, kind: "cjk" };
|
|
1069
|
+
return { value: char, start: index, end, kind: "word" };
|
|
1070
|
+
}
|
|
1071
|
+
function tokenizeText(text) {
|
|
1072
|
+
const tokens = [];
|
|
1073
|
+
let index = 0;
|
|
1074
|
+
while (index < text.length) {
|
|
1075
|
+
const first = readToken(text, index);
|
|
1076
|
+
if (!first) break;
|
|
1077
|
+
if (first.kind === "cjk") {
|
|
1078
|
+
tokens.push(first);
|
|
1079
|
+
index = first.end;
|
|
1080
|
+
continue;
|
|
1081
|
+
}
|
|
1082
|
+
if (!WORD_CHAR2.test(first.value)) {
|
|
1083
|
+
index = first.end;
|
|
1084
|
+
continue;
|
|
1085
|
+
}
|
|
1086
|
+
let end = first.end;
|
|
1087
|
+
while (end < text.length) {
|
|
1088
|
+
const next = readToken(text, end);
|
|
1089
|
+
if (!next || next.kind === "cjk") break;
|
|
1090
|
+
if (!WORD_CHAR2.test(next.value) && !COMBINING_MARK2.test(next.value)) break;
|
|
1091
|
+
end = next.end;
|
|
1092
|
+
}
|
|
1093
|
+
tokens.push({ value: text.slice(index, end), start: index, end, kind: "word" });
|
|
1094
|
+
index = end;
|
|
1095
|
+
}
|
|
1096
|
+
return tokens;
|
|
1097
|
+
}
|
|
1098
|
+
function codePointLength(value) {
|
|
1099
|
+
return [...value].length;
|
|
1100
|
+
}
|
|
1101
|
+
function caseModeOf(entry) {
|
|
1102
|
+
if (entry.caseSensitive === true) return "yes";
|
|
1103
|
+
if (entry.caseSensitive === false) return "no";
|
|
1104
|
+
return "permissive";
|
|
1105
|
+
}
|
|
1106
|
+
function caseMatches(term, text, mode) {
|
|
1107
|
+
if (term.length !== text.length) return false;
|
|
1108
|
+
if (mode === "yes") return term === text;
|
|
1109
|
+
if (mode === "no") return term.toLowerCase() === text.toLowerCase();
|
|
1110
|
+
for (let index = 0; index < term.length; index += 1) {
|
|
1111
|
+
const termChar = term[index];
|
|
1112
|
+
const textChar = text[index];
|
|
1113
|
+
if (termChar === textChar) continue;
|
|
1114
|
+
if (termChar.toLowerCase() !== textChar.toLowerCase()) return false;
|
|
1115
|
+
if (termChar !== termChar.toLowerCase()) return false;
|
|
1116
|
+
}
|
|
1117
|
+
return true;
|
|
1118
|
+
}
|
|
1119
|
+
function levenshtein(a, b) {
|
|
1120
|
+
if (a === b) return 0;
|
|
1121
|
+
if (a.length === 0) return b.length;
|
|
1122
|
+
if (b.length === 0) return a.length;
|
|
1123
|
+
let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
|
|
1124
|
+
for (let i = 1; i <= a.length; i += 1) {
|
|
1125
|
+
const current = new Array(b.length + 1).fill(0);
|
|
1126
|
+
current[0] = i;
|
|
1127
|
+
for (let j = 1; j <= b.length; j += 1) {
|
|
1128
|
+
const cost = a[i - 1] === b[j - 1] ? 0 : 1;
|
|
1129
|
+
current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + cost);
|
|
1130
|
+
}
|
|
1131
|
+
previous = current;
|
|
1132
|
+
}
|
|
1133
|
+
return previous[b.length];
|
|
1134
|
+
}
|
|
1135
|
+
function similarity(a, b) {
|
|
1136
|
+
const longest = Math.max(a.length, b.length);
|
|
1137
|
+
if (longest === 0) return 1;
|
|
1138
|
+
return 1 - levenshtein(a, b) / longest;
|
|
1139
|
+
}
|
|
1140
|
+
function languageAllowed2(entryLanguage, wanted) {
|
|
1141
|
+
if (wanted === void 0) return true;
|
|
1142
|
+
if (entryLanguage === void 0 || entryLanguage === "") return true;
|
|
1143
|
+
return languageMatches(entryLanguage, wanted);
|
|
1144
|
+
}
|
|
1145
|
+
function scanExactOrPrefix(tokens, termTokens, caseMode, options) {
|
|
1146
|
+
const matches = [];
|
|
1147
|
+
const lastIndex = termTokens.length - 1;
|
|
1148
|
+
for (let start = 0; start + termTokens.length <= tokens.length; start += 1) {
|
|
1149
|
+
let matched = true;
|
|
1150
|
+
let exactCase = true;
|
|
1151
|
+
let extended = false;
|
|
1152
|
+
for (let offset = 0; offset <= lastIndex; offset += 1) {
|
|
1153
|
+
const termValue = termTokens[offset];
|
|
1154
|
+
const textToken = tokens[start + offset];
|
|
1155
|
+
if (offset === lastIndex && options.suffixLimit >= 0) {
|
|
1156
|
+
if (textToken.value.length < termValue.length) {
|
|
1157
|
+
matched = false;
|
|
1158
|
+
break;
|
|
1159
|
+
}
|
|
1160
|
+
const head = textToken.value.slice(0, termValue.length);
|
|
1161
|
+
if (!caseMatches(termValue, head, caseMode)) {
|
|
1162
|
+
matched = false;
|
|
1163
|
+
break;
|
|
1164
|
+
}
|
|
1165
|
+
const suffix = textToken.value.slice(termValue.length);
|
|
1166
|
+
if (suffix !== "" && codePointLength(suffix) > options.suffixLimit) {
|
|
1167
|
+
matched = false;
|
|
1168
|
+
break;
|
|
1169
|
+
}
|
|
1170
|
+
if (head !== termValue) exactCase = false;
|
|
1171
|
+
if (suffix !== "") extended = true;
|
|
1172
|
+
} else {
|
|
1173
|
+
if (!caseMatches(termValue, textToken.value, caseMode)) {
|
|
1174
|
+
matched = false;
|
|
1175
|
+
break;
|
|
1176
|
+
}
|
|
1177
|
+
if (textToken.value !== termValue) exactCase = false;
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
if (!matched) continue;
|
|
1181
|
+
const first = tokens[start];
|
|
1182
|
+
const last = tokens[start + lastIndex];
|
|
1183
|
+
matches.push({
|
|
1184
|
+
start: first.start,
|
|
1185
|
+
end: last.end,
|
|
1186
|
+
exactCase,
|
|
1187
|
+
kind: extended ? options.kind : "exact"
|
|
1188
|
+
});
|
|
1189
|
+
}
|
|
1190
|
+
return matches;
|
|
1191
|
+
}
|
|
1192
|
+
function scanFuzzy(tokens, term, caseMode) {
|
|
1193
|
+
const normalizedTerm = caseMode === "yes" ? term : term.toLowerCase();
|
|
1194
|
+
const matches = [];
|
|
1195
|
+
for (const token of tokens) {
|
|
1196
|
+
if (token.kind !== "word") continue;
|
|
1197
|
+
const normalizedText = caseMode === "yes" ? token.value : token.value.toLowerCase();
|
|
1198
|
+
if (similarity(normalizedTerm, normalizedText) >= FUZZY_THRESHOLD) {
|
|
1199
|
+
matches.push({
|
|
1200
|
+
start: token.start,
|
|
1201
|
+
end: token.end,
|
|
1202
|
+
exactCase: token.value === term,
|
|
1203
|
+
kind: "fuzzy"
|
|
1204
|
+
});
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
return matches;
|
|
1208
|
+
}
|
|
1209
|
+
function matchTerms(text, entries, options = {}) {
|
|
1210
|
+
const source = text.normalize("NFC");
|
|
1211
|
+
const tokens = tokenizeText(source);
|
|
1212
|
+
const hits = [];
|
|
1213
|
+
const warnings = [];
|
|
1214
|
+
const seenWarnings = /* @__PURE__ */ new Set();
|
|
1215
|
+
const emittedHits = /* @__PURE__ */ new Set();
|
|
1216
|
+
const warn = (message) => {
|
|
1217
|
+
if (seenWarnings.has(message)) return;
|
|
1218
|
+
seenWarnings.add(message);
|
|
1219
|
+
warnings.push(message);
|
|
1220
|
+
};
|
|
1221
|
+
for (const entry of entries) {
|
|
1222
|
+
if (!languageAllowed2(entry.sourceLanguage, options.sourceLanguage)) continue;
|
|
1223
|
+
if (!languageAllowed2(entry.targetLanguage, options.targetLanguage)) continue;
|
|
1224
|
+
const caseMode = caseModeOf(entry);
|
|
1225
|
+
const mode = entry.matching ?? "prefix50";
|
|
1226
|
+
const forms = /* @__PURE__ */ new Set();
|
|
1227
|
+
for (const form of [entry.source, entry.target]) {
|
|
1228
|
+
const normalized = form.normalize("NFC").trim();
|
|
1229
|
+
if (normalized !== "") forms.add(normalized);
|
|
1230
|
+
}
|
|
1231
|
+
for (const form of forms) {
|
|
1232
|
+
let termTokens = tokenizeText(form).map((token) => token.value);
|
|
1233
|
+
if (termTokens.length === 0) continue;
|
|
1234
|
+
if (termTokens.length > MAX_TERM_TOKENS) {
|
|
1235
|
+
warn(`glossary: term "${form}" exceeds ${MAX_TERM_TOKENS} tokens; skipped`);
|
|
1236
|
+
continue;
|
|
1237
|
+
}
|
|
1238
|
+
let candidates = [];
|
|
1239
|
+
if (mode === "fuzzy" && options.fuzzy === true) {
|
|
1240
|
+
if (termTokens.length === 1) candidates = scanFuzzy(tokens, termTokens[0], caseMode);
|
|
1241
|
+
else warn(`glossary: fuzzy matching only supports single-token terms; "${form}" skipped`);
|
|
1242
|
+
} else if (mode === "custom") {
|
|
1243
|
+
if (form.endsWith("*") && !form.slice(0, -1).includes("*")) {
|
|
1244
|
+
termTokens = tokenizeText(form.slice(0, -1)).map((token) => token.value);
|
|
1245
|
+
if (termTokens.length === 0) continue;
|
|
1246
|
+
if (termTokens.length > MAX_TERM_TOKENS) {
|
|
1247
|
+
warn(`glossary: custom term "${form}" exceeds ${MAX_TERM_TOKENS} tokens; skipped`);
|
|
1248
|
+
continue;
|
|
1249
|
+
}
|
|
1250
|
+
candidates = scanExactOrPrefix(tokens, termTokens, caseMode, {
|
|
1251
|
+
suffixLimit: Number.POSITIVE_INFINITY,
|
|
1252
|
+
kind: "custom"
|
|
1253
|
+
});
|
|
1254
|
+
} else {
|
|
1255
|
+
warn(`glossary: unsupported custom pattern "${form}"; matched as exact`);
|
|
1256
|
+
candidates = scanExactOrPrefix(tokens, termTokens, caseMode, { suffixLimit: -1, kind: "exact" });
|
|
1257
|
+
}
|
|
1258
|
+
} else {
|
|
1259
|
+
const isPrefix = mode === "prefix50";
|
|
1260
|
+
const lastToken = termTokens[termTokens.length - 1];
|
|
1261
|
+
candidates = scanExactOrPrefix(tokens, termTokens, caseMode, {
|
|
1262
|
+
suffixLimit: isPrefix ? codePointLength(lastToken) : -1,
|
|
1263
|
+
kind: isPrefix ? "prefix50" : "exact"
|
|
1264
|
+
});
|
|
1265
|
+
}
|
|
1266
|
+
for (const candidate of candidates) {
|
|
1267
|
+
const key = `${entry.id}:${candidate.start}:${candidate.end}:${candidate.kind}`;
|
|
1268
|
+
if (emittedHits.has(key)) continue;
|
|
1269
|
+
emittedHits.add(key);
|
|
1270
|
+
hits.push({
|
|
1271
|
+
entry,
|
|
1272
|
+
start: candidate.start,
|
|
1273
|
+
end: candidate.end,
|
|
1274
|
+
matchKind: candidate.kind,
|
|
1275
|
+
caseMatch: candidate.exactCase ? "exact" : "case_only_diff"
|
|
1276
|
+
});
|
|
1277
|
+
}
|
|
1278
|
+
}
|
|
1279
|
+
}
|
|
1280
|
+
const deduped = [];
|
|
1281
|
+
for (const hit of hits) {
|
|
1282
|
+
const covering = deduped.findIndex(
|
|
1283
|
+
(kept) => kept.entry.id === hit.entry.id && kept.start < hit.end && hit.start < kept.end
|
|
1284
|
+
);
|
|
1285
|
+
if (covering < 0) {
|
|
1286
|
+
deduped.push(hit);
|
|
1287
|
+
continue;
|
|
1288
|
+
}
|
|
1289
|
+
const existing = deduped[covering];
|
|
1290
|
+
if (hit.end - hit.start > existing.end - existing.start) deduped[covering] = hit;
|
|
1291
|
+
}
|
|
1292
|
+
hits.length = 0;
|
|
1293
|
+
hits.push(...deduped);
|
|
1294
|
+
hits.sort((left, right) => {
|
|
1295
|
+
if (left.start !== right.start) return left.start - right.start;
|
|
1296
|
+
if (left.end !== right.end) return right.end - left.end;
|
|
1297
|
+
if (left.entry.id !== right.entry.id) return left.entry.id < right.entry.id ? -1 : 1;
|
|
1298
|
+
return 0;
|
|
1299
|
+
});
|
|
1300
|
+
return { hits, issues: [], warnings };
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// packages/l10n-core/src/qa/rules.ts
|
|
1304
|
+
var RULES = [
|
|
1305
|
+
// structural class
|
|
1306
|
+
{ id: "EMPTY_TARGET", severity: "critical" },
|
|
1307
|
+
{ id: "UNTRANSLATED_COPY", severity: "critical" },
|
|
1308
|
+
{ id: "PLACEHOLDER_MISSING", severity: "critical" },
|
|
1309
|
+
{ id: "PLACEHOLDER_EXTRA", severity: "critical" },
|
|
1310
|
+
{ id: "TAG_MISMATCH", severity: "critical" },
|
|
1311
|
+
{ id: "URL_CHANGED", severity: "critical" },
|
|
1312
|
+
{ id: "TB_SOURCE_FORBIDDEN", severity: "major", requiresTerms: true },
|
|
1313
|
+
{ id: "TB_FORBIDDEN_USED", severity: "critical", requiresTerms: true },
|
|
1314
|
+
{ id: "UNPAIRED_BRACKET", severity: "major" },
|
|
1315
|
+
// content class
|
|
1316
|
+
{ id: "NUMBER_MISMATCH", severity: "major" },
|
|
1317
|
+
{ id: "DATE_MISMATCH", severity: "major" },
|
|
1318
|
+
{ id: "DUPLICATE_WORD", severity: "major" },
|
|
1319
|
+
{ id: "TERM_MISSING", severity: "major", requiresTerms: true },
|
|
1320
|
+
{ id: "SOURCE_LEFTOVER", severity: "major" },
|
|
1321
|
+
{ id: "PUNCT_DUP", severity: "major" },
|
|
1322
|
+
{ id: "TRAILING_COLON", severity: "minor" },
|
|
1323
|
+
// language variant / style class
|
|
1324
|
+
{ id: "SENTENCE_END_PUNCT", severity: "minor" },
|
|
1325
|
+
{ id: "WHITESPACE", severity: "minor" },
|
|
1326
|
+
{ id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
|
|
1327
|
+
{ id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
|
|
1328
|
+
];
|
|
1329
|
+
var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
|
|
1330
|
+
var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
|
|
1331
|
+
var TERM_RULE_IDS = RULES.filter((rule) => rule.requiresTerms).map((rule) => rule.id);
|
|
1332
|
+
var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
|
|
1333
|
+
function normalizeForCompare(value) {
|
|
1334
|
+
return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
|
|
1335
|
+
}
|
|
1336
|
+
var CJK_RE = /[\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff]/u;
|
|
1337
|
+
function cjkFraction(value) {
|
|
1338
|
+
let cjk = 0;
|
|
1339
|
+
let visible = 0;
|
|
1340
|
+
for (const char of value) {
|
|
1341
|
+
if (/\s/u.test(char)) continue;
|
|
1342
|
+
visible += 1;
|
|
1343
|
+
if (CJK_RE.test(char)) cjk += 1;
|
|
1344
|
+
}
|
|
1345
|
+
return visible === 0 ? 0 : cjk / visible;
|
|
1346
|
+
}
|
|
1347
|
+
function isCjkHeavy(value) {
|
|
1348
|
+
return cjkFraction(value) >= 0.3;
|
|
1349
|
+
}
|
|
1350
|
+
function charCount(value) {
|
|
1351
|
+
return Array.from(value).length;
|
|
1352
|
+
}
|
|
1353
|
+
function wordTokens(value) {
|
|
1354
|
+
const matches = value.normalize("NFKC").match(/[\p{L}\p{N}][\p{L}\p{N}'’_-]*/gu);
|
|
1355
|
+
return matches ?? [];
|
|
1356
|
+
}
|
|
1357
|
+
function escapeRegExp(value) {
|
|
1358
|
+
return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
1359
|
+
}
|
|
1360
|
+
function truncate(value, max = 120) {
|
|
1361
|
+
const collapsed = value.replace(/\s+/gu, " ").trim();
|
|
1362
|
+
return collapsed.length <= max ? collapsed : `${collapsed.slice(0, max - 1)}\u2026`;
|
|
1363
|
+
}
|
|
1364
|
+
function multiset(values) {
|
|
1365
|
+
const counts = /* @__PURE__ */ new Map();
|
|
1366
|
+
for (const value of values) counts.set(value, (counts.get(value) ?? 0) + 1);
|
|
1367
|
+
return counts;
|
|
1368
|
+
}
|
|
1369
|
+
function multisetDelta(expected, actual) {
|
|
1370
|
+
const expectedCounts = multiset(expected);
|
|
1371
|
+
const actualCounts = multiset(actual);
|
|
1372
|
+
const missing = [];
|
|
1373
|
+
const extra = [];
|
|
1374
|
+
for (const [value, count] of expectedCounts) {
|
|
1375
|
+
for (let index = actualCounts.get(value) ?? 0; index < count; index += 1) missing.push(value);
|
|
1376
|
+
}
|
|
1377
|
+
for (const [value, count] of actualCounts) {
|
|
1378
|
+
for (let index = expectedCounts.get(value) ?? 0; index < count; index += 1) extra.push(value);
|
|
1379
|
+
}
|
|
1380
|
+
missing.sort();
|
|
1381
|
+
extra.sort();
|
|
1382
|
+
return { missing, extra };
|
|
1383
|
+
}
|
|
1384
|
+
function draft(context, issue) {
|
|
1385
|
+
context.drafts.push(issue);
|
|
1386
|
+
}
|
|
1387
|
+
var PLACEHOLDER_PATTERNS = [
|
|
1388
|
+
/%(?:\d+\$)?[-+#0 ]*\d*(?:\.\d+)?[sdfuxXoc]/gu,
|
|
1389
|
+
/\{\{[^{}\n]{1,64}\}\}/gu,
|
|
1390
|
+
/\$\{[^{}\n]{1,64}\}/gu,
|
|
1391
|
+
/\{[A-Za-z_][A-Za-z0-9_.:-]{0,63}\}/gu,
|
|
1392
|
+
/\{\d{1,6}\}/gu
|
|
1393
|
+
];
|
|
1394
|
+
function collectPlaceholders(value) {
|
|
1395
|
+
const matches = [];
|
|
1396
|
+
for (const pattern of PLACEHOLDER_PATTERNS) {
|
|
1397
|
+
const regex = new RegExp(pattern.source, pattern.flags);
|
|
1398
|
+
for (const match of value.matchAll(regex)) {
|
|
1399
|
+
if (match.index === void 0) continue;
|
|
1400
|
+
matches.push({ text: match[0], start: match.index, end: match.index + match[0].length });
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
matches.sort((left, right) => left.start - right.start || right.end - left.end);
|
|
1404
|
+
const result = [];
|
|
1405
|
+
let lastEnd = -1;
|
|
1406
|
+
for (const match of matches) {
|
|
1407
|
+
if (match.start < lastEnd) continue;
|
|
1408
|
+
result.push(match);
|
|
1409
|
+
lastEnd = match.end;
|
|
1410
|
+
}
|
|
1411
|
+
return result;
|
|
1412
|
+
}
|
|
1413
|
+
function checkPlaceholders(context) {
|
|
1414
|
+
const sourcePlaceholders = collectPlaceholders(context.sourceInner);
|
|
1415
|
+
const targetPlaceholders = collectPlaceholders(context.targetInner);
|
|
1416
|
+
if (sourcePlaceholders.length === 0 && targetPlaceholders.length === 0) return;
|
|
1417
|
+
const delta = multisetDelta(
|
|
1418
|
+
sourcePlaceholders.map((token) => token.text),
|
|
1419
|
+
targetPlaceholders.map((token) => token.text)
|
|
1420
|
+
);
|
|
1421
|
+
if (delta.missing.length > 0) {
|
|
1422
|
+
const first = sourcePlaceholders.find((token) => token.text === delta.missing[0]);
|
|
1423
|
+
draft(context, {
|
|
1424
|
+
ruleId: "PLACEHOLDER_MISSING",
|
|
1425
|
+
message: `\u8BD1\u6587\u7F3A\u5C11\u5360\u4F4D\u7B26: ${delta.missing.join(", ")}`,
|
|
1426
|
+
evidence: `source placeholders: [${sourcePlaceholders.map((token) => token.text).join(", ")}]`,
|
|
1427
|
+
suggestion: `\u4FDD\u7559\u5360\u4F4D\u7B26: ${delta.missing.join(", ")}`,
|
|
1428
|
+
...first === void 0 ? {} : { sourceRange: { start: first.start, end: first.end } }
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
if (delta.extra.length > 0) {
|
|
1432
|
+
const first = targetPlaceholders.find((token) => token.text === delta.extra[0]);
|
|
1433
|
+
draft(context, {
|
|
1434
|
+
ruleId: "PLACEHOLDER_EXTRA",
|
|
1435
|
+
message: `\u8BD1\u6587\u591A\u51FA\u5360\u4F4D\u7B26: ${delta.extra.join(", ")}`,
|
|
1436
|
+
evidence: `target placeholders: [${targetPlaceholders.map((token) => token.text).join(", ")}]`,
|
|
1437
|
+
...first === void 0 ? {} : { targetRange: { start: first.start, end: first.end } }
|
|
1438
|
+
});
|
|
1439
|
+
}
|
|
1440
|
+
}
|
|
1441
|
+
function checkTags(context) {
|
|
1442
|
+
const sourceTags = context.segment.sourceTags;
|
|
1443
|
+
const targetTags = context.segment.targetTags;
|
|
1444
|
+
if (sourceTags.length === 0 && targetTags.length === 0) return;
|
|
1445
|
+
const sourceSignature = sourceTags.join("|");
|
|
1446
|
+
const targetSignature = targetTags.join("|");
|
|
1447
|
+
if (sourceSignature === targetSignature) return;
|
|
1448
|
+
const sameMultiset = sourceTags.length === targetTags.length && JSON.stringify([...sourceTags].sort()) === JSON.stringify([...targetTags].sort());
|
|
1449
|
+
draft(context, {
|
|
1450
|
+
ruleId: "TAG_MISMATCH",
|
|
1451
|
+
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(", ")}]`,
|
|
1452
|
+
evidence: `source tags: [${sourceTags.join(", ")}], target tags: [${targetTags.join(", ")}]`,
|
|
1453
|
+
suggestion: "\u4FDD\u6301 inline tag \u6570\u91CF\u4E0E\u987A\u5E8F\u548C\u6E90\u6587\u4E00\u81F4"
|
|
1454
|
+
});
|
|
1455
|
+
}
|
|
1456
|
+
var URL_PATTERNS = [
|
|
1457
|
+
/\bhttps?:\/\/[^\s<>"']+/giu,
|
|
1458
|
+
/\bwww\.[^\s<>"']+/giu,
|
|
1459
|
+
/\bmailto:[^\s<>"']+/giu,
|
|
1460
|
+
/\b[\w.+-]+@[\w-]+\.[\w.-]+\b/gu,
|
|
1461
|
+
/\b[A-Za-z]:\\[^\s<>"']+/gu,
|
|
1462
|
+
/\\\\[^\s<>"']+/gu,
|
|
1463
|
+
/(?:^|[\s("'[,。;:])(\/(?:[\w.-]+\/)+[\w.-]+\.(?:json|yaml|yml|xml|txt|csv|tsv|png|jpe?g|gif|svg|pdf|docx?|xlsx?|pptx?|zip|log|md|html?|js|ts|css|ini|conf|config|exe|dll|sh|bat|py))\b/giu
|
|
1464
|
+
];
|
|
1465
|
+
function extractUrls(value) {
|
|
1466
|
+
const found = [];
|
|
1467
|
+
for (const pattern of URL_PATTERNS) {
|
|
1468
|
+
const regex = new RegExp(pattern.source, pattern.flags);
|
|
1469
|
+
for (const match of value.matchAll(regex)) {
|
|
1470
|
+
let text = pattern.source.includes("(?:^|[\\s(") ? match[1] ?? "" : match[0];
|
|
1471
|
+
text = text.replace(/[),.;:!?'"\]】。,;:]+$/u, "");
|
|
1472
|
+
if (text.length > 0) found.push(text);
|
|
1473
|
+
}
|
|
1474
|
+
}
|
|
1475
|
+
const unique = /* @__PURE__ */ new Map();
|
|
1476
|
+
for (const item of found) {
|
|
1477
|
+
const key = item.toLocaleLowerCase();
|
|
1478
|
+
if (!unique.has(key)) unique.set(key, item);
|
|
1479
|
+
}
|
|
1480
|
+
return Array.from(unique.values()).sort();
|
|
1481
|
+
}
|
|
1482
|
+
function checkUrls(context) {
|
|
1483
|
+
const sourceUrls = extractUrls(context.sourceInner.length > 0 ? context.sourceInner : context.source);
|
|
1484
|
+
const targetUrls = extractUrls(context.targetInner.length > 0 ? context.targetInner : context.target);
|
|
1485
|
+
if (sourceUrls.length === 0) return;
|
|
1486
|
+
const targetSet = new Set(targetUrls.map((url) => url.toLocaleLowerCase()));
|
|
1487
|
+
const sourceSet = new Set(sourceUrls.map((url) => url.toLocaleLowerCase()));
|
|
1488
|
+
const missing = sourceUrls.filter((url) => !targetSet.has(url.toLocaleLowerCase()));
|
|
1489
|
+
const extra = targetUrls.filter((url) => !sourceSet.has(url.toLocaleLowerCase()));
|
|
1490
|
+
if (missing.length === 0 && extra.length === 0) return;
|
|
1491
|
+
const firstMissing = missing[0];
|
|
1492
|
+
const index = firstMissing === void 0 ? -1 : context.source.indexOf(firstMissing);
|
|
1493
|
+
draft(context, {
|
|
1494
|
+
ruleId: "URL_CHANGED",
|
|
1495
|
+
message: missing.length > 0 ? `\u8BD1\u6587\u7F3A\u5C11/\u6539\u52A8\u4E86 URL\u3001\u90AE\u7BB1\u6216\u8DEF\u5F84: ${missing.join(", ")}` : `\u8BD1\u6587\u591A\u51FA\u6E90\u6587\u6CA1\u6709\u7684 URL\u3001\u90AE\u7BB1\u6216\u8DEF\u5F84: ${extra.join(", ")}`,
|
|
1496
|
+
evidence: `source: [${sourceUrls.join(", ")}] target: [${targetUrls.join(", ")}]`,
|
|
1497
|
+
...firstMissing === void 0 ? {} : { suggestion: `\u4FDD\u7559\u539F\u6587: ${firstMissing}` },
|
|
1498
|
+
...index < 0 || firstMissing === void 0 ? {} : { sourceRange: { start: index, end: index + firstMissing.length } }
|
|
1499
|
+
});
|
|
1500
|
+
}
|
|
1501
|
+
var MONTHS = new Map(
|
|
1502
|
+
[
|
|
1503
|
+
"january",
|
|
1504
|
+
"february",
|
|
1505
|
+
"march",
|
|
1506
|
+
"april",
|
|
1507
|
+
"may",
|
|
1508
|
+
"june",
|
|
1509
|
+
"july",
|
|
1510
|
+
"august",
|
|
1511
|
+
"september",
|
|
1512
|
+
"october",
|
|
1513
|
+
"november",
|
|
1514
|
+
"december"
|
|
1515
|
+
].map((month, index) => [month, index + 1])
|
|
1516
|
+
);
|
|
1517
|
+
var MONTH_ALIASES = new Map([
|
|
1518
|
+
...MONTHS,
|
|
1519
|
+
...Array.from(MONTHS, ([month, number2]) => [month.slice(0, 3), number2]),
|
|
1520
|
+
["sept", 9]
|
|
1521
|
+
]);
|
|
1522
|
+
var CJK_DIGITS = /* @__PURE__ */ new Map([
|
|
1523
|
+
["\u3007", "0"],
|
|
1524
|
+
["\u96F6", "0"],
|
|
1525
|
+
["\u4E00", "1"],
|
|
1526
|
+
["\u4E8C", "2"],
|
|
1527
|
+
["\u4E09", "3"],
|
|
1528
|
+
["\u56DB", "4"],
|
|
1529
|
+
["\u4E94", "5"],
|
|
1530
|
+
["\u516D", "6"],
|
|
1531
|
+
["\u4E03", "7"],
|
|
1532
|
+
["\u516B", "8"],
|
|
1533
|
+
["\u4E5D", "9"]
|
|
1534
|
+
]);
|
|
1535
|
+
function pad2(value) {
|
|
1536
|
+
return String(value).padStart(2, "0");
|
|
1537
|
+
}
|
|
1538
|
+
function validMonthDay(month, day) {
|
|
1539
|
+
return month >= 1 && month <= 12 && day >= 1 && day <= 31;
|
|
1540
|
+
}
|
|
1541
|
+
var DATE_PATTERNS = [
|
|
1542
|
+
/\b(\d{4})[-/.](\d{1,2})[-/.](\d{1,2})\b/gu,
|
|
1543
|
+
/\b(\d{1,2})[-/.](\d{1,2})[-/.](\d{2,4})\b/gu,
|
|
1544
|
+
/(\d{4}|[〇零一二三四五六七八九]{4})年(\d{1,2})月(?:(\d{1,2})日)?/gu
|
|
1545
|
+
];
|
|
1546
|
+
function extractDates(value) {
|
|
1547
|
+
const tokens = [];
|
|
1548
|
+
const push = (key, text, start) => {
|
|
1549
|
+
tokens.push({ key, text, span: { start, end: start + text.length } });
|
|
1550
|
+
};
|
|
1551
|
+
for (const match of value.matchAll(DATE_PATTERNS[0] ?? /$^/gu)) {
|
|
1552
|
+
const [text = "", year = "", month = "", day = ""] = match;
|
|
1553
|
+
if (match.index === void 0) continue;
|
|
1554
|
+
const monthNumber = Number(month);
|
|
1555
|
+
const dayNumber = Number(day);
|
|
1556
|
+
if (!validMonthDay(monthNumber, dayNumber)) continue;
|
|
1557
|
+
push(`${year}-${pad2(monthNumber)}-${pad2(dayNumber)}`, text, match.index);
|
|
1558
|
+
}
|
|
1559
|
+
for (const match of value.matchAll(DATE_PATTERNS[1] ?? /$^/gu)) {
|
|
1560
|
+
const [text = "", first = "", second = "", yearRaw = ""] = match;
|
|
1561
|
+
if (match.index === void 0) continue;
|
|
1562
|
+
let day = Number(first);
|
|
1563
|
+
let month = Number(second);
|
|
1564
|
+
if (month > 12 && day <= 12) {
|
|
1565
|
+
const swap = day;
|
|
1566
|
+
day = month;
|
|
1567
|
+
month = swap;
|
|
1568
|
+
}
|
|
1569
|
+
if (!validMonthDay(month, day)) continue;
|
|
1570
|
+
if (yearRaw.length === 2) {
|
|
1571
|
+
push(`${yearRaw}-${pad2(month)}-${pad2(day)}`, text, match.index);
|
|
1572
|
+
continue;
|
|
1573
|
+
}
|
|
1574
|
+
const year = Number(yearRaw);
|
|
1575
|
+
if (year < 1e3 || year > 2999) continue;
|
|
1576
|
+
push(`${year}-${pad2(month)}-${pad2(day)}`, text, match.index);
|
|
1577
|
+
}
|
|
1578
|
+
for (const match of value.matchAll(DATE_PATTERNS[2] ?? /$^/gu)) {
|
|
1579
|
+
const [text = "", yearRaw = "", month = "", day] = match;
|
|
1580
|
+
if (match.index === void 0) continue;
|
|
1581
|
+
let year;
|
|
1582
|
+
if (/^\d{4}$/u.test(yearRaw)) {
|
|
1583
|
+
year = yearRaw;
|
|
1584
|
+
} else {
|
|
1585
|
+
let converted = "";
|
|
1586
|
+
for (const char of yearRaw) converted += CJK_DIGITS.get(char) ?? "";
|
|
1587
|
+
if (converted.length !== 4) continue;
|
|
1588
|
+
year = converted;
|
|
1589
|
+
}
|
|
1590
|
+
const monthNumber = Number(month);
|
|
1591
|
+
if (!validMonthDay(monthNumber, 1)) continue;
|
|
1592
|
+
if (day === void 0) {
|
|
1593
|
+
push(`${year}-${pad2(monthNumber)}`, text, match.index);
|
|
1594
|
+
} else {
|
|
1595
|
+
const dayNumber = Number(day);
|
|
1596
|
+
if (!validMonthDay(monthNumber, dayNumber)) continue;
|
|
1597
|
+
push(`${year}-${pad2(monthNumber)}-${pad2(dayNumber)}`, text, match.index);
|
|
1598
|
+
}
|
|
1599
|
+
}
|
|
1600
|
+
const monthNames = Array.from(MONTH_ALIASES.keys()).sort((left, right) => right.length - left.length).map(escapeRegExp).join("|");
|
|
1601
|
+
const mdy = new RegExp(
|
|
1602
|
+
`\\b(${monthNames})[ .]+(\\d{1,2})(?:st|nd|rd|th)?(?:[, ]+(\\d{4}))?\\b`,
|
|
1603
|
+
"giu"
|
|
1604
|
+
);
|
|
1605
|
+
for (const match of value.matchAll(mdy)) {
|
|
1606
|
+
const [text = "", monthRaw = "", day = "", year] = match;
|
|
1607
|
+
if (match.index === void 0) continue;
|
|
1608
|
+
const month = MONTH_ALIASES.get(monthRaw.toLowerCase());
|
|
1609
|
+
if (month === void 0) continue;
|
|
1610
|
+
const dayNumber = Number(day);
|
|
1611
|
+
if (!validMonthDay(month, dayNumber)) continue;
|
|
1612
|
+
push(
|
|
1613
|
+
year === void 0 ? `?-${pad2(month)}-${pad2(dayNumber)}` : `${year}-${pad2(month)}-${pad2(dayNumber)}`,
|
|
1614
|
+
text,
|
|
1615
|
+
match.index
|
|
1616
|
+
);
|
|
1617
|
+
}
|
|
1618
|
+
const dmy = new RegExp(`\\b(\\d{1,2})(?:st|nd|rd|th)?[ .]+(${monthNames})[ .]*(\\d{4})?\\b`, "giu");
|
|
1619
|
+
for (const match of value.matchAll(dmy)) {
|
|
1620
|
+
const [text = "", day = "", monthRaw = "", year] = match;
|
|
1621
|
+
if (match.index === void 0) continue;
|
|
1622
|
+
const month = MONTH_ALIASES.get(monthRaw.toLowerCase());
|
|
1623
|
+
if (month === void 0) continue;
|
|
1624
|
+
const dayNumber = Number(day);
|
|
1625
|
+
if (!validMonthDay(month, dayNumber)) continue;
|
|
1626
|
+
push(
|
|
1627
|
+
year === void 0 ? `?-${pad2(month)}-${pad2(dayNumber)}` : `${year}-${pad2(month)}-${pad2(dayNumber)}`,
|
|
1628
|
+
text,
|
|
1629
|
+
match.index
|
|
1630
|
+
);
|
|
1631
|
+
}
|
|
1632
|
+
tokens.sort((left, right) => left.span.start - right.span.start);
|
|
1633
|
+
const result = [];
|
|
1634
|
+
let lastEnd = -1;
|
|
1635
|
+
for (const token of tokens) {
|
|
1636
|
+
if (token.span.start < lastEnd) continue;
|
|
1637
|
+
result.push(token);
|
|
1638
|
+
lastEnd = token.span.end;
|
|
1639
|
+
}
|
|
1640
|
+
return result;
|
|
1641
|
+
}
|
|
1642
|
+
function maskSpans(value, spans) {
|
|
1643
|
+
if (spans.length === 0) return value;
|
|
1644
|
+
const chars = Array.from(value);
|
|
1645
|
+
for (const span of spans) {
|
|
1646
|
+
for (let index = span.start; index < span.end && index < chars.length; index += 1) chars[index] = " ";
|
|
1647
|
+
}
|
|
1648
|
+
return chars.join("");
|
|
1649
|
+
}
|
|
1650
|
+
var NUMBER_RE = /(?<![\p{L}\p{N}_])[-+]?\d+(?:[.,]\d+)*(?:[eE][-+]?\d+)?(?![\p{L}\p{N}_])/gu;
|
|
1651
|
+
function normalizeNumber(raw) {
|
|
1652
|
+
let value = raw;
|
|
1653
|
+
const sign = value.startsWith("-") ? "-" : "";
|
|
1654
|
+
value = value.replace(/^[+-]/u, "");
|
|
1655
|
+
if (/^\d{1,3}(?:,\d{3})+(?:\.\d+)?$/u.test(value)) {
|
|
1656
|
+
value = value.replaceAll(",", "");
|
|
1657
|
+
} else if (/^\d{1,3}(?:\.\d{3}){2,}$/u.test(value)) {
|
|
1658
|
+
value = value.replaceAll(".", "");
|
|
1659
|
+
}
|
|
1660
|
+
if (!/^\d/u.test(value)) return raw.normalize("NFKC");
|
|
1661
|
+
if (/^\d+\.0+$/u.test(value)) value = value.replace(/\.0+$/u, "");
|
|
1662
|
+
return `${sign}${value}`;
|
|
1663
|
+
}
|
|
1664
|
+
function extractNumbers(value) {
|
|
1665
|
+
const masked = maskSpans(
|
|
1666
|
+
value,
|
|
1667
|
+
extractDates(value).map((token) => token.span)
|
|
1668
|
+
);
|
|
1669
|
+
const results = [];
|
|
1670
|
+
for (const match of masked.matchAll(NUMBER_RE)) results.push(normalizeNumber(match[0]));
|
|
1671
|
+
return results;
|
|
1672
|
+
}
|
|
1673
|
+
function checkNumbers(context) {
|
|
1674
|
+
const sourceNumbers = extractNumbers(context.source);
|
|
1675
|
+
const targetNumbers = extractNumbers(context.target);
|
|
1676
|
+
if (sourceNumbers.length === 0 && targetNumbers.length === 0) return;
|
|
1677
|
+
const delta = multisetDelta(sourceNumbers, targetNumbers);
|
|
1678
|
+
if (delta.missing.length === 0 && delta.extra.length === 0) return;
|
|
1679
|
+
const firstMissing = delta.missing[0];
|
|
1680
|
+
const index = firstMissing === void 0 ? -1 : context.target.indexOf(firstMissing);
|
|
1681
|
+
draft(context, {
|
|
1682
|
+
ruleId: "NUMBER_MISMATCH",
|
|
1683
|
+
message: delta.missing.length > 0 ? `\u8BD1\u6587\u7F3A\u5C11/\u6539\u52A8\u4E86\u6570\u5B57: ${delta.missing.join(", ")}` : `\u8BD1\u6587\u591A\u51FA\u6570\u5B57: ${delta.extra.join(", ")}`,
|
|
1684
|
+
evidence: `source numbers: [${sourceNumbers.join(", ")}] target numbers: [${targetNumbers.join(", ")}]`,
|
|
1685
|
+
...firstMissing === void 0 ? {} : { suggestion: `\u6838\u5BF9\u6570\u5B57: ${firstMissing}` },
|
|
1686
|
+
...index < 0 || firstMissing === void 0 ? {} : { targetRange: { start: index, end: index + firstMissing.length } }
|
|
1687
|
+
});
|
|
1688
|
+
}
|
|
1689
|
+
function checkDates(context) {
|
|
1690
|
+
const sourceDates = extractDates(context.source);
|
|
1691
|
+
const targetDates = extractDates(context.target);
|
|
1692
|
+
if (sourceDates.length === 0 && targetDates.length === 0) return;
|
|
1693
|
+
const delta = multisetDelta(
|
|
1694
|
+
sourceDates.map((token) => token.key),
|
|
1695
|
+
targetDates.map((token) => token.key)
|
|
1696
|
+
);
|
|
1697
|
+
if (delta.missing.length === 0 && delta.extra.length === 0) return;
|
|
1698
|
+
const firstMissing = sourceDates.find((token) => delta.missing.includes(token.key));
|
|
1699
|
+
draft(context, {
|
|
1700
|
+
ruleId: "DATE_MISMATCH",
|
|
1701
|
+
message: delta.missing.length > 0 ? `\u8BD1\u6587\u7F3A\u5C11/\u6539\u52A8\u4E86\u65E5\u671F: ${delta.missing.join(", ")}` : `\u8BD1\u6587\u591A\u51FA\u65E5\u671F: ${delta.extra.join(", ")}`,
|
|
1702
|
+
evidence: `source dates: [${sourceDates.map((token) => token.text).join(", ")}] target dates: [${targetDates.map((token) => token.text).join(", ")}]`,
|
|
1703
|
+
...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
|
|
1704
|
+
});
|
|
1705
|
+
}
|
|
1706
|
+
var BRACKET_PAIRS = /* @__PURE__ */ new Map([
|
|
1707
|
+
["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
|
|
1708
|
+
["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
|
|
1709
|
+
["[", /* @__PURE__ */ new Set(["]", "\u3011"])],
|
|
1710
|
+
["\u3010", /* @__PURE__ */ new Set(["]", "\u3011"])],
|
|
1711
|
+
["{", /* @__PURE__ */ new Set(["}", "\uFF5D"])],
|
|
1712
|
+
["\uFF5B", /* @__PURE__ */ new Set(["}", "\uFF5D"])],
|
|
1713
|
+
["\u3008", /* @__PURE__ */ new Set(["\u3009", ">"])],
|
|
1714
|
+
["\u300A", /* @__PURE__ */ new Set(["\u300B"])],
|
|
1715
|
+
["\u300C", /* @__PURE__ */ new Set(["\u300D"])],
|
|
1716
|
+
["\u300E", /* @__PURE__ */ new Set(["\u300F"])],
|
|
1717
|
+
["<", /* @__PURE__ */ new Set([">"])]
|
|
1718
|
+
]);
|
|
1719
|
+
var CLOSE_TO_OPEN = /* @__PURE__ */ new Map([
|
|
1720
|
+
[")", "("],
|
|
1721
|
+
["\uFF09", "\uFF08"],
|
|
1722
|
+
["]", "["],
|
|
1723
|
+
["\u3011", "\u3010"],
|
|
1724
|
+
["}", "{"],
|
|
1725
|
+
["\uFF5D", "\uFF5B"],
|
|
1726
|
+
["\u3009", "\u3008"],
|
|
1727
|
+
["\u300B", "\u300A"],
|
|
1728
|
+
["\u300D", "\u300C"],
|
|
1729
|
+
["\u300F", "\u300E"],
|
|
1730
|
+
[">", "<"]
|
|
1731
|
+
]);
|
|
1732
|
+
function unpairedTokens(value) {
|
|
1733
|
+
const result = [];
|
|
1734
|
+
const stack = [];
|
|
1735
|
+
let offset = 0;
|
|
1736
|
+
for (const char of value) {
|
|
1737
|
+
const closes = BRACKET_PAIRS.get(char);
|
|
1738
|
+
if (closes) {
|
|
1739
|
+
stack.push({ open: char, index: offset });
|
|
1740
|
+
offset += 1;
|
|
1741
|
+
continue;
|
|
1742
|
+
}
|
|
1743
|
+
const expectedOpen = CLOSE_TO_OPEN.get(char);
|
|
1744
|
+
if (expectedOpen) {
|
|
1745
|
+
const top = stack[stack.length - 1];
|
|
1746
|
+
const topCloses = top === void 0 ? void 0 : BRACKET_PAIRS.get(top.open);
|
|
1747
|
+
if (top !== void 0 && topCloses?.has(char) === true) {
|
|
1748
|
+
stack.pop();
|
|
1749
|
+
} else {
|
|
1750
|
+
result.push({ char, index: offset, kind: "bracket" });
|
|
1751
|
+
}
|
|
1752
|
+
}
|
|
1753
|
+
offset += 1;
|
|
1754
|
+
}
|
|
1755
|
+
for (const entry of stack) result.push({ char: entry.open, index: entry.index, kind: "bracket" });
|
|
1756
|
+
const quotePairs = [["\u201C", "\u201D"], ["\u2018", "\u2019"]];
|
|
1757
|
+
for (const [open, close] of quotePairs) {
|
|
1758
|
+
const openCount = Array.from(value).filter((char) => char === open).length;
|
|
1759
|
+
const closeCount = Array.from(value).filter((char) => char === close).length;
|
|
1760
|
+
if (openCount !== closeCount) {
|
|
1761
|
+
const index = value.indexOf(openCount > closeCount ? open : close);
|
|
1762
|
+
result.push({ char: openCount > closeCount ? open : close, index: index < 0 ? 0 : index, kind: "quote" });
|
|
1763
|
+
}
|
|
1764
|
+
}
|
|
1765
|
+
const straightQuotes = Array.from(value).filter((char) => char === '"').length;
|
|
1766
|
+
if (straightQuotes % 2 === 1) {
|
|
1767
|
+
const index = value.indexOf('"');
|
|
1768
|
+
result.push({ char: '"', index: index < 0 ? 0 : index, kind: "quote" });
|
|
1769
|
+
}
|
|
1770
|
+
result.sort((left, right) => left.index - right.index);
|
|
1771
|
+
return result;
|
|
1772
|
+
}
|
|
1773
|
+
function checkUnpairedBrackets(context) {
|
|
1774
|
+
if (context.target.trim().length === 0) return;
|
|
1775
|
+
const unpaired = unpairedTokens(context.target);
|
|
1776
|
+
if (unpaired.length === 0) return;
|
|
1777
|
+
const first = unpaired[0];
|
|
1778
|
+
draft(context, {
|
|
1779
|
+
ruleId: "UNPAIRED_BRACKET",
|
|
1780
|
+
message: `\u8BD1\u6587\u62EC\u53F7/\u5F15\u53F7\u4E0D\u914D\u5BF9: ${unpaired.slice(0, 5).map((entry) => `'${entry.char}'`).join(", ")}`,
|
|
1781
|
+
evidence: `text: ${truncate(context.target)}`,
|
|
1782
|
+
suggestion: "\u68C0\u67E5\u62EC\u53F7\u3001\u5F15\u53F7\u662F\u5426\u6210\u5BF9\uFF08\u5168\u89D2/\u534A\u89D2\u9700\u5BF9\u5E94\uFF09",
|
|
1783
|
+
...first === void 0 ? {} : { targetRange: { start: first.index, end: first.index + 1 } }
|
|
1784
|
+
});
|
|
1785
|
+
}
|
|
1786
|
+
var CJK_REDUPLICATION_OK = /* @__PURE__ */ new Set([
|
|
1787
|
+
"\u8C22\u8C22",
|
|
1788
|
+
"\u770B\u770B",
|
|
1789
|
+
"\u6162\u6162",
|
|
1790
|
+
"\u521A\u521A",
|
|
1791
|
+
"\u5988\u5988",
|
|
1792
|
+
"\u7238\u7238",
|
|
1793
|
+
"\u54E5\u54E5",
|
|
1794
|
+
"\u59D0\u59D0",
|
|
1795
|
+
"\u5F1F\u5F1F",
|
|
1796
|
+
"\u59B9\u59B9",
|
|
1797
|
+
"\u661F\u661F",
|
|
1798
|
+
"\u5929\u5929",
|
|
1799
|
+
"\u5E74\u5E74",
|
|
1800
|
+
"\u4EBA\u4EBA",
|
|
1801
|
+
"\u5904\u5904",
|
|
1802
|
+
"\u65F6\u65F6",
|
|
1803
|
+
"\u597D\u597D",
|
|
1804
|
+
"\u591A\u591A",
|
|
1805
|
+
"\u5E38\u5E38",
|
|
1806
|
+
"\u5F80\u5F80",
|
|
1807
|
+
"\u6E10\u6E10",
|
|
1808
|
+
"\u9ED8\u9ED8",
|
|
1809
|
+
"\u6084\u6084",
|
|
1810
|
+
"\u8F7B\u8F7B",
|
|
1811
|
+
"\u6DF1\u6DF1",
|
|
1812
|
+
"\u8FDC\u8FDC",
|
|
1813
|
+
"\u9AD8\u9AD8",
|
|
1814
|
+
"\u5927\u5927",
|
|
1815
|
+
"\u5C0F\u5C0F",
|
|
1816
|
+
"\u7EA2\u7EA2",
|
|
1817
|
+
"\u767D\u767D",
|
|
1818
|
+
"\u4E00\u4E00",
|
|
1819
|
+
"\u4E2A\u4E2A",
|
|
1820
|
+
"\u4EF6\u4EF6",
|
|
1821
|
+
"\u6761\u6761",
|
|
1822
|
+
"\u79CD\u79CD",
|
|
1823
|
+
"\u5C42\u5C42",
|
|
1824
|
+
"\u6B65\u6B65",
|
|
1825
|
+
"\u6B21\u6B21",
|
|
1826
|
+
"\u591C\u591C",
|
|
1827
|
+
"\u58F0\u58F0",
|
|
1828
|
+
"\u5B57\u5B57",
|
|
1829
|
+
"\u53E5\u53E5",
|
|
1830
|
+
"\u6735\u6735",
|
|
1831
|
+
"\u7247\u7247",
|
|
1832
|
+
"\u70B9\u70B9",
|
|
1833
|
+
"\u6EF4\u6EF4",
|
|
1834
|
+
"\u4E1D\u4E1D",
|
|
1835
|
+
"\u9635\u9635",
|
|
1836
|
+
"\u7FA4\u7FA4",
|
|
1837
|
+
"\u961F\u961F",
|
|
1838
|
+
"\u672C\u672C",
|
|
1839
|
+
"\u53F0\u53F0",
|
|
1840
|
+
"\u5BB6\u5BB6",
|
|
1841
|
+
"\u6237\u6237",
|
|
1842
|
+
"\u6751\u6751",
|
|
1843
|
+
"\u9547\u9547",
|
|
1844
|
+
"\u4EC5\u4EC5",
|
|
1845
|
+
"\u504F\u504F",
|
|
1846
|
+
"\u6070\u6070",
|
|
1847
|
+
"\u7EDF\u7EDF",
|
|
1848
|
+
"\u901A\u901A",
|
|
1849
|
+
"\u5386\u5386",
|
|
1850
|
+
"\u803F\u803F",
|
|
1851
|
+
"\u6829\u6829",
|
|
1852
|
+
"\u6D25\u6D25",
|
|
1853
|
+
"\u8DC3\u8DC3",
|
|
1854
|
+
"\u6124\u6124",
|
|
1855
|
+
"\u60BB\u60BB",
|
|
1856
|
+
"\u7A83\u7A83",
|
|
1857
|
+
"\u59D7\u59D7",
|
|
1858
|
+
"\u5F90\u5F90",
|
|
1859
|
+
"\u7F13\u7F13",
|
|
1860
|
+
"\u5189\u5189",
|
|
1861
|
+
"\u6ED4\u6ED4",
|
|
1862
|
+
"\u6EDA\u6EDA",
|
|
1863
|
+
"\u832B\u832B",
|
|
1864
|
+
"\u82CD\u82CD",
|
|
1865
|
+
"\u90C1\u90C1",
|
|
1866
|
+
"\u8471\u8471",
|
|
1867
|
+
"\u54D7\u54D7",
|
|
1868
|
+
"\u6C99\u6C99",
|
|
1869
|
+
"\u9686\u9686",
|
|
1870
|
+
"\u549A\u549A",
|
|
1871
|
+
"\u545C\u545C",
|
|
1872
|
+
"\u6C6A\u6C6A",
|
|
1873
|
+
"\u55B5\u55B5",
|
|
1874
|
+
"\u54A9\u54A9"
|
|
1875
|
+
]);
|
|
1876
|
+
function checkDuplicateWords(context) {
|
|
1877
|
+
const target = context.target;
|
|
1878
|
+
if (target.trim().length === 0) return;
|
|
1879
|
+
let emitted = 0;
|
|
1880
|
+
const latin = /(?<![\p{L}\p{N}])([\p{L}][\p{L}'’_-]{1,})(\s+)\1(?![\p{L}\p{N}])/giu;
|
|
1881
|
+
for (const match of target.matchAll(latin)) {
|
|
1882
|
+
if (match.index === void 0) continue;
|
|
1883
|
+
draft(context, {
|
|
1884
|
+
ruleId: "DUPLICATE_WORD",
|
|
1885
|
+
message: `\u76F8\u90BB\u91CD\u590D\u8BCD: '${match[1]} ${match[1]}'`,
|
|
1886
|
+
evidence: truncate(match[0]),
|
|
1887
|
+
targetRange: { start: match.index, end: match.index + match[0].length }
|
|
1888
|
+
});
|
|
1889
|
+
emitted += 1;
|
|
1890
|
+
if (emitted >= 3) return;
|
|
1891
|
+
}
|
|
1892
|
+
const chars = Array.from(target);
|
|
1893
|
+
let cjkOffset = 0;
|
|
1894
|
+
for (let index = 0; index + 1 < chars.length; index += 1) {
|
|
1895
|
+
const first = chars[index];
|
|
1896
|
+
const second = chars[index + 1];
|
|
1897
|
+
if (first === void 0 || second === void 0 || first !== second || !CJK_RE.test(first)) {
|
|
1898
|
+
cjkOffset += first === void 0 ? 0 : charCount(first);
|
|
1899
|
+
continue;
|
|
1900
|
+
}
|
|
1901
|
+
const pair = first + second;
|
|
1902
|
+
if (!CJK_REDUPLICATION_OK.has(pair)) {
|
|
1903
|
+
draft(context, {
|
|
1904
|
+
ruleId: "DUPLICATE_WORD",
|
|
1905
|
+
message: `CJK \u91CD\u590D\u5B57: '${pair}'`,
|
|
1906
|
+
evidence: `text: ${truncate(target)}`,
|
|
1907
|
+
targetRange: { start: cjkOffset, end: cjkOffset + 2 }
|
|
1908
|
+
});
|
|
1909
|
+
emitted += 1;
|
|
1910
|
+
if (emitted >= 3) return;
|
|
1911
|
+
}
|
|
1912
|
+
cjkOffset += 2;
|
|
1913
|
+
index += 1;
|
|
1914
|
+
}
|
|
1915
|
+
}
|
|
1916
|
+
var PUNCT_DUP_EXEMPT = /* @__PURE__ */ new Set(["\u2026", "\u2014", "\uFF0D", "*", "=", "-", "~", "\uFF5E", "/"]);
|
|
1917
|
+
function checkPunctDup(context) {
|
|
1918
|
+
if (context.target.trim().length === 0) return;
|
|
1919
|
+
const regex = /([!?.,;:。?!,、;:])\1+/gu;
|
|
1920
|
+
let emitted = 0;
|
|
1921
|
+
for (const match of context.target.matchAll(regex)) {
|
|
1922
|
+
if (match.index === void 0) continue;
|
|
1923
|
+
if (PUNCT_DUP_EXEMPT.has(match[1] ?? "")) continue;
|
|
1924
|
+
draft(context, {
|
|
1925
|
+
ruleId: "PUNCT_DUP",
|
|
1926
|
+
message: `\u8FDE\u7EED\u91CD\u590D\u6807\u70B9: '${match[0]}'`,
|
|
1927
|
+
evidence: match[0],
|
|
1928
|
+
targetRange: { start: match.index, end: match.index + match[0].length }
|
|
1929
|
+
});
|
|
1930
|
+
emitted += 1;
|
|
1931
|
+
if (emitted >= 3) return;
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
function checkSourceLeftover(context) {
|
|
1935
|
+
const { source, target } = context;
|
|
1936
|
+
if (source.trim().length === 0 || target.trim().length === 0) return;
|
|
1937
|
+
if (normalizeForCompare(source) === normalizeForCompare(target)) return;
|
|
1938
|
+
const normalizedTarget = normalizeForCompare(target);
|
|
1939
|
+
for (const match of source.normalize("NFKC").matchAll(/[\u4e00-\u9fff]{4,}/gu)) {
|
|
1940
|
+
const run = match[0];
|
|
1941
|
+
const index = normalizedTarget.indexOf(run);
|
|
1942
|
+
if (index < 0) continue;
|
|
1943
|
+
draft(context, {
|
|
1944
|
+
ruleId: "SOURCE_LEFTOVER",
|
|
1945
|
+
message: `\u8BD1\u6587\u6B8B\u7559\u6E90\u6587\u7247\u6BB5: '${truncate(run)}'`,
|
|
1946
|
+
evidence: `source run: ${truncate(run)}`,
|
|
1947
|
+
targetRange: { start: index, end: index + run.length }
|
|
1948
|
+
});
|
|
1949
|
+
return;
|
|
1950
|
+
}
|
|
1951
|
+
const tokens = wordTokens(source);
|
|
1952
|
+
if (tokens.length < 3) return;
|
|
1953
|
+
for (let start = 0; start + 3 <= tokens.length; start += 1) {
|
|
1954
|
+
const window = tokens.slice(start, start + 3);
|
|
1955
|
+
if (window.some((token) => token.length < 2)) continue;
|
|
1956
|
+
const pattern = new RegExp(`\\b${window.map(escapeRegExp).join("\\s+")}\\b`, "iu");
|
|
1957
|
+
const match = pattern.exec(target);
|
|
1958
|
+
if (!match || match.index === void 0) continue;
|
|
1959
|
+
draft(context, {
|
|
1960
|
+
ruleId: "SOURCE_LEFTOVER",
|
|
1961
|
+
message: `\u8BD1\u6587\u6B8B\u7559\u6E90\u6587\u7247\u6BB5: '${window.join(" ")}'`,
|
|
1962
|
+
evidence: `source window: ${window.join(" ")}`,
|
|
1963
|
+
targetRange: { start: match.index, end: match.index + match[0].length }
|
|
1964
|
+
});
|
|
1965
|
+
return;
|
|
1966
|
+
}
|
|
1967
|
+
}
|
|
1968
|
+
function checkTrailingColon(context) {
|
|
1969
|
+
const source = context.source.trimEnd();
|
|
1970
|
+
const target = context.target.trimEnd();
|
|
1971
|
+
if (target.length === 0) return;
|
|
1972
|
+
if (!source.endsWith(":") && !source.endsWith("\uFF1A")) return;
|
|
1973
|
+
if (target.endsWith(":") || target.endsWith("\uFF1A")) return;
|
|
1974
|
+
draft(context, {
|
|
1975
|
+
ruleId: "TRAILING_COLON",
|
|
1976
|
+
message: "\u6E90\u6587\u4EE5\u5192\u53F7\u7ED3\u5C3E\uFF0C\u8BD1\u6587\u672A\u4EE5\u5192\u53F7\u7ED3\u5C3E",
|
|
1977
|
+
evidence: `source tail: '${truncate(source.slice(-30))}' target tail: '${truncate(target.slice(-30))}'`,
|
|
1978
|
+
suggestion: "\u4FDD\u6301\u5217\u8868\u5F15\u5BFC\u5192\u53F7\u4E00\u81F4"
|
|
1979
|
+
});
|
|
1980
|
+
}
|
|
1981
|
+
var TERMINAL_CLASS = /* @__PURE__ */ new Map([
|
|
1982
|
+
[".", "period"],
|
|
1983
|
+
["\u3002", "period"],
|
|
1984
|
+
["\uFF0E", "period"],
|
|
1985
|
+
["?", "question"],
|
|
1986
|
+
["\uFF1F", "question"],
|
|
1987
|
+
["!", "exclamation"],
|
|
1988
|
+
["\uFF01", "exclamation"]
|
|
1989
|
+
]);
|
|
1990
|
+
var TRAILING_CLOSERS = /* @__PURE__ */ new Set([")", "\uFF09", "]", "\u3011", "\u300D", "\u300F", "\u201D", "\u2019", '"', "'", "\u300B", "\u3009"]);
|
|
1991
|
+
function terminalPunctuation(value) {
|
|
1992
|
+
for (let index = value.length - 1; index >= 0; index -= 1) {
|
|
1993
|
+
const char = value[index];
|
|
1994
|
+
if (char === void 0) return void 0;
|
|
1995
|
+
if (/\s/u.test(char)) continue;
|
|
1996
|
+
if (TRAILING_CLOSERS.has(char)) continue;
|
|
1997
|
+
const className = TERMINAL_CLASS.get(char);
|
|
1998
|
+
if (className === void 0) return void 0;
|
|
1999
|
+
return { char, index, className };
|
|
2000
|
+
}
|
|
2001
|
+
return void 0;
|
|
2002
|
+
}
|
|
2003
|
+
function checkSentenceEndPunct(context) {
|
|
2004
|
+
const source = context.source.trimEnd();
|
|
2005
|
+
const target = context.target.trimEnd();
|
|
2006
|
+
if (source.length === 0 || target.length === 0) return;
|
|
2007
|
+
if (source.endsWith("...") || source.endsWith("\u2026")) return;
|
|
2008
|
+
const sourceTerminal = terminalPunctuation(source);
|
|
2009
|
+
if (sourceTerminal === void 0) return;
|
|
2010
|
+
if (sourceTerminal.className === "period" && wordTokens(source).length <= 2) return;
|
|
2011
|
+
const targetTerminal = terminalPunctuation(target);
|
|
2012
|
+
if (targetTerminal === void 0) {
|
|
2013
|
+
draft(context, {
|
|
2014
|
+
ruleId: "SENTENCE_END_PUNCT",
|
|
2015
|
+
message: "\u8BD1\u6587\u7F3A\u5C11\u53E5\u672B\u6807\u70B9",
|
|
2016
|
+
evidence: `source ends with '${sourceTerminal.char}', target has no sentence-final punctuation`,
|
|
2017
|
+
suggestion: context.targetIsCjk ? "\u8865\u5168\u5168\u89D2\u53E5\u672B\u6807\u70B9" : "\u8865\u5168\u53E5\u672B\u6807\u70B9"
|
|
2018
|
+
});
|
|
2019
|
+
return;
|
|
2020
|
+
}
|
|
2021
|
+
if (targetTerminal.className === sourceTerminal.className) return;
|
|
2022
|
+
draft(context, {
|
|
2023
|
+
ruleId: "SENTENCE_END_PUNCT",
|
|
2024
|
+
message: `\u53E5\u672B\u6807\u70B9\u7C7B\u578B\u4E0D\u5339\u914D: \u6E90\u6587 '${sourceTerminal.char}' / \u8BD1\u6587 '${targetTerminal.char}'`,
|
|
2025
|
+
evidence: `source tail: '${truncate(source.slice(-20))}' target tail: '${truncate(target.slice(-20))}'`,
|
|
2026
|
+
targetRange: { start: targetTerminal.index, end: targetTerminal.index + 1 }
|
|
2027
|
+
});
|
|
2028
|
+
}
|
|
2029
|
+
function checkWhitespace(context) {
|
|
2030
|
+
const raw = context.targetInner;
|
|
2031
|
+
if (raw.length === 0) return;
|
|
2032
|
+
if (raw !== raw.trim()) {
|
|
2033
|
+
draft(context, {
|
|
2034
|
+
ruleId: "WHITESPACE",
|
|
2035
|
+
message: "\u8BD1\u6587\u5B58\u5728\u9996\u5C3E\u7A7A\u767D",
|
|
2036
|
+
evidence: "target inner starts/ends with whitespace",
|
|
2037
|
+
suggestion: "\u5220\u9664\u9996\u5C3E\u7A7A\u767D"
|
|
2038
|
+
});
|
|
2039
|
+
}
|
|
2040
|
+
const doubleSpace = /[ \t]{2,}/u.exec(raw);
|
|
2041
|
+
if (doubleSpace && doubleSpace.index !== void 0) {
|
|
2042
|
+
draft(context, {
|
|
2043
|
+
ruleId: "WHITESPACE",
|
|
2044
|
+
message: "\u8BD1\u6587\u5B58\u5728\u8FDE\u7EED\u7A7A\u683C",
|
|
2045
|
+
evidence: "target contains 2+ consecutive spaces/tabs",
|
|
2046
|
+
targetRange: { start: doubleSpace.index, end: doubleSpace.index + doubleSpace[0].length }
|
|
2047
|
+
});
|
|
2048
|
+
}
|
|
2049
|
+
const ideographic = raw.indexOf("\u3000");
|
|
2050
|
+
if (ideographic >= 0) {
|
|
2051
|
+
draft(context, {
|
|
2052
|
+
ruleId: "WHITESPACE",
|
|
2053
|
+
message: "\u8BD1\u6587\u542B\u5168\u89D2\u7A7A\u683C\uFF08U+3000\uFF09",
|
|
2054
|
+
evidence: "target contains ideographic space",
|
|
2055
|
+
targetRange: { start: ideographic, end: ideographic + 1 }
|
|
2056
|
+
});
|
|
2057
|
+
}
|
|
2058
|
+
const nbsp = raw.indexOf("\xA0");
|
|
2059
|
+
if (nbsp >= 0) {
|
|
2060
|
+
draft(context, {
|
|
2061
|
+
ruleId: "WHITESPACE",
|
|
2062
|
+
message: "\u8BD1\u6587\u542B\u4E0D\u6362\u884C\u7A7A\u683C\uFF08U+00A0\uFF09",
|
|
2063
|
+
evidence: "target contains non-breaking space",
|
|
2064
|
+
targetRange: { start: nbsp, end: nbsp + 1 }
|
|
2065
|
+
});
|
|
2066
|
+
}
|
|
2067
|
+
}
|
|
2068
|
+
function checkFullwidthHalfwidth(context) {
|
|
2069
|
+
const target = context.target;
|
|
2070
|
+
if (target.trim().length === 0) return;
|
|
2071
|
+
const fullwidth = /[\uff10-\uff19\uff21-\uff3a\uff41-\uff5a]/u.exec(target);
|
|
2072
|
+
if (fullwidth && fullwidth.index !== void 0) {
|
|
2073
|
+
draft(context, {
|
|
2074
|
+
ruleId: "FULLWIDTH_HALFWIDTH",
|
|
2075
|
+
message: "\u8BD1\u6587\u4E2D\u5B57\u6BCD/\u6570\u5B57\u4F7F\u7528\u5168\u89D2\u5F62\u5F0F",
|
|
2076
|
+
evidence: `found '${fullwidth[0]}'`,
|
|
2077
|
+
suggestion: "\u5B57\u6BCD\u4E0E\u6570\u5B57\u901A\u5E38\u4F7F\u7528\u534A\u89D2\u5F62\u5F0F",
|
|
2078
|
+
targetRange: { start: fullwidth.index, end: fullwidth.index + 1 }
|
|
2079
|
+
});
|
|
2080
|
+
return;
|
|
2081
|
+
}
|
|
2082
|
+
if (!context.targetIsCjk) return;
|
|
2083
|
+
const mixed = /[\u4e00-\u9fff][,;:!?]|[,;:!?][\u4e00-\u9fff]/u.exec(target);
|
|
2084
|
+
if (mixed === null || mixed.index === void 0) return;
|
|
2085
|
+
draft(context, {
|
|
2086
|
+
ruleId: "FULLWIDTH_HALFWIDTH",
|
|
2087
|
+
message: "\u4E2D\u6587\u4E0E\u534A\u89D2\u6807\u70B9\u6DF7\u6392",
|
|
2088
|
+
evidence: `found '${mixed[0]}'`,
|
|
2089
|
+
suggestion: "\u4E2D\u6587\u8BED\u5883\u4F7F\u7528\u5168\u89D2\u6807\u70B9",
|
|
2090
|
+
targetRange: { start: mixed.index, end: mixed.index + mixed[0].length }
|
|
2091
|
+
});
|
|
2092
|
+
}
|
|
2093
|
+
function checkLengthRatio(context) {
|
|
2094
|
+
const sourceLength = charCount(context.source.trim());
|
|
2095
|
+
const targetLength = charCount(context.target.trim());
|
|
2096
|
+
if (sourceLength === 0 || targetLength === 0) return;
|
|
2097
|
+
const sourceIsCjk = isCjkHeavy(context.source);
|
|
2098
|
+
const targetIsCjk = context.targetIsCjk;
|
|
2099
|
+
let min = 0.4;
|
|
2100
|
+
let max = 2.5;
|
|
2101
|
+
if (targetIsCjk && !sourceIsCjk) {
|
|
2102
|
+
min = 0.15;
|
|
2103
|
+
max = 1.2;
|
|
2104
|
+
} else if (sourceIsCjk && !targetIsCjk) {
|
|
2105
|
+
min = 0.5;
|
|
2106
|
+
max = 3;
|
|
2107
|
+
} else if (sourceIsCjk && targetIsCjk) {
|
|
2108
|
+
min = 0.5;
|
|
2109
|
+
max = 2;
|
|
2110
|
+
}
|
|
2111
|
+
const ratio = targetLength / sourceLength;
|
|
2112
|
+
if (ratio >= min && ratio <= max) return;
|
|
2113
|
+
draft(context, {
|
|
2114
|
+
ruleId: "LENGTH_RATIO",
|
|
2115
|
+
message: `\u8BD1\u6587/\u6E90\u6587\u957F\u5EA6\u6BD4 ${ratio.toFixed(2)} \u8D85\u51FA\u5EFA\u8BAE\u533A\u95F4 [${min}, ${max}]`,
|
|
2116
|
+
evidence: `source length: ${sourceLength}, target length: ${targetLength}`,
|
|
2117
|
+
suggestion: "\u786E\u8BA4\u662F\u5426\u5B58\u5728\u6F0F\u8BD1\u6216\u5197\u4F59\uFF08\u4EC5\u63D0\u793A\uFF09"
|
|
2118
|
+
});
|
|
2119
|
+
}
|
|
2120
|
+
function isApproved(entry) {
|
|
2121
|
+
return entry.status === void 0 || entry.status === "approved";
|
|
2122
|
+
}
|
|
2123
|
+
function longestHits(hits) {
|
|
2124
|
+
return hits.filter(
|
|
2125
|
+
(hit) => !hits.some(
|
|
2126
|
+
(other) => other !== hit && other.start <= hit.start && other.end >= hit.end && other.end - other.start > hit.end - hit.start
|
|
2127
|
+
)
|
|
2128
|
+
);
|
|
2129
|
+
}
|
|
2130
|
+
function targetMentionsTerm(entry, targetText, targetHits) {
|
|
2131
|
+
if (targetHits.some((hit) => hit.entry.id === entry.id)) return true;
|
|
2132
|
+
const expected = (entry.target ?? "").trim();
|
|
2133
|
+
if (expected.length === 0) return true;
|
|
2134
|
+
if (entry.caseSensitive === true) return targetText.includes(expected);
|
|
2135
|
+
return normalizeForCompare(targetText).includes(normalizeForCompare(expected));
|
|
2136
|
+
}
|
|
2137
|
+
function checkTermRules(context) {
|
|
2138
|
+
const termHits = context.termHits;
|
|
2139
|
+
if (termHits === void 0) return;
|
|
2140
|
+
const sourceHits = longestHits(termHits.source);
|
|
2141
|
+
const targetHits = termHits.target;
|
|
2142
|
+
const emittedTermIds = /* @__PURE__ */ new Set();
|
|
2143
|
+
for (const hit of sourceHits) {
|
|
2144
|
+
const entry = hit.entry;
|
|
2145
|
+
if (entry.forbidden === true) {
|
|
2146
|
+
draft(context, {
|
|
2147
|
+
ruleId: "TB_SOURCE_FORBIDDEN",
|
|
2148
|
+
message: `\u6E90\u6587\u51FA\u73B0\u7981\u7528\u672F\u8BED: '${entry.source}'`,
|
|
2149
|
+
evidence: `source term: ${truncate(entry.source)}`,
|
|
2150
|
+
suggestion: "\u66FF\u6362\u6E90\u6587\u6216\u786E\u8BA4\u672F\u8BED\u5E93\u72B6\u6001",
|
|
2151
|
+
sourceRange: { start: hit.start, end: hit.end },
|
|
2152
|
+
termId: entry.id
|
|
2153
|
+
});
|
|
2154
|
+
emittedTermIds.add(`src:${entry.id}`);
|
|
2155
|
+
continue;
|
|
2156
|
+
}
|
|
2157
|
+
if (!isApproved(entry)) continue;
|
|
2158
|
+
if ((entry.target ?? "").trim().length === 0) continue;
|
|
2159
|
+
if (targetMentionsTerm(entry, context.target, targetHits)) continue;
|
|
2160
|
+
draft(context, {
|
|
2161
|
+
ruleId: "TERM_MISSING",
|
|
2162
|
+
message: `\u8BD1\u6587\u672A\u4F7F\u7528 approved \u672F\u8BED '${entry.source}' \u2192 '${entry.target}'`,
|
|
2163
|
+
evidence: `source term hit: ${truncate(entry.source)}`,
|
|
2164
|
+
suggestion: entry.target,
|
|
2165
|
+
sourceRange: { start: hit.start, end: hit.end },
|
|
2166
|
+
termId: entry.id
|
|
2167
|
+
});
|
|
2168
|
+
}
|
|
2169
|
+
for (const entry of context.glossary.values()) {
|
|
2170
|
+
if (entry.forbidden !== true) continue;
|
|
2171
|
+
const expected = (entry.target ?? "").trim();
|
|
2172
|
+
if (expected.length === 0) continue;
|
|
2173
|
+
const targetHit = targetHits.find((hit) => hit.entry.id === entry.id);
|
|
2174
|
+
const mentioned = targetHit !== void 0 || (entry.caseSensitive === true ? context.target.includes(expected) : normalizeForCompare(context.target).includes(normalizeForCompare(expected)));
|
|
2175
|
+
if (!mentioned) continue;
|
|
2176
|
+
if (emittedTermIds.has(`tgt:${entry.id}`)) continue;
|
|
2177
|
+
draft(context, {
|
|
2178
|
+
ruleId: "TB_FORBIDDEN_USED",
|
|
2179
|
+
message: `\u8BD1\u6587\u4F7F\u7528\u4E86\u7981\u7528\u672F\u8BED: '${entry.source}' \u2192 '${entry.target}'`,
|
|
2180
|
+
evidence: `target term: ${truncate(entry.target)}`,
|
|
2181
|
+
...targetHit === void 0 ? {} : { targetRange: { start: targetHit.start, end: targetHit.end } },
|
|
2182
|
+
termId: entry.id
|
|
2183
|
+
});
|
|
2184
|
+
}
|
|
2185
|
+
}
|
|
2186
|
+
var CHECK_GROUPS = [
|
|
2187
|
+
{ ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
|
|
2188
|
+
{ ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
|
|
2189
|
+
{ ids: ["PLACEHOLDER_MISSING", "PLACEHOLDER_EXTRA"], run: checkPlaceholders },
|
|
2190
|
+
{ ids: ["TAG_MISMATCH"], run: checkTags },
|
|
2191
|
+
{ ids: ["URL_CHANGED"], run: checkUrls },
|
|
2192
|
+
{ ids: ["TB_SOURCE_FORBIDDEN", "TB_FORBIDDEN_USED", "TERM_MISSING"], run: checkTermRules },
|
|
2193
|
+
{ ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
|
|
2194
|
+
{ ids: ["NUMBER_MISMATCH"], run: checkNumbers },
|
|
2195
|
+
{ ids: ["DATE_MISMATCH"], run: checkDates },
|
|
2196
|
+
{ ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
|
|
2197
|
+
{ ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
|
|
2198
|
+
{ ids: ["PUNCT_DUP"], run: checkPunctDup },
|
|
2199
|
+
{ ids: ["TRAILING_COLON"], run: checkTrailingColon },
|
|
2200
|
+
{ ids: ["SENTENCE_END_PUNCT"], run: checkSentenceEndPunct },
|
|
2201
|
+
{ ids: ["WHITESPACE"], run: checkWhitespace },
|
|
2202
|
+
{ ids: ["FULLWIDTH_HALFWIDTH"], run: checkFullwidthHalfwidth },
|
|
2203
|
+
{ ids: ["LENGTH_RATIO"], run: checkLengthRatio }
|
|
2204
|
+
];
|
|
2205
|
+
function checkEmptyTargetGroup(context) {
|
|
2206
|
+
const source = context.source;
|
|
2207
|
+
if (source.trim().length === 0) return;
|
|
2208
|
+
const targetMissing = context.segment.targetRange === null && context.target.trim().length === 0 && context.targetInner.trim().length === 0;
|
|
2209
|
+
const empty = context.target.trim().length === 0 && context.targetInner.trim().length === 0;
|
|
2210
|
+
if (!targetMissing && !empty) return;
|
|
2211
|
+
draft(context, {
|
|
2212
|
+
ruleId: "EMPTY_TARGET",
|
|
2213
|
+
message: targetMissing ? "\u76EE\u6807\u7F3A\u5931\uFF08target \u5143\u7D20\u4E0D\u5B58\u5728\uFF09" : "\u8BD1\u6587\u4E3A\u7A7A\uFF08\u6E90\u6587\u975E\u7A7A\uFF09",
|
|
2214
|
+
evidence: `source: ${truncate(source)}`
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
function checkUntranslatedCopyGroup(context) {
|
|
2218
|
+
const { source, target } = context;
|
|
2219
|
+
if (source.trim().length === 0 || target.trim().length === 0) return;
|
|
2220
|
+
if (normalizeForCompare(source) !== normalizeForCompare(target)) return;
|
|
2221
|
+
const tokens = wordTokens(source);
|
|
2222
|
+
const cjkChars = Array.from(source).filter((char) => CJK_RE.test(char)).length;
|
|
2223
|
+
if (tokens.length <= 1 && cjkChars < 2 && source.trim().length <= 4) return;
|
|
2224
|
+
draft(context, {
|
|
2225
|
+
ruleId: "UNTRANSLATED_COPY",
|
|
2226
|
+
message: "\u8BD1\u6587\u4E0E\u6E90\u6587\u5B8C\u5168\u76F8\u540C\uFF08\u672A\u7FFB\u8BD1\uFF09",
|
|
2227
|
+
evidence: `text: ${truncate(source)}`,
|
|
2228
|
+
targetRange: { start: 0, end: charCount(target) }
|
|
2229
|
+
});
|
|
2230
|
+
}
|
|
2231
|
+
function isApplicable(ruleId, segment) {
|
|
2232
|
+
const hasSource = segment.sourceText.trim().length > 0;
|
|
2233
|
+
const hasTargetText = segment.targetText.trim().length > 0;
|
|
2234
|
+
const hasTargetContainer = segment.targetRange !== null;
|
|
2235
|
+
switch (ruleId) {
|
|
2236
|
+
case "EMPTY_TARGET":
|
|
2237
|
+
return hasSource;
|
|
2238
|
+
case "TAG_MISMATCH":
|
|
2239
|
+
return hasSource && (hasTargetText || hasTargetContainer);
|
|
2240
|
+
case "WHITESPACE":
|
|
2241
|
+
return hasTargetContainer || segment.targetInner.length > 0;
|
|
2242
|
+
case "UNTRANSLATED_COPY":
|
|
2243
|
+
case "PLACEHOLDER_MISSING":
|
|
2244
|
+
case "PLACEHOLDER_EXTRA":
|
|
2245
|
+
case "URL_CHANGED":
|
|
2246
|
+
case "TB_SOURCE_FORBIDDEN":
|
|
2247
|
+
case "TB_FORBIDDEN_USED":
|
|
2248
|
+
case "UNPAIRED_BRACKET":
|
|
2249
|
+
case "NUMBER_MISMATCH":
|
|
2250
|
+
case "DATE_MISMATCH":
|
|
2251
|
+
case "DUPLICATE_WORD":
|
|
2252
|
+
case "TERM_MISSING":
|
|
2253
|
+
case "SOURCE_LEFTOVER":
|
|
2254
|
+
case "PUNCT_DUP":
|
|
2255
|
+
case "TRAILING_COLON":
|
|
2256
|
+
case "SENTENCE_END_PUNCT":
|
|
2257
|
+
case "FULLWIDTH_HALFWIDTH":
|
|
2258
|
+
case "LENGTH_RATIO":
|
|
2259
|
+
return hasSource && hasTargetText;
|
|
2260
|
+
default:
|
|
2261
|
+
return hasSource && hasTargetText;
|
|
2262
|
+
}
|
|
2263
|
+
}
|
|
2264
|
+
function termAssetAvailable(options) {
|
|
2265
|
+
if (options.segmentTermHits !== void 0 && options.segmentTermHits.size > 0) return true;
|
|
2266
|
+
return options.glossary !== void 0 && options.glossary.length > 0;
|
|
2267
|
+
}
|
|
2268
|
+
function resolveEnabledRules(options) {
|
|
2269
|
+
if (options.enabledRules !== void 0) {
|
|
2270
|
+
const enabled2 = /* @__PURE__ */ new Set();
|
|
2271
|
+
for (const ruleId of options.enabledRules) {
|
|
2272
|
+
if (RULE_INDEX.has(ruleId)) enabled2.add(ruleId);
|
|
2273
|
+
}
|
|
2274
|
+
return enabled2;
|
|
2275
|
+
}
|
|
2276
|
+
const enabled = new Set(DEFAULT_ENABLED);
|
|
2277
|
+
if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
|
|
2278
|
+
return enabled;
|
|
2279
|
+
}
|
|
2280
|
+
function termGlossary(options) {
|
|
2281
|
+
const entries = /* @__PURE__ */ new Map();
|
|
2282
|
+
for (const entry of options.glossary ?? []) entries.set(entry.id, entry);
|
|
2283
|
+
for (const hits of options.segmentTermHits?.values() ?? []) {
|
|
2284
|
+
for (const hit of [...hits.source, ...hits.target]) {
|
|
2285
|
+
if (!entries.has(hit.entry.id)) entries.set(hit.entry.id, hit.entry);
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
return entries;
|
|
2289
|
+
}
|
|
2290
|
+
function buildContext(segment, options, glossary) {
|
|
2291
|
+
const termHits = options.segmentTermHits?.get(segment.id);
|
|
2292
|
+
return {
|
|
2293
|
+
segment,
|
|
2294
|
+
source: segment.sourceText,
|
|
2295
|
+
target: segment.targetText,
|
|
2296
|
+
sourceInner: segment.sourceInner,
|
|
2297
|
+
targetInner: segment.targetInner,
|
|
2298
|
+
targetIsCjk: isCjkHeavy(segment.targetText),
|
|
2299
|
+
glossary,
|
|
2300
|
+
drafts: [],
|
|
2301
|
+
...termHits === void 0 ? {} : { termHits }
|
|
2302
|
+
};
|
|
2303
|
+
}
|
|
2304
|
+
function toIssue(draft2, segment, ruleId) {
|
|
2305
|
+
return {
|
|
2306
|
+
ruleId,
|
|
2307
|
+
severity: RULE_SEVERITY.get(ruleId) ?? "major",
|
|
2308
|
+
segmentId: segment.id,
|
|
2309
|
+
segmentIndex: segment.index,
|
|
2310
|
+
message: draft2.message,
|
|
2311
|
+
evidence: draft2.evidence,
|
|
2312
|
+
...draft2.suggestion === void 0 ? {} : { suggestion: draft2.suggestion },
|
|
2313
|
+
...draft2.sourceRange === void 0 ? {} : { sourceRange: draft2.sourceRange },
|
|
2314
|
+
...draft2.targetRange === void 0 ? {} : { targetRange: draft2.targetRange },
|
|
2315
|
+
origin: "deterministic",
|
|
2316
|
+
...draft2.termId === void 0 ? {} : { termId: draft2.termId }
|
|
2317
|
+
};
|
|
2318
|
+
}
|
|
2319
|
+
function compareIssues(left, right) {
|
|
2320
|
+
const leftRule = RULE_INDEX.get(left.ruleId) ?? Number.MAX_SAFE_INTEGER;
|
|
2321
|
+
const rightRule = RULE_INDEX.get(right.ruleId) ?? Number.MAX_SAFE_INTEGER;
|
|
2322
|
+
if (leftRule !== rightRule) return leftRule - rightRule;
|
|
2323
|
+
if (left.segmentIndex !== right.segmentIndex) return left.segmentIndex - right.segmentIndex;
|
|
2324
|
+
if (left.segmentId !== right.segmentId) return left.segmentId < right.segmentId ? -1 : 1;
|
|
2325
|
+
const leftStart = left.targetRange?.start ?? left.sourceRange?.start ?? 0;
|
|
2326
|
+
const rightStart = right.targetRange?.start ?? right.sourceRange?.start ?? 0;
|
|
2327
|
+
return leftStart - rightStart;
|
|
2328
|
+
}
|
|
2329
|
+
function runDeterministicChecks(segments, options = {}) {
|
|
2330
|
+
const enabled = resolveEnabledRules(options);
|
|
2331
|
+
const termAvailable = termAssetAvailable(options);
|
|
2332
|
+
const glossary = termGlossary(options);
|
|
2333
|
+
const state = /* @__PURE__ */ new Map();
|
|
2334
|
+
for (const rule of RULES) {
|
|
2335
|
+
if (!enabled.has(rule.id)) continue;
|
|
2336
|
+
if (rule.requiresTerms === true && !termAvailable) {
|
|
2337
|
+
state.set(rule.id, {
|
|
2338
|
+
status: "skipped",
|
|
2339
|
+
reason: "no glossary/segmentTermHits provided; term rules were not executed",
|
|
2340
|
+
checkedSegments: 0
|
|
2341
|
+
});
|
|
2342
|
+
continue;
|
|
2343
|
+
}
|
|
2344
|
+
state.set(rule.id, { status: "executed", reason: "", checkedSegments: 0 });
|
|
2345
|
+
}
|
|
2346
|
+
const issues = [];
|
|
2347
|
+
for (const segment of segments) {
|
|
2348
|
+
const context = buildContext(segment, options, glossary);
|
|
2349
|
+
const applicable = /* @__PURE__ */ new Set();
|
|
2350
|
+
for (const ruleId of enabled) {
|
|
2351
|
+
if (state.get(ruleId)?.status !== "executed") continue;
|
|
2352
|
+
if (isApplicable(ruleId, segment)) applicable.add(ruleId);
|
|
2353
|
+
}
|
|
2354
|
+
if (applicable.size === 0) continue;
|
|
2355
|
+
const groupsRun = /* @__PURE__ */ new Set();
|
|
2356
|
+
const failedRules = /* @__PURE__ */ new Set();
|
|
2357
|
+
for (const group of CHECK_GROUPS) {
|
|
2358
|
+
const groupRules = group.ids.filter((ruleId) => applicable.has(ruleId));
|
|
2359
|
+
if (groupRules.length === 0) continue;
|
|
2360
|
+
const key = group.ids.join("|");
|
|
2361
|
+
if (groupsRun.has(key)) continue;
|
|
2362
|
+
groupsRun.add(key);
|
|
2363
|
+
try {
|
|
2364
|
+
group.run(context);
|
|
2365
|
+
} catch (caught) {
|
|
2366
|
+
const message = caught instanceof Error ? caught.message : String(caught);
|
|
2367
|
+
for (const ruleId of groupRules) failedRules.add(`${ruleId}\0${message}`);
|
|
2368
|
+
}
|
|
2369
|
+
}
|
|
2370
|
+
for (const ruleId of applicable) {
|
|
2371
|
+
const execution = state.get(ruleId);
|
|
2372
|
+
if (execution === void 0) continue;
|
|
2373
|
+
const failed = Array.from(failedRules).some((entry) => entry.startsWith(`${ruleId}\0`));
|
|
2374
|
+
if (failed) {
|
|
2375
|
+
const message = Array.from(failedRules).find((entry) => entry.startsWith(`${ruleId}\0`))?.split("\0")[1] ?? "rule threw";
|
|
2376
|
+
state.set(ruleId, {
|
|
2377
|
+
status: "failed",
|
|
2378
|
+
reason: `rule threw: ${message}`,
|
|
2379
|
+
checkedSegments: execution.checkedSegments
|
|
2380
|
+
});
|
|
2381
|
+
continue;
|
|
2382
|
+
}
|
|
2383
|
+
execution.checkedSegments += 1;
|
|
2384
|
+
}
|
|
2385
|
+
for (const draft2 of context.drafts) {
|
|
2386
|
+
if (!applicable.has(draft2.ruleId)) continue;
|
|
2387
|
+
issues.push(toIssue(draft2, segment, draft2.ruleId));
|
|
2388
|
+
}
|
|
2389
|
+
}
|
|
2390
|
+
const rules = [];
|
|
2391
|
+
for (const rule of RULES) {
|
|
2392
|
+
const execution = state.get(rule.id);
|
|
2393
|
+
if (execution === void 0) continue;
|
|
2394
|
+
const status = execution.status === "executed" && execution.checkedSegments === 0 ? "not_applicable" : execution.status;
|
|
2395
|
+
rules.push({
|
|
2396
|
+
ruleId: rule.id,
|
|
2397
|
+
status,
|
|
2398
|
+
reason: status === "not_applicable" ? "no applicable segments" : execution.reason,
|
|
2399
|
+
checkedSegments: execution.checkedSegments
|
|
2400
|
+
});
|
|
2401
|
+
}
|
|
2402
|
+
issues.sort(compareIssues);
|
|
2403
|
+
const coverageIncomplete = rules.some((rule) => rule.status === "skipped" || rule.status === "failed");
|
|
2404
|
+
const anyExecuted = rules.some(
|
|
2405
|
+
(rule) => rule.status === "executed" || rule.status === "not_applicable"
|
|
2406
|
+
);
|
|
2407
|
+
const checkedSegments = anyExecuted ? segments.length : 0;
|
|
2408
|
+
return {
|
|
2409
|
+
issues,
|
|
2410
|
+
totalSegments: segments.length,
|
|
2411
|
+
checkedSegments,
|
|
2412
|
+
coverage: coverageIncomplete ? "incomplete" : "complete",
|
|
2413
|
+
rules
|
|
2414
|
+
};
|
|
2415
|
+
}
|
|
2416
|
+
|
|
2417
|
+
// packages/l10n-core/src/qa/report.ts
|
|
2418
|
+
function renderJson(result) {
|
|
2419
|
+
const issues = result.issues.map((issue) => ({
|
|
2420
|
+
ruleId: issue.ruleId,
|
|
2421
|
+
severity: issue.severity,
|
|
2422
|
+
segmentId: issue.segmentId,
|
|
2423
|
+
segmentIndex: issue.segmentIndex,
|
|
2424
|
+
message: issue.message,
|
|
2425
|
+
evidence: issue.evidence,
|
|
2426
|
+
...issue.suggestion === void 0 ? {} : { suggestion: issue.suggestion },
|
|
2427
|
+
...issue.sourceRange === void 0 ? {} : { sourceRange: { start: issue.sourceRange.start, end: issue.sourceRange.end } },
|
|
2428
|
+
...issue.targetRange === void 0 ? {} : { targetRange: { start: issue.targetRange.start, end: issue.targetRange.end } },
|
|
2429
|
+
...issue.termId === void 0 ? {} : { termId: issue.termId },
|
|
2430
|
+
origin: issue.origin
|
|
2431
|
+
}));
|
|
2432
|
+
return JSON.stringify(
|
|
2433
|
+
{
|
|
2434
|
+
totalSegments: result.totalSegments,
|
|
2435
|
+
checkedSegments: result.checkedSegments,
|
|
2436
|
+
coverage: result.coverage,
|
|
2437
|
+
issueCount: issues.length,
|
|
2438
|
+
issues,
|
|
2439
|
+
rules: result.rules.map((rule) => ({
|
|
2440
|
+
ruleId: rule.ruleId,
|
|
2441
|
+
status: rule.status,
|
|
2442
|
+
reason: rule.reason,
|
|
2443
|
+
checkedSegments: rule.checkedSegments
|
|
2444
|
+
}))
|
|
2445
|
+
},
|
|
2446
|
+
null,
|
|
2447
|
+
2
|
|
2448
|
+
);
|
|
2449
|
+
}
|
|
2450
|
+
|
|
2451
|
+
// packages/l10n-core/src/workflows/check.ts
|
|
2452
|
+
function termMatchOptions(document, segment) {
|
|
2453
|
+
const file = document.files[segment.location.fileIndex];
|
|
2454
|
+
const options = {};
|
|
2455
|
+
if (file?.sourceLanguage) options.sourceLanguage = file.sourceLanguage;
|
|
2456
|
+
if (file?.targetLanguage) options.targetLanguage = file.targetLanguage;
|
|
2457
|
+
return options;
|
|
2458
|
+
}
|
|
2459
|
+
function checkDocument(document, options = {}) {
|
|
2460
|
+
const glossary = options.glossary ?? [];
|
|
2461
|
+
const segmentTermHits = /* @__PURE__ */ new Map();
|
|
2462
|
+
if (glossary.length > 0) {
|
|
2463
|
+
for (const segment of document.segments) {
|
|
2464
|
+
const termOptions = termMatchOptions(document, segment);
|
|
2465
|
+
const sourceMatches = matchTerms(segment.sourceText, glossary, termOptions);
|
|
2466
|
+
const targetMatches = matchTerms(segment.targetText, glossary, termOptions);
|
|
2467
|
+
segmentTermHits.set(segment.id, {
|
|
2468
|
+
source: sourceMatches.hits,
|
|
2469
|
+
target: targetMatches.hits
|
|
2470
|
+
});
|
|
2471
|
+
}
|
|
2472
|
+
}
|
|
2473
|
+
const qaOptions = {
|
|
2474
|
+
...options.glossary === void 0 ? {} : { glossary: options.glossary },
|
|
2475
|
+
...segmentTermHits.size === 0 ? {} : { segmentTermHits },
|
|
2476
|
+
...options.enabledRules === void 0 ? {} : { enabledRules: options.enabledRules }
|
|
2477
|
+
};
|
|
2478
|
+
const qa = runDeterministicChecks(document.segments, qaOptions);
|
|
2479
|
+
return { qa, segmentTermHits };
|
|
2480
|
+
}
|
|
2481
|
+
|
|
2482
|
+
// packages/l10n-core/src/formats/bilingual.ts
|
|
2483
|
+
import ExcelJS from "exceljs";
|
|
2484
|
+
|
|
2485
|
+
// packages/l10n-core/src/formats/ooxml.ts
|
|
2486
|
+
import { unzipSync, zipSync } from "fflate";
|
|
2487
|
+
|
|
2488
|
+
// packages/l10n-mcp/src/server.ts
|
|
2489
|
+
var MAX_BYTES = 200 * 1024 * 1024;
|
|
2490
|
+
async function readUtf8(path) {
|
|
2491
|
+
const absolute = resolve(path);
|
|
2492
|
+
const value = await readFile2(absolute, "utf8");
|
|
2493
|
+
if (Buffer.byteLength(value, "utf8") > MAX_BYTES) throw new Error("file exceeds size limit");
|
|
2494
|
+
return value;
|
|
2495
|
+
}
|
|
2496
|
+
function loadDocument(xml, path) {
|
|
2497
|
+
return parseXliff(xml, { filename: basename(path) });
|
|
2498
|
+
}
|
|
2499
|
+
function textResult(value) {
|
|
2500
|
+
return { content: [{ type: "text", text: typeof value === "string" ? value : JSON.stringify(value) }] };
|
|
2501
|
+
}
|
|
2502
|
+
function createL10nMcpServer(options = {}) {
|
|
2503
|
+
const server = new McpServer({ name: "langlink-l10n", version: "0.0.0" });
|
|
2504
|
+
server.registerTool(
|
|
2505
|
+
"l10n_inspect",
|
|
2506
|
+
{
|
|
2507
|
+
description: "Inspect a local XLIFF 1.2/2.0/2.1 or SDLXLIFF file (no source/target text returned).",
|
|
2508
|
+
inputSchema: { path: z.string().describe("XLIFF/SDLXLIFF file path") }
|
|
2509
|
+
},
|
|
2510
|
+
async ({ path }) => {
|
|
2511
|
+
const document = loadDocument(await readUtf8(path), path);
|
|
2512
|
+
let missingTarget = 0;
|
|
2513
|
+
let tagMismatch = 0;
|
|
2514
|
+
for (const segment of document.segments) {
|
|
2515
|
+
if (!segment.targetRange) missingTarget += 1;
|
|
2516
|
+
if (segment.sourceTags.join("|") !== segment.targetTags.join("|")) tagMismatch += 1;
|
|
2517
|
+
}
|
|
2518
|
+
return textResult({
|
|
2519
|
+
format: document.format,
|
|
2520
|
+
version: document.version,
|
|
2521
|
+
segments: document.segments.length,
|
|
2522
|
+
missingTarget,
|
|
2523
|
+
tagMismatch,
|
|
2524
|
+
warnings: document.warnings,
|
|
2525
|
+
languages: document.files.map((file) => `${file.sourceLanguage}->${file.targetLanguage}`)
|
|
2526
|
+
});
|
|
2527
|
+
}
|
|
2528
|
+
);
|
|
2529
|
+
server.registerTool(
|
|
2530
|
+
"l10n_match_tm",
|
|
2531
|
+
{
|
|
2532
|
+
description: "Match a local XLIFF/SDLXLIFF against a TMX 1.4b file; returns per-segment match metadata.",
|
|
2533
|
+
inputSchema: {
|
|
2534
|
+
path: z.string(),
|
|
2535
|
+
tm: z.string(),
|
|
2536
|
+
threshold: z.number().int().min(0).max(100).optional(),
|
|
2537
|
+
maxSegments: z.number().int().min(1).max(500).optional()
|
|
2538
|
+
}
|
|
2539
|
+
},
|
|
2540
|
+
async ({ path, tm, threshold, maxSegments }) => {
|
|
2541
|
+
const document = loadDocument(await readUtf8(path), path);
|
|
2542
|
+
const entries = parseTmx(await readUtf8(tm)).entries;
|
|
2543
|
+
if (entries.length === 0) throw new Error("TMX contains no usable entries");
|
|
2544
|
+
const limit = maxSegments ?? 50;
|
|
2545
|
+
const matches = document.segments.slice(0, limit).map((segment) => {
|
|
2546
|
+
const file = document.files[segment.location.fileIndex];
|
|
2547
|
+
const results = matchTm(entries, segment.sourceText, {
|
|
2548
|
+
threshold: threshold ?? 75,
|
|
2549
|
+
maxMatches: 1,
|
|
2550
|
+
...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
|
|
2551
|
+
...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
|
|
2552
|
+
});
|
|
2553
|
+
return {
|
|
2554
|
+
segmentId: segment.id,
|
|
2555
|
+
matchRate: results[0]?.matchRate ?? 0,
|
|
2556
|
+
similarity: results[0]?.similarity ?? 0,
|
|
2557
|
+
matchType: results[0]?.matchType ?? "no_match"
|
|
2558
|
+
};
|
|
2559
|
+
});
|
|
2560
|
+
return textResult({ segments: document.segments.length, checked: matches.length, matches });
|
|
2561
|
+
}
|
|
2562
|
+
);
|
|
2563
|
+
server.registerTool(
|
|
2564
|
+
"l10n_match_terms",
|
|
2565
|
+
{
|
|
2566
|
+
description: "Match a local XLIFF/SDLXLIFF against a CSV glossary; returns term hits and forbidden counts.",
|
|
2567
|
+
inputSchema: {
|
|
2568
|
+
path: z.string(),
|
|
2569
|
+
glossary: z.string(),
|
|
2570
|
+
maxSegments: z.number().int().min(1).max(500).optional()
|
|
2571
|
+
}
|
|
2572
|
+
},
|
|
2573
|
+
async ({ path, glossary, maxSegments }) => {
|
|
2574
|
+
const document = loadDocument(await readUtf8(path), path);
|
|
2575
|
+
const entries = parseGlossaryCsv(await readUtf8(glossary)).entries;
|
|
2576
|
+
const limit = maxSegments ?? 50;
|
|
2577
|
+
const hits = [];
|
|
2578
|
+
let forbidden = 0;
|
|
2579
|
+
for (const segment of document.segments.slice(0, limit)) {
|
|
2580
|
+
const file = document.files[segment.location.fileIndex];
|
|
2581
|
+
for (const side of ["source", "target"]) {
|
|
2582
|
+
const result = matchTerms(segment[`${side}Text`], entries, {
|
|
2583
|
+
...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
|
|
2584
|
+
...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
|
|
2585
|
+
});
|
|
2586
|
+
for (const hit of result.hits) {
|
|
2587
|
+
if (hit.entry.forbidden) forbidden += 1;
|
|
2588
|
+
hits.push({ segmentId: segment.id, side, termId: hit.entry.id, sourceTerm: hit.entry.source, targetTerm: hit.entry.target, matchKind: hit.matchKind, forbidden: hit.entry.forbidden === true });
|
|
2589
|
+
}
|
|
2590
|
+
}
|
|
2591
|
+
}
|
|
2592
|
+
return textResult({ segments: document.segments.length, entries: entries.length, hitCount: hits.length, forbiddenCount: forbidden, hits: hits.slice(0, 200) });
|
|
2593
|
+
}
|
|
2594
|
+
);
|
|
2595
|
+
server.registerTool(
|
|
2596
|
+
"l10n_check",
|
|
2597
|
+
{
|
|
2598
|
+
description: "Run deterministic QA on a local XLIFF/SDLXLIFF, optionally with TMX and CSV glossary.",
|
|
2599
|
+
inputSchema: {
|
|
2600
|
+
path: z.string(),
|
|
2601
|
+
tm: z.string().optional(),
|
|
2602
|
+
glossary: z.string().optional(),
|
|
2603
|
+
maxIssues: z.number().int().min(1).max(500).optional()
|
|
2604
|
+
}
|
|
2605
|
+
},
|
|
2606
|
+
async ({ path, tm, glossary, maxIssues }) => {
|
|
2607
|
+
const document = loadDocument(await readUtf8(path), path);
|
|
2608
|
+
const tmEntries = tm ? parseTmx(await readUtf8(tm)).entries : [];
|
|
2609
|
+
const glossaryEntries = glossary ? parseGlossaryCsv(await readUtf8(glossary)).entries : [];
|
|
2610
|
+
const result = checkDocument(document, { tmEntries, glossary: glossaryEntries });
|
|
2611
|
+
const limited = maxIssues ?? 100;
|
|
2612
|
+
const bySeverity = {};
|
|
2613
|
+
for (const issue of result.qa.issues) bySeverity[issue.severity] = (bySeverity[issue.severity] ?? 0) + 1;
|
|
2614
|
+
return textResult({
|
|
2615
|
+
coverage: result.qa.coverage,
|
|
2616
|
+
totalSegments: result.qa.totalSegments,
|
|
2617
|
+
checkedSegments: result.qa.checkedSegments,
|
|
2618
|
+
issues: result.qa.issues.length,
|
|
2619
|
+
bySeverity,
|
|
2620
|
+
report: renderJson(result.qa),
|
|
2621
|
+
returned: Math.min(limited, result.qa.issues.length),
|
|
2622
|
+
firstIssues: result.qa.issues.slice(0, limited)
|
|
2623
|
+
});
|
|
2624
|
+
}
|
|
2625
|
+
);
|
|
2626
|
+
if (options.assetBaseUrl) {
|
|
2627
|
+
server.registerTool(
|
|
2628
|
+
"l10n_asset_info",
|
|
2629
|
+
{
|
|
2630
|
+
description: "Query the configured read-only company asset service for availability and counts.",
|
|
2631
|
+
inputSchema: {}
|
|
2632
|
+
},
|
|
2633
|
+
async () => {
|
|
2634
|
+
const response = await fetch(`${options.assetBaseUrl.replace(/\/$/u, "")}/health`);
|
|
2635
|
+
if (!response.ok) throw new Error(`asset service returned ${response.status}`);
|
|
2636
|
+
return textResult(await response.json());
|
|
2637
|
+
}
|
|
2638
|
+
);
|
|
2639
|
+
}
|
|
2640
|
+
return server;
|
|
2641
|
+
}
|
|
2642
|
+
async function runConfigCommand(args) {
|
|
2643
|
+
const jsonOutput = args.includes("--json");
|
|
2644
|
+
const write = args.includes("--write");
|
|
2645
|
+
const homeIndex = args.indexOf("--home");
|
|
2646
|
+
const home = homeIndex >= 0 ? args[homeIndex + 1] : void 0;
|
|
2647
|
+
const clientIndex = args.indexOf("--client");
|
|
2648
|
+
const requested = clientIndex >= 0 ? args[clientIndex + 1] : "all";
|
|
2649
|
+
const allClients = ["codex", "workbuddy", "zcode", "trae", "agents"];
|
|
2650
|
+
const clients = requested === "all" ? allClients : allClients.includes(requested) ? [requested] : [];
|
|
2651
|
+
if (clients.length === 0) {
|
|
2652
|
+
throw new Error("--client must be one of codex, workbuddy, zcode, trae, agents, all");
|
|
2653
|
+
}
|
|
2654
|
+
const recipes = clients.map((client) => clientRecipe(client, home));
|
|
2655
|
+
if (write) {
|
|
2656
|
+
const results = [];
|
|
2657
|
+
for (const recipe of recipes) results.push(await writeRecipe(recipe));
|
|
2658
|
+
if (jsonOutput) {
|
|
2659
|
+
console.log(JSON.stringify({ recipes, writes: results }, null, 2));
|
|
2660
|
+
} else {
|
|
2661
|
+
for (const result of results) {
|
|
2662
|
+
console.log(`${result.client}: ${result.action}${result.path ? ` ${result.path}` : ""}${result.backup ? ` (backup ${result.backup})` : ""}`);
|
|
2663
|
+
}
|
|
2664
|
+
}
|
|
2665
|
+
return;
|
|
2666
|
+
}
|
|
2667
|
+
if (jsonOutput) {
|
|
2668
|
+
console.log(JSON.stringify({ recipes }, null, 2));
|
|
2669
|
+
return;
|
|
2670
|
+
}
|
|
2671
|
+
for (const recipe of recipes) {
|
|
2672
|
+
console.log(`# ${recipe.client}${recipe.path ? ` \u2014 ${recipe.path}` : " \u2014 UI only"}`);
|
|
2673
|
+
for (const note of recipe.notes) console.log(`# ${note}`);
|
|
2674
|
+
console.log(recipe.content);
|
|
2675
|
+
}
|
|
2676
|
+
}
|
|
2677
|
+
async function main() {
|
|
2678
|
+
const command = process.argv[2];
|
|
2679
|
+
if (command === "config" || command === "setup") {
|
|
2680
|
+
await runConfigCommand(process.argv.slice(3));
|
|
2681
|
+
return;
|
|
2682
|
+
}
|
|
2683
|
+
if (command === "--help" || command === "-h") {
|
|
2684
|
+
console.log("Usage: langlink-l10n-mcp [config [--client codex|workbuddy|zcode|trae|agents|all] [--write] [--json] [--home DIR]]");
|
|
2685
|
+
return;
|
|
2686
|
+
}
|
|
2687
|
+
const server = createL10nMcpServer({
|
|
2688
|
+
...process.env.L10N_ASSET_BASE_URL ? { assetBaseUrl: process.env.L10N_ASSET_BASE_URL } : {}
|
|
2689
|
+
});
|
|
2690
|
+
await server.connect(new StdioServerTransport());
|
|
2691
|
+
console.error("[langlink-l10n-mcp] stdio server ready");
|
|
2692
|
+
}
|
|
2693
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
|
|
2694
|
+
main().catch((error) => {
|
|
2695
|
+
console.error(error);
|
|
2696
|
+
process.exitCode = 1;
|
|
2697
|
+
});
|
|
2698
|
+
}
|
|
2699
|
+
export {
|
|
2700
|
+
createL10nMcpServer
|
|
2701
|
+
};
|
|
2702
|
+
//# sourceMappingURL=index.js.map
|