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