hybridtm 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -1
- package/dist/cli/cliUtils.d.ts +19 -0
- package/dist/cli/cliUtils.js +67 -0
- package/dist/cli/cliUtils.js.map +1 -0
- package/dist/cli/createCommand.d.ts +13 -0
- package/dist/cli/createCommand.js +44 -0
- package/dist/cli/createCommand.js.map +1 -0
- package/dist/cli/hybridtmCli.d.ts +13 -0
- package/dist/cli/hybridtmCli.js +73 -0
- package/dist/cli/hybridtmCli.js.map +1 -0
- package/dist/cli/importCommand.d.ts +13 -0
- package/dist/cli/importCommand.js +105 -0
- package/dist/cli/importCommand.js.map +1 -0
- package/dist/cli/matchCommand.d.ts +13 -0
- package/dist/cli/matchCommand.js +195 -0
- package/dist/cli/matchCommand.js.map +1 -0
- package/dist/cli/registryCommands.d.ts +15 -0
- package/dist/cli/registryCommands.js +59 -0
- package/dist/cli/registryCommands.js.map +1 -0
- package/dist/hybridtm.d.ts +3 -3
- package/dist/hybridtm.js +4 -1
- package/dist/hybridtm.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/xliffHandler.d.ts +14 -36
- package/dist/xliffHandler.js +152 -296
- package/dist/xliffHandler.js.map +1 -1
- package/dist/xliffReader.d.ts +1 -3
- package/dist/xliffReader.js +12 -6
- package/dist/xliffReader.js.map +1 -1
- package/package.json +26 -5
package/dist/xliffHandler.js
CHANGED
|
@@ -10,17 +10,11 @@
|
|
|
10
10
|
* Maxprograms - initial API and implementation
|
|
11
11
|
*******************************************************************************/
|
|
12
12
|
import { createWriteStream } from "node:fs";
|
|
13
|
-
import {
|
|
13
|
+
import { XliffGroup, XliffMeta, XliffMrk, XliffPc, XliffSegment, XliffSource, XliffTarget } from "typesxliff";
|
|
14
14
|
import { DEFAULT_IMPORT_OPTIONS, resolveImportOptions } from './importOptions.js';
|
|
15
|
-
import { Utils } from './utils.js';
|
|
16
15
|
export class XLIFFHandler {
|
|
17
|
-
inCdData = false;
|
|
18
|
-
currentCData = new CData('');
|
|
19
|
-
stack = [];
|
|
20
16
|
srcLang = '';
|
|
21
17
|
tgtLang = '';
|
|
22
|
-
original = '';
|
|
23
|
-
fileId = '';
|
|
24
18
|
writeStream;
|
|
25
19
|
completionPromise;
|
|
26
20
|
entryCount = 0;
|
|
@@ -33,172 +27,84 @@ export class XLIFFHandler {
|
|
|
33
27
|
});
|
|
34
28
|
this.options = resolveImportOptions(options);
|
|
35
29
|
}
|
|
36
|
-
setGrammar(grammar) {
|
|
37
|
-
// do nothing
|
|
38
|
-
}
|
|
39
30
|
getEntryCount() {
|
|
40
31
|
return this.entryCount;
|
|
41
32
|
}
|
|
42
33
|
waitForCompletion() {
|
|
43
34
|
return this.completionPromise;
|
|
44
35
|
}
|
|
45
|
-
|
|
46
|
-
this.
|
|
47
|
-
this.
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
endDocument() {
|
|
56
|
-
// Close the write stream when document ends
|
|
36
|
+
process(document) {
|
|
37
|
+
this.srcLang = document.getSrcLang();
|
|
38
|
+
this.tgtLang = document.getTrgLang() ?? '';
|
|
39
|
+
document.getFiles().forEach((file) => {
|
|
40
|
+
const fileId = file.getId();
|
|
41
|
+
const original = file.getOriginal() ?? '';
|
|
42
|
+
this.collectUnits(file.getEntries()).forEach((unit) => {
|
|
43
|
+
this.processUnit(fileId, original, unit);
|
|
44
|
+
});
|
|
45
|
+
});
|
|
57
46
|
this.writeStream.end();
|
|
58
47
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
atts.forEach((att) => {
|
|
65
|
-
element.setAttribute(att);
|
|
66
|
-
});
|
|
67
|
-
if ("xliff" === name) {
|
|
68
|
-
const version = element.getAttribute("version");
|
|
69
|
-
if (!version || !version.getValue().startsWith("2.")) {
|
|
70
|
-
throw new Error("Unsupported XLIFF version");
|
|
71
|
-
}
|
|
72
|
-
const srcLang = element.getAttribute("srcLang");
|
|
73
|
-
if (!srcLang) {
|
|
74
|
-
throw new Error("Missing @srcLang attribute in <xliff>");
|
|
48
|
+
collectUnits(entries) {
|
|
49
|
+
const units = [];
|
|
50
|
+
entries.forEach((entry) => {
|
|
51
|
+
if (entry instanceof XliffGroup) {
|
|
52
|
+
units.push(...this.collectUnits(entry.getEntries()));
|
|
75
53
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
throw new Error("Missing @trgLang attribute in <xliff>");
|
|
54
|
+
else {
|
|
55
|
+
units.push(entry);
|
|
79
56
|
}
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
57
|
+
});
|
|
58
|
+
return units;
|
|
59
|
+
}
|
|
60
|
+
processUnit(fileId, original, unit) {
|
|
61
|
+
const unitId = unit.getId();
|
|
62
|
+
const items = unit.getItems();
|
|
63
|
+
const segmentItems = items.filter((item) => item instanceof XliffSegment);
|
|
64
|
+
const segments = this.buildSegments(fileId, unitId, unit, segmentItems);
|
|
65
|
+
if (segmentItems.length === 1) {
|
|
66
|
+
// A unit with exactly one <segment> has no distinct "whole unit" content:
|
|
67
|
+
// store one entry, framed as the unit entry, filtered/annotated using the
|
|
68
|
+
// same criteria (state, skipEmpty, skipUnconfirmed, metadata) a segment
|
|
69
|
+
// entry would use.
|
|
70
|
+
if (segments.length === 1) {
|
|
71
|
+
this.writeUnitEntry(fileId, original, unitId, items, segments[0].metadata, segments[0].segmentId);
|
|
89
72
|
}
|
|
90
|
-
this.fileId = id.getValue();
|
|
91
|
-
}
|
|
92
|
-
if (this.stack.length > 0) {
|
|
93
|
-
this.stack[this.stack.length - 1].addElement(element);
|
|
94
|
-
}
|
|
95
|
-
this.stack.push(element);
|
|
96
|
-
}
|
|
97
|
-
endElement(name) {
|
|
98
|
-
if ("unit" === name) {
|
|
99
|
-
const unit = this.stack[this.stack.length - 1];
|
|
100
|
-
this.processUnit(unit);
|
|
101
|
-
}
|
|
102
|
-
this.stack.pop();
|
|
103
|
-
}
|
|
104
|
-
internalSubset(declaration) {
|
|
105
|
-
// do nothing
|
|
106
|
-
}
|
|
107
|
-
characters(ch) {
|
|
108
|
-
if (this.inCdData) {
|
|
109
|
-
this.currentCData.setValue(this.currentCData.getValue() + ch);
|
|
110
73
|
return;
|
|
111
74
|
}
|
|
112
|
-
const textNode = new TextNode(ch);
|
|
113
|
-
// ignore characters outside of elements
|
|
114
|
-
if (this.stack.length > 0) {
|
|
115
|
-
this.stack[this.stack.length - 1].addTextNode(textNode);
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
ignorableWhitespace(ch) {
|
|
119
|
-
const textNode = new TextNode(ch);
|
|
120
|
-
// ignore characters outside of elements
|
|
121
|
-
if (this.stack.length > 0) {
|
|
122
|
-
this.stack[this.stack.length - 1].addTextNode(textNode);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
comment(ch) {
|
|
126
|
-
// do nothing
|
|
127
|
-
}
|
|
128
|
-
processingInstruction(target, data) {
|
|
129
|
-
// do nothing
|
|
130
|
-
}
|
|
131
|
-
startCDATA() {
|
|
132
|
-
this.inCdData = true;
|
|
133
|
-
}
|
|
134
|
-
endCDATA() {
|
|
135
|
-
this.inCdData = false;
|
|
136
|
-
}
|
|
137
|
-
startDTD(name, publicId, systemId) {
|
|
138
|
-
// do nothing
|
|
139
|
-
}
|
|
140
|
-
endDTD() {
|
|
141
|
-
// do nothing
|
|
142
|
-
}
|
|
143
|
-
skippedEntity(name) {
|
|
144
|
-
// do nothing
|
|
145
|
-
}
|
|
146
|
-
processUnit(unit) {
|
|
147
|
-
const id = unit.getAttribute('id');
|
|
148
|
-
if (!id) {
|
|
149
|
-
throw new Error('Missing @id attribute in <unit>');
|
|
150
|
-
}
|
|
151
|
-
const unitId = id.getValue();
|
|
152
|
-
const segmentElements = unit.getChildren().filter((child) => child.getName() === 'segment');
|
|
153
|
-
let segments = [];
|
|
154
|
-
if (segmentElements.length > 0) {
|
|
155
|
-
segments = this.buildSegments(unitId, unit, segmentElements);
|
|
156
|
-
if (segments.length === 0) {
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
const fallback = this.buildSingleSegment(unitId, unit);
|
|
162
|
-
if (!fallback) {
|
|
163
|
-
return;
|
|
164
|
-
}
|
|
165
|
-
segments = [fallback];
|
|
166
|
-
}
|
|
167
75
|
const segmentCount = segments.length;
|
|
168
76
|
segments.forEach((segment, index) => {
|
|
169
77
|
const segmentIndex = index + 1;
|
|
170
|
-
this.writeSegmentEntries(unitId, segmentIndex, segmentCount, segment);
|
|
78
|
+
this.writeSegmentEntries(fileId, original, unitId, segmentIndex, segmentCount, segment);
|
|
171
79
|
});
|
|
172
|
-
|
|
173
|
-
this.writeMergedEntry(unitId, segments);
|
|
174
|
-
}
|
|
80
|
+
this.writeUnitEntry(fileId, original, unitId, items);
|
|
175
81
|
}
|
|
176
|
-
buildSegments(unitId, unit,
|
|
82
|
+
buildSegments(fileId, unitId, unit, segmentItems) {
|
|
177
83
|
const segments = [];
|
|
178
|
-
|
|
179
|
-
const processed = this.buildSegmentData(unitId, unit, segment);
|
|
84
|
+
segmentItems.forEach((segment) => {
|
|
85
|
+
const processed = this.buildSegmentData(fileId, unitId, unit, segment);
|
|
180
86
|
if (processed) {
|
|
181
87
|
segments.push(processed);
|
|
182
88
|
}
|
|
183
89
|
});
|
|
184
90
|
return segments;
|
|
185
91
|
}
|
|
186
|
-
buildSegmentData(unitId, unit, segment) {
|
|
187
|
-
const sourceElement = segment.
|
|
188
|
-
const targetElement = segment.
|
|
92
|
+
buildSegmentData(fileId, unitId, unit, segment) {
|
|
93
|
+
const sourceElement = segment.getSource();
|
|
94
|
+
const targetElement = segment.getTarget();
|
|
189
95
|
if (!sourceElement || !targetElement) {
|
|
190
96
|
return null;
|
|
191
97
|
}
|
|
192
|
-
const pureSource =
|
|
193
|
-
const pureTarget =
|
|
98
|
+
const pureSource = XLIFFHandler.getPureText(sourceElement.getContent());
|
|
99
|
+
const pureTarget = XLIFFHandler.getPureText(targetElement.getContent());
|
|
194
100
|
if (pureSource.trim().length === 0) {
|
|
195
101
|
return null;
|
|
196
102
|
}
|
|
197
103
|
if (this.options.skipEmpty && pureTarget.trim().length === 0) {
|
|
198
104
|
return null;
|
|
199
105
|
}
|
|
200
|
-
const
|
|
201
|
-
const stateRank = this.getStateRank(
|
|
106
|
+
const state = segment.getState();
|
|
107
|
+
const stateRank = this.getStateRank(state);
|
|
202
108
|
const minRank = this.getStateRankFromOption(this.options.minState);
|
|
203
109
|
if (stateRank > 0 && stateRank < minRank) {
|
|
204
110
|
return null;
|
|
@@ -206,9 +112,9 @@ export class XLIFFHandler {
|
|
|
206
112
|
if (stateRank === 0 && this.options.skipUnconfirmed) {
|
|
207
113
|
return null;
|
|
208
114
|
}
|
|
209
|
-
const segmentId = segment.
|
|
115
|
+
const segmentId = segment.getId();
|
|
210
116
|
const metadata = this.options.extractMetadata
|
|
211
|
-
? this.extractMetadata(unitId, unit, segment,
|
|
117
|
+
? this.extractMetadata(fileId, unitId, unit, segment, state, segmentId)
|
|
212
118
|
: undefined;
|
|
213
119
|
return {
|
|
214
120
|
source: sourceElement,
|
|
@@ -219,80 +125,46 @@ export class XLIFFHandler {
|
|
|
219
125
|
segmentId
|
|
220
126
|
};
|
|
221
127
|
}
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
const combinedTarget = new XMLElement('target');
|
|
225
|
-
unit.getChildren().forEach((child) => {
|
|
226
|
-
if (child.getName() === 'segment' || child.getName() === 'ignorable') {
|
|
227
|
-
const source = child.getChild('source');
|
|
228
|
-
if (source) {
|
|
229
|
-
this.appendElementContent(combinedSource, source);
|
|
230
|
-
}
|
|
231
|
-
const target = child.getChild('target');
|
|
232
|
-
if (target) {
|
|
233
|
-
this.appendElementContent(combinedTarget, target);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
});
|
|
237
|
-
const pureSource = Utils.getPureText(combinedSource);
|
|
238
|
-
const pureTarget = Utils.getPureText(combinedTarget);
|
|
239
|
-
if (pureSource.trim().length === 0) {
|
|
240
|
-
return null;
|
|
241
|
-
}
|
|
242
|
-
if (this.options.skipEmpty && pureTarget.trim().length === 0) {
|
|
243
|
-
return null;
|
|
244
|
-
}
|
|
245
|
-
const metadata = this.options.extractMetadata
|
|
246
|
-
? this.extractMetadata(unitId, unit, unit)
|
|
247
|
-
: undefined;
|
|
248
|
-
return {
|
|
249
|
-
source: combinedSource,
|
|
250
|
-
target: combinedTarget,
|
|
251
|
-
pureSource,
|
|
252
|
-
pureTarget,
|
|
253
|
-
metadata
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
writeSegmentEntries(unitId, segmentIndex, segmentCount, segment) {
|
|
257
|
-
const metadata = this.applySegmentPosition(segment.metadata, unitId, segment.segmentId, segmentIndex, segmentCount);
|
|
128
|
+
writeSegmentEntries(fileId, original, unitId, segmentIndex, segmentCount, segment) {
|
|
129
|
+
const metadata = this.applySegmentPosition(segment.metadata, fileId, unitId, segment.segmentId, segmentIndex, segmentCount);
|
|
258
130
|
this.writeEntry({
|
|
259
131
|
language: this.srcLang,
|
|
260
|
-
fileId
|
|
132
|
+
fileId,
|
|
261
133
|
unitId,
|
|
262
|
-
original
|
|
134
|
+
original,
|
|
263
135
|
pureText: segment.pureSource,
|
|
264
|
-
element: segment.source.toString(),
|
|
136
|
+
element: segment.source.toElement().toString(),
|
|
265
137
|
segmentIndex,
|
|
266
138
|
segmentCount,
|
|
267
139
|
metadata
|
|
268
140
|
});
|
|
269
141
|
this.writeEntry({
|
|
270
142
|
language: this.tgtLang,
|
|
271
|
-
fileId
|
|
143
|
+
fileId,
|
|
272
144
|
unitId,
|
|
273
|
-
original
|
|
145
|
+
original,
|
|
274
146
|
pureText: segment.pureTarget,
|
|
275
|
-
element: segment.target.toString(),
|
|
147
|
+
element: segment.target.toElement().toString(),
|
|
276
148
|
segmentIndex,
|
|
277
149
|
segmentCount,
|
|
278
150
|
metadata
|
|
279
151
|
});
|
|
280
152
|
}
|
|
281
|
-
applySegmentPosition(metadata, unitId, explicitSegmentId, segmentIndex, segmentCount) {
|
|
153
|
+
applySegmentPosition(metadata, fileId, unitId, explicitSegmentId, segmentIndex, segmentCount) {
|
|
282
154
|
if (!metadata) {
|
|
283
155
|
return undefined;
|
|
284
156
|
}
|
|
285
157
|
if (!metadata.segment) {
|
|
286
158
|
metadata.segment = {
|
|
287
159
|
provider: 'xliff',
|
|
288
|
-
fileId
|
|
160
|
+
fileId,
|
|
289
161
|
unitId,
|
|
290
162
|
segmentId: explicitSegmentId
|
|
291
163
|
};
|
|
292
164
|
}
|
|
293
165
|
else {
|
|
294
166
|
metadata.segment.provider = metadata.segment.provider || 'xliff';
|
|
295
|
-
metadata.segment.fileId = metadata.segment.fileId ||
|
|
167
|
+
metadata.segment.fileId = metadata.segment.fileId || fileId;
|
|
296
168
|
metadata.segment.unitId = metadata.segment.unitId || unitId;
|
|
297
169
|
if (!metadata.segment.segmentId && explicitSegmentId) {
|
|
298
170
|
metadata.segment.segmentId = explicitSegmentId;
|
|
@@ -305,42 +177,68 @@ export class XLIFFHandler {
|
|
|
305
177
|
metadata.segment.segmentCount = segmentCount;
|
|
306
178
|
return metadata;
|
|
307
179
|
}
|
|
308
|
-
|
|
309
|
-
const mergedSource =
|
|
310
|
-
const mergedTarget =
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
this.appendElementContent(mergedTarget, segment.target);
|
|
314
|
-
});
|
|
315
|
-
const pureSource = Utils.getPureText(mergedSource);
|
|
316
|
-
const pureTarget = Utils.getPureText(mergedTarget);
|
|
180
|
+
writeUnitEntry(fileId, original, unitId, items, metadata, segmentId) {
|
|
181
|
+
const mergedSource = this.mergeSource(items);
|
|
182
|
+
const mergedTarget = this.mergeTarget(items);
|
|
183
|
+
const pureSource = XLIFFHandler.getPureText(mergedSource.getContent());
|
|
184
|
+
const pureTarget = XLIFFHandler.getPureText(mergedTarget.getContent());
|
|
317
185
|
if (pureSource.trim().length === 0) {
|
|
318
186
|
return;
|
|
319
187
|
}
|
|
320
188
|
if (this.options.skipEmpty && pureTarget.trim().length === 0) {
|
|
321
189
|
return;
|
|
322
190
|
}
|
|
191
|
+
const segmentCount = items.filter((item) => item instanceof XliffSegment).length;
|
|
192
|
+
const resolvedMetadata = this.applySegmentPosition(metadata, fileId, unitId, segmentId, 0, segmentCount);
|
|
323
193
|
this.writeEntry({
|
|
324
194
|
language: this.srcLang,
|
|
325
|
-
fileId
|
|
195
|
+
fileId,
|
|
326
196
|
unitId,
|
|
327
|
-
original
|
|
197
|
+
original,
|
|
328
198
|
pureText: pureSource,
|
|
329
|
-
element: mergedSource.toString(),
|
|
199
|
+
element: mergedSource.toElement().toString(),
|
|
330
200
|
segmentIndex: 0,
|
|
331
|
-
segmentCount
|
|
201
|
+
segmentCount,
|
|
202
|
+
metadata: resolvedMetadata
|
|
332
203
|
});
|
|
333
204
|
this.writeEntry({
|
|
334
205
|
language: this.tgtLang,
|
|
335
|
-
fileId
|
|
206
|
+
fileId,
|
|
336
207
|
unitId,
|
|
337
|
-
original
|
|
208
|
+
original,
|
|
338
209
|
pureText: pureTarget,
|
|
339
|
-
element: mergedTarget.toString(),
|
|
210
|
+
element: mergedTarget.toElement().toString(),
|
|
340
211
|
segmentIndex: 0,
|
|
341
|
-
segmentCount
|
|
212
|
+
segmentCount,
|
|
213
|
+
metadata: resolvedMetadata
|
|
342
214
|
});
|
|
343
215
|
}
|
|
216
|
+
mergeSource(items) {
|
|
217
|
+
const merged = new XliffSource();
|
|
218
|
+
items.forEach((item) => {
|
|
219
|
+
const source = item.getSource();
|
|
220
|
+
if (source) {
|
|
221
|
+
merged.getContent().push(...source.getContent());
|
|
222
|
+
}
|
|
223
|
+
});
|
|
224
|
+
return merged;
|
|
225
|
+
}
|
|
226
|
+
mergeTarget(items) {
|
|
227
|
+
// @order re-sequences a target relative to the source when a unit is resegmented
|
|
228
|
+
// differently on the target side; items without @order keep their document position.
|
|
229
|
+
const ordered = items
|
|
230
|
+
.map((item, index) => ({ item, position: item.getTarget()?.getOrder() ?? (index + 1) }))
|
|
231
|
+
.filter((entry) => entry.item.getTarget() !== undefined)
|
|
232
|
+
.sort((a, b) => Number(a.position) - Number(b.position));
|
|
233
|
+
const merged = new XliffTarget();
|
|
234
|
+
ordered.forEach(({ item }) => {
|
|
235
|
+
const target = item.getTarget();
|
|
236
|
+
if (target) {
|
|
237
|
+
merged.getContent().push(...target.getContent());
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
return merged;
|
|
241
|
+
}
|
|
344
242
|
writeEntry(entry) {
|
|
345
243
|
const payload = {
|
|
346
244
|
language: entry.language,
|
|
@@ -361,42 +259,31 @@ export class XLIFFHandler {
|
|
|
361
259
|
hasMetadata(metadata) {
|
|
362
260
|
return !!metadata && Object.keys(metadata).length > 0;
|
|
363
261
|
}
|
|
364
|
-
|
|
365
|
-
const content = element.getContent();
|
|
366
|
-
content.forEach((node) => {
|
|
367
|
-
if (node instanceof TextNode) {
|
|
368
|
-
target.addTextNode(node);
|
|
369
|
-
}
|
|
370
|
-
else if (node instanceof XMLElement) {
|
|
371
|
-
target.addElement(node);
|
|
372
|
-
}
|
|
373
|
-
});
|
|
374
|
-
}
|
|
375
|
-
extractMetadata(unitId, unit, segment, rawState, segmentId) {
|
|
262
|
+
extractMetadata(fileId, unitId, unit, segment, state, segmentId) {
|
|
376
263
|
const metadata = {};
|
|
377
|
-
if (this.isTranslationState(
|
|
378
|
-
metadata.state =
|
|
264
|
+
if (this.isTranslationState(state)) {
|
|
265
|
+
metadata.state = state;
|
|
379
266
|
}
|
|
380
|
-
const subState =
|
|
267
|
+
const subState = segment.getSubState();
|
|
381
268
|
if (subState) {
|
|
382
269
|
metadata.subState = subState;
|
|
383
270
|
}
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
this.assignMetadataString(metadata, '
|
|
387
|
-
this.assignMetadataString(metadata, '
|
|
388
|
-
this.assignMetadataString(metadata, '
|
|
389
|
-
this.assignMetadataString(metadata, '
|
|
390
|
-
this.assignMetadataString(metadata, '
|
|
271
|
+
const unitAttrs = unit.getOtherAttributes();
|
|
272
|
+
const segmentAttrs = segment.getOtherAttributes();
|
|
273
|
+
this.assignMetadataString(metadata, 'creationDate', this.readOtherAttribute(segmentAttrs, 'creationDate') || this.readOtherAttribute(unitAttrs, 'creationDate'));
|
|
274
|
+
this.assignMetadataString(metadata, 'creationId', this.readOtherAttribute(segmentAttrs, 'creationId') || this.readOtherAttribute(unitAttrs, 'creationId'));
|
|
275
|
+
this.assignMetadataString(metadata, 'changeDate', this.readOtherAttribute(segmentAttrs, 'changeDate') || this.readOtherAttribute(unitAttrs, 'changeDate'));
|
|
276
|
+
this.assignMetadataString(metadata, 'changeId', this.readOtherAttribute(segmentAttrs, 'changeId') || this.readOtherAttribute(unitAttrs, 'changeId'));
|
|
277
|
+
this.assignMetadataString(metadata, 'creationTool', this.readOtherAttribute(segmentAttrs, 'creationTool') || this.readOtherAttribute(unitAttrs, 'creationTool'));
|
|
278
|
+
this.assignMetadataString(metadata, 'creationToolVersion', this.readOtherAttribute(segmentAttrs, 'creationToolVersion') || this.readOtherAttribute(unitAttrs, 'creationToolVersion'));
|
|
279
|
+
this.assignMetadataString(metadata, 'context', this.readOtherAttribute(segmentAttrs, 'context') || this.readOtherAttribute(unitAttrs, 'context'));
|
|
391
280
|
const notes = [];
|
|
392
281
|
this.collectNotes(unit, notes);
|
|
393
|
-
this.collectNotes(segment, notes);
|
|
394
282
|
if (notes.length > 0) {
|
|
395
283
|
metadata.notes = notes;
|
|
396
284
|
}
|
|
397
285
|
const properties = {};
|
|
398
286
|
this.collectMetadataProperties(unit, properties);
|
|
399
|
-
this.collectMetadataProperties(segment, properties);
|
|
400
287
|
if (Object.keys(properties).length > 0) {
|
|
401
288
|
metadata.properties = properties;
|
|
402
289
|
if (!metadata.context) {
|
|
@@ -408,7 +295,7 @@ export class XLIFFHandler {
|
|
|
408
295
|
}
|
|
409
296
|
metadata.segment = {
|
|
410
297
|
provider: 'xliff',
|
|
411
|
-
fileId
|
|
298
|
+
fileId,
|
|
412
299
|
unitId,
|
|
413
300
|
segmentId
|
|
414
301
|
};
|
|
@@ -419,78 +306,47 @@ export class XLIFFHandler {
|
|
|
419
306
|
metadata[key] = value;
|
|
420
307
|
}
|
|
421
308
|
}
|
|
422
|
-
|
|
423
|
-
const attribute =
|
|
309
|
+
readOtherAttribute(otherAttributes, name) {
|
|
310
|
+
const attribute = otherAttributes.find((att) => att.getName() === name);
|
|
424
311
|
if (!attribute) {
|
|
425
312
|
return undefined;
|
|
426
313
|
}
|
|
427
314
|
const value = attribute.getValue();
|
|
428
315
|
return value && value.length > 0 ? value : undefined;
|
|
429
316
|
}
|
|
430
|
-
|
|
431
|
-
const
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
if (target) {
|
|
437
|
-
const targetValue = this.readAttribute(target, 'subState');
|
|
438
|
-
if (targetValue) {
|
|
439
|
-
return targetValue;
|
|
440
|
-
}
|
|
441
|
-
}
|
|
442
|
-
const source = segment.getChild('source');
|
|
443
|
-
if (source) {
|
|
444
|
-
const sourceValue = this.readAttribute(source, 'subState');
|
|
445
|
-
if (sourceValue) {
|
|
446
|
-
return sourceValue;
|
|
447
|
-
}
|
|
448
|
-
}
|
|
449
|
-
return undefined;
|
|
450
|
-
}
|
|
451
|
-
collectNotes(element, accumulator) {
|
|
452
|
-
element.getChildren().forEach((child) => {
|
|
453
|
-
if (child.getName() === 'notes') {
|
|
454
|
-
child.getChildren().forEach((noteElement) => {
|
|
455
|
-
if (noteElement.getName() === 'note') {
|
|
456
|
-
const value = Utils.getPureText(noteElement).trim();
|
|
457
|
-
if (value.length > 0) {
|
|
458
|
-
accumulator.push(value);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
});
|
|
317
|
+
collectNotes(unit, accumulator) {
|
|
318
|
+
const notes = unit.getNotes()?.getNotes() ?? [];
|
|
319
|
+
notes.forEach((note) => {
|
|
320
|
+
const value = note.getText().trim();
|
|
321
|
+
if (value.length > 0) {
|
|
322
|
+
accumulator.push(value);
|
|
462
323
|
}
|
|
463
324
|
});
|
|
464
325
|
}
|
|
465
|
-
collectMetadataProperties(
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
if (key.length > 0 && content.length > 0) {
|
|
482
|
-
properties[key] = content;
|
|
483
|
-
}
|
|
484
|
-
});
|
|
485
|
-
});
|
|
326
|
+
collectMetadataProperties(unit, properties) {
|
|
327
|
+
const metaGroups = unit.getMetadata()?.getMetaGroups() ?? [];
|
|
328
|
+
metaGroups.forEach((metaGroup) => this.collectFromMetaGroup(metaGroup, properties));
|
|
329
|
+
}
|
|
330
|
+
collectFromMetaGroup(metaGroup, properties) {
|
|
331
|
+
const category = metaGroup.getCategory();
|
|
332
|
+
metaGroup.getItems().forEach((item) => {
|
|
333
|
+
if (item instanceof XliffMeta) {
|
|
334
|
+
const key = category ? category + ':' + item.getType() : item.getType();
|
|
335
|
+
const content = item.getText().trim();
|
|
336
|
+
if (key.length > 0 && content.length > 0) {
|
|
337
|
+
properties[key] = content;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
this.collectFromMetaGroup(item, properties);
|
|
486
342
|
}
|
|
487
343
|
});
|
|
488
344
|
}
|
|
489
|
-
getStateRank(
|
|
490
|
-
if (!this.isTranslationState(
|
|
345
|
+
getStateRank(state) {
|
|
346
|
+
if (!this.isTranslationState(state)) {
|
|
491
347
|
return 0;
|
|
492
348
|
}
|
|
493
|
-
return this.getStateRankFromOption(
|
|
349
|
+
return this.getStateRankFromOption(state);
|
|
494
350
|
}
|
|
495
351
|
getStateRankFromOption(state) {
|
|
496
352
|
if (!state) {
|
|
@@ -515,18 +371,18 @@ export class XLIFFHandler {
|
|
|
515
371
|
|| value === 'reviewed'
|
|
516
372
|
|| value === 'final';
|
|
517
373
|
}
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
test += node.getValue();
|
|
374
|
+
static getPureText(content) {
|
|
375
|
+
let text = '';
|
|
376
|
+
content.forEach((item) => {
|
|
377
|
+
if (typeof item === 'string') {
|
|
378
|
+
text += item;
|
|
379
|
+
}
|
|
380
|
+
else if (item instanceof XliffPc || item instanceof XliffMrk) {
|
|
381
|
+
text += XLIFFHandler.getPureText(item.getContent());
|
|
527
382
|
}
|
|
383
|
+
// ph, sc, ec, sm, em and cp purposely ignored, matching prior pure-text extraction
|
|
528
384
|
});
|
|
529
|
-
return
|
|
385
|
+
return text;
|
|
530
386
|
}
|
|
531
387
|
}
|
|
532
388
|
//# sourceMappingURL=xliffHandler.js.map
|