@worldware/msg-cli 0.6.2 → 0.6.3
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 +9 -0
- package/dist/{chunk-WOGVKE3R.mjs → chunk-KOTPQTLG.mjs} +33 -15
- package/dist/{chunk-HZHAP6XM.mjs → chunk-XH4RUUAJ.mjs} +8 -1
- package/dist/commands/export.cjs +8 -1
- package/dist/commands/export.mjs +1 -1
- package/dist/commands/import.cjs +33 -15
- package/dist/commands/import.mjs +1 -1
- package/dist/lib/export-helpers.cjs +8 -1
- package/dist/lib/export-helpers.mjs +1 -1
- package/dist/lib/import-helpers.cjs +33 -15
- package/dist/lib/import-helpers.d.mts +5 -2
- package/dist/lib/import-helpers.d.ts +5 -2
- package/dist/lib/import-helpers.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -247,6 +247,7 @@ msg export -p myApp
|
|
|
247
247
|
- **Message notes** — Emitted as unit-level `<notes>` with category (e.g. `description`, `context`, `parameters`).
|
|
248
248
|
- **Message attributes** — `dnt` → unit `translate="no"`; message `dir` is serialized as the unit’s `srcDir` unless it is `"auto"`.
|
|
249
249
|
- **Message format** — Resolved `format` (`NONE` / `MF1` / `MF2`, including inheritance from resource/project) is written as the unit `type` attribute using the XLIFF custom form `msg:NONE`, `msg:MF1`, or `msg:MF2`.
|
|
250
|
+
- **Significant whitespace** — A unit gets `xml:space="preserve"` when its source text has leading or trailing whitespace, a tab or line break, or a run of spaces. This tells translation tools, and `msg import`, to keep whitespace exactly (e.g. `"Total: "`). For PGS units the case bodies are checked, not the multi-line MF2 message. Carriage returns are written as ` `, because XML parsing would otherwise turn them into line feeds.
|
|
250
251
|
|
|
251
252
|
**Plural, gender, select (PGS):** Classifiable plural/select messages are exported to the [XLIFF 2.2 Plural, Gender, and Select module](https://docs.oasis-open.org/xliff/xliff-core/v2.2/xliff-extended-v2.2-part2.html) (`xmlns:pgs="urn:oasis:names:tc:xliff:pgs:1.0"`): `pgs:switch` on the `<unit>`, and one `<segment>` per variant with `pgs:case`.
|
|
252
253
|
|
|
@@ -263,6 +264,14 @@ Import translations from bilingual XLIFF 2.0 or 2.2 files in `l10n/xliff` to JSO
|
|
|
263
264
|
- Unit `type` of `msg:NONE` / `msg:MF1` / `msg:MF2` (or bare `NONE` / `MF1` / `MF2`) is restored as the message `attributes.format`.
|
|
264
265
|
- Units with `pgs:switch` are reassembled from segment bodies (`<target>` when present): **MF1** → nested ICU MessageFormat 1; otherwise (including missing type) → MessageFormat 2 `.match`. Single-segment units are unchanged.
|
|
265
266
|
|
|
267
|
+
**Whitespace on import:**
|
|
268
|
+
|
|
269
|
+
- `xml:space` is inherited per XLIFF 2: `<xliff>` → `<file>` → `<group>` → `<unit>` → `<source>`/`<target>`, defaulting to `default`. Invalid values are ignored.
|
|
270
|
+
- `preserve` — segment text is kept exactly, including leading/trailing whitespace and whitespace-only values.
|
|
271
|
+
- `default` — leading and trailing XML whitespace (space, tab, CR, LF) is trimmed; non-breaking and other Unicode spaces are kept.
|
|
272
|
+
- Character references such as ` ` and ` ` are decoded.
|
|
273
|
+
- Notes are not affected: import writes translation files without notes.
|
|
274
|
+
|
|
266
275
|
```bash
|
|
267
276
|
msg import [-p <projectName>] [-l <locale>]
|
|
268
277
|
```
|
|
@@ -100,7 +100,10 @@ var inlineContentParser = new XMLParser({
|
|
|
100
100
|
attributeNamePrefix: "@_",
|
|
101
101
|
preserveOrder: true,
|
|
102
102
|
trimValues: false,
|
|
103
|
-
parseTagValue: false
|
|
103
|
+
parseTagValue: false,
|
|
104
|
+
// Decode numeric character references ( ,  ), which fast-xml-parser leaves
|
|
105
|
+
// as literal text by default. Export writes CR as so it survives parsing.
|
|
106
|
+
htmlEntities: true
|
|
104
107
|
});
|
|
105
108
|
function parseXliff20(xml) {
|
|
106
109
|
try {
|
|
@@ -126,14 +129,22 @@ function categoryToNoteType(category) {
|
|
|
126
129
|
};
|
|
127
130
|
return map[lower] ?? category.toUpperCase();
|
|
128
131
|
}
|
|
129
|
-
function
|
|
132
|
+
function resolveXmlSpace(el, inherited) {
|
|
133
|
+
const own = el && typeof el === "object" ? el["@_xml:space"] : void 0;
|
|
134
|
+
return own === "preserve" || own === "default" ? own : inherited;
|
|
135
|
+
}
|
|
136
|
+
function trimXmlWhitespace(text) {
|
|
137
|
+
return text.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
|
|
138
|
+
}
|
|
139
|
+
function extractSegmentText(segment, space) {
|
|
130
140
|
if (segment == null) return "";
|
|
131
141
|
const seg = segment;
|
|
132
142
|
const content = seg.target ?? seg.source;
|
|
133
143
|
const raw = content && typeof content === "object" ? content["#text"] : content;
|
|
134
144
|
if (typeof raw !== "string" || raw === "") return "";
|
|
135
145
|
const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
|
|
136
|
-
|
|
146
|
+
const text = extractInlineText(root.c);
|
|
147
|
+
return resolveXmlSpace(content, space) === "preserve" ? text : trimXmlWhitespace(text);
|
|
137
148
|
}
|
|
138
149
|
function codePointFromHex(hex) {
|
|
139
150
|
const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
|
|
@@ -166,30 +177,32 @@ function extractInlineText(nodes) {
|
|
|
166
177
|
}
|
|
167
178
|
return text;
|
|
168
179
|
}
|
|
169
|
-
function processUnitsFromItems(units, fileAttrs) {
|
|
180
|
+
function processUnitsFromItems(units, fileAttrs, space) {
|
|
170
181
|
const messages = [];
|
|
171
182
|
for (const u of units) {
|
|
172
|
-
const msg = processUnit(u, fileAttrs);
|
|
183
|
+
const msg = processUnit(u, fileAttrs, space);
|
|
173
184
|
if (msg) messages.push(msg);
|
|
174
185
|
}
|
|
175
186
|
return messages;
|
|
176
187
|
}
|
|
177
|
-
function processGroupsForMessages(groups, fileAttrs) {
|
|
188
|
+
function processGroupsForMessages(groups, fileAttrs, space) {
|
|
178
189
|
const messages = [];
|
|
179
190
|
for (const item of groups) {
|
|
180
191
|
const obj = item;
|
|
192
|
+
const groupSpace = resolveXmlSpace(obj, space);
|
|
181
193
|
if (obj.unit) {
|
|
182
194
|
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
183
|
-
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
195
|
+
messages.push(...processUnitsFromItems(units, fileAttrs, groupSpace));
|
|
184
196
|
}
|
|
185
197
|
if (obj.group) {
|
|
186
198
|
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
187
|
-
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
199
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs, groupSpace));
|
|
188
200
|
}
|
|
189
201
|
}
|
|
190
202
|
return messages;
|
|
191
203
|
}
|
|
192
|
-
function processUnit(unit, fileAttrs) {
|
|
204
|
+
function processUnit(unit, fileAttrs, inheritedSpace) {
|
|
205
|
+
const space = resolveXmlSpace(unit, inheritedSpace);
|
|
193
206
|
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
194
207
|
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
195
208
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
@@ -211,14 +224,14 @@ function processUnit(unit, fileAttrs) {
|
|
|
211
224
|
}
|
|
212
225
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
213
226
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
214
|
-
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
227
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg, space)).join("");
|
|
215
228
|
let value;
|
|
216
229
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
217
230
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
218
231
|
)) {
|
|
219
232
|
const importSegments = segments.map((seg) => ({
|
|
220
233
|
caseAttr: seg["@_pgs:case"],
|
|
221
|
-
body: extractSegmentText(seg)
|
|
234
|
+
body: extractSegmentText(seg, space)
|
|
222
235
|
}));
|
|
223
236
|
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
224
237
|
value = rebuilt ?? joinSegmentTexts();
|
|
@@ -237,7 +250,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
237
250
|
notes
|
|
238
251
|
};
|
|
239
252
|
}
|
|
240
|
-
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
253
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales, xliffSpace = "default") {
|
|
241
254
|
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
242
255
|
if (!trgLang) return null;
|
|
243
256
|
if (!targetLocales.includes(trgLang)) return null;
|
|
@@ -267,14 +280,17 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
267
280
|
trgDir,
|
|
268
281
|
translate
|
|
269
282
|
};
|
|
283
|
+
const fileSpace = resolveXmlSpace(fileEl, xliffSpace);
|
|
270
284
|
const allMessages = [
|
|
271
285
|
...processUnitsFromItems(
|
|
272
286
|
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
273
|
-
fileAttrs
|
|
287
|
+
fileAttrs,
|
|
288
|
+
fileSpace
|
|
274
289
|
),
|
|
275
290
|
...processGroupsForMessages(
|
|
276
291
|
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
277
|
-
fileAttrs
|
|
292
|
+
fileAttrs,
|
|
293
|
+
fileSpace
|
|
278
294
|
)
|
|
279
295
|
];
|
|
280
296
|
const resourceData = {
|
|
@@ -308,13 +324,15 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
308
324
|
if (!xliffTrgLang) return null;
|
|
309
325
|
const fileEls = xliffRoot.file;
|
|
310
326
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
327
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
311
328
|
const resources = [];
|
|
312
329
|
for (const fileEl of files) {
|
|
313
330
|
const resource = extractResourceFromXliffFile(
|
|
314
331
|
fileEl,
|
|
315
332
|
xliffTrgLang,
|
|
316
333
|
project,
|
|
317
|
-
targetLocales
|
|
334
|
+
targetLocales,
|
|
335
|
+
xliffSpace
|
|
318
336
|
);
|
|
319
337
|
if (resource) {
|
|
320
338
|
const json = resource.toJSON(true);
|
|
@@ -86,7 +86,10 @@ function filterResourceGroupsByProject(groups, projectName) {
|
|
|
86
86
|
return groups.filter((g) => g.project === projectName);
|
|
87
87
|
}
|
|
88
88
|
function escapeXml(s) {
|
|
89
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
89
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\r/g, " ");
|
|
90
|
+
}
|
|
91
|
+
function needsPreservedSpace(text) {
|
|
92
|
+
return /^[ \t\r\n]|[ \t\r\n]$|[\t\r\n]| {2}/.test(text);
|
|
90
93
|
}
|
|
91
94
|
function sanitizeForXmlId(s) {
|
|
92
95
|
const sanitized = s.replace(/[^a-zA-Z0-9._:-]/g, "_").replace(/_+/g, "_");
|
|
@@ -180,6 +183,10 @@ function resourceGroupToXliff22(group) {
|
|
|
180
183
|
if (pgsExport) {
|
|
181
184
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
182
185
|
}
|
|
186
|
+
const sourceTexts = pgsExport ? pgsExport.segments.map((seg) => seg.sourcePattern) : [msg.value];
|
|
187
|
+
if (sourceTexts.some(needsPreservedSpace)) {
|
|
188
|
+
msgAttrs.push('xml:space="preserve"');
|
|
189
|
+
}
|
|
183
190
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
184
191
|
const msgNotes = msg.notes ?? [];
|
|
185
192
|
if (msgNotes.length > 0) {
|
package/dist/commands/export.cjs
CHANGED
|
@@ -410,7 +410,10 @@ function filterResourceGroupsByProject(groups, projectName) {
|
|
|
410
410
|
return groups.filter((g) => g.project === projectName);
|
|
411
411
|
}
|
|
412
412
|
function escapeXml(s) {
|
|
413
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
413
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\r/g, " ");
|
|
414
|
+
}
|
|
415
|
+
function needsPreservedSpace(text) {
|
|
416
|
+
return /^[ \t\r\n]|[ \t\r\n]$|[\t\r\n]| {2}/.test(text);
|
|
414
417
|
}
|
|
415
418
|
function sanitizeForXmlId(s) {
|
|
416
419
|
const sanitized = s.replace(/[^a-zA-Z0-9._:-]/g, "_").replace(/_+/g, "_");
|
|
@@ -504,6 +507,10 @@ function resourceGroupToXliff22(group) {
|
|
|
504
507
|
if (pgsExport) {
|
|
505
508
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
506
509
|
}
|
|
510
|
+
const sourceTexts = pgsExport ? pgsExport.segments.map((seg) => seg.sourcePattern) : [msg.value];
|
|
511
|
+
if (sourceTexts.some(needsPreservedSpace)) {
|
|
512
|
+
msgAttrs.push('xml:space="preserve"');
|
|
513
|
+
}
|
|
507
514
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
508
515
|
const msgNotes = msg.notes ?? [];
|
|
509
516
|
if (msgNotes.length > 0) {
|
package/dist/commands/export.mjs
CHANGED
package/dist/commands/import.cjs
CHANGED
|
@@ -452,7 +452,10 @@ var inlineContentParser = new import_fast_xml_parser.XMLParser({
|
|
|
452
452
|
attributeNamePrefix: "@_",
|
|
453
453
|
preserveOrder: true,
|
|
454
454
|
trimValues: false,
|
|
455
|
-
parseTagValue: false
|
|
455
|
+
parseTagValue: false,
|
|
456
|
+
// Decode numeric character references ( ,  ), which fast-xml-parser leaves
|
|
457
|
+
// as literal text by default. Export writes CR as so it survives parsing.
|
|
458
|
+
htmlEntities: true
|
|
456
459
|
});
|
|
457
460
|
function parseXliff20(xml) {
|
|
458
461
|
try {
|
|
@@ -478,14 +481,22 @@ function categoryToNoteType(category) {
|
|
|
478
481
|
};
|
|
479
482
|
return map[lower] ?? category.toUpperCase();
|
|
480
483
|
}
|
|
481
|
-
function
|
|
484
|
+
function resolveXmlSpace(el, inherited) {
|
|
485
|
+
const own = el && typeof el === "object" ? el["@_xml:space"] : void 0;
|
|
486
|
+
return own === "preserve" || own === "default" ? own : inherited;
|
|
487
|
+
}
|
|
488
|
+
function trimXmlWhitespace(text) {
|
|
489
|
+
return text.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
|
|
490
|
+
}
|
|
491
|
+
function extractSegmentText(segment, space) {
|
|
482
492
|
if (segment == null) return "";
|
|
483
493
|
const seg = segment;
|
|
484
494
|
const content = seg.target ?? seg.source;
|
|
485
495
|
const raw = content && typeof content === "object" ? content["#text"] : content;
|
|
486
496
|
if (typeof raw !== "string" || raw === "") return "";
|
|
487
497
|
const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
|
|
488
|
-
|
|
498
|
+
const text = extractInlineText(root.c);
|
|
499
|
+
return resolveXmlSpace(content, space) === "preserve" ? text : trimXmlWhitespace(text);
|
|
489
500
|
}
|
|
490
501
|
function codePointFromHex(hex) {
|
|
491
502
|
const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
|
|
@@ -518,30 +529,32 @@ function extractInlineText(nodes) {
|
|
|
518
529
|
}
|
|
519
530
|
return text;
|
|
520
531
|
}
|
|
521
|
-
function processUnitsFromItems(units, fileAttrs) {
|
|
532
|
+
function processUnitsFromItems(units, fileAttrs, space) {
|
|
522
533
|
const messages = [];
|
|
523
534
|
for (const u of units) {
|
|
524
|
-
const msg = processUnit(u, fileAttrs);
|
|
535
|
+
const msg = processUnit(u, fileAttrs, space);
|
|
525
536
|
if (msg) messages.push(msg);
|
|
526
537
|
}
|
|
527
538
|
return messages;
|
|
528
539
|
}
|
|
529
|
-
function processGroupsForMessages(groups, fileAttrs) {
|
|
540
|
+
function processGroupsForMessages(groups, fileAttrs, space) {
|
|
530
541
|
const messages = [];
|
|
531
542
|
for (const item of groups) {
|
|
532
543
|
const obj = item;
|
|
544
|
+
const groupSpace = resolveXmlSpace(obj, space);
|
|
533
545
|
if (obj.unit) {
|
|
534
546
|
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
535
|
-
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
547
|
+
messages.push(...processUnitsFromItems(units, fileAttrs, groupSpace));
|
|
536
548
|
}
|
|
537
549
|
if (obj.group) {
|
|
538
550
|
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
539
|
-
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
551
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs, groupSpace));
|
|
540
552
|
}
|
|
541
553
|
}
|
|
542
554
|
return messages;
|
|
543
555
|
}
|
|
544
|
-
function processUnit(unit, fileAttrs) {
|
|
556
|
+
function processUnit(unit, fileAttrs, inheritedSpace) {
|
|
557
|
+
const space = resolveXmlSpace(unit, inheritedSpace);
|
|
545
558
|
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
546
559
|
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
547
560
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
@@ -563,14 +576,14 @@ function processUnit(unit, fileAttrs) {
|
|
|
563
576
|
}
|
|
564
577
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
565
578
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
566
|
-
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
579
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg, space)).join("");
|
|
567
580
|
let value;
|
|
568
581
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
569
582
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
570
583
|
)) {
|
|
571
584
|
const importSegments = segments.map((seg) => ({
|
|
572
585
|
caseAttr: seg["@_pgs:case"],
|
|
573
|
-
body: extractSegmentText(seg)
|
|
586
|
+
body: extractSegmentText(seg, space)
|
|
574
587
|
}));
|
|
575
588
|
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
576
589
|
value = rebuilt ?? joinSegmentTexts();
|
|
@@ -589,7 +602,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
589
602
|
notes
|
|
590
603
|
};
|
|
591
604
|
}
|
|
592
|
-
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
605
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales, xliffSpace = "default") {
|
|
593
606
|
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
594
607
|
if (!trgLang) return null;
|
|
595
608
|
if (!targetLocales.includes(trgLang)) return null;
|
|
@@ -619,14 +632,17 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
619
632
|
trgDir,
|
|
620
633
|
translate
|
|
621
634
|
};
|
|
635
|
+
const fileSpace = resolveXmlSpace(fileEl, xliffSpace);
|
|
622
636
|
const allMessages = [
|
|
623
637
|
...processUnitsFromItems(
|
|
624
638
|
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
625
|
-
fileAttrs
|
|
639
|
+
fileAttrs,
|
|
640
|
+
fileSpace
|
|
626
641
|
),
|
|
627
642
|
...processGroupsForMessages(
|
|
628
643
|
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
629
|
-
fileAttrs
|
|
644
|
+
fileAttrs,
|
|
645
|
+
fileSpace
|
|
630
646
|
)
|
|
631
647
|
];
|
|
632
648
|
const resourceData = {
|
|
@@ -660,13 +676,15 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
660
676
|
if (!xliffTrgLang) return null;
|
|
661
677
|
const fileEls = xliffRoot.file;
|
|
662
678
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
679
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
663
680
|
const resources = [];
|
|
664
681
|
for (const fileEl of files) {
|
|
665
682
|
const resource = extractResourceFromXliffFile(
|
|
666
683
|
fileEl,
|
|
667
684
|
xliffTrgLang,
|
|
668
685
|
project,
|
|
669
|
-
targetLocales
|
|
686
|
+
targetLocales,
|
|
687
|
+
xliffSpace
|
|
670
688
|
);
|
|
671
689
|
if (resource) {
|
|
672
690
|
const json = resource.toJSON(true);
|
package/dist/commands/import.mjs
CHANGED
|
@@ -347,7 +347,10 @@ function filterResourceGroupsByProject(groups, projectName) {
|
|
|
347
347
|
return groups.filter((g) => g.project === projectName);
|
|
348
348
|
}
|
|
349
349
|
function escapeXml(s) {
|
|
350
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
350
|
+
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'").replace(/\r/g, " ");
|
|
351
|
+
}
|
|
352
|
+
function needsPreservedSpace(text) {
|
|
353
|
+
return /^[ \t\r\n]|[ \t\r\n]$|[\t\r\n]| {2}/.test(text);
|
|
351
354
|
}
|
|
352
355
|
function sanitizeForXmlId(s) {
|
|
353
356
|
const sanitized = s.replace(/[^a-zA-Z0-9._:-]/g, "_").replace(/_+/g, "_");
|
|
@@ -441,6 +444,10 @@ function resourceGroupToXliff22(group) {
|
|
|
441
444
|
if (pgsExport) {
|
|
442
445
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
443
446
|
}
|
|
447
|
+
const sourceTexts = pgsExport ? pgsExport.segments.map((seg) => seg.sourcePattern) : [msg.value];
|
|
448
|
+
if (sourceTexts.some(needsPreservedSpace)) {
|
|
449
|
+
msgAttrs.push('xml:space="preserve"');
|
|
450
|
+
}
|
|
444
451
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
445
452
|
const msgNotes = msg.notes ?? [];
|
|
446
453
|
if (msgNotes.length > 0) {
|
|
@@ -393,7 +393,10 @@ var inlineContentParser = new import_fast_xml_parser.XMLParser({
|
|
|
393
393
|
attributeNamePrefix: "@_",
|
|
394
394
|
preserveOrder: true,
|
|
395
395
|
trimValues: false,
|
|
396
|
-
parseTagValue: false
|
|
396
|
+
parseTagValue: false,
|
|
397
|
+
// Decode numeric character references ( ,  ), which fast-xml-parser leaves
|
|
398
|
+
// as literal text by default. Export writes CR as so it survives parsing.
|
|
399
|
+
htmlEntities: true
|
|
397
400
|
});
|
|
398
401
|
function parseXliff20(xml) {
|
|
399
402
|
try {
|
|
@@ -419,14 +422,22 @@ function categoryToNoteType(category) {
|
|
|
419
422
|
};
|
|
420
423
|
return map[lower] ?? category.toUpperCase();
|
|
421
424
|
}
|
|
422
|
-
function
|
|
425
|
+
function resolveXmlSpace(el, inherited) {
|
|
426
|
+
const own = el && typeof el === "object" ? el["@_xml:space"] : void 0;
|
|
427
|
+
return own === "preserve" || own === "default" ? own : inherited;
|
|
428
|
+
}
|
|
429
|
+
function trimXmlWhitespace(text) {
|
|
430
|
+
return text.replace(/^[ \t\r\n]+|[ \t\r\n]+$/g, "");
|
|
431
|
+
}
|
|
432
|
+
function extractSegmentText(segment, space) {
|
|
423
433
|
if (segment == null) return "";
|
|
424
434
|
const seg = segment;
|
|
425
435
|
const content = seg.target ?? seg.source;
|
|
426
436
|
const raw = content && typeof content === "object" ? content["#text"] : content;
|
|
427
437
|
if (typeof raw !== "string" || raw === "") return "";
|
|
428
438
|
const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
|
|
429
|
-
|
|
439
|
+
const text = extractInlineText(root.c);
|
|
440
|
+
return resolveXmlSpace(content, space) === "preserve" ? text : trimXmlWhitespace(text);
|
|
430
441
|
}
|
|
431
442
|
function codePointFromHex(hex) {
|
|
432
443
|
const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
|
|
@@ -459,30 +470,32 @@ function extractInlineText(nodes) {
|
|
|
459
470
|
}
|
|
460
471
|
return text;
|
|
461
472
|
}
|
|
462
|
-
function processUnitsFromItems(units, fileAttrs) {
|
|
473
|
+
function processUnitsFromItems(units, fileAttrs, space) {
|
|
463
474
|
const messages = [];
|
|
464
475
|
for (const u of units) {
|
|
465
|
-
const msg = processUnit(u, fileAttrs);
|
|
476
|
+
const msg = processUnit(u, fileAttrs, space);
|
|
466
477
|
if (msg) messages.push(msg);
|
|
467
478
|
}
|
|
468
479
|
return messages;
|
|
469
480
|
}
|
|
470
|
-
function processGroupsForMessages(groups, fileAttrs) {
|
|
481
|
+
function processGroupsForMessages(groups, fileAttrs, space) {
|
|
471
482
|
const messages = [];
|
|
472
483
|
for (const item of groups) {
|
|
473
484
|
const obj = item;
|
|
485
|
+
const groupSpace = resolveXmlSpace(obj, space);
|
|
474
486
|
if (obj.unit) {
|
|
475
487
|
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
476
|
-
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
488
|
+
messages.push(...processUnitsFromItems(units, fileAttrs, groupSpace));
|
|
477
489
|
}
|
|
478
490
|
if (obj.group) {
|
|
479
491
|
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
480
|
-
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
492
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs, groupSpace));
|
|
481
493
|
}
|
|
482
494
|
}
|
|
483
495
|
return messages;
|
|
484
496
|
}
|
|
485
|
-
function processUnit(unit, fileAttrs) {
|
|
497
|
+
function processUnit(unit, fileAttrs, inheritedSpace) {
|
|
498
|
+
const space = resolveXmlSpace(unit, inheritedSpace);
|
|
486
499
|
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
487
500
|
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
488
501
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
@@ -504,14 +517,14 @@ function processUnit(unit, fileAttrs) {
|
|
|
504
517
|
}
|
|
505
518
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
506
519
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
507
|
-
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
520
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg, space)).join("");
|
|
508
521
|
let value;
|
|
509
522
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
510
523
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
511
524
|
)) {
|
|
512
525
|
const importSegments = segments.map((seg) => ({
|
|
513
526
|
caseAttr: seg["@_pgs:case"],
|
|
514
|
-
body: extractSegmentText(seg)
|
|
527
|
+
body: extractSegmentText(seg, space)
|
|
515
528
|
}));
|
|
516
529
|
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
517
530
|
value = rebuilt ?? joinSegmentTexts();
|
|
@@ -530,7 +543,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
530
543
|
notes
|
|
531
544
|
};
|
|
532
545
|
}
|
|
533
|
-
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
546
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales, xliffSpace = "default") {
|
|
534
547
|
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
535
548
|
if (!trgLang) return null;
|
|
536
549
|
if (!targetLocales.includes(trgLang)) return null;
|
|
@@ -560,14 +573,17 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
560
573
|
trgDir,
|
|
561
574
|
translate
|
|
562
575
|
};
|
|
576
|
+
const fileSpace = resolveXmlSpace(fileEl, xliffSpace);
|
|
563
577
|
const allMessages = [
|
|
564
578
|
...processUnitsFromItems(
|
|
565
579
|
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
566
|
-
fileAttrs
|
|
580
|
+
fileAttrs,
|
|
581
|
+
fileSpace
|
|
567
582
|
),
|
|
568
583
|
...processGroupsForMessages(
|
|
569
584
|
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
570
|
-
fileAttrs
|
|
585
|
+
fileAttrs,
|
|
586
|
+
fileSpace
|
|
571
587
|
)
|
|
572
588
|
];
|
|
573
589
|
const resourceData = {
|
|
@@ -601,13 +617,15 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
601
617
|
if (!xliffTrgLang) return null;
|
|
602
618
|
const fileEls = xliffRoot.file;
|
|
603
619
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
620
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
604
621
|
const resources = [];
|
|
605
622
|
for (const fileEl of files) {
|
|
606
623
|
const resource = extractResourceFromXliffFile(
|
|
607
624
|
fileEl,
|
|
608
625
|
xliffTrgLang,
|
|
609
626
|
project,
|
|
610
|
-
targetLocales
|
|
627
|
+
targetLocales,
|
|
628
|
+
xliffSpace
|
|
611
629
|
);
|
|
612
630
|
if (resource) {
|
|
613
631
|
const json = resource.toJSON(true);
|
|
@@ -67,19 +67,22 @@ declare function importMsgProject(projectsDir: string, projectName: string): Pro
|
|
|
67
67
|
* @throws Error if XML is malformed
|
|
68
68
|
*/
|
|
69
69
|
declare function parseXliff20(xml: string): object;
|
|
70
|
+
/** XLIFF `xml:space` value: `preserve` keeps all whitespace; `default` lets the importer trim. */
|
|
71
|
+
type XmlSpace = "default" | "preserve";
|
|
70
72
|
/**
|
|
71
73
|
* Extracts MsgResource from parsed XLIFF 2.x object for a single file element.
|
|
72
74
|
* @param fileEl - Parsed file element
|
|
73
75
|
* @param xliffTrgLang - trgLang from xliff root (may be undefined for monolingual)
|
|
74
76
|
* @param project - MsgProject instance
|
|
75
77
|
* @param targetLocales - Array of supported target locale keys
|
|
78
|
+
* @param xliffSpace - `xml:space` from the xliff root, inherited by the file (default `default`)
|
|
76
79
|
* @returns MsgResource or null if file should be skipped (monolingual or unsupported locale)
|
|
77
80
|
*/
|
|
78
81
|
declare function extractResourceFromXliffFile(fileEl: Record<string, unknown>, xliffTrgLang: string | undefined, project: {
|
|
79
82
|
locales: {
|
|
80
83
|
targetLocales: Record<string, string[]>;
|
|
81
84
|
};
|
|
82
|
-
}, targetLocales: string[]): MsgResource | null;
|
|
85
|
+
}, targetLocales: string[], xliffSpace?: XmlSpace): MsgResource | null;
|
|
83
86
|
/** Result of processing one XLIFF file: project, locale, and resources to write. */
|
|
84
87
|
interface ImportResult {
|
|
85
88
|
project: string;
|
|
@@ -105,4 +108,4 @@ declare function processXliffFile(xliffPath: string, projectsDir: string, projec
|
|
|
105
108
|
*/
|
|
106
109
|
declare function writeTranslationFiles(translationsDir: string, result: ImportResult): Promise<void>;
|
|
107
110
|
|
|
108
|
-
export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|
|
111
|
+
export { type ImportResult, type XmlSpace, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|
|
@@ -67,19 +67,22 @@ declare function importMsgProject(projectsDir: string, projectName: string): Pro
|
|
|
67
67
|
* @throws Error if XML is malformed
|
|
68
68
|
*/
|
|
69
69
|
declare function parseXliff20(xml: string): object;
|
|
70
|
+
/** XLIFF `xml:space` value: `preserve` keeps all whitespace; `default` lets the importer trim. */
|
|
71
|
+
type XmlSpace = "default" | "preserve";
|
|
70
72
|
/**
|
|
71
73
|
* Extracts MsgResource from parsed XLIFF 2.x object for a single file element.
|
|
72
74
|
* @param fileEl - Parsed file element
|
|
73
75
|
* @param xliffTrgLang - trgLang from xliff root (may be undefined for monolingual)
|
|
74
76
|
* @param project - MsgProject instance
|
|
75
77
|
* @param targetLocales - Array of supported target locale keys
|
|
78
|
+
* @param xliffSpace - `xml:space` from the xliff root, inherited by the file (default `default`)
|
|
76
79
|
* @returns MsgResource or null if file should be skipped (monolingual or unsupported locale)
|
|
77
80
|
*/
|
|
78
81
|
declare function extractResourceFromXliffFile(fileEl: Record<string, unknown>, xliffTrgLang: string | undefined, project: {
|
|
79
82
|
locales: {
|
|
80
83
|
targetLocales: Record<string, string[]>;
|
|
81
84
|
};
|
|
82
|
-
}, targetLocales: string[]): MsgResource | null;
|
|
85
|
+
}, targetLocales: string[], xliffSpace?: XmlSpace): MsgResource | null;
|
|
83
86
|
/** Result of processing one XLIFF file: project, locale, and resources to write. */
|
|
84
87
|
interface ImportResult {
|
|
85
88
|
project: string;
|
|
@@ -105,4 +108,4 @@ declare function processXliffFile(xliffPath: string, projectsDir: string, projec
|
|
|
105
108
|
*/
|
|
106
109
|
declare function writeTranslationFiles(translationsDir: string, result: ImportResult): Promise<void>;
|
|
107
110
|
|
|
108
|
-
export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|
|
111
|
+
export { type ImportResult, type XmlSpace, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|