Repository navigation
Expand file tree
/
Copy path03-json-roundtrip.ts
More file actions
99 lines (87 loc) · 3.23 KB
/
Copy path03-json-roundtrip.ts
File metadata and controls
99 lines (87 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*******************************************************************************
* Copyright (c) 2026 Maxprograms.
*
* This program and the accompanying materials are made available under the
* terms of the TypesXLIFF License Agreement, which accompanies this
* distribution and is available at: LICENSE.md
*
* Contributors:
* Maxprograms - initial API and implementation
*******************************************************************************/
/**
* Example: JSON round-trip and target/@order verification
*
* Run with:
* npx ts-node docs/examples/03-json-roundtrip.ts
*/
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import {
JsonToXliff,
XliffDocument,
XliffFile,
XliffSegment,
XliffSource,
XliffTarget,
XliffToJson,
XliffUnit
} from "typesxliff";
const __filename: string = fileURLToPath(import.meta.url);
const __dirname: string = dirname(__filename);
const outputFile: string = join(__dirname, "roundtrip-output.xlf");
function collectTargetOrders(doc: XliffDocument): Array<string> {
const values: Array<string> = [];
for (const file of doc.getFiles()) {
for (const entry of file.getEntries()) {
if (!(entry instanceof XliffUnit)) {
continue;
}
for (const item of entry.getItems()) {
if (!(item instanceof XliffSegment)) {
continue;
}
const order: number | string | undefined = item.getTarget()?.getOrder();
if (order !== undefined) {
values.push(String(order));
}
}
}
}
return values;
}
const originalDoc: XliffDocument = new XliffDocument("2.1", "en", "es");
const file: XliffFile = new XliffFile("f1");
const unit: XliffUnit = new XliffUnit("u1");
const segment1: XliffSegment = new XliffSegment("s1");
const source1: XliffSource = new XliffSource();
source1.addText("Hello");
const target1: XliffTarget = new XliffTarget();
target1.addText("Hola");
target1.setOrder("1");
segment1.setSource(source1);
segment1.setTarget(target1);
unit.addSegment(segment1);
const segment2: XliffSegment = new XliffSegment("s2");
const source2: XliffSource = new XliffSource();
source2.addText("World");
const target2: XliffTarget = new XliffTarget();
target2.addText("Mundo");
target2.setOrder("2");
segment2.setSource(source2);
segment2.setTarget(target2);
unit.addSegment(segment2);
file.addUnit(unit);
originalDoc.addFile(file);
const originalOrders: Array<string> = collectTargetOrders(originalDoc);
const jsonObject: ReturnType<typeof XliffToJson.toJsonObject> = XliffToJson.toJsonObject(originalDoc);
const roundTripDoc: XliffDocument = JsonToXliff.fromJsonObject(jsonObject);
const roundTripOrders: Array<string> = collectTargetOrders(roundTripDoc);
if (JSON.stringify(originalOrders) !== JSON.stringify(roundTripOrders)) {
console.error("Round-trip changed target/@order values.");
console.error("Original : " + originalOrders.join(", "));
console.error("Roundtrip: " + roundTripOrders.join(", "));
process.exit(1);
}
roundTripDoc.writeDocument(outputFile, true);
console.log("Round-trip preserved target/@order values.");
console.log("Written to: " + outputFile);