@langlink/dsh-l10n 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/dist/index.js +1913 -231
- package/dist/index.js.map +4 -4
- package/package.json +2 -2
- package/skills/l10n-assets/SKILL.md +41 -9
- package/skills/l10n-check/SKILL.md +4 -1
- package/skills/l10n-project/SKILL.md +19 -17
- package/skills/l10n-translate/SKILL.md +5 -2
package/dist/index.js
CHANGED
|
@@ -1,9 +1,171 @@
|
|
|
1
|
-
// packages/dsh-l10n/src/tools-
|
|
2
|
-
import { createHash as createHash2 } from "node:crypto";
|
|
3
|
-
import { readFile, stat, writeFile } from "node:fs/promises";
|
|
4
|
-
import { basename, resolve } from "node:path";
|
|
1
|
+
// packages/dsh-l10n/src/tools-platform.ts
|
|
5
2
|
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
6
3
|
|
|
4
|
+
// packages/workbench-sdk/src/index.ts
|
|
5
|
+
var PlatformApiError = class extends Error {
|
|
6
|
+
status;
|
|
7
|
+
code;
|
|
8
|
+
requestId;
|
|
9
|
+
constructor(status, body, requestId) {
|
|
10
|
+
super(typeof body.message === "string" ? body.message : typeof body.detail === "string" ? body.detail : `Platform request failed (${status})`);
|
|
11
|
+
this.name = "PlatformApiError";
|
|
12
|
+
this.status = status;
|
|
13
|
+
this.code = typeof body.code === "string" ? body.code : typeof body.error === "string" ? body.error : "request_failed";
|
|
14
|
+
this.requestId = typeof body.requestId === "string" ? body.requestId : requestId;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
function createPlatformClient(options) {
|
|
18
|
+
const parsed = new URL(options.baseUrl);
|
|
19
|
+
const loopback = ["127.0.0.1", "localhost", "[::1]"].includes(parsed.hostname);
|
|
20
|
+
if (parsed.protocol !== "https:" && !(loopback && parsed.protocol === "http:") || parsed.username || parsed.password || parsed.search || parsed.hash || parsed.pathname !== "/") throw new Error("platform baseUrl must be an HTTPS origin (HTTP only on loopback)");
|
|
21
|
+
const base = parsed.origin;
|
|
22
|
+
const fetcher = options.fetcher ?? fetch;
|
|
23
|
+
const timeoutMs = options.timeoutMs ?? 3e4;
|
|
24
|
+
if (!Number.isInteger(timeoutMs) || timeoutMs < 100 || timeoutMs > 12e4) throw new Error("timeoutMs must be 100..120000");
|
|
25
|
+
const path = (...parts) => `/api/v1/${parts.map(encodeURIComponent).join("/")}`;
|
|
26
|
+
async function response(method, endpoint, body, request = {}) {
|
|
27
|
+
const timeout = AbortSignal.timeout(timeoutMs);
|
|
28
|
+
const signal = request.signal ? AbortSignal.any([request.signal, timeout]) : timeout;
|
|
29
|
+
signal.throwIfAborted();
|
|
30
|
+
const provider = options.token;
|
|
31
|
+
const token = typeof provider === "string" ? provider : await new Promise((resolve6, reject) => {
|
|
32
|
+
const aborted = () => {
|
|
33
|
+
signal.removeEventListener("abort", aborted);
|
|
34
|
+
reject(signal.reason);
|
|
35
|
+
};
|
|
36
|
+
signal.addEventListener("abort", aborted, { once: true });
|
|
37
|
+
Promise.resolve().then(() => {
|
|
38
|
+
signal.throwIfAborted();
|
|
39
|
+
return provider();
|
|
40
|
+
}).then(
|
|
41
|
+
(value) => {
|
|
42
|
+
signal.removeEventListener("abort", aborted);
|
|
43
|
+
resolve6(value);
|
|
44
|
+
},
|
|
45
|
+
(error) => {
|
|
46
|
+
signal.removeEventListener("abort", aborted);
|
|
47
|
+
reject(error);
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
if (signal.aborted) aborted();
|
|
51
|
+
});
|
|
52
|
+
signal.throwIfAborted();
|
|
53
|
+
if (!token || /[\r\n]/u.test(token)) throw new Error("a valid platform Token is required");
|
|
54
|
+
const headers = { authorization: `Bearer ${token}`, accept: "application/json" };
|
|
55
|
+
if (body !== void 0) headers["content-type"] = "application/json";
|
|
56
|
+
if (request.idempotencyKey) headers["idempotency-key"] = request.idempotencyKey;
|
|
57
|
+
const result = await fetcher(base + endpoint, { method, headers, redirect: "error", credentials: "omit", ...body === void 0 ? {} : { body: JSON.stringify(body) }, signal });
|
|
58
|
+
if (!result.ok) {
|
|
59
|
+
const parsed2 = await result.json().catch(() => ({}));
|
|
60
|
+
throw new PlatformApiError(result.status, parsed2 && typeof parsed2 === "object" && !Array.isArray(parsed2) ? parsed2 : {}, result.headers.get("x-request-id") ?? void 0);
|
|
61
|
+
}
|
|
62
|
+
return result;
|
|
63
|
+
}
|
|
64
|
+
async function json(method, endpoint, body, request) {
|
|
65
|
+
return await (await response(method, endpoint, body, request)).json();
|
|
66
|
+
}
|
|
67
|
+
return {
|
|
68
|
+
qaRules: () => json("GET", path("qa-rules")),
|
|
69
|
+
configurations: (resource, offset = 0) => json("GET", `${path(resource)}?offset=${offset}`),
|
|
70
|
+
configurationVersion: (resource, id, version) => json("GET", `${path(resource, id)}?version=${version}`),
|
|
71
|
+
documentConfiguration: (id) => json("GET", path("documents", id, "configuration")),
|
|
72
|
+
setDocumentConfiguration: (id, input) => json("PATCH", path("documents", id, "configuration"), input),
|
|
73
|
+
me: () => json("GET", path("me")),
|
|
74
|
+
plugins: () => json("GET", path("plugins")),
|
|
75
|
+
models: () => json("GET", path("models")),
|
|
76
|
+
createDocument: (input, request) => json("POST", path("documents"), input, request),
|
|
77
|
+
documents: (offset = 0, limit = 50) => json("GET", `${path("documents")}?offset=${offset}&limit=${limit}`),
|
|
78
|
+
documentChanges: (id, after) => json("GET", `${path("documents", id, "changes")}?after=${encodeURIComponent(after)}`),
|
|
79
|
+
document: (id, offset = 0, limit = 50, request) => json("GET", `${path("documents", id)}?segments.offset=${offset}&segments.limit=${limit}`, void 0, request),
|
|
80
|
+
segment: (id, segmentId) => json("GET", path("documents", id, "segments", segmentId)),
|
|
81
|
+
edit: (id, segmentId, input, request) => json("PATCH", path("documents", id, "segments", segmentId), input, request),
|
|
82
|
+
attachAssets: (id, input, request) => json("POST", path("documents", id, "assets"), input, request),
|
|
83
|
+
segmentAssets: (id, segmentId) => json("GET", path("documents", id, "segments", segmentId, "assets")),
|
|
84
|
+
comments: (id, segmentId) => json("GET", path("documents", id, "comments") + (segmentId ? `?segmentId=${encodeURIComponent(segmentId)}` : "")),
|
|
85
|
+
addComment: (id, segmentId, body) => json("POST", path("documents", id, "comments"), { segmentId, body }),
|
|
86
|
+
setStyle: (id, style, customStyleInstruction) => json("PATCH", path("documents", id, "style"), { style, ...customStyleInstruction === void 0 ? {} : { customStyleInstruction } }),
|
|
87
|
+
setModels: (id, input) => json("PATCH", path("documents", id, "models"), input),
|
|
88
|
+
createJob: (id, input, request) => json("POST", path("documents", id, "jobs"), input, request),
|
|
89
|
+
job: (id, request) => json("GET", path("jobs", id), void 0, request),
|
|
90
|
+
cancelJob: (id) => json("POST", path("jobs", id, "cancel"), {}),
|
|
91
|
+
retryJob: (id, request) => json("POST", path("jobs", id, "retry"), {}, request),
|
|
92
|
+
undoJob: (id) => json("POST", path("jobs", id, "undo"), {}),
|
|
93
|
+
jobResults: (id) => json("GET", path("jobs", id, "results")),
|
|
94
|
+
updateSuggestion: (id, segmentId, input) => json("PATCH", path("documents", id, "segments", segmentId, "suggestion"), input),
|
|
95
|
+
discardSuggestion: (id, segmentId, input) => json("DELETE", path("documents", id, "segments", segmentId, "suggestion"), input),
|
|
96
|
+
applySuggestion: (id, segmentId, input) => json("POST", path("documents", id, "segments", segmentId, "apply-suggestion"), input),
|
|
97
|
+
exportDocument: async (id, request) => {
|
|
98
|
+
const result = await response("POST", path("documents", id, "export"), {}, request);
|
|
99
|
+
const body = await result.json();
|
|
100
|
+
return body;
|
|
101
|
+
},
|
|
102
|
+
async waitForJob(id, settings = {}) {
|
|
103
|
+
const duration = settings.timeoutMs ?? 12e4;
|
|
104
|
+
const deadline = Date.now() + duration;
|
|
105
|
+
const interval = settings.intervalMs ?? 500;
|
|
106
|
+
if (!Number.isInteger(duration) || duration < 1 || duration > 36e5 || !Number.isInteger(interval) || interval < 50 || interval > 5e3) throw new Error("invalid polling bounds");
|
|
107
|
+
const deadlineSignal = AbortSignal.timeout(duration);
|
|
108
|
+
const signal = settings.signal ? AbortSignal.any([settings.signal, deadlineSignal]) : deadlineSignal;
|
|
109
|
+
for (; ; ) {
|
|
110
|
+
settings.signal?.throwIfAborted();
|
|
111
|
+
if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
|
|
112
|
+
let result;
|
|
113
|
+
try {
|
|
114
|
+
result = await json("GET", path("jobs", id), void 0, { signal });
|
|
115
|
+
} catch (error) {
|
|
116
|
+
if (deadlineSignal.aborted && !settings.signal?.aborted) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
|
|
117
|
+
throw error;
|
|
118
|
+
}
|
|
119
|
+
if (!["queued", "running"].includes(result.status)) return result;
|
|
120
|
+
if (Date.now() >= deadline) throw new Error("job polling deadline exceeded; the server task has not been cancelled");
|
|
121
|
+
await new Promise((resolve6, reject) => {
|
|
122
|
+
const done = () => {
|
|
123
|
+
settings.signal?.removeEventListener("abort", abort);
|
|
124
|
+
resolve6();
|
|
125
|
+
};
|
|
126
|
+
const timer = setTimeout(done, Math.min(interval, Math.max(0, deadline - Date.now())));
|
|
127
|
+
const abort = () => {
|
|
128
|
+
clearTimeout(timer);
|
|
129
|
+
settings.signal?.removeEventListener("abort", abort);
|
|
130
|
+
reject(settings.signal?.reason);
|
|
131
|
+
};
|
|
132
|
+
settings.signal?.addEventListener("abort", abort, { once: true });
|
|
133
|
+
if (settings.signal?.aborted) abort();
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// packages/dsh-l10n/src/tools-platform.ts
|
|
141
|
+
function createPlatformJobTool(options) {
|
|
142
|
+
const client = createPlatformClient(options);
|
|
143
|
+
return defineTool({
|
|
144
|
+
name: "cat_platform_job",
|
|
145
|
+
description: "Create a revision-safe platform suggestion/QA job for explicitly selected segments. Never auto-applies results. Use a stable idempotency key when repeating a request.",
|
|
146
|
+
parameters: {
|
|
147
|
+
documentId: { type: "string", required: true, description: "Authorized platform document ID" },
|
|
148
|
+
type: { type: "string", required: true, description: "translate, polish, aiqa or check" },
|
|
149
|
+
segmentIds: { type: "array", items: { type: "string" }, required: true, description: "Stable selected IDs" },
|
|
150
|
+
idempotencyKey: { type: "string", required: true, description: "Unique request key; reuse only for identical requests" }
|
|
151
|
+
},
|
|
152
|
+
output: { schema: { type: "object", additionalProperties: false, properties: { jobId: { type: "string" } } }, render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }] },
|
|
153
|
+
async execute(args, exec) {
|
|
154
|
+
if (!["translate", "polish", "aiqa", "check"].includes(args.type)) throw new Error("unknown platform job type");
|
|
155
|
+
return client.createJob(args.documentId, { type: args.type, segmentIds: args.segmentIds }, { idempotencyKey: args.idempotencyKey, signal: exec.signal });
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// packages/dsh-l10n/src/tools-project.ts
|
|
161
|
+
import { defineTool as defineTool2 } from "@deepseek-ai/dsh-tools";
|
|
162
|
+
|
|
163
|
+
// packages/dsh-l10n/src/project.ts
|
|
164
|
+
import { createHash as createHash2 } from "node:crypto";
|
|
165
|
+
import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
|
|
166
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
167
|
+
import ExcelJS2 from "exceljs";
|
|
168
|
+
|
|
7
169
|
// packages/l10n-core/src/xml.ts
|
|
8
170
|
import { createHash } from "node:crypto";
|
|
9
171
|
import { Parser } from "htmlparser2";
|
|
@@ -29,6 +191,7 @@ function parseXml(xml) {
|
|
|
29
191
|
children: []
|
|
30
192
|
};
|
|
31
193
|
const stack = [root];
|
|
194
|
+
let cdata = false;
|
|
32
195
|
const parser = new Parser(
|
|
33
196
|
{
|
|
34
197
|
onopentag(name2, attribs) {
|
|
@@ -36,11 +199,19 @@ function parseXml(xml) {
|
|
|
36
199
|
if (!parent) return;
|
|
37
200
|
const start = parser.startIndex;
|
|
38
201
|
const openEnd = parser.endIndex + 1;
|
|
202
|
+
const namespaces = { xml: "http://www.w3.org/XML/1998/namespace", ...parent.namespaces };
|
|
203
|
+
for (const [key, value] of Object.entries(attribs)) {
|
|
204
|
+
if (key === "xmlns") namespaces[""] = decodeXmlEntities(value);
|
|
205
|
+
else if (key.startsWith("xmlns:")) namespaces[key.slice(6)] = decodeXmlEntities(value);
|
|
206
|
+
}
|
|
207
|
+
const prefix = name2.includes(":") ? name2.slice(0, name2.indexOf(":")) : "";
|
|
39
208
|
const node = {
|
|
40
209
|
type: "element",
|
|
41
210
|
name: name2,
|
|
42
211
|
local: localName(name2),
|
|
43
212
|
attribs: { ...attribs },
|
|
213
|
+
namespaceUri: namespaces[prefix] ?? "",
|
|
214
|
+
namespaces,
|
|
44
215
|
start,
|
|
45
216
|
openEnd,
|
|
46
217
|
innerStart: openEnd,
|
|
@@ -64,9 +235,16 @@ function parseXml(xml) {
|
|
|
64
235
|
parent.children.push({
|
|
65
236
|
type: "text",
|
|
66
237
|
text,
|
|
238
|
+
...cdata ? { cdata: true } : {},
|
|
67
239
|
start: parser.startIndex,
|
|
68
240
|
end: parser.endIndex + 1
|
|
69
241
|
});
|
|
242
|
+
},
|
|
243
|
+
oncdatastart() {
|
|
244
|
+
cdata = true;
|
|
245
|
+
},
|
|
246
|
+
oncdataend() {
|
|
247
|
+
cdata = false;
|
|
70
248
|
}
|
|
71
249
|
},
|
|
72
250
|
{
|
|
@@ -81,8 +259,8 @@ function parseXml(xml) {
|
|
|
81
259
|
parser.end();
|
|
82
260
|
return root;
|
|
83
261
|
}
|
|
84
|
-
function validateXml(xml) {
|
|
85
|
-
const parser = new SaxesParser({ xmlns:
|
|
262
|
+
function validateXml(xml, namespaces = false) {
|
|
263
|
+
const parser = new SaxesParser({ xmlns: namespaces });
|
|
86
264
|
let error;
|
|
87
265
|
parser.on("error", (value) => {
|
|
88
266
|
error = value;
|
|
@@ -135,7 +313,7 @@ function decodeXmlEntities(value) {
|
|
|
135
313
|
function textOf(node) {
|
|
136
314
|
let value = "";
|
|
137
315
|
for (const child of node.children) {
|
|
138
|
-
value += child.type === "text" ? decodeXmlEntities(child.text) : textOf(child);
|
|
316
|
+
value += child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : textOf(child);
|
|
139
317
|
}
|
|
140
318
|
return value;
|
|
141
319
|
}
|
|
@@ -156,12 +334,9 @@ function collectTextNodes(node) {
|
|
|
156
334
|
function parseFragment(content) {
|
|
157
335
|
validateXml(`<fragment>${content}</fragment>`);
|
|
158
336
|
const root = parseXml(`<fragment>${content}</fragment>`);
|
|
159
|
-
const
|
|
160
|
-
if (!
|
|
161
|
-
return
|
|
162
|
-
}
|
|
163
|
-
function fragmentSignature(content) {
|
|
164
|
-
return elementSignature(parseFragment(content));
|
|
337
|
+
const fragment2 = elementChildren(root)[0];
|
|
338
|
+
if (!fragment2) throw new Error("Fragment root missing");
|
|
339
|
+
return fragment2;
|
|
165
340
|
}
|
|
166
341
|
function structureSummary(xml) {
|
|
167
342
|
const root = parseXml(xml);
|
|
@@ -188,7 +363,182 @@ function structureSummary(xml) {
|
|
|
188
363
|
};
|
|
189
364
|
}
|
|
190
365
|
|
|
366
|
+
// packages/l10n-core/src/formats/inline.ts
|
|
367
|
+
function container(node, namespace) {
|
|
368
|
+
return (node.namespaceUri ?? "") === namespace && ["g", "pc", "mrk"].includes(node.local) && node.attribs.translate !== "no" && node.attribs.mtype !== "protected";
|
|
369
|
+
}
|
|
370
|
+
function inlineCharacter(node) {
|
|
371
|
+
const read = (candidate) => {
|
|
372
|
+
if (candidate.local !== "ch" || !["MQXliff", "http://www.memoq.com/namespace"].includes(candidate.namespaceUri ?? "")) return void 0;
|
|
373
|
+
const value = candidate.attribs.val;
|
|
374
|
+
return value === void 0 ? void 0 : decodeXmlEntities(value);
|
|
375
|
+
};
|
|
376
|
+
const direct = read(node);
|
|
377
|
+
if (direct !== void 0) return direct;
|
|
378
|
+
if (node.local !== "ph" || !["", "urn:oasis:names:tc:xliff:document:1.2", "urn:oasis:names:tc:xliff:document:2.0"].includes(node.namespaceUri ?? "")) return void 0;
|
|
379
|
+
const elements = elementChildren(node);
|
|
380
|
+
if (elements.length === 1 && node.children.every((child) => child.type === "element" || !child.text.trim())) return read(elements[0]);
|
|
381
|
+
if (elements.length) return void 0;
|
|
382
|
+
const payload = textOf(node);
|
|
383
|
+
if (payload.length > 4096 || !payload.trim().startsWith("<")) return void 0;
|
|
384
|
+
try {
|
|
385
|
+
const context = { ...node, namespaces: { mq: "http://www.memoq.com/namespace", ...node.namespaces } };
|
|
386
|
+
const root = fragment(payload, context).root;
|
|
387
|
+
const codes2 = elementChildren(root);
|
|
388
|
+
if (codes2.length !== 1 || !root.children.every((child) => child.type === "element" || !child.text.trim())) return void 0;
|
|
389
|
+
return read(codes2[0]);
|
|
390
|
+
} catch {
|
|
391
|
+
return void 0;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
function inlineText(root) {
|
|
395
|
+
const namespace = root.namespaceUri ?? "";
|
|
396
|
+
const walk = (node) => node.children.map((child) => child.type === "text" ? child.cdata ? child.text : decodeXmlEntities(child.text) : container(child, namespace) ? walk(child) : inlineCharacter(child) ?? "").join("");
|
|
397
|
+
return walk(root);
|
|
398
|
+
}
|
|
399
|
+
function editIdentity(code) {
|
|
400
|
+
const bindings = [];
|
|
401
|
+
const walk = (node, children) => {
|
|
402
|
+
bindings.push([node.name, node.namespaceUri ?? "", Object.keys(node.attribs).filter((name2) => name2.includes(":") && !name2.startsWith("xmlns:")).sort().map((name2) => [name2, node.namespaces?.[name2.split(":")[0]] ?? ""])]);
|
|
403
|
+
if (children) for (const child of elementChildren(node)) walk(child, true);
|
|
404
|
+
};
|
|
405
|
+
walk(code.node, code.raw.length === code.node.end - code.node.start);
|
|
406
|
+
return JSON.stringify([code.raw, bindings]);
|
|
407
|
+
}
|
|
408
|
+
function codeIdentity(node, native = true, namespace = node.namespaceUri ?? "") {
|
|
409
|
+
const identity = ["id", "rid", "startRef", "dataRef", "dataRefStart", "dataRefEnd", "pos"].map((key) => [key, node.attribs[key] ?? null]);
|
|
410
|
+
return JSON.stringify([
|
|
411
|
+
node.namespaceUri ?? "",
|
|
412
|
+
node.local,
|
|
413
|
+
identity,
|
|
414
|
+
native && !container(node, namespace) ? inlineCharacter(node) ?? textOf(node) : ""
|
|
415
|
+
]);
|
|
416
|
+
}
|
|
417
|
+
function inlineCodeSignatures(root) {
|
|
418
|
+
const result = [], namespace = root.namespaceUri ?? "";
|
|
419
|
+
const walk = (parent) => {
|
|
420
|
+
for (const node of elementChildren(parent)) {
|
|
421
|
+
result.push(codeIdentity(node, true, namespace));
|
|
422
|
+
if (container(node, namespace)) walk(node);
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
walk(root);
|
|
426
|
+
return result;
|
|
427
|
+
}
|
|
428
|
+
function fragment(content, context) {
|
|
429
|
+
const escape = (value) => value.replaceAll("&", "&").replaceAll('"', """).replaceAll("<", "<");
|
|
430
|
+
const ns = Object.entries(context.namespaces ?? {}).filter(([prefix2]) => prefix2 !== "xml").map(([prefix2, value]) => ` xmlns${prefix2 ? `:${prefix2}` : ""}="${escape(value)}"`).join("");
|
|
431
|
+
let prefix = "workbench";
|
|
432
|
+
while (Object.hasOwn(context.namespaces ?? {}, prefix)) prefix += "_";
|
|
433
|
+
const uri = context.namespaceUri ?? "";
|
|
434
|
+
const name2 = uri ? `${prefix}:wrapper` : "wrapper";
|
|
435
|
+
const binding = uri ? ` xmlns:${prefix}="${escape(uri)}"` : "";
|
|
436
|
+
const xml = `<${name2}${ns}${binding}>${content}</${name2}>`;
|
|
437
|
+
validateXml(xml, true);
|
|
438
|
+
return { root: elementChildren(parseXml(xml))[0], xml };
|
|
439
|
+
}
|
|
440
|
+
function usesSourceTagInventory(segment) {
|
|
441
|
+
return segment.targetTags.length === 0 && segment.targetInner.trim() === "" && segment.sourceTags.length > 0 && segment.sourceNode !== void 0;
|
|
442
|
+
}
|
|
443
|
+
function codes(root, xml) {
|
|
444
|
+
const result = [];
|
|
445
|
+
const namespace = root.namespaceUri ?? "";
|
|
446
|
+
const walk = (parent) => {
|
|
447
|
+
for (const child of elementChildren(parent)) {
|
|
448
|
+
if (container(child, namespace) && child.openEnd !== child.end) {
|
|
449
|
+
result.push({ raw: xml.slice(child.start, child.openEnd), node: child, close: false });
|
|
450
|
+
walk(child);
|
|
451
|
+
result.push({ raw: xml.slice(child.innerEnd, child.end), node: child, close: true });
|
|
452
|
+
} else result.push({ raw: xml.slice(child.start, child.end), node: child, close: false });
|
|
453
|
+
}
|
|
454
|
+
};
|
|
455
|
+
walk(root);
|
|
456
|
+
return result;
|
|
457
|
+
}
|
|
458
|
+
function validateInlineEdit(segment, content) {
|
|
459
|
+
if (content === segment.targetInner) return;
|
|
460
|
+
if (segment.readOnlyReason) throw new Error(`segment is read-only: ${segment.readOnlyReason}`);
|
|
461
|
+
if (!segment.targetNode) throw new Error("target node not available");
|
|
462
|
+
if (/<!\[CDATA\[|<!--|<\?/.test(content)) throw new Error("XML metadata cannot be introduced by a text edit");
|
|
463
|
+
const seeded = usesSourceTagInventory(segment);
|
|
464
|
+
const old = fragment(seeded ? segment.sourceInner : segment.targetInner, seeded ? segment.sourceNode : segment.targetNode);
|
|
465
|
+
const next = fragment(content, segment.targetNode);
|
|
466
|
+
const original = codes(old.root, old.xml), edited = codes(next.root, next.xml);
|
|
467
|
+
const queues = /* @__PURE__ */ new Map();
|
|
468
|
+
original.forEach((code, i) => {
|
|
469
|
+
const key = editIdentity(code), q = queues.get(key) ?? [];
|
|
470
|
+
q.push(i);
|
|
471
|
+
queues.set(key, q);
|
|
472
|
+
});
|
|
473
|
+
const order = edited.map((code) => {
|
|
474
|
+
const index = queues.get(editIdentity(code))?.shift();
|
|
475
|
+
if (index === void 0) throw new Error("inline code identity, attributes or protected payload changed");
|
|
476
|
+
return index;
|
|
477
|
+
});
|
|
478
|
+
if (order.length !== original.length || [...queues.values()].some((queue) => queue.length)) throw new Error("inline codes cannot be added or removed");
|
|
479
|
+
if (order.every((value, i) => value === i)) return;
|
|
480
|
+
const source = segment.sourceNode ? fragment(segment.sourceInner, segment.sourceNode) : void 0;
|
|
481
|
+
const sourceCodes = source ? codes(source.root, source.xml).filter((code) => !code.close) : [];
|
|
482
|
+
const hintFor = (code) => {
|
|
483
|
+
if ((code.node.namespaceUri ?? "") !== (old.root.namespaceUri ?? "")) return void 0;
|
|
484
|
+
const matches = sourceCodes.filter((item) => codeIdentity(item.node, false) === codeIdentity(code.node, false));
|
|
485
|
+
const sourceHint = matches.find((item) => ["no", "firstNo"].includes(item.node.attribs.canReorder ?? ""))?.node.attribs.canReorder;
|
|
486
|
+
if (sourceHint && matches.length !== 1) throw new Error("ambiguous source editing hints require original code order");
|
|
487
|
+
return code.node.attribs.canReorder === "no" || sourceHint === "no" ? "no" : code.node.attribs.canReorder === "firstNo" || sourceHint === "firstNo" ? "firstNo" : void 0;
|
|
488
|
+
};
|
|
489
|
+
const position = new Map(order.map((value, i) => [value, i]));
|
|
490
|
+
const stack = [], pairs = [];
|
|
491
|
+
let fixedOrder = false;
|
|
492
|
+
for (let i = 0; i < original.length; i++) {
|
|
493
|
+
const code = original[i], node = code.node;
|
|
494
|
+
if (code.close || (node.namespaceUri ?? "") !== (old.root.namespaceUri ?? "")) continue;
|
|
495
|
+
const name2 = node.local;
|
|
496
|
+
if (node.attribs.isolated === "yes" || node.attribs.isolated === "true") fixedOrder = true;
|
|
497
|
+
if (["bpt", "bx", "sc", "sm"].includes(name2)) stack.push(i);
|
|
498
|
+
if (["ept", "ex", "ec", "em"].includes(name2)) {
|
|
499
|
+
const start = stack.pop(), open = start === void 0 ? void 0 : original[start].node;
|
|
500
|
+
const opener = { ept: "bpt", ex: "bx", ec: "sc", em: "sm" }[name2];
|
|
501
|
+
const matches = open && open.local === opener && (["ec", "em"].includes(name2) ? node.attribs.startRef === open.attribs.id && node.attribs.startRef !== void 0 : node.attribs.rid !== void 0 || open.attribs.rid !== void 0 ? node.attribs.rid === open.attribs.rid : true);
|
|
502
|
+
if (!matches || start === void 0) fixedOrder = true;
|
|
503
|
+
else pairs.push([start, i]);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
if (stack.length) fixedOrder = true;
|
|
507
|
+
if (fixedOrder) throw new Error("isolated, overlapping or incomplete codes must retain their original order");
|
|
508
|
+
const active = [];
|
|
509
|
+
const ends = new Map(pairs.map(([start, end]) => [end, start]));
|
|
510
|
+
const starts = new Set(pairs.map(([start]) => start));
|
|
511
|
+
for (const index of order) {
|
|
512
|
+
if (starts.has(index)) active.push(index);
|
|
513
|
+
if (ends.has(index) && active.pop() !== ends.get(index)) throw new Error("inline code pairs are crossed or reversed");
|
|
514
|
+
}
|
|
515
|
+
const logical = original.map((code, index) => ({ code, index })).filter(({ code }) => !code.close);
|
|
516
|
+
let group = [];
|
|
517
|
+
const groups = [];
|
|
518
|
+
for (const { code, index } of logical) {
|
|
519
|
+
const hint = hintFor(code);
|
|
520
|
+
if (hint === "firstNo") {
|
|
521
|
+
group = [index];
|
|
522
|
+
groups.push(group);
|
|
523
|
+
} else if (hint === "no") {
|
|
524
|
+
if (!group.length) throw new Error("non-reorderable codes must retain their original order");
|
|
525
|
+
group.push(index);
|
|
526
|
+
} else group = [];
|
|
527
|
+
}
|
|
528
|
+
const logicalOrder = order.filter((index) => !original[index].close);
|
|
529
|
+
for (const members of groups) {
|
|
530
|
+
const start = logicalOrder.indexOf(members[0]);
|
|
531
|
+
if (members.some((index, offset) => logicalOrder[start + offset] !== index)) throw new Error("non-reorderable code sequence was split or reordered");
|
|
532
|
+
}
|
|
533
|
+
for (const { code, index } of logical) {
|
|
534
|
+
if (hintFor(code) === "no" && ["g", "pc", "mrk"].includes(code.node.local)) {
|
|
535
|
+
if (position.get(index) !== index) throw new Error("non-reorderable container moved");
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
191
540
|
// packages/l10n-core/src/formats/xliff12.ts
|
|
541
|
+
var SDL_NAMESPACE = "http://sdl.com/FileTypes/SdlXliff/1.0";
|
|
192
542
|
var SDL_DATATYPE = "x-sdlfilterframework2";
|
|
193
543
|
function attr(node, name2) {
|
|
194
544
|
return node.attribs[name2];
|
|
@@ -196,15 +546,15 @@ function attr(node, name2) {
|
|
|
196
546
|
function isSegMrk(node) {
|
|
197
547
|
return node.local === "mrk" && node.attribs.mtype === "seg";
|
|
198
548
|
}
|
|
199
|
-
function sdlElement(segDefs) {
|
|
200
|
-
|
|
201
|
-
|
|
549
|
+
function sdlElement(segDefs, mid) {
|
|
550
|
+
const nodes = segDefs ? elementChildren(segDefs).filter((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) : [];
|
|
551
|
+
const matches = mid === void 0 ? nodes : nodes.filter((node) => node.attribs.id === mid);
|
|
552
|
+
return matches.length === 1 ? matches[0] : void 0;
|
|
202
553
|
}
|
|
203
|
-
function sdlState(
|
|
204
|
-
const seg = sdlElement(segDefs);
|
|
554
|
+
function sdlState(seg) {
|
|
205
555
|
if (!seg) return void 0;
|
|
206
556
|
const state = {};
|
|
207
|
-
const
|
|
557
|
+
const mapping2 = [
|
|
208
558
|
["id", "id"],
|
|
209
559
|
["conf", "conf"],
|
|
210
560
|
["origin", "origin"],
|
|
@@ -212,7 +562,7 @@ function sdlState(segDefs) {
|
|
|
212
562
|
["textMatch", "text-match"],
|
|
213
563
|
["locked", "locked"]
|
|
214
564
|
];
|
|
215
|
-
for (const [key, source] of
|
|
565
|
+
for (const [key, source] of mapping2) {
|
|
216
566
|
const value = seg.attribs[source];
|
|
217
567
|
if (value !== void 0) state[key] = value;
|
|
218
568
|
}
|
|
@@ -223,7 +573,7 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
|
|
|
223
573
|
const targetTags = targetContainer ? elementSignature(targetContainer) : [];
|
|
224
574
|
const id = `${fileIndex}:${unitId}:${mid ?? segmentIndex}`;
|
|
225
575
|
if (!targetContainer) warnings.push(`${id}: target missing`);
|
|
226
|
-
if (
|
|
576
|
+
if (sourceContainer && targetContainer && JSON.stringify(inlineCodeSignatures(sourceContainer).sort()) !== JSON.stringify(inlineCodeSignatures(targetContainer).sort())) {
|
|
227
577
|
warnings.push(`${id}: source/target inline tag signature differs`);
|
|
228
578
|
}
|
|
229
579
|
return {
|
|
@@ -232,8 +582,8 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
|
|
|
232
582
|
location: { fileIndex, unitId, ...mid === void 0 ? {} : { mid }, segmentIndex },
|
|
233
583
|
sourceInner: sourceContainer ? xml.slice(sourceContainer.innerStart, sourceContainer.innerEnd) : "",
|
|
234
584
|
targetInner: targetContainer ? xml.slice(targetContainer.innerStart, targetContainer.innerEnd) : "",
|
|
235
|
-
sourceText: sourceContainer ?
|
|
236
|
-
targetText: targetContainer ?
|
|
585
|
+
sourceText: sourceContainer ? inlineText(sourceContainer) : "",
|
|
586
|
+
targetText: targetContainer ? inlineText(targetContainer) : "",
|
|
237
587
|
sourceTags,
|
|
238
588
|
targetTags,
|
|
239
589
|
targetRange: targetContainer ? { start: targetContainer.innerStart, end: targetContainer.innerEnd } : null,
|
|
@@ -244,115 +594,126 @@ function makeSegment(fileIndex, unitId, segmentIndex, ordinal, sourceContainer,
|
|
|
244
594
|
};
|
|
245
595
|
}
|
|
246
596
|
function parseXliff(xml, options = {}) {
|
|
247
|
-
validateXml(xml);
|
|
597
|
+
validateXml(xml, true);
|
|
248
598
|
const root = elementChildren(parseXml(xml))[0];
|
|
249
|
-
if (!root) throw new Error("
|
|
250
|
-
if (root.local !== "xliff") throw new Error(`Not an XLIFF document: <${root.name}>`);
|
|
599
|
+
if (!root || root.local !== "xliff") throw new Error("Not an XLIFF document");
|
|
251
600
|
const version = attr(root, "version") ?? "";
|
|
252
601
|
const isXliff2 = version === "2.0" || version === "2.1";
|
|
253
|
-
if (version !== "1.2" && !isXliff2)
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
const
|
|
259
|
-
const segments = [];
|
|
260
|
-
const warnings = [];
|
|
261
|
-
const hasSdlNamespace = Object.keys(root.attribs).some((key) => key.toLowerCase() === "xmlns:sdl");
|
|
262
|
-
const allElements = descendants(root);
|
|
263
|
-
const hasSdlElement = allElements.some((element) => element.name.startsWith("sdl:"));
|
|
264
|
-
const fileNodes = allElements.filter((element) => element.local === "file");
|
|
602
|
+
if (version !== "1.2" && !isXliff2) throw new Error("Unsupported XLIFF version: " + (version || "(missing)") + "; supported: 1.2, 2.0 and 2.1");
|
|
603
|
+
const namespace = root.namespaceUri ?? "";
|
|
604
|
+
const expected = isXliff2 ? "urn:oasis:names:tc:xliff:document:2.0" : "urn:oasis:names:tc:xliff:document:1.2";
|
|
605
|
+
if (namespace !== "" && namespace !== expected) throw new Error("Unsupported XLIFF namespace: " + namespace);
|
|
606
|
+
const core = (node, local) => node.local === local && (node.namespaceUri ?? "") === namespace;
|
|
607
|
+
const children = (node, local) => elementChildren(node).filter((child) => core(child, local));
|
|
608
|
+
const files = [], segments = [], warnings = [];
|
|
265
609
|
const filename = options.filename?.toLowerCase() ?? "";
|
|
266
|
-
|
|
267
|
-
|
|
610
|
+
const hasSdl = descendants(root).some((node) => node.namespaceUri === SDL_NAMESPACE);
|
|
611
|
+
let format = version === "2.1" ? "xliff21" : isXliff2 ? "xliff20" : filename.endsWith(".mqxliff") ? "mqxliff" : hasSdl || filename.endsWith(".sdlxliff") ? "sdlxliff" : "xliff12";
|
|
612
|
+
const ids = /* @__PURE__ */ new Map();
|
|
613
|
+
children(root, "file").forEach((fileNode, fileIndex) => {
|
|
268
614
|
const datatype = attr(fileNode, "datatype");
|
|
269
|
-
if (datatype === SDL_DATATYPE) format = "sdlxliff";
|
|
270
|
-
|
|
615
|
+
if (!isXliff2 && datatype === SDL_DATATYPE) format = "sdlxliff";
|
|
616
|
+
files.push({
|
|
271
617
|
index: fileIndex,
|
|
272
|
-
sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(
|
|
273
|
-
targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(
|
|
618
|
+
sourceLanguage: attr(fileNode, isXliff2 ? "srcLang" : "source-language") ?? attr(root, isXliff2 ? "srcLang" : "source-language") ?? "",
|
|
619
|
+
targetLanguage: attr(fileNode, isXliff2 ? "trgLang" : "target-language") ?? attr(root, isXliff2 ? "trgLang" : "target-language") ?? "",
|
|
274
620
|
...datatype === void 0 ? {} : { datatype }
|
|
275
|
-
};
|
|
276
|
-
|
|
277
|
-
const
|
|
278
|
-
|
|
279
|
-
(
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
if (isXliff2) {
|
|
284
|
-
const segmentNodes = descendants(unit, (candidate) => candidate.local === "segment");
|
|
285
|
-
if (segmentNodes.length === 0) {
|
|
286
|
-
warnings.push(`${fileIndex}:${unitId}: XLIFF 2.0 unit has no segment`);
|
|
287
|
-
return;
|
|
621
|
+
});
|
|
622
|
+
let unitIndex = 0;
|
|
623
|
+
const inherited = (node, parent) => node.attribs.translate === "no" ? false : node.attribs.translate === "yes" ? true : parent;
|
|
624
|
+
const walk = (parent, translate) => {
|
|
625
|
+
for (const unit of elementChildren(parent)) {
|
|
626
|
+
if (core(unit, "group") || core(unit, "body")) {
|
|
627
|
+
walk(unit, core(unit, "group") ? inherited(unit, translate) : translate);
|
|
628
|
+
continue;
|
|
288
629
|
}
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
);
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
const segDefsElement = children.find((child) => child.local === "seg-defs");
|
|
317
|
-
const sdl = sdlState(segDefsElement);
|
|
318
|
-
const sdlNode = sdlElement(segDefsElement);
|
|
319
|
-
if (segSourceElement) {
|
|
320
|
-
const sourceMrks = descendants(segSourceElement, isSegMrk);
|
|
321
|
-
const targetMrks = targetElement ? descendants(targetElement, isSegMrk) : [];
|
|
322
|
-
sourceMrks.forEach((sourceMrk, segmentIndex) => {
|
|
323
|
-
const mid = attr(sourceMrk, "mid");
|
|
324
|
-
const targetMrk = mid ? targetMrks.find((candidate) => candidate.attribs.mid === mid) : targetMrks[segmentIndex];
|
|
325
|
-
if (targetMrk === void 0 && targetMrks.length > 0) {
|
|
326
|
-
warnings.push(`${fileIndex}:${unitId}:${mid ?? segmentIndex}: target mrk missing`);
|
|
630
|
+
if (!core(unit, isXliff2 ? "unit" : "trans-unit")) continue;
|
|
631
|
+
const unitId = attr(unit, "id") ?? "unit-" + unitIndex;
|
|
632
|
+
unitIndex += 1;
|
|
633
|
+
const mayTranslate = inherited(unit, translate);
|
|
634
|
+
const data = /* @__PURE__ */ Object.create(null);
|
|
635
|
+
const duplicateData = /* @__PURE__ */ new Set();
|
|
636
|
+
for (const original of children(unit, "originalData")) for (const node of children(original, "data")) {
|
|
637
|
+
const id = node.attribs.id;
|
|
638
|
+
if (!id) continue;
|
|
639
|
+
if (Object.hasOwn(data, id)) duplicateData.add(id);
|
|
640
|
+
else data[id] = textOf(node);
|
|
641
|
+
}
|
|
642
|
+
for (const id of duplicateData) {
|
|
643
|
+
delete data[id];
|
|
644
|
+
warnings.push(fileIndex + ":" + unitId + ": ambiguous originalData reference " + id);
|
|
645
|
+
}
|
|
646
|
+
const segDefs = elementChildren(unit).find((node) => node.local === "seg-defs" && node.namespaceUri === SDL_NAMESPACE);
|
|
647
|
+
const push = (source2, target2, mid, index, reason) => {
|
|
648
|
+
const sdlNode = sdlElement(segDefs, mid), sdl = sdlState(sdlNode);
|
|
649
|
+
const segment = makeSegment(fileIndex, unitId, index, segments.length, source2, target2, mid, sdl, sdlNode, xml, warnings);
|
|
650
|
+
if (Object.keys(data).length) segment.originalData = data;
|
|
651
|
+
const markup = target2 && /<!\[CDATA\[|<!--|<\?/.test(segment.targetInner);
|
|
652
|
+
const ambiguousSdl = segDefs && elementChildren(segDefs).some((node) => node.local === "seg" && node.namespaceUri === SDL_NAMESPACE) && !sdlNode;
|
|
653
|
+
const readOnly = reason ?? (!mayTranslate ? "translate=no" : ambiguousSdl ? "SDL segment state cannot be mapped uniquely" : ["true", "yes", "1"].includes(sdl?.locked ?? "") ? "SDL segment locked" : markup ? "CDATA, comments or processing instructions require lossless read-only handling" : void 0);
|
|
654
|
+
if (readOnly) {
|
|
655
|
+
segment.readOnlyReason = readOnly;
|
|
656
|
+
warnings.push(segment.id + ": read-only: " + readOnly);
|
|
327
657
|
}
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
658
|
+
const existing = ids.get(segment.id);
|
|
659
|
+
if (existing) {
|
|
660
|
+
existing.readOnlyReason = "ambiguous segment identity";
|
|
661
|
+
segment.readOnlyReason = "ambiguous segment identity";
|
|
662
|
+
do {
|
|
663
|
+
segment.id += ":duplicate:" + segment.index;
|
|
664
|
+
} while (ids.has(segment.id));
|
|
665
|
+
warnings.push(existing.id + ": ambiguous segment identity; read-only");
|
|
666
|
+
}
|
|
667
|
+
ids.set(segment.id, segment);
|
|
668
|
+
segments.push(segment);
|
|
669
|
+
};
|
|
670
|
+
if (isXliff2) {
|
|
671
|
+
const nodes = children(unit, "segment");
|
|
672
|
+
if (!nodes.length) warnings.push(fileIndex + ":" + unitId + ": unit has no supported segment");
|
|
673
|
+
nodes.forEach((node, index) => push(
|
|
674
|
+
children(node, "source")[0],
|
|
675
|
+
children(node, "target")[0],
|
|
676
|
+
attr(node, "id") ?? "seg-" + index,
|
|
677
|
+
index,
|
|
678
|
+
children(node, "source").length > 1 || children(node, "target").length > 1 ? "ambiguous source/target containers" : void 0
|
|
679
|
+
));
|
|
680
|
+
continue;
|
|
681
|
+
}
|
|
682
|
+
const source = children(unit, "source")[0], target = children(unit, "target")[0], segSource = children(unit, "seg-source")[0];
|
|
683
|
+
const sourceMrks = segSource ? descendants(segSource, (node) => core(node, "mrk") && isSegMrk(node)) : [];
|
|
684
|
+
if (sourceMrks.length) {
|
|
685
|
+
const targetMrks = target ? descendants(target, (node) => core(node, "mrk") && isSegMrk(node)) : [];
|
|
686
|
+
sourceMrks.forEach((mrk, index) => {
|
|
687
|
+
const mid = attr(mrk, "mid");
|
|
688
|
+
const sourceMatches = sourceMrks.filter((node) => node.attribs.mid === mid);
|
|
689
|
+
const matches = mid ? targetMrks.filter((node) => node.attribs.mid === mid) : [];
|
|
690
|
+
const whole = sourceMrks.length === 1 && !targetMrks.length ? target : void 0;
|
|
691
|
+
const unique = mid !== void 0 && sourceMatches.length === 1 && matches.length === 1;
|
|
692
|
+
const mapped = unique ? matches[0] : whole;
|
|
693
|
+
push(mrk, mapped, mid, index, !mapped || sourceMrks.length > 1 && (!mid || sourceMatches.length !== 1) ? "segmented target cannot be mapped uniquely by mid" : void 0);
|
|
694
|
+
});
|
|
695
|
+
} else if (source || target || segSource) push(
|
|
696
|
+
source ?? segSource,
|
|
697
|
+
target,
|
|
698
|
+
void 0,
|
|
699
|
+
0,
|
|
700
|
+
children(unit, "source").length > 1 || children(unit, "target").length > 1 ? "ambiguous source/target containers" : void 0
|
|
349
701
|
);
|
|
350
|
-
|
|
351
|
-
warnings.push(`${fileIndex}:${unitId}: no source/target`);
|
|
702
|
+
else warnings.push(fileIndex + ":" + unitId + ": no source/target");
|
|
352
703
|
}
|
|
353
|
-
}
|
|
704
|
+
};
|
|
705
|
+
if (isXliff2) walk(fileNode, inherited(fileNode, true));
|
|
706
|
+
else for (const body of children(fileNode, "body")) walk(body, true);
|
|
354
707
|
});
|
|
355
|
-
|
|
708
|
+
const ranged = segments.filter((segment) => segment.targetRange).sort((a, b) => a.targetRange.start - b.targetRange.start || b.targetRange.end - a.targetRange.end);
|
|
709
|
+
let previous;
|
|
710
|
+
for (const segment of ranged) {
|
|
711
|
+
if (previous && (segment.targetRange.start < previous.targetRange.end || segment.targetRange.start === previous.targetRange.start)) {
|
|
712
|
+
previous.readOnlyReason = segment.readOnlyReason = "overlapping target containers";
|
|
713
|
+
warnings.push(segment.id + ": overlapping target containers; read-only");
|
|
714
|
+
}
|
|
715
|
+
if (!previous || segment.targetRange.end > previous.targetRange.end) previous = segment;
|
|
716
|
+
}
|
|
356
717
|
return { format, version, files, segments, warnings, xml };
|
|
357
718
|
}
|
|
358
719
|
|
|
@@ -361,24 +722,17 @@ function escapeXmlText(value) {
|
|
|
361
722
|
return value.replaceAll("&", "&").replaceAll("<", "<").replaceAll(">", ">");
|
|
362
723
|
}
|
|
363
724
|
function resolveContent(segment, edit) {
|
|
725
|
+
if (segment.readOnlyReason && !(edit.xml && edit.content === segment.targetInner)) throw new Error(`segment is read-only: ${segment.readOnlyReason}`);
|
|
364
726
|
const hasInlineTags = segment.targetTags.length > 0;
|
|
365
727
|
if (hasInlineTags) {
|
|
366
728
|
if (!edit.xml) {
|
|
367
729
|
throw new Error("target contains inline tags; provide a validated XML fragment with xml=true");
|
|
368
730
|
}
|
|
369
|
-
|
|
370
|
-
if (signature.join("|") !== segment.targetTags.join("|")) {
|
|
371
|
-
throw new Error(
|
|
372
|
-
`inline tag signature mismatch: expected [${segment.targetTags.join(", ")}], got [${signature.join(", ")}]`
|
|
373
|
-
);
|
|
374
|
-
}
|
|
731
|
+
validateInlineEdit(segment, edit.content);
|
|
375
732
|
return edit.content;
|
|
376
733
|
}
|
|
377
734
|
if (edit.xml) {
|
|
378
|
-
|
|
379
|
-
if (signature.length > 0) {
|
|
380
|
-
throw new Error("target has no inline tags; XML fragment must not add tags");
|
|
381
|
-
}
|
|
735
|
+
validateInlineEdit(segment, edit.content);
|
|
382
736
|
return edit.content;
|
|
383
737
|
}
|
|
384
738
|
return escapeXmlText(edit.content);
|
|
@@ -398,10 +752,14 @@ function patchDocument(document, edits) {
|
|
|
398
752
|
continue;
|
|
399
753
|
}
|
|
400
754
|
try {
|
|
755
|
+
const content = resolveContent(segment, edit);
|
|
756
|
+
const target = segment.targetNode;
|
|
757
|
+
const opening = target ? document.xml.slice(target.start, target.openEnd) : "";
|
|
758
|
+
const expandEmptyTarget = content !== "" && target !== void 0 && target.openEnd === target.end && opening.endsWith("/>");
|
|
401
759
|
prepared.push({
|
|
402
|
-
start: segment.targetRange.start,
|
|
403
|
-
end: segment.targetRange.end,
|
|
404
|
-
content:
|
|
760
|
+
start: expandEmptyTarget ? target.start : segment.targetRange.start,
|
|
761
|
+
end: expandEmptyTarget ? target.end : segment.targetRange.end,
|
|
762
|
+
content: expandEmptyTarget ? `${opening.slice(0, -2)}>${content}</${target.name}>` : content
|
|
405
763
|
});
|
|
406
764
|
} catch (caught) {
|
|
407
765
|
errors.push({
|
|
@@ -505,6 +863,10 @@ function patchSdlStates(document, updates) {
|
|
|
505
863
|
errors.push({ segmentId, message: "sdl:seg node missing" });
|
|
506
864
|
continue;
|
|
507
865
|
}
|
|
866
|
+
if (segment.readOnlyReason) {
|
|
867
|
+
errors.push({ segmentId, message: `segment is read-only: ${segment.readOnlyReason}` });
|
|
868
|
+
continue;
|
|
869
|
+
}
|
|
508
870
|
const node = segment.sdlNode;
|
|
509
871
|
let tag = document.xml.slice(node.start, node.openEnd);
|
|
510
872
|
for (const [key, attribute] of ATTRIBUTE_NAMES) {
|
|
@@ -702,6 +1064,35 @@ function parseTmx(xml, options = {}) {
|
|
|
702
1064
|
}
|
|
703
1065
|
return { entries, warnings };
|
|
704
1066
|
}
|
|
1067
|
+
function mergeKey(entry) {
|
|
1068
|
+
const normalize = (value) => value.normalize("NFKC").replace(/\s+/gu, " ").trim();
|
|
1069
|
+
const context = entry.context;
|
|
1070
|
+
return [
|
|
1071
|
+
normalizeLanguage(entry.sourceLanguage),
|
|
1072
|
+
normalizeLanguage(entry.targetLanguage),
|
|
1073
|
+
normalize(entry.source),
|
|
1074
|
+
normalize(entry.target),
|
|
1075
|
+
normalize(context?.previousSource ?? ""),
|
|
1076
|
+
normalize(context?.nextSource ?? "")
|
|
1077
|
+
].join("\0");
|
|
1078
|
+
}
|
|
1079
|
+
function mergeTmEntries(...inputs) {
|
|
1080
|
+
const entries = [];
|
|
1081
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1082
|
+
let duplicates = 0;
|
|
1083
|
+
for (const input of inputs) {
|
|
1084
|
+
for (const entry of input) {
|
|
1085
|
+
const key = mergeKey(entry);
|
|
1086
|
+
if (seen.has(key)) {
|
|
1087
|
+
duplicates += 1;
|
|
1088
|
+
continue;
|
|
1089
|
+
}
|
|
1090
|
+
seen.add(key);
|
|
1091
|
+
entries.push(entry);
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
return { entries, duplicates };
|
|
1095
|
+
}
|
|
705
1096
|
|
|
706
1097
|
// packages/l10n-core/src/assets/glossary.ts
|
|
707
1098
|
var SOURCE_HEADERS = /* @__PURE__ */ new Set(["source", "source term", "term", "\u6E90\u6587", "\u539F\u6587"]);
|
|
@@ -841,6 +1232,123 @@ function parseGlossaryCsv(text, options = {}) {
|
|
|
841
1232
|
return { entries, warnings };
|
|
842
1233
|
}
|
|
843
1234
|
|
|
1235
|
+
// packages/l10n-core/src/assets/tbx.ts
|
|
1236
|
+
function findChild2(parent, local) {
|
|
1237
|
+
return elementChildren(parent).find((child) => child.local === local);
|
|
1238
|
+
}
|
|
1239
|
+
function findDescendant2(parent, local) {
|
|
1240
|
+
for (const child of elementChildren(parent)) {
|
|
1241
|
+
if (child.local === local) return child;
|
|
1242
|
+
const nested = findDescendant2(child, local);
|
|
1243
|
+
if (nested) return nested;
|
|
1244
|
+
}
|
|
1245
|
+
return void 0;
|
|
1246
|
+
}
|
|
1247
|
+
function langSetLanguage(langSet) {
|
|
1248
|
+
return langSet.attribs["xml:lang"] ?? langSet.attribs.lang ?? "";
|
|
1249
|
+
}
|
|
1250
|
+
function findLangSet(langSets, language) {
|
|
1251
|
+
const wanted = normalizeLanguage(language);
|
|
1252
|
+
const exact = langSets.find((langSet) => normalizeLanguage(langSetLanguage(langSet)) === wanted);
|
|
1253
|
+
if (exact) return exact;
|
|
1254
|
+
return langSets.find((langSet) => languageMatches(langSetLanguage(langSet), language));
|
|
1255
|
+
}
|
|
1256
|
+
function termOf(langSet) {
|
|
1257
|
+
const tig = findChild2(langSet, "tig");
|
|
1258
|
+
if (!tig) return void 0;
|
|
1259
|
+
const term = findDescendant2(tig, "term");
|
|
1260
|
+
const text = term ? textOf(term).trim() : "";
|
|
1261
|
+
return text === "" ? void 0 : text;
|
|
1262
|
+
}
|
|
1263
|
+
function isForbiddenLangSet(langSet) {
|
|
1264
|
+
const walk = (parent) => {
|
|
1265
|
+
for (const child of elementChildren(parent)) {
|
|
1266
|
+
if (child.local === "descrip" || child.local === "termNote") {
|
|
1267
|
+
const type = (child.attribs.type ?? child.attribs.name ?? "").trim().toLowerCase();
|
|
1268
|
+
const value = textOf(child).trim().toLowerCase();
|
|
1269
|
+
if (type === "administrativestatus" && /forbidden|deprecated/u.test(value)) return true;
|
|
1270
|
+
if (type === "forbidden" && /^(?:true|yes|1)$/u.test(value)) return true;
|
|
1271
|
+
}
|
|
1272
|
+
if (walk(child)) return true;
|
|
1273
|
+
}
|
|
1274
|
+
return false;
|
|
1275
|
+
};
|
|
1276
|
+
return walk(langSet);
|
|
1277
|
+
}
|
|
1278
|
+
function preferredLanguageOrder(langSets) {
|
|
1279
|
+
const seen = [];
|
|
1280
|
+
for (const langSet of langSets) {
|
|
1281
|
+
const language = langSetLanguage(langSet);
|
|
1282
|
+
if (language !== "" && !seen.some((item) => normalizeLanguage(item) === normalizeLanguage(language))) {
|
|
1283
|
+
seen.push(language);
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
return seen;
|
|
1287
|
+
}
|
|
1288
|
+
function parseTbx(xml, options = {}) {
|
|
1289
|
+
const source = xml.charCodeAt(0) === 65279 ? xml.slice(1) : xml;
|
|
1290
|
+
validateXml(source);
|
|
1291
|
+
const document = parseXml(source);
|
|
1292
|
+
const root = elementChildren(document).find((child) => child.local === "tbx");
|
|
1293
|
+
if (!root) throw new Error("Not a TBX document: missing <tbx> root element");
|
|
1294
|
+
const warnings = [];
|
|
1295
|
+
const body = findChild2(root, "body");
|
|
1296
|
+
if (!body) {
|
|
1297
|
+
warnings.push("TBX: missing <body> element; no entries parsed");
|
|
1298
|
+
return { entries: [], warnings };
|
|
1299
|
+
}
|
|
1300
|
+
const termEntries = elementChildren(body).filter((child) => child.local === "termEntry");
|
|
1301
|
+
if (termEntries.length === 0) {
|
|
1302
|
+
warnings.push("TBX: no <termEntry> elements found");
|
|
1303
|
+
return { entries: [], warnings };
|
|
1304
|
+
}
|
|
1305
|
+
const declared = preferredLanguageOrder(
|
|
1306
|
+
termEntries.flatMap((entry) => elementChildren(entry).filter((child) => child.local === "langSet"))
|
|
1307
|
+
);
|
|
1308
|
+
const sourceLanguage = options.sourceLanguage ?? declared[0];
|
|
1309
|
+
if (sourceLanguage === void 0) {
|
|
1310
|
+
warnings.push("TBX: source language could not be determined; no entries parsed");
|
|
1311
|
+
return { entries: [], warnings };
|
|
1312
|
+
}
|
|
1313
|
+
const targetLanguage = options.targetLanguage ?? declared.find(
|
|
1314
|
+
(language) => !languageMatches(language, sourceLanguage) && normalizeLanguage(language) !== normalizeLanguage(sourceLanguage)
|
|
1315
|
+
);
|
|
1316
|
+
if (targetLanguage === void 0) {
|
|
1317
|
+
warnings.push("TBX: target language could not be determined; no entries parsed");
|
|
1318
|
+
return { entries: [], warnings };
|
|
1319
|
+
}
|
|
1320
|
+
const entries = [];
|
|
1321
|
+
for (const [index, termEntry] of termEntries.entries()) {
|
|
1322
|
+
const label = termEntry.attribs.id ?? `#${index + 1}`;
|
|
1323
|
+
const langSets = elementChildren(termEntry).filter((child) => child.local === "langSet");
|
|
1324
|
+
const sourceLangSet = findLangSet(langSets, sourceLanguage);
|
|
1325
|
+
const targetLangSet = findLangSet(langSets, targetLanguage);
|
|
1326
|
+
if (!sourceLangSet) {
|
|
1327
|
+
warnings.push(`TBX: termEntry ${label} has no langSet for source language "${sourceLanguage}"; skipped`);
|
|
1328
|
+
continue;
|
|
1329
|
+
}
|
|
1330
|
+
if (!targetLangSet) {
|
|
1331
|
+
warnings.push(`TBX: termEntry ${label} has no langSet for target language "${targetLanguage}"; skipped`);
|
|
1332
|
+
continue;
|
|
1333
|
+
}
|
|
1334
|
+
const sourceTerm = termOf(sourceLangSet);
|
|
1335
|
+
const targetTerm = termOf(targetLangSet);
|
|
1336
|
+
if (sourceTerm === void 0 || targetTerm === void 0) {
|
|
1337
|
+
warnings.push(`TBX: termEntry ${label} has no <term> text on one side; skipped`);
|
|
1338
|
+
continue;
|
|
1339
|
+
}
|
|
1340
|
+
entries.push({
|
|
1341
|
+
id: `tbx-${entries.length + 1}`,
|
|
1342
|
+
source: sourceTerm,
|
|
1343
|
+
target: targetTerm,
|
|
1344
|
+
sourceLanguage,
|
|
1345
|
+
targetLanguage,
|
|
1346
|
+
...isForbiddenLangSet(sourceLangSet) || isForbiddenLangSet(targetLangSet) ? { forbidden: true } : {}
|
|
1347
|
+
});
|
|
1348
|
+
}
|
|
1349
|
+
return { entries, warnings };
|
|
1350
|
+
}
|
|
1351
|
+
|
|
844
1352
|
// packages/l10n-core/src/assets/tm-matcher.ts
|
|
845
1353
|
var ZERO_WIDTH = /[\u00ad\u200b-\u200d\u2060\ufeff]/gu;
|
|
846
1354
|
var QUOTES = /[\u2018\u2019\u201a\u201b\u2032]/gu;
|
|
@@ -1396,7 +1904,7 @@ function matchTerms(text, entries, options = {}) {
|
|
|
1396
1904
|
return { hits, issues: [], warnings };
|
|
1397
1905
|
}
|
|
1398
1906
|
|
|
1399
|
-
// packages/l10n-core/src/qa/
|
|
1907
|
+
// packages/l10n-core/src/qa/catalog.ts
|
|
1400
1908
|
var RULES = [
|
|
1401
1909
|
// structural class
|
|
1402
1910
|
{ id: "EMPTY_TARGET", severity: "critical" },
|
|
@@ -1411,8 +1919,11 @@ var RULES = [
|
|
|
1411
1919
|
// content class
|
|
1412
1920
|
{ id: "NUMBER_MISMATCH", severity: "major" },
|
|
1413
1921
|
{ id: "DATE_MISMATCH", severity: "major" },
|
|
1922
|
+
{ id: "DATE_FORMAT", severity: "minor", requiresLocale: true },
|
|
1414
1923
|
{ id: "DUPLICATE_WORD", severity: "major" },
|
|
1415
1924
|
{ id: "TERM_MISSING", severity: "major", requiresTerms: true },
|
|
1925
|
+
{ id: "TERMINOLOGY_INCONSISTENT", severity: "major", requiresTerms: true, scope: "document" },
|
|
1926
|
+
{ id: "SOURCE_TARGET_INCONSISTENT", severity: "major", scope: "document" },
|
|
1416
1927
|
{ id: "SOURCE_LEFTOVER", severity: "major" },
|
|
1417
1928
|
{ id: "PUNCT_DUP", severity: "major" },
|
|
1418
1929
|
{ id: "TRAILING_COLON", severity: "minor" },
|
|
@@ -1422,9 +1933,24 @@ var RULES = [
|
|
|
1422
1933
|
{ id: "FULLWIDTH_HALFWIDTH", severity: "minor" },
|
|
1423
1934
|
{ id: "LENGTH_RATIO", severity: "neutral", defaultOff: true }
|
|
1424
1935
|
];
|
|
1936
|
+
var QA_RULE_CATALOG = Object.freeze(RULES.map((rule, index) => Object.freeze({
|
|
1937
|
+
ruleId: rule.id,
|
|
1938
|
+
code: `QA-${String(index + 1).padStart(3, "0")}`,
|
|
1939
|
+
severity: rule.severity,
|
|
1940
|
+
defaultEnabled: rule.defaultOff !== true,
|
|
1941
|
+
scope: rule.scope ?? "segment",
|
|
1942
|
+
requiresTerms: rule.requiresTerms === true,
|
|
1943
|
+
requiresLocale: rule.requiresLocale === true
|
|
1944
|
+
})));
|
|
1945
|
+
|
|
1946
|
+
// packages/l10n-core/src/qa/rules.ts
|
|
1425
1947
|
var RULE_INDEX = new Map(RULES.map((rule, index) => [rule.id, index]));
|
|
1426
1948
|
var RULE_SEVERITY = new Map(RULES.map((rule) => [rule.id, rule.severity]));
|
|
1427
1949
|
var TERM_RULE_IDS = RULES.filter((rule) => rule.requiresTerms).map((rule) => rule.id);
|
|
1950
|
+
var LOCALE_RULE_IDS = RULES.filter((rule) => rule.requiresLocale).map((rule) => rule.id);
|
|
1951
|
+
var DOCUMENT_RULE_IDS = new Set(
|
|
1952
|
+
RULES.filter((rule) => rule.scope === "document").map((rule) => rule.id)
|
|
1953
|
+
);
|
|
1428
1954
|
var DEFAULT_ENABLED = RULES.filter((rule) => rule.defaultOff !== true).map((rule) => rule.id);
|
|
1429
1955
|
function normalizeForCompare(value) {
|
|
1430
1956
|
return value.normalize("NFKC").toLocaleLowerCase().replace(/\s+/gu, " ").trim();
|
|
@@ -1538,15 +2064,14 @@ function checkTags(context) {
|
|
|
1538
2064
|
const sourceTags = context.segment.sourceTags;
|
|
1539
2065
|
const targetTags = context.segment.targetTags;
|
|
1540
2066
|
if (sourceTags.length === 0 && targetTags.length === 0) return;
|
|
1541
|
-
const
|
|
1542
|
-
const
|
|
1543
|
-
if (
|
|
1544
|
-
const sameMultiset = sourceTags.length === targetTags.length && JSON.stringify([...sourceTags].sort()) === JSON.stringify([...targetTags].sort());
|
|
2067
|
+
const source = context.segment.sourceNode ? inlineCodeSignatures(context.segment.sourceNode) : sourceTags;
|
|
2068
|
+
const target = context.segment.targetNode ? inlineCodeSignatures(context.segment.targetNode) : targetTags;
|
|
2069
|
+
if (JSON.stringify([...source].sort()) === JSON.stringify([...target].sort())) return;
|
|
1545
2070
|
draft(context, {
|
|
1546
2071
|
ruleId: "TAG_MISMATCH",
|
|
1547
|
-
message:
|
|
1548
|
-
evidence:
|
|
1549
|
-
suggestion: "\
|
|
2072
|
+
message: `\u6807\u7B7E\u7684\u6570\u91CF\u3001\u6807\u8BC6\u6216\u539F\u751F\u4EE3\u7801\u4E0E\u6E90\u6587\u4E0D\u4E00\u81F4`,
|
|
2073
|
+
evidence: JSON.stringify({ source, target }),
|
|
2074
|
+
suggestion: "\u6838\u5BF9\u6807\u7B7E\u7684 ID\u3001\u914D\u5BF9\u5F15\u7528\u548C\u539F\u751F\u4EE3\u7801\uFF0C\u4FDD\u7559\u6E90\u6587\u4E2D\u7684\u6807\u7B7E"
|
|
1550
2075
|
});
|
|
1551
2076
|
}
|
|
1552
2077
|
var URL_PATTERNS = [
|
|
@@ -1799,6 +2324,47 @@ function checkDates(context) {
|
|
|
1799
2324
|
...firstMissing === void 0 ? {} : { sourceRange: { start: firstMissing.span.start, end: firstMissing.span.end } }
|
|
1800
2325
|
});
|
|
1801
2326
|
}
|
|
2327
|
+
function localeDateOrder(locale) {
|
|
2328
|
+
const normalized = locale.trim().toLowerCase().replace(/_/gu, "-");
|
|
2329
|
+
if (/^(?:zh|ja|ko)(?:-|$)/u.test(normalized)) return "ymd";
|
|
2330
|
+
if (/^en-us(?:-|$)/u.test(normalized)) return "mdy";
|
|
2331
|
+
if (/^(?:en-gb|en-au|en-nz|en-ie|de|fr|es|it|pt|nl|ru|pl|tr|sv|da|fi|nb|cs|hu|ro|bg|hr|sk|sl|uk|el)(?:-|$)/u.test(
|
|
2332
|
+
normalized
|
|
2333
|
+
)) {
|
|
2334
|
+
return "dmy";
|
|
2335
|
+
}
|
|
2336
|
+
return void 0;
|
|
2337
|
+
}
|
|
2338
|
+
function numericDateOrder(text) {
|
|
2339
|
+
if (/^(?:\d{4}|[〇零一二三四五六七八九]{4})年/u.test(text)) return "ymd";
|
|
2340
|
+
if (/^\d{4}[./-]\d{1,2}[./-]\d{1,2}$/u.test(text)) return "ymd";
|
|
2341
|
+
const match = /^(\d{1,2})[./-](\d{1,2})[./-]\d{2,4}$/u.exec(text);
|
|
2342
|
+
if (!match) return void 0;
|
|
2343
|
+
const first = Number(match[1]);
|
|
2344
|
+
const second = Number(match[2]);
|
|
2345
|
+
if (first > 12 && second <= 12) return "dmy";
|
|
2346
|
+
if (second > 12 && first <= 12) return "mdy";
|
|
2347
|
+
return void 0;
|
|
2348
|
+
}
|
|
2349
|
+
function checkDateFormat(context) {
|
|
2350
|
+
const targetLocale = context.locale?.targetLocale;
|
|
2351
|
+
if (targetLocale === void 0) return;
|
|
2352
|
+
const expected = localeDateOrder(targetLocale);
|
|
2353
|
+
if (expected === void 0) return;
|
|
2354
|
+
const dates = extractDates(context.target);
|
|
2355
|
+
if (dates.length === 0) return;
|
|
2356
|
+
for (const token of dates) {
|
|
2357
|
+
const actual = numericDateOrder(token.text);
|
|
2358
|
+
if (actual === void 0 || actual === expected) continue;
|
|
2359
|
+
draft(context, {
|
|
2360
|
+
ruleId: "DATE_FORMAT",
|
|
2361
|
+
message: `\u65E5\u671F\u683C\u5F0F\u4E0E\u76EE\u6807\u8BED\u8A00\u4E60\u60EF\u4E0D\u4E00\u81F4: '${token.text}' \u4E3A ${actual.toUpperCase()}\uFF0C${targetLocale} \u671F\u671B ${expected.toUpperCase()}`,
|
|
2362
|
+
evidence: `target date: "${token.text}"`,
|
|
2363
|
+
suggestion: expected === "ymd" ? "\u6539\u4E3A \u5E74-\u6708-\u65E5 \u987A\u5E8F" : expected === "dmy" ? "\u6539\u4E3A \u65E5/\u6708/\u5E74 \u987A\u5E8F" : "\u6539\u4E3A \u6708/\u65E5/\u5E74 \u987A\u5E8F",
|
|
2364
|
+
targetRange: { start: token.span.start, end: token.span.end }
|
|
2365
|
+
});
|
|
2366
|
+
}
|
|
2367
|
+
}
|
|
1802
2368
|
var BRACKET_PAIRS = /* @__PURE__ */ new Map([
|
|
1803
2369
|
["(", /* @__PURE__ */ new Set([")", "\uFF09"])],
|
|
1804
2370
|
["\uFF08", /* @__PURE__ */ new Set(["\uFF09", ")"])],
|
|
@@ -2284,6 +2850,91 @@ function checkTermRules(context) {
|
|
|
2284
2850
|
});
|
|
2285
2851
|
}
|
|
2286
2852
|
}
|
|
2853
|
+
function sourceKey(value) {
|
|
2854
|
+
return value.normalize("NFKC").replace(/\s+/gu, " ").trim();
|
|
2855
|
+
}
|
|
2856
|
+
function sourceTargetInconsistencies(contexts) {
|
|
2857
|
+
const groups = /* @__PURE__ */ new Map();
|
|
2858
|
+
for (const context of contexts) {
|
|
2859
|
+
if (context.source.trim().length === 0 || context.target.trim().length === 0) continue;
|
|
2860
|
+
const key = sourceKey(context.source);
|
|
2861
|
+
const group = groups.get(key);
|
|
2862
|
+
if (group === void 0) groups.set(key, [context]);
|
|
2863
|
+
else group.push(context);
|
|
2864
|
+
}
|
|
2865
|
+
const drafts = [];
|
|
2866
|
+
let checkedSegments = 0;
|
|
2867
|
+
let emitted = 0;
|
|
2868
|
+
for (const group of groups.values()) {
|
|
2869
|
+
if (group.length < 2) continue;
|
|
2870
|
+
checkedSegments += group.length;
|
|
2871
|
+
const targets = group.map((context) => normalizeForCompare(context.target));
|
|
2872
|
+
const variants = Array.from(new Set(targets));
|
|
2873
|
+
if (variants.length < 2) continue;
|
|
2874
|
+
const reference = variants[0];
|
|
2875
|
+
for (let index = 1; index < group.length && emitted < 50; index += 1) {
|
|
2876
|
+
if (targets[index] === reference) continue;
|
|
2877
|
+
const context = group[index];
|
|
2878
|
+
const segment = context?.segment;
|
|
2879
|
+
if (context === void 0 || segment === void 0) continue;
|
|
2880
|
+
drafts.push({
|
|
2881
|
+
segment,
|
|
2882
|
+
draft: {
|
|
2883
|
+
ruleId: "SOURCE_TARGET_INCONSISTENT",
|
|
2884
|
+
message: `\u540C\u4E00\u6E90\u6587\u5B58\u5728\u4E0D\u540C\u8BD1\u6CD5: '${truncate(context.source)}'`,
|
|
2885
|
+
evidence: `variants: [${variants.slice(0, 3).map((value) => truncate(value, 40)).join(" | ")}]`,
|
|
2886
|
+
suggestion: "\u7EDF\u4E00\u8BE5\u53E5\u6BB5\u7684\u8BD1\u6CD5"
|
|
2887
|
+
}
|
|
2888
|
+
});
|
|
2889
|
+
emitted += 1;
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
return { drafts, checkedSegments };
|
|
2893
|
+
}
|
|
2894
|
+
function terminologyInconsistencies(contexts) {
|
|
2895
|
+
const perTerm = /* @__PURE__ */ new Map();
|
|
2896
|
+
let checkedSegments = 0;
|
|
2897
|
+
for (const context of contexts) {
|
|
2898
|
+
if (context.termHits === void 0 || context.target.trim().length === 0) continue;
|
|
2899
|
+
const sourceHits = longestHits(context.termHits.source);
|
|
2900
|
+
if (sourceHits.length === 0) continue;
|
|
2901
|
+
checkedSegments += 1;
|
|
2902
|
+
for (const hit of sourceHits) {
|
|
2903
|
+
const entry = hit.entry;
|
|
2904
|
+
if (entry.forbidden === true || !isApproved(entry) || (entry.target ?? "").trim().length === 0) continue;
|
|
2905
|
+
const bucket = perTerm.get(entry.id) ?? { entry, items: [] };
|
|
2906
|
+
if (!bucket.items.some((item) => item.context === context)) {
|
|
2907
|
+
bucket.items.push({
|
|
2908
|
+
context,
|
|
2909
|
+
mentioned: targetMentionsTerm(entry, context.target, context.termHits.target)
|
|
2910
|
+
});
|
|
2911
|
+
}
|
|
2912
|
+
perTerm.set(entry.id, bucket);
|
|
2913
|
+
}
|
|
2914
|
+
}
|
|
2915
|
+
const drafts = [];
|
|
2916
|
+
let emitted = 0;
|
|
2917
|
+
for (const { entry, items } of perTerm.values()) {
|
|
2918
|
+
const mentioned = items.filter((item) => item.mentioned);
|
|
2919
|
+
const missing = items.filter((item) => !item.mentioned);
|
|
2920
|
+
if (mentioned.length === 0 || missing.length === 0) continue;
|
|
2921
|
+
for (const item of missing) {
|
|
2922
|
+
if (emitted >= 25) break;
|
|
2923
|
+
drafts.push({
|
|
2924
|
+
segment: item.context.segment,
|
|
2925
|
+
draft: {
|
|
2926
|
+
ruleId: "TERMINOLOGY_INCONSISTENT",
|
|
2927
|
+
message: `\u672F\u8BED '${entry.source}' \u2192 '${entry.target}' \u5728\u90E8\u5206\u53E5\u6BB5\u5DF2\u4F7F\u7528\u3001\u90E8\u5206\u672A\u4F7F\u7528\uFF08\u4E0D\u4E00\u81F4\uFF09`,
|
|
2928
|
+
evidence: `used in ${mentioned.length} segment(s), missing in ${missing.length}`,
|
|
2929
|
+
suggestion: entry.target,
|
|
2930
|
+
termId: entry.id
|
|
2931
|
+
}
|
|
2932
|
+
});
|
|
2933
|
+
emitted += 1;
|
|
2934
|
+
}
|
|
2935
|
+
}
|
|
2936
|
+
return { drafts, checkedSegments };
|
|
2937
|
+
}
|
|
2287
2938
|
var CHECK_GROUPS = [
|
|
2288
2939
|
{ ids: ["EMPTY_TARGET"], run: checkEmptyTargetGroup },
|
|
2289
2940
|
{ ids: ["UNTRANSLATED_COPY"], run: checkUntranslatedCopyGroup },
|
|
@@ -2294,6 +2945,7 @@ var CHECK_GROUPS = [
|
|
|
2294
2945
|
{ ids: ["UNPAIRED_BRACKET"], run: checkUnpairedBrackets },
|
|
2295
2946
|
{ ids: ["NUMBER_MISMATCH"], run: checkNumbers },
|
|
2296
2947
|
{ ids: ["DATE_MISMATCH"], run: checkDates },
|
|
2948
|
+
{ ids: ["DATE_FORMAT"], run: checkDateFormat },
|
|
2297
2949
|
{ ids: ["DUPLICATE_WORD"], run: checkDuplicateWords },
|
|
2298
2950
|
{ ids: ["SOURCE_LEFTOVER"], run: checkSourceLeftover },
|
|
2299
2951
|
{ ids: ["PUNCT_DUP"], run: checkPunctDup },
|
|
@@ -2356,6 +3008,7 @@ function isApplicable(ruleId, segment) {
|
|
|
2356
3008
|
case "UNPAIRED_BRACKET":
|
|
2357
3009
|
case "NUMBER_MISMATCH":
|
|
2358
3010
|
case "DATE_MISMATCH":
|
|
3011
|
+
case "DATE_FORMAT":
|
|
2359
3012
|
case "DUPLICATE_WORD":
|
|
2360
3013
|
case "TERM_MISSING":
|
|
2361
3014
|
case "SOURCE_LEFTOVER":
|
|
@@ -2382,6 +3035,7 @@ function resolveEnabledRules(options) {
|
|
|
2382
3035
|
return enabled2;
|
|
2383
3036
|
}
|
|
2384
3037
|
const enabled = new Set(DEFAULT_ENABLED);
|
|
3038
|
+
if (options.locale?.targetLocale === void 0) for (const ruleId of LOCALE_RULE_IDS) enabled.delete(ruleId);
|
|
2385
3039
|
if (!termAssetAvailable(options)) for (const ruleId of TERM_RULE_IDS) enabled.delete(ruleId);
|
|
2386
3040
|
return enabled;
|
|
2387
3041
|
}
|
|
@@ -2443,6 +3097,14 @@ function runDeterministicChecks(segments, options = {}) {
|
|
|
2443
3097
|
const state = /* @__PURE__ */ new Map();
|
|
2444
3098
|
for (const rule of RULES) {
|
|
2445
3099
|
if (!enabled.has(rule.id)) continue;
|
|
3100
|
+
if (rule.requiresLocale === true && options.locale?.targetLocale === void 0) {
|
|
3101
|
+
state.set(rule.id, {
|
|
3102
|
+
status: "skipped",
|
|
3103
|
+
reason: "no locale.targetLocale provided; locale rule was not executed",
|
|
3104
|
+
checkedSegments: 0
|
|
3105
|
+
});
|
|
3106
|
+
continue;
|
|
3107
|
+
}
|
|
2446
3108
|
if (rule.requiresTerms === true && !termAvailable) {
|
|
2447
3109
|
state.set(rule.id, {
|
|
2448
3110
|
status: "skipped",
|
|
@@ -2459,6 +3121,7 @@ function runDeterministicChecks(segments, options = {}) {
|
|
|
2459
3121
|
const applicable = /* @__PURE__ */ new Set();
|
|
2460
3122
|
for (const ruleId of enabled) {
|
|
2461
3123
|
if (state.get(ruleId)?.status !== "executed") continue;
|
|
3124
|
+
if (DOCUMENT_RULE_IDS.has(ruleId)) continue;
|
|
2462
3125
|
if (isApplicable(ruleId, segment)) applicable.add(ruleId);
|
|
2463
3126
|
}
|
|
2464
3127
|
if (applicable.size === 0) continue;
|
|
@@ -2497,6 +3160,18 @@ function runDeterministicChecks(segments, options = {}) {
|
|
|
2497
3160
|
issues.push(toIssue(draft2, segment, draft2.ruleId));
|
|
2498
3161
|
}
|
|
2499
3162
|
}
|
|
3163
|
+
const documentRules = RULES.filter(
|
|
3164
|
+
(rule) => rule.scope === "document" && state.get(rule.id)?.status === "executed"
|
|
3165
|
+
);
|
|
3166
|
+
if (documentRules.length > 0) {
|
|
3167
|
+
const contexts = segments.map((segment) => buildContext(segment, options, glossary));
|
|
3168
|
+
for (const rule of documentRules) {
|
|
3169
|
+
const result = rule.id === "TERMINOLOGY_INCONSISTENT" ? terminologyInconsistencies(contexts) : sourceTargetInconsistencies(contexts);
|
|
3170
|
+
for (const item of result.drafts) issues.push(toIssue(item.draft, item.segment, rule.id));
|
|
3171
|
+
const execution = state.get(rule.id);
|
|
3172
|
+
if (execution !== void 0) execution.checkedSegments = result.checkedSegments;
|
|
3173
|
+
}
|
|
3174
|
+
}
|
|
2500
3175
|
const rules = [];
|
|
2501
3176
|
for (const rule of RULES) {
|
|
2502
3177
|
const execution = state.get(rule.id);
|
|
@@ -2574,6 +3249,11 @@ function translateDocument(document, tmEntries, options = {}) {
|
|
|
2574
3249
|
let skipped = 0;
|
|
2575
3250
|
for (const segment of document.segments) {
|
|
2576
3251
|
if (segment.targetText.trim().length > 0) continue;
|
|
3252
|
+
if (segment.readOnlyReason) {
|
|
3253
|
+
warnings.push(`${segment.id}: read-only: ${segment.readOnlyReason}; skipped`);
|
|
3254
|
+
skipped += 1;
|
|
3255
|
+
continue;
|
|
3256
|
+
}
|
|
2577
3257
|
if (segment.targetRange === null) {
|
|
2578
3258
|
warnings.push(`${segment.id}: target missing; cannot fill`);
|
|
2579
3259
|
skipped += 1;
|
|
@@ -3318,35 +3998,945 @@ function polishDocument(document, edits) {
|
|
|
3318
3998
|
};
|
|
3319
3999
|
}
|
|
3320
4000
|
|
|
4001
|
+
// packages/dsh-l10n/src/project.ts
|
|
4002
|
+
var MANIFEST_DIRECTORY = ".langlink";
|
|
4003
|
+
var MANIFEST_FILENAME = "manifest.json";
|
|
4004
|
+
var MANIFEST_VERSION = 1;
|
|
4005
|
+
var MAX_ASSET_BYTES = 200 * 1024 * 1024;
|
|
4006
|
+
var MAX_ASSETS = 64;
|
|
4007
|
+
function manifestPathFor(root) {
|
|
4008
|
+
return join(root, MANIFEST_DIRECTORY, MANIFEST_FILENAME);
|
|
4009
|
+
}
|
|
4010
|
+
function isRecord(value) {
|
|
4011
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
4012
|
+
}
|
|
4013
|
+
function requireString(value, field) {
|
|
4014
|
+
if (typeof value !== "string" || value.trim() === "") throw new Error(`project manifest: ${field} must be a non-empty string`);
|
|
4015
|
+
return value;
|
|
4016
|
+
}
|
|
4017
|
+
function optionalString(value, field) {
|
|
4018
|
+
if (value === void 0 || value === null) return void 0;
|
|
4019
|
+
if (typeof value !== "string" || value.trim() === "") throw new Error(`project manifest: ${field} must be a non-empty string`);
|
|
4020
|
+
return value;
|
|
4021
|
+
}
|
|
4022
|
+
function requireArray(value, field) {
|
|
4023
|
+
if (!Array.isArray(value)) throw new Error(`project manifest: ${field} must be an array`);
|
|
4024
|
+
return value;
|
|
4025
|
+
}
|
|
4026
|
+
function parseMapping(value, field) {
|
|
4027
|
+
if (value === void 0) return void 0;
|
|
4028
|
+
if (!isRecord(value)) throw new Error(`project manifest: ${field} must be an object`);
|
|
4029
|
+
const mapping2 = {};
|
|
4030
|
+
if (value.sheet !== void 0) mapping2.sheet = requireString(value.sheet, `${field}.sheet`);
|
|
4031
|
+
if (value.headerRow !== void 0) {
|
|
4032
|
+
if (typeof value.headerRow !== "number" || !Number.isInteger(value.headerRow) || value.headerRow < 0) {
|
|
4033
|
+
throw new Error(`project manifest: ${field}.headerRow must be a non-negative integer`);
|
|
4034
|
+
}
|
|
4035
|
+
mapping2.headerRow = value.headerRow;
|
|
4036
|
+
}
|
|
4037
|
+
for (const key of ["sourceColumn", "targetColumn", "forbiddenColumn"]) {
|
|
4038
|
+
if (value[key] !== void 0) mapping2[key] = requireString(value[key], `${field}.${key}`);
|
|
4039
|
+
}
|
|
4040
|
+
return Object.keys(mapping2).length === 0 ? void 0 : mapping2;
|
|
4041
|
+
}
|
|
4042
|
+
function parseAsset(value, index) {
|
|
4043
|
+
if (!isRecord(value)) throw new Error(`project manifest: assets[${index}] must be an object`);
|
|
4044
|
+
const id = requireString(value.id, `assets[${index}].id`);
|
|
4045
|
+
if (!/^[A-Za-z0-9._-]{1,64}$/u.test(id)) throw new Error(`project manifest: assets[${index}].id "${id}" is not a safe identifier`);
|
|
4046
|
+
const kind = requireString(value.kind, `assets[${index}].kind`);
|
|
4047
|
+
if (kind !== "tm" && kind !== "tb" && kind !== "bilingual") {
|
|
4048
|
+
throw new Error(`project manifest: assets[${index}].kind must be tm, tb or bilingual`);
|
|
4049
|
+
}
|
|
4050
|
+
const format = requireString(value.format, `assets[${index}].format`);
|
|
4051
|
+
if (!["tmx", "tbx", "csv", "tsv", "txt", "xlsx"].includes(format)) {
|
|
4052
|
+
throw new Error(`project manifest: assets[${index}].format "${format}" is not supported`);
|
|
4053
|
+
}
|
|
4054
|
+
const entries = isRecord(value.counts) ? value.counts.entries : 0;
|
|
4055
|
+
if (typeof entries !== "number" || !Number.isInteger(entries) || entries < 0) {
|
|
4056
|
+
throw new Error(`project manifest: assets[${index}].counts.entries must be a non-negative integer`);
|
|
4057
|
+
}
|
|
4058
|
+
const addedAt = requireString(value.addedAt, `assets[${index}].addedAt`);
|
|
4059
|
+
const updatedAt = requireString(value.updatedAt, `assets[${index}].updatedAt`);
|
|
4060
|
+
const sourceLang = optionalString(value.sourceLang, `assets[${index}].sourceLang`);
|
|
4061
|
+
const targetLang = optionalString(value.targetLang, `assets[${index}].targetLang`);
|
|
4062
|
+
const mapping2 = parseMapping(value.mapping, `assets[${index}].mapping`);
|
|
4063
|
+
return {
|
|
4064
|
+
id,
|
|
4065
|
+
kind,
|
|
4066
|
+
format,
|
|
4067
|
+
path: requireString(value.path, `assets[${index}].path`),
|
|
4068
|
+
enabled: value.enabled === void 0 ? true : value.enabled === true,
|
|
4069
|
+
priority: typeof value.priority === "number" && Number.isFinite(value.priority) ? value.priority : 100,
|
|
4070
|
+
...sourceLang === void 0 ? {} : { sourceLang },
|
|
4071
|
+
...targetLang === void 0 ? {} : { targetLang },
|
|
4072
|
+
sha256: requireString(value.sha256, `assets[${index}].sha256`),
|
|
4073
|
+
size: typeof value.size === "number" && Number.isFinite(value.size) ? value.size : 0,
|
|
4074
|
+
counts: { entries },
|
|
4075
|
+
...mapping2 === void 0 ? {} : { mapping: mapping2 },
|
|
4076
|
+
addedAt,
|
|
4077
|
+
updatedAt
|
|
4078
|
+
};
|
|
4079
|
+
}
|
|
4080
|
+
function parseManifest(value, manifestPath) {
|
|
4081
|
+
if (!isRecord(value)) throw new Error(`project manifest is not a JSON object: ${manifestPath}`);
|
|
4082
|
+
if (value.version !== MANIFEST_VERSION) {
|
|
4083
|
+
throw new Error(`project manifest version ${String(value.version)} is not supported (expected ${MANIFEST_VERSION})`);
|
|
4084
|
+
}
|
|
4085
|
+
const qaInput = isRecord(value.qa) ? value.qa : void 0;
|
|
4086
|
+
const outputsInput = isRecord(value.outputs) ? value.outputs : void 0;
|
|
4087
|
+
let qa;
|
|
4088
|
+
if (qaInput !== void 0) {
|
|
4089
|
+
const parsed = {};
|
|
4090
|
+
const locale = optionalString(qaInput.locale, "qa.locale");
|
|
4091
|
+
if (locale !== void 0) parsed.locale = locale;
|
|
4092
|
+
if (typeof qaInput.threshold === "number") parsed.threshold = qaInput.threshold;
|
|
4093
|
+
if (typeof qaInput.maxMatches === "number") parsed.maxMatches = qaInput.maxMatches;
|
|
4094
|
+
if (Array.isArray(qaInput.untranslatedCopyWhitelist)) {
|
|
4095
|
+
parsed.untranslatedCopyWhitelist = qaInput.untranslatedCopyWhitelist.filter(
|
|
4096
|
+
(item) => typeof item === "string"
|
|
4097
|
+
);
|
|
4098
|
+
}
|
|
4099
|
+
qa = parsed;
|
|
4100
|
+
}
|
|
4101
|
+
let outputs;
|
|
4102
|
+
if (outputsInput !== void 0) {
|
|
4103
|
+
const dir = optionalString(outputsInput.dir, "outputs.dir");
|
|
4104
|
+
outputs = dir === void 0 ? {} : { dir };
|
|
4105
|
+
}
|
|
4106
|
+
return {
|
|
4107
|
+
version: MANIFEST_VERSION,
|
|
4108
|
+
name: requireString(value.name, "name"),
|
|
4109
|
+
sourceLang: requireString(value.sourceLang, "sourceLang"),
|
|
4110
|
+
targetLang: requireString(value.targetLang, "targetLang"),
|
|
4111
|
+
createdAt: requireString(value.createdAt, "createdAt"),
|
|
4112
|
+
updatedAt: requireString(value.updatedAt, "updatedAt"),
|
|
4113
|
+
files: requireArray(value.files ?? [], "files").map((entry, index) => {
|
|
4114
|
+
if (!isRecord(entry)) throw new Error(`project manifest: files[${index}] must be an object`);
|
|
4115
|
+
return { path: requireString(entry.path, `files[${index}].path`), sha256: requireString(entry.sha256, `files[${index}].sha256`) };
|
|
4116
|
+
}),
|
|
4117
|
+
assets: requireArray(value.assets ?? [], "assets").map(parseAsset),
|
|
4118
|
+
...qa === void 0 ? {} : { qa },
|
|
4119
|
+
...outputs === void 0 ? {} : { outputs }
|
|
4120
|
+
};
|
|
4121
|
+
}
|
|
4122
|
+
async function readManifest(root) {
|
|
4123
|
+
const manifestPath = manifestPathFor(root);
|
|
4124
|
+
let text;
|
|
4125
|
+
try {
|
|
4126
|
+
text = await readFile(manifestPath, "utf8");
|
|
4127
|
+
} catch {
|
|
4128
|
+
throw new Error(`no project manifest at ${manifestPath}; run l10n_project_init first`);
|
|
4129
|
+
}
|
|
4130
|
+
let parsed;
|
|
4131
|
+
try {
|
|
4132
|
+
parsed = JSON.parse(text);
|
|
4133
|
+
} catch (caught) {
|
|
4134
|
+
throw new Error(`project manifest is not valid JSON: ${caught instanceof Error ? caught.message : String(caught)}`);
|
|
4135
|
+
}
|
|
4136
|
+
return { root, manifestPath, manifest: parseManifest(parsed, manifestPath) };
|
|
4137
|
+
}
|
|
4138
|
+
async function writeManifest(handle) {
|
|
4139
|
+
const { manifest, manifestPath } = handle;
|
|
4140
|
+
manifest.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
4141
|
+
await mkdir(dirname(manifestPath), { recursive: true });
|
|
4142
|
+
const temp = `${manifestPath}.tmp-${process.pid}-${Date.now()}`;
|
|
4143
|
+
await writeFile(temp, `${JSON.stringify(manifest, null, 2)}
|
|
4144
|
+
`, "utf8");
|
|
4145
|
+
await rename(temp, manifestPath);
|
|
4146
|
+
}
|
|
4147
|
+
async function isDirectory(path) {
|
|
4148
|
+
try {
|
|
4149
|
+
return (await stat(path)).isDirectory();
|
|
4150
|
+
} catch {
|
|
4151
|
+
return false;
|
|
4152
|
+
}
|
|
4153
|
+
}
|
|
4154
|
+
async function hasManifest(root) {
|
|
4155
|
+
try {
|
|
4156
|
+
return (await stat(manifestPathFor(root))).isFile();
|
|
4157
|
+
} catch {
|
|
4158
|
+
return false;
|
|
4159
|
+
}
|
|
4160
|
+
}
|
|
4161
|
+
async function findProjectRootFrom(start) {
|
|
4162
|
+
let current = resolve(start);
|
|
4163
|
+
try {
|
|
4164
|
+
if (!await isDirectory(current)) current = dirname(current);
|
|
4165
|
+
} catch {
|
|
4166
|
+
current = dirname(current);
|
|
4167
|
+
}
|
|
4168
|
+
for (; ; ) {
|
|
4169
|
+
if (await hasManifest(current)) return current;
|
|
4170
|
+
const parent = dirname(current);
|
|
4171
|
+
if (parent === current) return void 0;
|
|
4172
|
+
current = parent;
|
|
4173
|
+
}
|
|
4174
|
+
}
|
|
4175
|
+
async function findProject(project, anchor) {
|
|
4176
|
+
if (project !== void 0 && project.trim() !== "") {
|
|
4177
|
+
const candidate = resolve(project);
|
|
4178
|
+
if (candidate.endsWith(MANIFEST_FILENAME)) {
|
|
4179
|
+
const root2 = dirname(dirname(candidate));
|
|
4180
|
+
return readManifest(root2);
|
|
4181
|
+
}
|
|
4182
|
+
if (!await isDirectory(candidate)) throw new Error(`project root is not a directory: ${candidate}`);
|
|
4183
|
+
return await hasManifest(candidate) ? readManifest(candidate) : void 0;
|
|
4184
|
+
}
|
|
4185
|
+
const start = anchor ?? process.cwd();
|
|
4186
|
+
const root = await findProjectRootFrom(start);
|
|
4187
|
+
return root === void 0 ? void 0 : readManifest(root);
|
|
4188
|
+
}
|
|
4189
|
+
async function requireProject(project, anchor) {
|
|
4190
|
+
const handle = await findProject(project, anchor);
|
|
4191
|
+
if (handle !== void 0) return handle;
|
|
4192
|
+
if (project === void 0) {
|
|
4193
|
+
const fromCwd = await findProject(void 0, process.cwd());
|
|
4194
|
+
if (fromCwd !== void 0) return fromCwd;
|
|
4195
|
+
}
|
|
4196
|
+
throw new Error("no project manifest found; run l10n_project_init or pass project (root or .langlink/manifest.json)");
|
|
4197
|
+
}
|
|
4198
|
+
async function initProject(options) {
|
|
4199
|
+
const root = resolve(options.root);
|
|
4200
|
+
if (await isDirectory(root)) {
|
|
4201
|
+
const manifest = await hasManifest(root);
|
|
4202
|
+
if (manifest && options.overwrite !== true) {
|
|
4203
|
+
throw new Error(`project manifest already exists at ${manifestPathFor(root)}; pass overwrite=true to update languages/QA settings`);
|
|
4204
|
+
}
|
|
4205
|
+
}
|
|
4206
|
+
await mkdir(root, { recursive: true });
|
|
4207
|
+
await mkdir(join(root, "in"), { recursive: true });
|
|
4208
|
+
await mkdir(join(root, "assets"), { recursive: true });
|
|
4209
|
+
await mkdir(join(root, "out"), { recursive: true });
|
|
4210
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4211
|
+
let handle;
|
|
4212
|
+
if (await hasManifest(root)) {
|
|
4213
|
+
handle = await readManifest(root);
|
|
4214
|
+
handle.manifest.name = options.name ?? handle.manifest.name;
|
|
4215
|
+
handle.manifest.sourceLang = options.sourceLang;
|
|
4216
|
+
handle.manifest.targetLang = options.targetLang;
|
|
4217
|
+
} else {
|
|
4218
|
+
handle = {
|
|
4219
|
+
root,
|
|
4220
|
+
manifestPath: manifestPathFor(root),
|
|
4221
|
+
manifest: {
|
|
4222
|
+
version: MANIFEST_VERSION,
|
|
4223
|
+
name: options.name ?? "localization",
|
|
4224
|
+
sourceLang: options.sourceLang,
|
|
4225
|
+
targetLang: options.targetLang,
|
|
4226
|
+
createdAt: now,
|
|
4227
|
+
updatedAt: now,
|
|
4228
|
+
files: [],
|
|
4229
|
+
assets: [],
|
|
4230
|
+
outputs: { dir: "out" }
|
|
4231
|
+
}
|
|
4232
|
+
};
|
|
4233
|
+
}
|
|
4234
|
+
const qa = handle.manifest.qa ?? {};
|
|
4235
|
+
if (options.locale !== void 0) qa.locale = options.locale;
|
|
4236
|
+
if (options.threshold !== void 0) qa.threshold = options.threshold;
|
|
4237
|
+
if (options.maxMatches !== void 0) qa.maxMatches = options.maxMatches;
|
|
4238
|
+
if (options.untranslatedCopyWhitelist !== void 0) qa.untranslatedCopyWhitelist = options.untranslatedCopyWhitelist;
|
|
4239
|
+
handle.manifest.qa = qa;
|
|
4240
|
+
await writeManifest(handle);
|
|
4241
|
+
return handle;
|
|
4242
|
+
}
|
|
4243
|
+
function formatOf(path) {
|
|
4244
|
+
const lower = path.toLowerCase();
|
|
4245
|
+
if (lower.endsWith(".tmx")) return "tmx";
|
|
4246
|
+
if (lower.endsWith(".tbx")) return "tbx";
|
|
4247
|
+
if (lower.endsWith(".csv")) return "csv";
|
|
4248
|
+
if (lower.endsWith(".tsv")) return "tsv";
|
|
4249
|
+
if (lower.endsWith(".txt")) return "txt";
|
|
4250
|
+
if (lower.endsWith(".xlsx")) return "xlsx";
|
|
4251
|
+
throw new Error(`unsupported asset format: ${path} (expected .tmx, .tbx, .csv, .tsv, .txt or .xlsx)`);
|
|
4252
|
+
}
|
|
4253
|
+
function normalizeHeader2(value) {
|
|
4254
|
+
return value.trim().toLowerCase().replace(/[\s_-]+/gu, "");
|
|
4255
|
+
}
|
|
4256
|
+
var SOURCE_HEADERS3 = /* @__PURE__ */ new Set(["source", "src", "sourcetext", "\u6E90\u6587", "\u539F\u6587", "\u6E90\u8BED\u8A00"]);
|
|
4257
|
+
var TARGET_HEADERS3 = /* @__PURE__ */ new Set(["target", "tgt", "targettext", "\u8BD1\u6587", "\u76EE\u6A19", "\u76EE\u6807\u8BED\u8A00"]);
|
|
4258
|
+
var ID_HEADERS2 = /* @__PURE__ */ new Set(["id", "segmentid", "\u7F16\u53F7"]);
|
|
4259
|
+
var LOCATION_HEADERS2 = /* @__PURE__ */ new Set(["location", "\u4F4D\u7F6E", "\u6587\u4EF6", "file"]);
|
|
4260
|
+
var FORBIDDEN_HEADERS2 = /* @__PURE__ */ new Set(["forbidden", "disabled", "\u7981\u7528", "\u7981\u7528\u672F\u8BED", "\u662F\u5426\u7981\u7528"]);
|
|
4261
|
+
function looksLikeHeader(row) {
|
|
4262
|
+
const normalized = row.map(normalizeHeader2);
|
|
4263
|
+
return normalized.some((cell) => SOURCE_HEADERS3.has(cell)) && normalized.some((cell) => TARGET_HEADERS3.has(cell));
|
|
4264
|
+
}
|
|
4265
|
+
function headerColumn(row, aliases) {
|
|
4266
|
+
return row.findIndex((cell) => aliases.has(normalizeHeader2(cell)));
|
|
4267
|
+
}
|
|
4268
|
+
function columnIndex(value, header) {
|
|
4269
|
+
const trimmed = value.trim();
|
|
4270
|
+
if (/^[A-Za-z]{1,3}$/u.test(trimmed)) {
|
|
4271
|
+
let index = 0;
|
|
4272
|
+
for (const char of trimmed.toUpperCase()) index = index * 26 + (char.charCodeAt(0) - 64);
|
|
4273
|
+
return index - 1;
|
|
4274
|
+
}
|
|
4275
|
+
if (/^\d+$/u.test(trimmed)) {
|
|
4276
|
+
const number = Number(trimmed);
|
|
4277
|
+
if (number < 1) throw new Error(`column index must be 1-based: ${value}`);
|
|
4278
|
+
return number - 1;
|
|
4279
|
+
}
|
|
4280
|
+
if (header !== void 0) {
|
|
4281
|
+
const normalized = normalizeHeader2(trimmed);
|
|
4282
|
+
const index = header.findIndex((cell) => normalizeHeader2(cell) === normalized);
|
|
4283
|
+
if (index >= 0) return index;
|
|
4284
|
+
}
|
|
4285
|
+
throw new Error(`cannot resolve column "${value}"; use a 1-based index, spreadsheet letter, or a header name`);
|
|
4286
|
+
}
|
|
4287
|
+
async function readMatrix(path, format, mapping2) {
|
|
4288
|
+
if (format === "xlsx") {
|
|
4289
|
+
const workbook = new ExcelJS2.Workbook();
|
|
4290
|
+
await workbook.xlsx.load(await readFile(path));
|
|
4291
|
+
const sheet = mapping2?.sheet;
|
|
4292
|
+
let worksheet = workbook.worksheets[0];
|
|
4293
|
+
if (sheet !== void 0) {
|
|
4294
|
+
worksheet = /^\d+$/u.test(sheet) ? workbook.worksheets[Number(sheet) - 1] : workbook.getWorksheet(sheet);
|
|
4295
|
+
}
|
|
4296
|
+
if (!worksheet) throw new Error(`XLSX worksheet "${sheet ?? "1"}" was not found`);
|
|
4297
|
+
const rows = [];
|
|
4298
|
+
worksheet.eachRow({ includeEmpty: false }, (row) => {
|
|
4299
|
+
const values = [];
|
|
4300
|
+
for (let index = 1; index <= row.cellCount; index += 1) values.push(row.getCell(index).text ?? "");
|
|
4301
|
+
rows.push(values);
|
|
4302
|
+
});
|
|
4303
|
+
return { rows, warnings: sheet === void 0 && workbook.worksheets.length > 1 ? [`XLSX has ${workbook.worksheets.length} worksheets; using the first`] : [] };
|
|
4304
|
+
}
|
|
4305
|
+
const text = (await readFile(path, "utf8")).replace(/^\uFEFF/u, "");
|
|
4306
|
+
if (text.includes("\uFFFD")) throw new Error("bilingual asset is not valid UTF-8");
|
|
4307
|
+
const warnings = [];
|
|
4308
|
+
if (format === "csv") {
|
|
4309
|
+
const firstLine = text.split(/\r?\n/u)[0] ?? "";
|
|
4310
|
+
const tabs = (firstLine.match(/\t/gu) ?? []).length;
|
|
4311
|
+
const commas = (firstLine.match(/,/gu) ?? []).length;
|
|
4312
|
+
return { rows: parseCsvRows(text, tabs > commas ? " " : ","), warnings };
|
|
4313
|
+
}
|
|
4314
|
+
if (format === "tsv" || format === "txt") {
|
|
4315
|
+
return {
|
|
4316
|
+
rows: text.split(/\r?\n/u).filter((line) => line.trim() !== "").map((line) => line.split(" ")),
|
|
4317
|
+
warnings
|
|
4318
|
+
};
|
|
4319
|
+
}
|
|
4320
|
+
throw new Error(`unsupported bilingual format: ${format}`);
|
|
4321
|
+
}
|
|
4322
|
+
async function parseBilingualAsset(path, format, mapping2) {
|
|
4323
|
+
const matrix = await readMatrix(path, format, mapping2);
|
|
4324
|
+
if (matrix.rows.length === 0) throw new Error("bilingual table is empty");
|
|
4325
|
+
const warnings = [...matrix.warnings];
|
|
4326
|
+
const first = matrix.rows[0] ?? [];
|
|
4327
|
+
const headerRow = mapping2?.headerRow ?? (looksLikeHeader(first) ? 1 : 0);
|
|
4328
|
+
const autoHeader = headerRow === 1 && looksLikeHeader(first);
|
|
4329
|
+
const header = headerRow > 0 ? matrix.rows[headerRow - 1] : void 0;
|
|
4330
|
+
const sourceColumn = mapping2?.sourceColumn !== void 0 ? columnIndex(mapping2.sourceColumn, header) : autoHeader ? headerColumn(first, SOURCE_HEADERS3) : 0;
|
|
4331
|
+
const targetColumn = mapping2?.targetColumn !== void 0 ? columnIndex(mapping2.targetColumn, header) : autoHeader ? headerColumn(first, TARGET_HEADERS3) : 1;
|
|
4332
|
+
if (sourceColumn < 0 || targetColumn < 0) {
|
|
4333
|
+
throw new Error("could not detect source/target columns; pass mapping.sourceColumn/targetColumn");
|
|
4334
|
+
}
|
|
4335
|
+
const idColumn = autoHeader ? headerColumn(first, ID_HEADERS2) : -1;
|
|
4336
|
+
const locationColumn = autoHeader ? headerColumn(first, LOCATION_HEADERS2) : -1;
|
|
4337
|
+
const rows = [];
|
|
4338
|
+
for (let index = headerRow; index < matrix.rows.length; index += 1) {
|
|
4339
|
+
const row = matrix.rows[index] ?? [];
|
|
4340
|
+
const source = (row[sourceColumn] ?? "").trim();
|
|
4341
|
+
const target = (row[targetColumn] ?? "").trim();
|
|
4342
|
+
if (source === "" && target === "") continue;
|
|
4343
|
+
if (source === "") {
|
|
4344
|
+
warnings.push(`row ${index + 1}: empty source`);
|
|
4345
|
+
continue;
|
|
4346
|
+
}
|
|
4347
|
+
const id = (idColumn >= 0 ? (row[idColumn] ?? "").trim() : "") || `row-${index + 1 - headerRow}`;
|
|
4348
|
+
rows.push({
|
|
4349
|
+
id,
|
|
4350
|
+
source,
|
|
4351
|
+
target,
|
|
4352
|
+
...locationColumn >= 0 && (row[locationColumn] ?? "").trim() !== "" ? { location: (row[locationColumn] ?? "").trim() } : {}
|
|
4353
|
+
});
|
|
4354
|
+
}
|
|
4355
|
+
if (rows.length === 0) throw new Error("bilingual table has no usable rows");
|
|
4356
|
+
return { rows, warnings };
|
|
4357
|
+
}
|
|
4358
|
+
function glossaryFlag(value) {
|
|
4359
|
+
return /^(?:true|1|yes|y|是|禁用)$/iu.test(value.trim());
|
|
4360
|
+
}
|
|
4361
|
+
async function readGlossaryEntries(path, format, sourceLang, targetLang, mapping2) {
|
|
4362
|
+
if (format === "tbx") {
|
|
4363
|
+
if (mapping2 !== void 0) throw new Error("mapping is only supported for CSV/TSV glossaries");
|
|
4364
|
+
const parsed = parseTbx((await readFile(path, "utf8")).replace(/^\uFEFF/u, ""), {
|
|
4365
|
+
...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
|
|
4366
|
+
...targetLang === void 0 ? {} : { targetLanguage: targetLang }
|
|
4367
|
+
});
|
|
4368
|
+
return { entries: parsed.entries, warnings: parsed.warnings };
|
|
4369
|
+
}
|
|
4370
|
+
if (mapping2 === void 0) {
|
|
4371
|
+
const text = (await readFile(path, "utf8")).replace(/^\uFEFF/u, "");
|
|
4372
|
+
if (text.includes("\uFFFD")) throw new Error("glossary asset is not valid UTF-8");
|
|
4373
|
+
const parsed = parseGlossaryCsv(text, {
|
|
4374
|
+
...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
|
|
4375
|
+
...targetLang === void 0 ? {} : { targetLanguage: targetLang }
|
|
4376
|
+
});
|
|
4377
|
+
return { entries: parsed.entries, warnings: parsed.warnings };
|
|
4378
|
+
}
|
|
4379
|
+
const matrix = await readMatrix(path, format, mapping2);
|
|
4380
|
+
if (matrix.rows.length === 0) throw new Error("glossary table is empty");
|
|
4381
|
+
const warnings = [...matrix.warnings];
|
|
4382
|
+
const first = matrix.rows[0] ?? [];
|
|
4383
|
+
const headerRow = mapping2.headerRow ?? (looksLikeHeader(first) ? 1 : 0);
|
|
4384
|
+
const autoHeader = headerRow === 1 && looksLikeHeader(first);
|
|
4385
|
+
const header = headerRow > 0 ? matrix.rows[headerRow - 1] : void 0;
|
|
4386
|
+
const sourceColumn = mapping2.sourceColumn !== void 0 ? columnIndex(mapping2.sourceColumn, header) : autoHeader ? headerColumn(first, SOURCE_HEADERS3) : 0;
|
|
4387
|
+
const targetColumn = mapping2.targetColumn !== void 0 ? columnIndex(mapping2.targetColumn, header) : autoHeader ? headerColumn(first, TARGET_HEADERS3) : 1;
|
|
4388
|
+
const forbiddenColumn = mapping2.forbiddenColumn !== void 0 ? columnIndex(mapping2.forbiddenColumn, header) : autoHeader ? headerColumn(first, FORBIDDEN_HEADERS2) : -1;
|
|
4389
|
+
if (sourceColumn < 0 || targetColumn < 0) {
|
|
4390
|
+
throw new Error("could not resolve glossary source/target columns; pass mapping.sourceColumn/targetColumn");
|
|
4391
|
+
}
|
|
4392
|
+
const entries = [];
|
|
4393
|
+
for (let index = headerRow; index < matrix.rows.length; index += 1) {
|
|
4394
|
+
const row = matrix.rows[index] ?? [];
|
|
4395
|
+
const source = (row[sourceColumn] ?? "").trim();
|
|
4396
|
+
const target = (row[targetColumn] ?? "").trim();
|
|
4397
|
+
if (source === "" && target === "") continue;
|
|
4398
|
+
if (source === "" || target === "") {
|
|
4399
|
+
warnings.push(`row ${index + 1} is missing ${source === "" ? "source" : "target"}; skipped`);
|
|
4400
|
+
continue;
|
|
4401
|
+
}
|
|
4402
|
+
const forbidden = forbiddenColumn >= 0 ? glossaryFlag(row[forbiddenColumn] ?? "") : false;
|
|
4403
|
+
entries.push({
|
|
4404
|
+
id: `glossary-${entries.length + 1}`,
|
|
4405
|
+
source,
|
|
4406
|
+
target,
|
|
4407
|
+
...sourceLang === void 0 ? {} : { sourceLanguage: sourceLang },
|
|
4408
|
+
...targetLang === void 0 ? {} : { targetLanguage: targetLang },
|
|
4409
|
+
...forbidden ? { forbidden: true } : {}
|
|
4410
|
+
});
|
|
4411
|
+
}
|
|
4412
|
+
return { entries, warnings };
|
|
4413
|
+
}
|
|
4414
|
+
function bilingualRowsToTm(rows, assetId, sourceLang, targetLang) {
|
|
4415
|
+
return rows.filter((row) => row.target.trim() !== "").map((row) => ({
|
|
4416
|
+
id: `${assetId}:${row.id}`,
|
|
4417
|
+
source: row.source,
|
|
4418
|
+
target: row.target,
|
|
4419
|
+
sourceLanguage: sourceLang,
|
|
4420
|
+
targetLanguage: targetLang,
|
|
4421
|
+
origin: "tm"
|
|
4422
|
+
}));
|
|
4423
|
+
}
|
|
4424
|
+
async function parseAssetFile(absolutePath, kind, format, options) {
|
|
4425
|
+
const warnings = [];
|
|
4426
|
+
if (kind === "tm") {
|
|
4427
|
+
if (format !== "tmx") throw new Error("kind tm requires a .tmx file");
|
|
4428
|
+
const parsed2 = parseTmx((await readFile(absolutePath, "utf8")).replace(/^\uFEFF/u, ""));
|
|
4429
|
+
warnings.push(...parsed2.warnings);
|
|
4430
|
+
const entries2 = parsed2.entries.map((entry) => ({
|
|
4431
|
+
...entry,
|
|
4432
|
+
id: `${options.id}:${entry.id}`,
|
|
4433
|
+
...options.sourceLang === void 0 ? {} : { sourceLanguage: options.sourceLang },
|
|
4434
|
+
...options.targetLang === void 0 ? {} : { targetLanguage: options.targetLang }
|
|
4435
|
+
}));
|
|
4436
|
+
return { counts: { entries: entries2.length }, warnings, tmEntries: entries2, glossary: [] };
|
|
4437
|
+
}
|
|
4438
|
+
if (kind === "tb") {
|
|
4439
|
+
if (format !== "tbx" && format !== "csv" && format !== "tsv") {
|
|
4440
|
+
throw new Error("kind tb requires a .tbx, .csv or .tsv glossary");
|
|
4441
|
+
}
|
|
4442
|
+
const parsed2 = await readGlossaryEntries(
|
|
4443
|
+
absolutePath,
|
|
4444
|
+
format,
|
|
4445
|
+
options.sourceLang,
|
|
4446
|
+
options.targetLang,
|
|
4447
|
+
options.mapping
|
|
4448
|
+
);
|
|
4449
|
+
warnings.push(...parsed2.warnings);
|
|
4450
|
+
const entries2 = parsed2.entries.map((entry) => ({ ...entry, id: `${options.id}:${entry.id}` }));
|
|
4451
|
+
return { counts: { entries: entries2.length }, warnings, tmEntries: [], glossary: entries2 };
|
|
4452
|
+
}
|
|
4453
|
+
if (format !== "csv" && format !== "tsv" && format !== "txt" && format !== "xlsx") {
|
|
4454
|
+
throw new Error("kind bilingual requires a .csv, .tsv, .txt or .xlsx table");
|
|
4455
|
+
}
|
|
4456
|
+
const parsed = await parseBilingualAsset(absolutePath, format, options.mapping);
|
|
4457
|
+
warnings.push(...parsed.warnings);
|
|
4458
|
+
const sourceLang = options.sourceLang;
|
|
4459
|
+
const targetLang = options.targetLang;
|
|
4460
|
+
if (sourceLang === void 0 || targetLang === void 0) {
|
|
4461
|
+
throw new Error("bilingual assets require sourceLang and targetLang (pass them on l10n_asset_add or set them on the project)");
|
|
4462
|
+
}
|
|
4463
|
+
const entries = bilingualRowsToTm(parsed.rows, options.id, sourceLang, targetLang);
|
|
4464
|
+
return { counts: { entries: entries.length }, warnings, tmEntries: entries, glossary: [] };
|
|
4465
|
+
}
|
|
4466
|
+
function kindFromPath(path, format, source) {
|
|
4467
|
+
if (format === "tmx") return { kind: "tm", format };
|
|
4468
|
+
if (format === "tbx") return { kind: "tb", format };
|
|
4469
|
+
if (format === "xlsx") return { kind: "bilingual", format };
|
|
4470
|
+
if (format === "tsv" || format === "txt") return { kind: "bilingual", format };
|
|
4471
|
+
if (format === "csv") {
|
|
4472
|
+
let firstLine = "";
|
|
4473
|
+
try {
|
|
4474
|
+
firstLine = source.split(/\r?\n/u)[0] ?? "";
|
|
4475
|
+
} catch {
|
|
4476
|
+
firstLine = "";
|
|
4477
|
+
}
|
|
4478
|
+
const header = firstLine.split(",").map(normalizeHeader2);
|
|
4479
|
+
if (header.some((cell) => FORBIDDEN_HEADERS2.has(cell))) return { kind: "tb", format };
|
|
4480
|
+
if (header.some((cell) => ID_HEADERS2.has(cell)) || header.some((cell) => LOCATION_HEADERS2.has(cell))) {
|
|
4481
|
+
return { kind: "bilingual", format };
|
|
4482
|
+
}
|
|
4483
|
+
return { kind: "tb", format };
|
|
4484
|
+
}
|
|
4485
|
+
throw new Error(`unsupported asset format: ${path}`);
|
|
4486
|
+
}
|
|
4487
|
+
function absoluteAssetPath(root, path) {
|
|
4488
|
+
return isAbsolute(path) ? resolve(path) : resolve(root, path);
|
|
4489
|
+
}
|
|
4490
|
+
function storedAssetPath(root, absolutePath) {
|
|
4491
|
+
const relativePath = relative(root, absolutePath);
|
|
4492
|
+
if (relativePath === "" || relativePath.startsWith("..") || isAbsolute(relativePath)) return absolutePath;
|
|
4493
|
+
return relativePath.split("\\").join("/");
|
|
4494
|
+
}
|
|
4495
|
+
function nextAssetId(manifest) {
|
|
4496
|
+
const used = new Set(manifest.assets.map((asset) => asset.id));
|
|
4497
|
+
for (let index = 1; index <= MAX_ASSETS + 1; index += 1) {
|
|
4498
|
+
const id = `asset-${index}`;
|
|
4499
|
+
if (!used.has(id)) return id;
|
|
4500
|
+
}
|
|
4501
|
+
throw new Error(`project already has ${MAX_ASSETS} assets`);
|
|
4502
|
+
}
|
|
4503
|
+
async function readAssetBytes(absolutePath) {
|
|
4504
|
+
const info = await stat(absolutePath);
|
|
4505
|
+
if (!info.isFile()) throw new Error(`asset is not a regular file: ${absolutePath}`);
|
|
4506
|
+
if (info.size > MAX_ASSET_BYTES) throw new Error(`asset exceeds ${MAX_ASSET_BYTES} bytes: ${absolutePath}`);
|
|
4507
|
+
const buffer = await readFile(absolutePath);
|
|
4508
|
+
return { size: buffer.byteLength, sha256: createHash2("sha256").update(buffer).digest("hex") };
|
|
4509
|
+
}
|
|
4510
|
+
async function addAsset(handle, options) {
|
|
4511
|
+
const absolutePath = absoluteAssetPath(handle.root, options.path);
|
|
4512
|
+
let info;
|
|
4513
|
+
try {
|
|
4514
|
+
info = await stat(absolutePath);
|
|
4515
|
+
} catch {
|
|
4516
|
+
throw new Error(`asset file does not exist: ${absolutePath}`);
|
|
4517
|
+
}
|
|
4518
|
+
if (!info.isFile()) throw new Error(`asset is not a regular file: ${absolutePath}`);
|
|
4519
|
+
const format = options.format ?? formatOf(absolutePath);
|
|
4520
|
+
let peek = "";
|
|
4521
|
+
if (format === "csv") peek = (await readFile(absolutePath, "utf8")).slice(0, 4096);
|
|
4522
|
+
const inferred = options.kind === void 0 ? kindFromPath(absolutePath, format, peek) : void 0;
|
|
4523
|
+
const kind = options.kind ?? inferred?.kind;
|
|
4524
|
+
if (kind === void 0) throw new Error(`unsupported asset format: ${absolutePath}`);
|
|
4525
|
+
if (kind !== "bilingual" && format !== "tmx" && format !== "tbx" && format !== "csv" && format !== "tsv") {
|
|
4526
|
+
throw new Error(`kind ${kind} does not support format ${format}`);
|
|
4527
|
+
}
|
|
4528
|
+
const sourceLang = options.sourceLang ?? handle.manifest.sourceLang;
|
|
4529
|
+
const targetLang = options.targetLang ?? handle.manifest.targetLang;
|
|
4530
|
+
const id = options.id ?? nextAssetId(handle.manifest);
|
|
4531
|
+
if (!/^[A-Za-z0-9._-]{1,64}$/u.test(id)) throw new Error("asset id must match [A-Za-z0-9._-]{1,64}");
|
|
4532
|
+
if (handle.manifest.assets.some((asset2) => asset2.id === id)) throw new Error(`asset id already exists: ${id}`);
|
|
4533
|
+
if (handle.manifest.assets.some((asset2) => absoluteAssetPath(handle.root, asset2.path) === absolutePath && asset2.kind === kind)) {
|
|
4534
|
+
throw new Error(`asset already registered for ${absolutePath} (${kind}); use l10n_asset_update`);
|
|
4535
|
+
}
|
|
4536
|
+
if (handle.manifest.assets.length >= MAX_ASSETS) throw new Error(`project already has ${MAX_ASSETS} assets`);
|
|
4537
|
+
const { size, sha256 } = await readAssetBytes(absolutePath);
|
|
4538
|
+
const parsed = await parseAssetFile(absolutePath, kind, format, {
|
|
4539
|
+
id,
|
|
4540
|
+
...sourceLang === void 0 ? {} : { sourceLang },
|
|
4541
|
+
...targetLang === void 0 ? {} : { targetLang },
|
|
4542
|
+
...options.mapping === void 0 ? {} : { mapping: options.mapping }
|
|
4543
|
+
});
|
|
4544
|
+
if (parsed.counts.entries === 0) throw new Error("asset contains no usable entries");
|
|
4545
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4546
|
+
const asset = {
|
|
4547
|
+
id,
|
|
4548
|
+
kind,
|
|
4549
|
+
format,
|
|
4550
|
+
path: storedAssetPath(handle.root, absolutePath),
|
|
4551
|
+
enabled: options.enabled !== false,
|
|
4552
|
+
priority: options.priority ?? 100,
|
|
4553
|
+
...sourceLang === void 0 ? {} : { sourceLang },
|
|
4554
|
+
...targetLang === void 0 ? {} : { targetLang },
|
|
4555
|
+
sha256,
|
|
4556
|
+
size,
|
|
4557
|
+
counts: parsed.counts,
|
|
4558
|
+
...options.mapping === void 0 ? {} : { mapping: options.mapping },
|
|
4559
|
+
addedAt: now,
|
|
4560
|
+
updatedAt: now
|
|
4561
|
+
};
|
|
4562
|
+
handle.manifest.assets.push(asset);
|
|
4563
|
+
await writeManifest(handle);
|
|
4564
|
+
return { asset, warnings: parsed.warnings };
|
|
4565
|
+
}
|
|
4566
|
+
async function updateAsset(handle, options) {
|
|
4567
|
+
const asset = handle.manifest.assets.find((item) => item.id === options.id);
|
|
4568
|
+
if (asset === void 0) throw new Error(`unknown asset id: ${options.id}`);
|
|
4569
|
+
const absolutePath = absoluteAssetPath(handle.root, asset.path);
|
|
4570
|
+
const warnings = [];
|
|
4571
|
+
const needsRescan = options.rescan === true || options.mapping !== void 0 || options.sourceLang !== void 0 || options.targetLang !== void 0;
|
|
4572
|
+
if (options.enabled !== void 0) asset.enabled = options.enabled;
|
|
4573
|
+
if (options.priority !== void 0) {
|
|
4574
|
+
if (!Number.isFinite(options.priority)) throw new Error("priority must be a finite number");
|
|
4575
|
+
asset.priority = options.priority;
|
|
4576
|
+
}
|
|
4577
|
+
if (options.sourceLang !== void 0) asset.sourceLang = options.sourceLang;
|
|
4578
|
+
if (options.targetLang !== void 0) asset.targetLang = options.targetLang;
|
|
4579
|
+
if (options.mapping !== void 0) asset.mapping = options.mapping;
|
|
4580
|
+
if (needsRescan) {
|
|
4581
|
+
const parsed = await parseAssetFile(absolutePath, asset.kind, asset.format, {
|
|
4582
|
+
id: asset.id,
|
|
4583
|
+
...asset.sourceLang === void 0 ? {} : { sourceLang: asset.sourceLang },
|
|
4584
|
+
...asset.targetLang === void 0 ? {} : { targetLang: asset.targetLang },
|
|
4585
|
+
...asset.mapping === void 0 ? {} : { mapping: asset.mapping }
|
|
4586
|
+
});
|
|
4587
|
+
if (parsed.counts.entries === 0) throw new Error("asset contains no usable entries after rescan");
|
|
4588
|
+
const { size, sha256 } = await readAssetBytes(absolutePath);
|
|
4589
|
+
asset.counts = parsed.counts;
|
|
4590
|
+
asset.size = size;
|
|
4591
|
+
asset.sha256 = sha256;
|
|
4592
|
+
warnings.push(...parsed.warnings);
|
|
4593
|
+
}
|
|
4594
|
+
asset.updatedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
4595
|
+
await writeManifest(handle);
|
|
4596
|
+
return { asset, warnings };
|
|
4597
|
+
}
|
|
4598
|
+
async function removeAsset(handle, id) {
|
|
4599
|
+
const index = handle.manifest.assets.findIndex((asset) => asset.id === id);
|
|
4600
|
+
if (index < 0) throw new Error(`unknown asset id: ${id}`);
|
|
4601
|
+
const removed = handle.manifest.assets[index];
|
|
4602
|
+
handle.manifest.assets.splice(index, 1);
|
|
4603
|
+
await writeManifest(handle);
|
|
4604
|
+
return { removed, remaining: handle.manifest.assets.length };
|
|
4605
|
+
}
|
|
4606
|
+
function assetAbsolutePath(handle, asset) {
|
|
4607
|
+
return absoluteAssetPath(handle.root, asset.path);
|
|
4608
|
+
}
|
|
4609
|
+
function normalizeLanguageTag(language) {
|
|
4610
|
+
return language.trim().replace(/_/gu, "-").toLowerCase();
|
|
4611
|
+
}
|
|
4612
|
+
function languageCompatible(actual, wanted) {
|
|
4613
|
+
if (actual === void 0 || wanted === void 0 || wanted.trim() === "") return true;
|
|
4614
|
+
const a = normalizeLanguageTag(actual);
|
|
4615
|
+
const w = normalizeLanguageTag(wanted);
|
|
4616
|
+
if (a === w) return true;
|
|
4617
|
+
return a.split("-")[0] === w.split("-")[0];
|
|
4618
|
+
}
|
|
4619
|
+
async function collectProjectAssets(handle, options = {}) {
|
|
4620
|
+
const tmEntries = [];
|
|
4621
|
+
const glossary = [];
|
|
4622
|
+
const used = [];
|
|
4623
|
+
const warnings = [];
|
|
4624
|
+
const enabled = handle.manifest.assets.filter((asset) => asset.enabled).sort((left, right) => right.priority - left.priority || left.id.localeCompare(right.id));
|
|
4625
|
+
for (const asset of enabled) {
|
|
4626
|
+
if (!languageCompatible(asset.sourceLang, options.sourceLanguage) || !languageCompatible(asset.targetLang, options.targetLanguage)) {
|
|
4627
|
+
continue;
|
|
4628
|
+
}
|
|
4629
|
+
const absolutePath = assetAbsolutePath(handle, asset);
|
|
4630
|
+
let parsed;
|
|
4631
|
+
try {
|
|
4632
|
+
parsed = await parseAssetFile(absolutePath, asset.kind, asset.format, {
|
|
4633
|
+
id: asset.id,
|
|
4634
|
+
...asset.sourceLang === void 0 ? {} : { sourceLang: asset.sourceLang },
|
|
4635
|
+
...asset.targetLang === void 0 ? {} : { targetLang: asset.targetLang },
|
|
4636
|
+
...asset.mapping === void 0 ? {} : { mapping: asset.mapping }
|
|
4637
|
+
});
|
|
4638
|
+
} catch (caught) {
|
|
4639
|
+
warnings.push(`asset ${asset.id} (${asset.path}) could not be parsed: ${caught instanceof Error ? caught.message : String(caught)}`);
|
|
4640
|
+
continue;
|
|
4641
|
+
}
|
|
4642
|
+
tmEntries.push(...parsed.tmEntries);
|
|
4643
|
+
glossary.push(...parsed.glossary);
|
|
4644
|
+
used.push({ id: asset.id, kind: asset.kind, path: asset.path, entries: parsed.counts.entries });
|
|
4645
|
+
}
|
|
4646
|
+
const merged = mergeTmEntries(tmEntries);
|
|
4647
|
+
return { tmEntries: merged.entries, glossary, used, warnings };
|
|
4648
|
+
}
|
|
4649
|
+
async function projectStatus(handle, verify = false) {
|
|
4650
|
+
const assets = [];
|
|
4651
|
+
for (const asset of handle.manifest.assets) {
|
|
4652
|
+
const absolutePath = assetAbsolutePath(handle, asset);
|
|
4653
|
+
let exists = false;
|
|
4654
|
+
let sizeOnDisk;
|
|
4655
|
+
let hashMatch;
|
|
4656
|
+
try {
|
|
4657
|
+
const info = await stat(absolutePath);
|
|
4658
|
+
exists = info.isFile();
|
|
4659
|
+
if (exists) {
|
|
4660
|
+
sizeOnDisk = info.size;
|
|
4661
|
+
if (verify) {
|
|
4662
|
+
const { sha256 } = await readAssetBytes(absolutePath);
|
|
4663
|
+
hashMatch = sha256 === asset.sha256;
|
|
4664
|
+
}
|
|
4665
|
+
}
|
|
4666
|
+
} catch {
|
|
4667
|
+
exists = false;
|
|
4668
|
+
}
|
|
4669
|
+
assets.push({ ...asset, absolutePath, exists, ...sizeOnDisk === void 0 ? {} : { sizeOnDisk }, ...hashMatch === void 0 ? {} : { hashMatch } });
|
|
4670
|
+
}
|
|
4671
|
+
return {
|
|
4672
|
+
root: handle.root,
|
|
4673
|
+
manifestPath: handle.manifestPath,
|
|
4674
|
+
name: handle.manifest.name,
|
|
4675
|
+
sourceLang: handle.manifest.sourceLang,
|
|
4676
|
+
targetLang: handle.manifest.targetLang,
|
|
4677
|
+
createdAt: handle.manifest.createdAt,
|
|
4678
|
+
updatedAt: handle.manifest.updatedAt,
|
|
4679
|
+
qa: handle.manifest.qa,
|
|
4680
|
+
assets,
|
|
4681
|
+
enabledAssets: assets.filter((asset) => asset.enabled).length,
|
|
4682
|
+
trackedFiles: handle.manifest.files.length
|
|
4683
|
+
};
|
|
4684
|
+
}
|
|
4685
|
+
function projectDefaults(handle) {
|
|
4686
|
+
return handle.manifest.qa;
|
|
4687
|
+
}
|
|
4688
|
+
|
|
4689
|
+
// packages/dsh-l10n/src/tools-project.ts
|
|
4690
|
+
var mappingSchema = {
|
|
4691
|
+
type: "object",
|
|
4692
|
+
additionalProperties: false,
|
|
4693
|
+
description: "Bilingual-table layout overrides; omit to auto-detect a source/target header.",
|
|
4694
|
+
properties: {
|
|
4695
|
+
sheet: { type: "string", description: "XLSX worksheet name or 1-based index." },
|
|
4696
|
+
headerRow: { type: "integer", description: "1-based header row; 0 means no header." },
|
|
4697
|
+
sourceColumn: { type: "string", description: "1-based column, spreadsheet letter, or header name." },
|
|
4698
|
+
targetColumn: { type: "string", description: "1-based column, spreadsheet letter, or header name." },
|
|
4699
|
+
forbiddenColumn: { type: "string", description: "Glossary-only forbidden flag column." }
|
|
4700
|
+
}
|
|
4701
|
+
};
|
|
4702
|
+
function mapping(value) {
|
|
4703
|
+
if (value === void 0) return void 0;
|
|
4704
|
+
return value;
|
|
4705
|
+
}
|
|
4706
|
+
function createProjectInitTool() {
|
|
4707
|
+
return defineTool2({
|
|
4708
|
+
name: "l10n_project_init",
|
|
4709
|
+
description: "Create or update a local localization project (.langlink/manifest.json) with language pair and directories. Does not overwrite existing assets unless overwrite=true.",
|
|
4710
|
+
parameters: {
|
|
4711
|
+
root: { type: "string", required: true, description: "Project root directory (created if missing)." },
|
|
4712
|
+
sourceLang: { type: "string", required: true, description: "Source language tag, e.g. en-US." },
|
|
4713
|
+
targetLang: { type: "string", required: true, description: "Target language tag, e.g. zh-CN." },
|
|
4714
|
+
name: { type: "string", description: "Project name; defaults to localization." },
|
|
4715
|
+
locale: { type: "string", description: "Default target locale for QA language-variant rules." },
|
|
4716
|
+
threshold: { type: "integer", description: "Default TM match threshold 50..100 for translate/match tools." },
|
|
4717
|
+
maxMatches: { type: "integer", description: "Default number of TM candidates per segment." },
|
|
4718
|
+
untranslatedCopyWhitelist: {
|
|
4719
|
+
type: "array",
|
|
4720
|
+
items: { type: "string" },
|
|
4721
|
+
description: "Default source texts exempt from UNTRANSLATED_COPY."
|
|
4722
|
+
},
|
|
4723
|
+
overwrite: { type: "boolean", description: "Update language pair/QA settings of an existing manifest; assets are preserved." }
|
|
4724
|
+
},
|
|
4725
|
+
output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
|
|
4726
|
+
async execute(args) {
|
|
4727
|
+
if (args.threshold !== void 0 && (args.threshold < 50 || args.threshold > 100)) {
|
|
4728
|
+
throw new Error("threshold must be 50..100");
|
|
4729
|
+
}
|
|
4730
|
+
if (args.maxMatches !== void 0 && args.maxMatches < 1) throw new Error("maxMatches must be >= 1");
|
|
4731
|
+
const handle = await initProject({
|
|
4732
|
+
root: args.root,
|
|
4733
|
+
sourceLang: args.sourceLang,
|
|
4734
|
+
targetLang: args.targetLang,
|
|
4735
|
+
...args.name === void 0 ? {} : { name: args.name },
|
|
4736
|
+
...args.locale === void 0 ? {} : { locale: args.locale },
|
|
4737
|
+
...args.threshold === void 0 ? {} : { threshold: args.threshold },
|
|
4738
|
+
...args.maxMatches === void 0 ? {} : { maxMatches: args.maxMatches },
|
|
4739
|
+
...args.untranslatedCopyWhitelist === void 0 ? {} : { untranslatedCopyWhitelist: args.untranslatedCopyWhitelist },
|
|
4740
|
+
...args.overwrite === void 0 ? {} : { overwrite: args.overwrite }
|
|
4741
|
+
});
|
|
4742
|
+
return JSON.stringify({
|
|
4743
|
+
root: handle.root,
|
|
4744
|
+
manifestPath: handle.manifestPath,
|
|
4745
|
+
name: handle.manifest.name,
|
|
4746
|
+
sourceLang: handle.manifest.sourceLang,
|
|
4747
|
+
targetLang: handle.manifest.targetLang,
|
|
4748
|
+
assets: handle.manifest.assets.length,
|
|
4749
|
+
qa: handle.manifest.qa ?? {}
|
|
4750
|
+
});
|
|
4751
|
+
}
|
|
4752
|
+
});
|
|
4753
|
+
}
|
|
4754
|
+
function createProjectStatusTool() {
|
|
4755
|
+
return defineTool2({
|
|
4756
|
+
name: "l10n_project_status",
|
|
4757
|
+
description: "Read the local project manifest: language pair, QA defaults, tracked files and registered assets (enabled state, counts, existence, optional hash check).",
|
|
4758
|
+
parameters: {
|
|
4759
|
+
project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
|
|
4760
|
+
input: { type: "string", description: "File to discover the project from when project is omitted." },
|
|
4761
|
+
verify: { type: "boolean", description: "Re-hash asset files and report hashMatch; slower for large assets." }
|
|
4762
|
+
},
|
|
4763
|
+
output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
|
|
4764
|
+
async execute(args) {
|
|
4765
|
+
const handle = await requireProject(args.project, args.input);
|
|
4766
|
+
const status = await projectStatus(handle, args.verify === true);
|
|
4767
|
+
const warnings = [];
|
|
4768
|
+
for (const asset of status.assets) {
|
|
4769
|
+
if (!asset.exists) warnings.push(`asset ${asset.id} is missing: ${asset.absolutePath}`);
|
|
4770
|
+
else if (asset.hashMatch === false) warnings.push(`asset ${asset.id} changed since import; run l10n_asset_update with rescan=true`);
|
|
4771
|
+
}
|
|
4772
|
+
return JSON.stringify({ ...status, warnings });
|
|
4773
|
+
}
|
|
4774
|
+
});
|
|
4775
|
+
}
|
|
4776
|
+
function createAssetAddTool() {
|
|
4777
|
+
return defineTool2({
|
|
4778
|
+
name: "l10n_asset_add",
|
|
4779
|
+
description: "Register a local asset in the project manifest and parse it now. TMX = translation memory; TBX/CSV glossary = terminology; CSV/TSV/TXT/XLSX bilingual table = TM pairs (source\u2192target). Auto-detects kind from the file, or pass kind/mapping explicitly.",
|
|
4780
|
+
parameters: {
|
|
4781
|
+
path: { type: "string", required: true, description: "Asset file path (relative to the project root or absolute)." },
|
|
4782
|
+
project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered from path when omitted." },
|
|
4783
|
+
kind: { type: "string", enum: ["tm", "tb", "bilingual"], description: "Asset kind; auto-detected when omitted." },
|
|
4784
|
+
format: { type: "string", enum: ["tmx", "tbx", "csv", "tsv", "txt", "xlsx"], description: "File format; inferred from extension when omitted." },
|
|
4785
|
+
id: { type: "string", description: "Asset id; defaults to the next asset-N." },
|
|
4786
|
+
enabled: { type: "boolean", description: "Whether the asset participates in matching/QA; default true." },
|
|
4787
|
+
priority: { type: "integer", description: "Higher priority is used first; default 100." },
|
|
4788
|
+
sourceLang: { type: "string", description: "Asset source language; defaults to the project language pair." },
|
|
4789
|
+
targetLang: { type: "string", description: "Asset target language; defaults to the project language pair." },
|
|
4790
|
+
mapping: mappingSchema
|
|
4791
|
+
},
|
|
4792
|
+
output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
|
|
4793
|
+
async execute(args) {
|
|
4794
|
+
const handle = await requireProject(args.project, args.path);
|
|
4795
|
+
const result = await addAsset(handle, {
|
|
4796
|
+
path: args.path,
|
|
4797
|
+
...args.kind === void 0 ? {} : { kind: args.kind },
|
|
4798
|
+
...args.format === void 0 ? {} : { format: args.format },
|
|
4799
|
+
...args.id === void 0 ? {} : { id: args.id },
|
|
4800
|
+
...args.enabled === void 0 ? {} : { enabled: args.enabled },
|
|
4801
|
+
...args.priority === void 0 ? {} : { priority: args.priority },
|
|
4802
|
+
...args.sourceLang === void 0 ? {} : { sourceLang: args.sourceLang },
|
|
4803
|
+
...args.targetLang === void 0 ? {} : { targetLang: args.targetLang },
|
|
4804
|
+
...mapping(args.mapping) === void 0 ? {} : { mapping: mapping(args.mapping) }
|
|
4805
|
+
});
|
|
4806
|
+
return JSON.stringify({
|
|
4807
|
+
root: handle.root,
|
|
4808
|
+
asset: result.asset,
|
|
4809
|
+
warnings: result.warnings
|
|
4810
|
+
});
|
|
4811
|
+
}
|
|
4812
|
+
});
|
|
4813
|
+
}
|
|
4814
|
+
function createAssetUpdateTool() {
|
|
4815
|
+
return defineTool2({
|
|
4816
|
+
name: "l10n_asset_update",
|
|
4817
|
+
description: "Update a registered asset: enable/disable for the session, priority, language pair, import mapping, or rescan the file (hash/counts). Changing languages/mapping/rescan re-parses immediately.",
|
|
4818
|
+
parameters: {
|
|
4819
|
+
id: { type: "string", required: true, description: "Asset id from l10n_project_status." },
|
|
4820
|
+
project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
|
|
4821
|
+
input: { type: "string", description: "File to discover the project from when project is omitted." },
|
|
4822
|
+
enabled: { type: "boolean", description: "Whether the asset participates in matching/QA." },
|
|
4823
|
+
priority: { type: "integer", description: "Higher priority is used first." },
|
|
4824
|
+
sourceLang: { type: "string", description: "Asset source language." },
|
|
4825
|
+
targetLang: { type: "string", description: "Asset target language." },
|
|
4826
|
+
mapping: mappingSchema,
|
|
4827
|
+
rescan: { type: "boolean", description: "Re-read the file, recompute hash/counts and report changed warnings." }
|
|
4828
|
+
},
|
|
4829
|
+
output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
|
|
4830
|
+
async execute(args) {
|
|
4831
|
+
const handle = await requireProject(args.project, args.input);
|
|
4832
|
+
const result = await updateAsset(handle, {
|
|
4833
|
+
id: args.id,
|
|
4834
|
+
...args.enabled === void 0 ? {} : { enabled: args.enabled },
|
|
4835
|
+
...args.priority === void 0 ? {} : { priority: args.priority },
|
|
4836
|
+
...args.sourceLang === void 0 ? {} : { sourceLang: args.sourceLang },
|
|
4837
|
+
...args.targetLang === void 0 ? {} : { targetLang: args.targetLang },
|
|
4838
|
+
...mapping(args.mapping) === void 0 ? {} : { mapping: mapping(args.mapping) },
|
|
4839
|
+
...args.rescan === void 0 ? {} : { rescan: args.rescan }
|
|
4840
|
+
});
|
|
4841
|
+
return JSON.stringify({ root: handle.root, asset: result.asset, warnings: result.warnings });
|
|
4842
|
+
}
|
|
4843
|
+
});
|
|
4844
|
+
}
|
|
4845
|
+
function createAssetRemoveTool() {
|
|
4846
|
+
return defineTool2({
|
|
4847
|
+
name: "l10n_asset_remove",
|
|
4848
|
+
description: "Remove an asset entry from the project manifest. The asset file on disk is left untouched.",
|
|
4849
|
+
parameters: {
|
|
4850
|
+
id: { type: "string", required: true, description: "Asset id from l10n_project_status." },
|
|
4851
|
+
project: { type: "string", description: "Project root or .langlink/manifest.json; auto-discovered when omitted." },
|
|
4852
|
+
input: { type: "string", description: "File to discover the project from when project is omitted." }
|
|
4853
|
+
},
|
|
4854
|
+
output: { schema: { type: "string" }, render: (_args, value) => [{ type: "text", text: value }] },
|
|
4855
|
+
async execute(args) {
|
|
4856
|
+
const handle = await requireProject(args.project, args.input);
|
|
4857
|
+
const result = await removeAsset(handle, args.id);
|
|
4858
|
+
return JSON.stringify({ root: handle.root, removed: result.removed.id, remaining: result.remaining });
|
|
4859
|
+
}
|
|
4860
|
+
});
|
|
4861
|
+
}
|
|
4862
|
+
async function optionalProject(project, anchor) {
|
|
4863
|
+
return findProject(project, anchor);
|
|
4864
|
+
}
|
|
4865
|
+
|
|
3321
4866
|
// packages/dsh-l10n/src/tools-phase1.ts
|
|
4867
|
+
import { createHash as createHash3 } from "node:crypto";
|
|
4868
|
+
import { readFile as readFile2, stat as stat2, writeFile as writeFile2 } from "node:fs/promises";
|
|
4869
|
+
import { basename, resolve as resolve2 } from "node:path";
|
|
4870
|
+
import { defineTool as defineTool3 } from "@deepseek-ai/dsh-tools";
|
|
3322
4871
|
var MAX_BYTES = 200 * 1024 * 1024;
|
|
3323
4872
|
async function readBytes(path) {
|
|
3324
|
-
const absolute =
|
|
3325
|
-
const info = await
|
|
4873
|
+
const absolute = resolve2(path);
|
|
4874
|
+
const info = await stat2(absolute);
|
|
3326
4875
|
if (!info.isFile()) throw new Error("path is not a regular file");
|
|
3327
4876
|
if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
|
|
3328
|
-
return
|
|
4877
|
+
return readFile2(absolute);
|
|
3329
4878
|
}
|
|
3330
4879
|
async function readUtf8(path) {
|
|
3331
|
-
const absolute =
|
|
3332
|
-
const info = await
|
|
4880
|
+
const absolute = resolve2(path);
|
|
4881
|
+
const info = await stat2(absolute);
|
|
3333
4882
|
if (!info.isFile()) throw new Error("path is not a regular file");
|
|
3334
4883
|
if (info.size > MAX_BYTES) throw new Error(`file exceeds ${MAX_BYTES} bytes`);
|
|
3335
|
-
const value = await
|
|
4884
|
+
const value = await readFile2(absolute, { encoding: "utf8" });
|
|
3336
4885
|
if (value.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
|
|
3337
4886
|
return value;
|
|
3338
4887
|
}
|
|
4888
|
+
async function readGlossary(path) {
|
|
4889
|
+
const text = await readUtf8(path);
|
|
4890
|
+
return /\.tbx$/iu.test(path) ? parseTbx(text) : parseGlossaryCsv(text);
|
|
4891
|
+
}
|
|
4892
|
+
async function loadDocumentAssets(document, anchor, options) {
|
|
4893
|
+
const warnings = [];
|
|
4894
|
+
const tmEntries = [];
|
|
4895
|
+
const glossary = [];
|
|
4896
|
+
let used = [];
|
|
4897
|
+
const file = document.files[0];
|
|
4898
|
+
let project;
|
|
4899
|
+
if (options.project !== void 0) {
|
|
4900
|
+
project = await findProject(options.project, anchor);
|
|
4901
|
+
} else {
|
|
4902
|
+
try {
|
|
4903
|
+
project = await findProject(void 0, anchor);
|
|
4904
|
+
} catch (caught) {
|
|
4905
|
+
warnings.push(`project ignored: ${caught instanceof Error ? caught.message : String(caught)}`);
|
|
4906
|
+
}
|
|
4907
|
+
}
|
|
4908
|
+
if (project !== void 0) {
|
|
4909
|
+
const collected = await collectProjectAssets(project, {
|
|
4910
|
+
...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
|
|
4911
|
+
...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
|
|
4912
|
+
});
|
|
4913
|
+
tmEntries.push(...collected.tmEntries);
|
|
4914
|
+
glossary.push(...collected.glossary);
|
|
4915
|
+
used = collected.used;
|
|
4916
|
+
warnings.push(...collected.warnings);
|
|
4917
|
+
}
|
|
4918
|
+
if (options.tm !== void 0) tmEntries.push(...parseTmx(await readUtf8(options.tm)).entries);
|
|
4919
|
+
if (options.glossary !== void 0) glossary.push(...(await readGlossary(options.glossary)).entries);
|
|
4920
|
+
return {
|
|
4921
|
+
...project === void 0 ? {} : { project },
|
|
4922
|
+
tmEntries: mergeTmEntries(tmEntries).entries,
|
|
4923
|
+
glossary,
|
|
4924
|
+
used,
|
|
4925
|
+
warnings
|
|
4926
|
+
};
|
|
4927
|
+
}
|
|
3339
4928
|
function documentFromXml(xml, path) {
|
|
3340
4929
|
return parseXliff(xml, { filename: basename(path) });
|
|
3341
4930
|
}
|
|
3342
4931
|
function createMatchTmTool() {
|
|
3343
|
-
return
|
|
4932
|
+
return defineTool3({
|
|
3344
4933
|
name: "l10n_match_tm",
|
|
3345
|
-
description: "Match a local XLIFF/SDLXLIFF file against a TMX
|
|
4934
|
+
description: "Match a local XLIFF/SDLXLIFF file against a TMX and/or the enabled TM/bilingual assets of the enclosing project. Returns match-rate metadata per segment (no file modification).",
|
|
3346
4935
|
parameters: {
|
|
3347
4936
|
path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
|
|
3348
|
-
tm: { type: "string",
|
|
3349
|
-
|
|
4937
|
+
tm: { type: "string", description: "Optional TMX 1.4b file path; project assets are used too." },
|
|
4938
|
+
project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
|
|
4939
|
+
threshold: { type: "integer", description: "Minimum matchRate 50..100; default from project or 75." },
|
|
3350
4940
|
maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
|
|
3351
4941
|
},
|
|
3352
4942
|
output: {
|
|
@@ -3356,13 +4946,20 @@ function createMatchTmTool() {
|
|
|
3356
4946
|
async execute(args) {
|
|
3357
4947
|
const xml = await readUtf8(args.path);
|
|
3358
4948
|
const document = documentFromXml(xml, args.path);
|
|
3359
|
-
const
|
|
3360
|
-
|
|
3361
|
-
|
|
4949
|
+
const assets = await loadDocumentAssets(document, args.path, {
|
|
4950
|
+
...args.project === void 0 ? {} : { project: args.project },
|
|
4951
|
+
...args.tm === void 0 ? {} : { tm: args.tm }
|
|
4952
|
+
});
|
|
4953
|
+
if (assets.tmEntries.length === 0) {
|
|
4954
|
+
throw new Error("no TM entries: pass tm or register TM/bilingual assets with l10n_asset_add");
|
|
4955
|
+
}
|
|
4956
|
+
const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
|
|
4957
|
+
const threshold = args.threshold ?? defaults?.threshold ?? 75;
|
|
4958
|
+
if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
|
|
3362
4959
|
const limit = Math.max(1, args.maxSegments ?? 50);
|
|
3363
4960
|
const matches = document.segments.slice(0, limit).map((segment) => {
|
|
3364
4961
|
const file = document.files[segment.location.fileIndex];
|
|
3365
|
-
const candidates = matchTm(
|
|
4962
|
+
const candidates = matchTm(assets.tmEntries, segment.sourceText, {
|
|
3366
4963
|
threshold,
|
|
3367
4964
|
maxMatches: 1,
|
|
3368
4965
|
...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
|
|
@@ -3381,18 +4978,23 @@ function createMatchTmTool() {
|
|
|
3381
4978
|
segments: document.segments.length,
|
|
3382
4979
|
checked: matches.length,
|
|
3383
4980
|
threshold,
|
|
4981
|
+
tmEntries: assets.tmEntries.length,
|
|
4982
|
+
...assets.project === void 0 ? {} : { project: assets.project.root },
|
|
4983
|
+
assetsUsed: assets.used,
|
|
4984
|
+
warnings: assets.warnings,
|
|
3384
4985
|
matches
|
|
3385
4986
|
});
|
|
3386
4987
|
}
|
|
3387
4988
|
});
|
|
3388
4989
|
}
|
|
3389
4990
|
function createMatchTermsTool() {
|
|
3390
|
-
return
|
|
4991
|
+
return defineTool3({
|
|
3391
4992
|
name: "l10n_match_terms",
|
|
3392
|
-
description: "Match a local XLIFF/SDLXLIFF file against a CSV glossary. Returns term hits and forbidden-term counts (no file modification).",
|
|
4993
|
+
description: "Match a local XLIFF/SDLXLIFF file against a CSV/TBX glossary and/or the enabled terminology assets of the enclosing project. Returns term hits and forbidden-term counts (no file modification).",
|
|
3393
4994
|
parameters: {
|
|
3394
4995
|
path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
|
|
3395
|
-
glossary: { type: "string",
|
|
4996
|
+
glossary: { type: "string", description: "Optional CSV or TBX-Basic/Minis glossary path; project assets are used too." },
|
|
4997
|
+
project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
|
|
3396
4998
|
maxSegments: { type: "integer", description: "Limit reported segments; default 50." }
|
|
3397
4999
|
},
|
|
3398
5000
|
output: {
|
|
@@ -3402,15 +5004,21 @@ function createMatchTermsTool() {
|
|
|
3402
5004
|
async execute(args) {
|
|
3403
5005
|
const xml = await readUtf8(args.path);
|
|
3404
5006
|
const document = documentFromXml(xml, args.path);
|
|
3405
|
-
const
|
|
3406
|
-
|
|
5007
|
+
const assets = await loadDocumentAssets(document, args.path, {
|
|
5008
|
+
...args.project === void 0 ? {} : { project: args.project },
|
|
5009
|
+
...args.glossary === void 0 ? {} : { glossary: args.glossary }
|
|
5010
|
+
});
|
|
5011
|
+
const glossary = assets.glossary;
|
|
5012
|
+
if (glossary.length === 0) {
|
|
5013
|
+
throw new Error("no glossary entries: pass glossary or register TB/CSV assets with l10n_asset_add");
|
|
5014
|
+
}
|
|
3407
5015
|
const limit = Math.max(1, args.maxSegments ?? 50);
|
|
3408
5016
|
const hits = [];
|
|
3409
5017
|
let forbidden = 0;
|
|
3410
5018
|
for (const segment of document.segments.slice(0, limit)) {
|
|
3411
5019
|
const file = document.files[segment.location.fileIndex];
|
|
3412
5020
|
for (const side of ["source", "target"]) {
|
|
3413
|
-
const result = matchTerms(segment[`${side}Text`], glossary
|
|
5021
|
+
const result = matchTerms(segment[`${side}Text`], glossary, {
|
|
3414
5022
|
...file?.sourceLanguage ? { sourceLanguage: file.sourceLanguage } : {},
|
|
3415
5023
|
...file?.targetLanguage ? { targetLanguage: file.targetLanguage } : {}
|
|
3416
5024
|
});
|
|
@@ -3434,7 +5042,10 @@ function createMatchTermsTool() {
|
|
|
3434
5042
|
format: document.format,
|
|
3435
5043
|
segments: document.segments.length,
|
|
3436
5044
|
checked: Math.min(limit, document.segments.length),
|
|
3437
|
-
entries: glossary.
|
|
5045
|
+
entries: glossary.length,
|
|
5046
|
+
...assets.project === void 0 ? {} : { project: assets.project.root },
|
|
5047
|
+
assetsUsed: assets.used,
|
|
5048
|
+
warnings: assets.warnings,
|
|
3438
5049
|
hits: hits.slice(0, 200),
|
|
3439
5050
|
hitCount: hits.length,
|
|
3440
5051
|
forbiddenCount: forbidden
|
|
@@ -3443,13 +5054,14 @@ function createMatchTermsTool() {
|
|
|
3443
5054
|
});
|
|
3444
5055
|
}
|
|
3445
5056
|
function createCheckTool() {
|
|
3446
|
-
return
|
|
5057
|
+
return defineTool3({
|
|
3447
5058
|
name: "l10n_check",
|
|
3448
|
-
description: "Run deterministic QA on a local XLIFF/SDLXLIFF file, optionally with a TMX and
|
|
5059
|
+
description: "Run deterministic QA on a local XLIFF/SDLXLIFF file, optionally with a TMX/glossary and the enabled assets of the enclosing project. Returns rule/severity counts and a bounded issue list; never modifies files.",
|
|
3449
5060
|
parameters: {
|
|
3450
5061
|
path: { type: "string", required: true, description: "XLIFF/SDLXLIFF file path." },
|
|
3451
|
-
tm: { type: "string", description: "Optional TMX 1.4b file path." },
|
|
3452
|
-
glossary: { type: "string", description: "Optional CSV glossary path." },
|
|
5062
|
+
tm: { type: "string", description: "Optional TMX 1.4b file path; project TM assets are used too." },
|
|
5063
|
+
glossary: { type: "string", description: "Optional CSV or TBX-Basic/Minis glossary path; project term assets are used too." },
|
|
5064
|
+
project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
|
|
3453
5065
|
maxIssues: { type: "integer", description: "Max issues returned; default 100." },
|
|
3454
5066
|
sourceWhitelist: {
|
|
3455
5067
|
type: "array",
|
|
@@ -3462,7 +5074,7 @@ function createCheckTool() {
|
|
|
3462
5074
|
},
|
|
3463
5075
|
targetLocale: {
|
|
3464
5076
|
type: "string",
|
|
3465
|
-
description: "Target locale hint (e.g. zh-CN) used by language-variant rules."
|
|
5077
|
+
description: "Target locale hint (e.g. zh-CN) used by language-variant rules; defaults to project qa.locale."
|
|
3466
5078
|
}
|
|
3467
5079
|
},
|
|
3468
5080
|
output: {
|
|
@@ -3472,18 +5084,26 @@ function createCheckTool() {
|
|
|
3472
5084
|
async execute(args) {
|
|
3473
5085
|
const xml = await readUtf8(args.path);
|
|
3474
5086
|
const document = documentFromXml(xml, args.path);
|
|
3475
|
-
const
|
|
3476
|
-
|
|
5087
|
+
const assets = await loadDocumentAssets(document, args.path, {
|
|
5088
|
+
...args.project === void 0 ? {} : { project: args.project },
|
|
5089
|
+
...args.tm === void 0 ? {} : { tm: args.tm },
|
|
5090
|
+
...args.glossary === void 0 ? {} : { glossary: args.glossary }
|
|
5091
|
+
});
|
|
5092
|
+
const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
|
|
5093
|
+
const whitelist = [
|
|
5094
|
+
...args.sourceWhitelist ?? [],
|
|
5095
|
+
...defaults?.untranslatedCopyWhitelist ?? []
|
|
5096
|
+
];
|
|
3477
5097
|
const checked = checkDocument(document, {
|
|
3478
|
-
tmEntries,
|
|
3479
|
-
glossary,
|
|
3480
|
-
...
|
|
5098
|
+
tmEntries: assets.tmEntries,
|
|
5099
|
+
glossary: assets.glossary,
|
|
5100
|
+
...whitelist.length === 0 && args.shortSegmentMaxLength === void 0 ? {} : {
|
|
3481
5101
|
untranslatedCopy: {
|
|
3482
|
-
...
|
|
5102
|
+
...whitelist.length === 0 ? {} : { whitelist: Array.from(new Set(whitelist)) },
|
|
3483
5103
|
...args.shortSegmentMaxLength === void 0 ? {} : { shortSegmentMaxLength: args.shortSegmentMaxLength }
|
|
3484
5104
|
}
|
|
3485
5105
|
},
|
|
3486
|
-
...args.targetLocale === void 0 ? {} : { locale: { targetLocale: args.targetLocale } }
|
|
5106
|
+
...(args.targetLocale ?? defaults?.locale) === void 0 ? {} : { locale: { targetLocale: args.targetLocale ?? defaults?.locale } }
|
|
3487
5107
|
});
|
|
3488
5108
|
const bySeverity = {};
|
|
3489
5109
|
const byRule = {};
|
|
@@ -3498,6 +5118,9 @@ function createCheckTool() {
|
|
|
3498
5118
|
checkedSegments: checked.qa.checkedSegments,
|
|
3499
5119
|
coverage: checked.qa.coverage,
|
|
3500
5120
|
totalIssues: checked.qa.issues.length,
|
|
5121
|
+
...assets.project === void 0 ? {} : { project: assets.project.root },
|
|
5122
|
+
assetsUsed: assets.used,
|
|
5123
|
+
warnings: assets.warnings,
|
|
3501
5124
|
bySeverity,
|
|
3502
5125
|
byRule,
|
|
3503
5126
|
issues: checked.qa.issues.slice(0, limit).map((issue) => ({
|
|
@@ -3515,14 +5138,15 @@ function createCheckTool() {
|
|
|
3515
5138
|
});
|
|
3516
5139
|
}
|
|
3517
5140
|
function createTranslateTool() {
|
|
3518
|
-
return
|
|
5141
|
+
return defineTool3({
|
|
3519
5142
|
name: "l10n_translate",
|
|
3520
|
-
description: "Fill empty targets in a local XLIFF/SDLXLIFF file from a TMX, writing a NEW file. Existing targets and tagged segments are never overwritten.",
|
|
5143
|
+
description: "Fill empty targets in a local XLIFF/SDLXLIFF file from a TMX and/or the enabled TM/bilingual project assets, writing a NEW file. Existing targets and tagged segments are never overwritten.",
|
|
3521
5144
|
parameters: {
|
|
3522
5145
|
path: { type: "string", required: true, description: "Input XLIFF/SDLXLIFF path." },
|
|
3523
|
-
tm: { type: "string",
|
|
5146
|
+
tm: { type: "string", description: "Optional TMX 1.4b file path; project TM assets are used too." },
|
|
5147
|
+
project: { type: "string", description: "Optional project root or .langlink/manifest.json; auto-discovered from path." },
|
|
3524
5148
|
output: { type: "string", required: true, description: "New output file path; must differ from input." },
|
|
3525
|
-
threshold: { type: "integer", description: "Minimum matchRate 50..100; default 75." },
|
|
5149
|
+
threshold: { type: "integer", description: "Minimum matchRate 50..100; default from project or 75." },
|
|
3526
5150
|
setSdlConf: { type: "string", description: "SDLXLIFF only: set sdl:seg@conf on filled segments." }
|
|
3527
5151
|
},
|
|
3528
5152
|
output: {
|
|
@@ -3534,35 +5158,60 @@ function createTranslateTool() {
|
|
|
3534
5158
|
applied: { type: "integer" },
|
|
3535
5159
|
skipped: { type: "integer" },
|
|
3536
5160
|
warnings: { type: "array", items: { type: "string" } },
|
|
3537
|
-
sha256: { type: "string" }
|
|
5161
|
+
sha256: { type: "string" },
|
|
5162
|
+
project: { type: "string" },
|
|
5163
|
+
assetsUsed: {
|
|
5164
|
+
type: "array",
|
|
5165
|
+
items: {
|
|
5166
|
+
type: "object",
|
|
5167
|
+
additionalProperties: false,
|
|
5168
|
+
properties: {
|
|
5169
|
+
id: { type: "string" },
|
|
5170
|
+
kind: { type: "string" },
|
|
5171
|
+
path: { type: "string" },
|
|
5172
|
+
entries: { type: "integer" }
|
|
5173
|
+
}
|
|
5174
|
+
}
|
|
5175
|
+
}
|
|
3538
5176
|
}
|
|
3539
5177
|
},
|
|
3540
5178
|
render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
|
|
3541
5179
|
},
|
|
3542
5180
|
async execute(args) {
|
|
3543
|
-
const input =
|
|
3544
|
-
const output =
|
|
5181
|
+
const input = resolve2(args.path);
|
|
5182
|
+
const output = resolve2(args.output);
|
|
3545
5183
|
if (input === output) throw new Error("refusing to overwrite the input file");
|
|
3546
5184
|
const xml = await readUtf8(input);
|
|
3547
5185
|
const document = documentFromXml(xml, input);
|
|
3548
|
-
const
|
|
3549
|
-
|
|
3550
|
-
|
|
5186
|
+
const assets = await loadDocumentAssets(document, input, {
|
|
5187
|
+
...args.project === void 0 ? {} : { project: args.project },
|
|
5188
|
+
...args.tm === void 0 ? {} : { tm: args.tm }
|
|
5189
|
+
});
|
|
5190
|
+
if (assets.tmEntries.length === 0) {
|
|
5191
|
+
throw new Error("no TM entries: pass tm or register TM/bilingual assets with l10n_asset_add");
|
|
5192
|
+
}
|
|
5193
|
+
const defaults = assets.project === void 0 ? void 0 : projectDefaults(assets.project);
|
|
5194
|
+
const threshold = args.threshold ?? defaults?.threshold ?? 75;
|
|
5195
|
+
if (threshold < 50 || threshold > 100) throw new Error("threshold must be 50..100");
|
|
5196
|
+
const result = translateDocument(document, assets.tmEntries, {
|
|
5197
|
+
threshold,
|
|
3551
5198
|
...args.setSdlConf === void 0 ? {} : { setSdlState: true, sdlConf: args.setSdlConf }
|
|
3552
5199
|
});
|
|
3553
|
-
await
|
|
5200
|
+
await writeFile2(output, result.xml, "utf8");
|
|
3554
5201
|
return {
|
|
3555
5202
|
output,
|
|
3556
5203
|
applied: result.applied,
|
|
3557
5204
|
skipped: result.skipped,
|
|
3558
|
-
warnings: result.warnings,
|
|
3559
|
-
sha256:
|
|
5205
|
+
warnings: [...assets.warnings, ...result.warnings],
|
|
5206
|
+
sha256: createHash3("sha256").update(result.xml, "utf8").digest("hex"),
|
|
5207
|
+
...assets.project === void 0 ? {} : { project: assets.project.root },
|
|
5208
|
+
assetsUsed: assets.used
|
|
3560
5209
|
};
|
|
3561
5210
|
}
|
|
3562
5211
|
});
|
|
3563
5212
|
}
|
|
3564
5213
|
function createConvertTool() {
|
|
3565
|
-
return
|
|
5214
|
+
return defineTool3({
|
|
3566
5215
|
name: "l10n_convert",
|
|
3567
5216
|
description: "Convert a bilingual CSV/TXT/XLSX table between formats. Writes a new file; never modifies the input.",
|
|
3568
5217
|
parameters: {
|
|
@@ -3583,23 +5232,23 @@ function createConvertTool() {
|
|
|
3583
5232
|
render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
|
|
3584
5233
|
},
|
|
3585
5234
|
async execute(args) {
|
|
3586
|
-
const input =
|
|
3587
|
-
const output =
|
|
5235
|
+
const input = resolve2(args.input);
|
|
5236
|
+
const output = resolve2(args.output);
|
|
3588
5237
|
if (input === output) throw new Error("refusing to overwrite the input file");
|
|
3589
5238
|
const table = await parseBilingualFileAsync(input, await readBytes(input));
|
|
3590
5239
|
if (args.to === "xlsx") {
|
|
3591
|
-
await
|
|
5240
|
+
await writeFile2(output, await writeBilingualXlsx(table.rows));
|
|
3592
5241
|
} else if (args.to === "csv") {
|
|
3593
|
-
await
|
|
5242
|
+
await writeFile2(output, writeBilingualCsv(table.rows, { delimiter: "," }), "utf8");
|
|
3594
5243
|
} else {
|
|
3595
|
-
await
|
|
5244
|
+
await writeFile2(output, writeBilingualCsv(table.rows, { delimiter: " ", header: false }), "utf8");
|
|
3596
5245
|
}
|
|
3597
5246
|
return { output, rows: table.rows.length, warnings: table.warnings };
|
|
3598
5247
|
}
|
|
3599
5248
|
});
|
|
3600
5249
|
}
|
|
3601
5250
|
function createValidateTool() {
|
|
3602
|
-
return
|
|
5251
|
+
return defineTool3({
|
|
3603
5252
|
name: "l10n_validate",
|
|
3604
5253
|
description: "Validate one local XLIFF 1.2/SDLXLIFF file: XML well-formedness, segment counts, missing targets, inline-tag mismatches. Read-only.",
|
|
3605
5254
|
parameters: {
|
|
@@ -3631,22 +5280,22 @@ function createValidateTool() {
|
|
|
3631
5280
|
}
|
|
3632
5281
|
|
|
3633
5282
|
// packages/dsh-l10n/src/tools-phase2.ts
|
|
3634
|
-
import { readFile as
|
|
3635
|
-
import { basename as basename2, resolve as
|
|
3636
|
-
import { defineTool as
|
|
5283
|
+
import { readFile as readFile3, stat as stat3, writeFile as writeFile3 } from "node:fs/promises";
|
|
5284
|
+
import { basename as basename2, resolve as resolve3 } from "node:path";
|
|
5285
|
+
import { defineTool as defineTool4 } from "@deepseek-ai/dsh-tools";
|
|
3637
5286
|
var MAX_BYTES2 = 200 * 1024 * 1024;
|
|
3638
5287
|
async function readBytes2(path) {
|
|
3639
|
-
const absolute =
|
|
3640
|
-
const info = await
|
|
5288
|
+
const absolute = resolve3(path);
|
|
5289
|
+
const info = await stat3(absolute);
|
|
3641
5290
|
if (!info.isFile()) throw new Error("path is not a regular file");
|
|
3642
5291
|
if (info.size > MAX_BYTES2) throw new Error(`file exceeds ${MAX_BYTES2} bytes`);
|
|
3643
|
-
return
|
|
5292
|
+
return readFile3(absolute);
|
|
3644
5293
|
}
|
|
3645
5294
|
async function readText(path) {
|
|
3646
5295
|
return (await readBytes2(path)).toString("utf8");
|
|
3647
5296
|
}
|
|
3648
5297
|
function createExtractDocTool() {
|
|
3649
|
-
return
|
|
5298
|
+
return defineTool4({
|
|
3650
5299
|
name: "l10n_extract_doc",
|
|
3651
5300
|
description: "Extract local DOCX paragraph text, XLSX text cells or PPTX slide text into a bilingual table plus a map.json. Writes new files; the original is never modified.",
|
|
3652
5301
|
parameters: {
|
|
@@ -3670,18 +5319,18 @@ function createExtractDocTool() {
|
|
|
3670
5319
|
render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
|
|
3671
5320
|
},
|
|
3672
5321
|
async execute(args) {
|
|
3673
|
-
const input =
|
|
3674
|
-
const rowsOutput =
|
|
3675
|
-
const mapOutput =
|
|
5322
|
+
const input = resolve3(args.path);
|
|
5323
|
+
const rowsOutput = resolve3(args.rowsOutput);
|
|
5324
|
+
const mapOutput = resolve3(args.mapOutput);
|
|
3676
5325
|
if (input === rowsOutput || input === mapOutput) throw new Error("refusing to overwrite the input file");
|
|
3677
5326
|
const result = extractOoxml(await readBytes2(input), basename2(input));
|
|
3678
5327
|
const format = args.format ?? (rowsOutput.toLowerCase().endsWith(".xlsx") ? "xlsx" : "csv");
|
|
3679
5328
|
if (format === "xlsx") {
|
|
3680
|
-
await
|
|
5329
|
+
await writeFile3(rowsOutput, await writeBilingualXlsx(result.rows));
|
|
3681
5330
|
} else {
|
|
3682
|
-
await
|
|
5331
|
+
await writeFile3(rowsOutput, writeBilingualCsv(result.rows, { delimiter: "," }), "utf8");
|
|
3683
5332
|
}
|
|
3684
|
-
await
|
|
5333
|
+
await writeFile3(mapOutput, JSON.stringify(result.map, null, 2), "utf8");
|
|
3685
5334
|
return {
|
|
3686
5335
|
format: result.format,
|
|
3687
5336
|
rows: result.rows.length,
|
|
@@ -3693,7 +5342,7 @@ function createExtractDocTool() {
|
|
|
3693
5342
|
});
|
|
3694
5343
|
}
|
|
3695
5344
|
function createBackfillTool() {
|
|
3696
|
-
return
|
|
5345
|
+
return defineTool4({
|
|
3697
5346
|
name: "l10n_backfill",
|
|
3698
5347
|
description: "Backfill translated bilingual rows into a NEW DOCX/XLSX/PPTX file using a previously generated map.json. The original is never modified.",
|
|
3699
5348
|
parameters: {
|
|
@@ -3716,8 +5365,8 @@ function createBackfillTool() {
|
|
|
3716
5365
|
render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
|
|
3717
5366
|
},
|
|
3718
5367
|
async execute(args) {
|
|
3719
|
-
const original =
|
|
3720
|
-
const output =
|
|
5368
|
+
const original = resolve3(args.original);
|
|
5369
|
+
const output = resolve3(args.output);
|
|
3721
5370
|
if (original === output) throw new Error("refusing to overwrite the input file");
|
|
3722
5371
|
const map = JSON.parse(await readText(args.map));
|
|
3723
5372
|
if (map.format !== "docx" && map.format !== "xlsx" && map.format !== "pptx") throw new Error("invalid map.json");
|
|
@@ -3726,7 +5375,7 @@ function createBackfillTool() {
|
|
|
3726
5375
|
table.rows.filter((row) => row.target.trim() !== "").map((row) => [row.id, row.target])
|
|
3727
5376
|
);
|
|
3728
5377
|
const result = backfillOoxml(await readBytes2(original), map, translations);
|
|
3729
|
-
await
|
|
5378
|
+
await writeFile3(output, result.buffer);
|
|
3730
5379
|
return {
|
|
3731
5380
|
output,
|
|
3732
5381
|
applied: result.applied,
|
|
@@ -3737,7 +5386,7 @@ function createBackfillTool() {
|
|
|
3737
5386
|
});
|
|
3738
5387
|
}
|
|
3739
5388
|
function createPolishTool() {
|
|
3740
|
-
return
|
|
5389
|
+
return defineTool4({
|
|
3741
5390
|
name: "l10n_polish",
|
|
3742
5391
|
description: "Apply reviewed target edits (plain text for untagged segments; inline XML fragments for tagged segments) and write a NEW XLIFF/SDLXLIFF file. Fails closed on tag/structure mismatch.",
|
|
3743
5392
|
parameters: {
|
|
@@ -3775,8 +5424,8 @@ function createPolishTool() {
|
|
|
3775
5424
|
render: (_args, value) => [{ type: "text", text: JSON.stringify(value) }]
|
|
3776
5425
|
},
|
|
3777
5426
|
async execute(args) {
|
|
3778
|
-
const input =
|
|
3779
|
-
const output =
|
|
5427
|
+
const input = resolve3(args.path);
|
|
5428
|
+
const output = resolve3(args.output);
|
|
3780
5429
|
if (input === output) throw new Error("refusing to overwrite the input file");
|
|
3781
5430
|
const parsed = JSON.parse(args.edits);
|
|
3782
5431
|
const edits = /* @__PURE__ */ new Map();
|
|
@@ -3793,7 +5442,7 @@ function createPolishTool() {
|
|
|
3793
5442
|
if (result.applied === 0 && result.warnings.length > 0) {
|
|
3794
5443
|
throw new Error(result.warnings.join("; "));
|
|
3795
5444
|
}
|
|
3796
|
-
await
|
|
5445
|
+
await writeFile3(output, result.xml, "utf8");
|
|
3797
5446
|
return {
|
|
3798
5447
|
output,
|
|
3799
5448
|
applied: result.applied,
|
|
@@ -3806,7 +5455,7 @@ function createPolishTool() {
|
|
|
3806
5455
|
}
|
|
3807
5456
|
|
|
3808
5457
|
// packages/dsh-l10n/src/tools-assets.ts
|
|
3809
|
-
import { defineTool as
|
|
5458
|
+
import { defineTool as defineTool5 } from "@deepseek-ai/dsh-tools";
|
|
3810
5459
|
function isPrivateOrReservedHost(hostname) {
|
|
3811
5460
|
const host = hostname.toLowerCase().replace(/^\[/u, "").replace(/\]$/u, "");
|
|
3812
5461
|
if (host === "localhost" || host.endsWith(".localhost") || host === "::1" || host === "::") return true;
|
|
@@ -3859,7 +5508,7 @@ function resolveAssetEndpoint(name2) {
|
|
|
3859
5508
|
return new URL(raw);
|
|
3860
5509
|
}
|
|
3861
5510
|
function createAssetInfoTool(baseUrl) {
|
|
3862
|
-
return
|
|
5511
|
+
return defineTool5({
|
|
3863
5512
|
name: "l10n_assets",
|
|
3864
5513
|
description: "Read-only company localization asset service: health, glossary entries, or TM entries. Targets the prod endpoint by default.",
|
|
3865
5514
|
parameters: {
|
|
@@ -3873,7 +5522,10 @@ function createAssetInfoTool(baseUrl) {
|
|
|
3873
5522
|
async execute(args) {
|
|
3874
5523
|
if (!baseUrl) throw new Error("asset endpoint is not configured");
|
|
3875
5524
|
const path = args.view === "health" ? "health" : args.view === "glossary" ? "glossary" : "tm";
|
|
3876
|
-
const response = await fetch(new URL(path, baseUrl), {
|
|
5525
|
+
const response = await fetch(new URL(path, baseUrl), {
|
|
5526
|
+
signal: AbortSignal.timeout(15e3),
|
|
5527
|
+
redirect: "error"
|
|
5528
|
+
});
|
|
3877
5529
|
if (!response.ok) throw new Error(`asset service returned ${response.status}`);
|
|
3878
5530
|
const value = await response.json();
|
|
3879
5531
|
if (Array.isArray(value.entries) && args.view !== "health") {
|
|
@@ -3886,9 +5538,9 @@ function createAssetInfoTool(baseUrl) {
|
|
|
3886
5538
|
}
|
|
3887
5539
|
|
|
3888
5540
|
// packages/dsh-l10n/src/tools-semantic.ts
|
|
3889
|
-
import { readFile as
|
|
3890
|
-
import { basename as basename3, resolve as
|
|
3891
|
-
import { defineTool as
|
|
5541
|
+
import { readFile as readFile4 } from "node:fs/promises";
|
|
5542
|
+
import { basename as basename3, resolve as resolve4 } from "node:path";
|
|
5543
|
+
import { defineTool as defineTool6 } from "@deepseek-ai/dsh-tools";
|
|
3892
5544
|
function createDshSemanticProvider(llm, provider, model) {
|
|
3893
5545
|
return {
|
|
3894
5546
|
async complete({ prompt }) {
|
|
@@ -3908,7 +5560,7 @@ function createDshSemanticProvider(llm, provider, model) {
|
|
|
3908
5560
|
};
|
|
3909
5561
|
}
|
|
3910
5562
|
function createSemanticCheckTool(llm) {
|
|
3911
|
-
return
|
|
5563
|
+
return defineTool6({
|
|
3912
5564
|
name: "l10n_semantic_check",
|
|
3913
5565
|
description: "Run semantic localization QA through the dsh LLM service. Requires a configured provider/model; returns validated issues only (evidence must be uniquely locatable).",
|
|
3914
5566
|
parameters: {
|
|
@@ -3923,7 +5575,7 @@ function createSemanticCheckTool(llm) {
|
|
|
3923
5575
|
},
|
|
3924
5576
|
async execute(args) {
|
|
3925
5577
|
if (!llm) throw new Error("dsh LLM service is unavailable; configure a model provider first");
|
|
3926
|
-
const xml = await
|
|
5578
|
+
const xml = await readFile4(resolve4(args.path), "utf8");
|
|
3927
5579
|
const document = parseXliff(xml, { filename: basename3(args.path) });
|
|
3928
5580
|
const result = await runSemanticCheck(
|
|
3929
5581
|
document,
|
|
@@ -3943,17 +5595,17 @@ function createSemanticCheckTool(llm) {
|
|
|
3943
5595
|
}
|
|
3944
5596
|
|
|
3945
5597
|
// packages/dsh-l10n/src/tool.ts
|
|
3946
|
-
import { readFile as
|
|
3947
|
-
import { basename as basename4, resolve as
|
|
3948
|
-
import { defineTool as
|
|
5598
|
+
import { readFile as readFile5, stat as stat4 } from "node:fs/promises";
|
|
5599
|
+
import { basename as basename4, resolve as resolve5 } from "node:path";
|
|
5600
|
+
import { defineTool as defineTool7 } from "@deepseek-ai/dsh-tools";
|
|
3949
5601
|
var MAX_BYTES3 = 200 * 1024 * 1024;
|
|
3950
5602
|
async function inspectFile(path, signal) {
|
|
3951
5603
|
signal?.throwIfAborted();
|
|
3952
|
-
const absolute =
|
|
3953
|
-
const info = await
|
|
5604
|
+
const absolute = resolve5(path);
|
|
5605
|
+
const info = await stat4(absolute);
|
|
3954
5606
|
if (!info.isFile()) throw new Error("path is not a regular file");
|
|
3955
5607
|
if (info.size > MAX_BYTES3) throw new Error(`file exceeds ${MAX_BYTES3} bytes`);
|
|
3956
|
-
const xml = await
|
|
5608
|
+
const xml = await readFile5(absolute, { encoding: "utf8", signal });
|
|
3957
5609
|
if (xml.includes("\uFFFD")) throw new Error("file is not valid UTF-8");
|
|
3958
5610
|
signal?.throwIfAborted();
|
|
3959
5611
|
const document = parseXliff(xml, { filename: basename4(absolute) });
|
|
@@ -3985,7 +5637,7 @@ async function inspectFile(path, signal) {
|
|
|
3985
5637
|
};
|
|
3986
5638
|
}
|
|
3987
5639
|
function createInspectTool() {
|
|
3988
|
-
return
|
|
5640
|
+
return defineTool7({
|
|
3989
5641
|
name: "l10n_inspect",
|
|
3990
5642
|
description: "Inspect a local XLIFF 1.2 or SDLXLIFF file. Returns format, languages, segment count, inline-tag statistics, warnings and a structure hash. Never returns source or target text. Read-only; does not modify the file.",
|
|
3991
5643
|
parameters: {
|
|
@@ -4026,9 +5678,19 @@ function createInspectTool() {
|
|
|
4026
5678
|
var name = "langlink-l10n";
|
|
4027
5679
|
var inject = ["tools"];
|
|
4028
5680
|
function apply(ctx) {
|
|
5681
|
+
const baseUrl = process.env.WORKBENCH_PLATFORM_URL, token = process.env.WORKBENCH_PLATFORM_TOKEN;
|
|
5682
|
+
if (baseUrl || token) {
|
|
5683
|
+
if (!baseUrl || !token) throw new Error("platform mode requires both URL and Token");
|
|
5684
|
+
ctx.tools.register(createPlatformJobTool({ baseUrl, token }));
|
|
5685
|
+
}
|
|
4029
5686
|
const llm = ctx.get?.("llm");
|
|
4030
5687
|
const assetRoot = () => resolveAssetEndpoint("prod");
|
|
4031
5688
|
for (const tool of [
|
|
5689
|
+
createProjectInitTool(),
|
|
5690
|
+
createProjectStatusTool(),
|
|
5691
|
+
createAssetAddTool(),
|
|
5692
|
+
createAssetUpdateTool(),
|
|
5693
|
+
createAssetRemoveTool(),
|
|
4032
5694
|
createInspectTool(),
|
|
4033
5695
|
createMatchTmTool(),
|
|
4034
5696
|
createMatchTermsTool(),
|
|
@@ -4046,9 +5708,15 @@ function apply(ctx) {
|
|
|
4046
5708
|
}
|
|
4047
5709
|
}
|
|
4048
5710
|
export {
|
|
5711
|
+
addAsset,
|
|
4049
5712
|
apply,
|
|
4050
5713
|
assertSafeAssetBaseUrl,
|
|
5714
|
+
assetAbsolutePath,
|
|
5715
|
+
collectProjectAssets,
|
|
5716
|
+
createAssetAddTool,
|
|
4051
5717
|
createAssetInfoTool,
|
|
5718
|
+
createAssetRemoveTool,
|
|
5719
|
+
createAssetUpdateTool,
|
|
4052
5720
|
createBackfillTool,
|
|
4053
5721
|
createCheckTool,
|
|
4054
5722
|
createConvertTool,
|
|
@@ -4057,13 +5725,27 @@ export {
|
|
|
4057
5725
|
createInspectTool,
|
|
4058
5726
|
createMatchTermsTool,
|
|
4059
5727
|
createMatchTmTool,
|
|
5728
|
+
createPlatformJobTool,
|
|
4060
5729
|
createPolishTool,
|
|
5730
|
+
createProjectInitTool,
|
|
5731
|
+
createProjectStatusTool,
|
|
4061
5732
|
createSemanticCheckTool,
|
|
4062
5733
|
createTranslateTool,
|
|
4063
5734
|
createValidateTool,
|
|
5735
|
+
findProject,
|
|
5736
|
+
initProject,
|
|
4064
5737
|
inject,
|
|
4065
5738
|
inspectFile,
|
|
4066
5739
|
name,
|
|
4067
|
-
|
|
5740
|
+
optionalProject,
|
|
5741
|
+
parseAssetFile,
|
|
5742
|
+
projectDefaults,
|
|
5743
|
+
projectStatus,
|
|
5744
|
+
readManifest,
|
|
5745
|
+
removeAsset,
|
|
5746
|
+
requireProject,
|
|
5747
|
+
resolveAssetEndpoint,
|
|
5748
|
+
updateAsset,
|
|
5749
|
+
writeManifest
|
|
4068
5750
|
};
|
|
4069
5751
|
//# sourceMappingURL=index.js.map
|