@worldware/msg-cli 0.6.1 → 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-3MCULZ7L.mjs → chunk-KOTPQTLG.mjs} +76 -36
- package/dist/{chunk-PQJA3H4Z.mjs → chunk-QMSV2PP2.mjs} +1 -1
- package/dist/{chunk-WGI2EFOS.mjs → chunk-XH4RUUAJ.mjs} +10 -3
- package/dist/{chunk-HH6ZOHO4.mjs → chunk-Z7H6FUD6.mjs} +2 -6
- package/dist/commands/export.cjs +9 -4
- package/dist/commands/export.mjs +3 -3
- package/dist/commands/import.cjs +76 -38
- package/dist/commands/import.mjs +3 -3
- package/dist/lib/export-helpers.cjs +9 -4
- package/dist/lib/export-helpers.mjs +3 -3
- package/dist/lib/import-helpers.cjs +75 -37
- package/dist/lib/import-helpers.d.mts +5 -2
- package/dist/lib/import-helpers.d.ts +5 -2
- package/dist/lib/import-helpers.mjs +3 -3
- package/dist/lib/pgs-mf1.cjs +1 -3
- package/dist/lib/pgs-mf1.mjs +2 -2
- package/dist/lib/pgs-mf2.cjs +2 -6
- package/dist/lib/pgs-mf2.d.mts +1 -1
- package/dist/lib/pgs-mf2.d.ts +1 -1
- package/dist/lib/pgs-mf2.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
|
```
|
|
@@ -3,10 +3,10 @@ import {
|
|
|
3
3
|
} from "./chunk-Q775ZGC5.mjs";
|
|
4
4
|
import {
|
|
5
5
|
pgsImportToMf1Message
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-QMSV2PP2.mjs";
|
|
7
7
|
import {
|
|
8
8
|
pgsImportToSelectMessage
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-Z7H6FUD6.mjs";
|
|
10
10
|
import {
|
|
11
11
|
dynamicImportFromUrl
|
|
12
12
|
} from "./chunk-KRQA4FRY.mjs";
|
|
@@ -89,7 +89,21 @@ async function importMsgProject(projectsDir, projectName) {
|
|
|
89
89
|
}
|
|
90
90
|
var xmlParser = new XMLParser({
|
|
91
91
|
ignoreAttributes: false,
|
|
92
|
-
attributeNamePrefix: "@_"
|
|
92
|
+
attributeNamePrefix: "@_",
|
|
93
|
+
// Keep segment content as raw XML: the default (non-order-preserving) mode
|
|
94
|
+
// groups mixed content by tag, scrambling text around inline elements.
|
|
95
|
+
// extractSegmentText re-parses it in document order.
|
|
96
|
+
stopNodes: ["*.source", "*.target"]
|
|
97
|
+
});
|
|
98
|
+
var inlineContentParser = new XMLParser({
|
|
99
|
+
ignoreAttributes: false,
|
|
100
|
+
attributeNamePrefix: "@_",
|
|
101
|
+
preserveOrder: true,
|
|
102
|
+
trimValues: 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
|
|
93
107
|
});
|
|
94
108
|
function parseXliff20(xml) {
|
|
95
109
|
try {
|
|
@@ -115,59 +129,80 @@ function categoryToNoteType(category) {
|
|
|
115
129
|
};
|
|
116
130
|
return map[lower] ?? category.toUpperCase();
|
|
117
131
|
}
|
|
118
|
-
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) {
|
|
119
140
|
if (segment == null) return "";
|
|
120
141
|
const seg = segment;
|
|
121
|
-
const
|
|
122
|
-
|
|
123
|
-
if (
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
return "";
|
|
142
|
+
const content = seg.target ?? seg.source;
|
|
143
|
+
const raw = content && typeof content === "object" ? content["#text"] : content;
|
|
144
|
+
if (typeof raw !== "string" || raw === "") return "";
|
|
145
|
+
const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
|
|
146
|
+
const text = extractInlineText(root.c);
|
|
147
|
+
return resolveXmlSpace(content, space) === "preserve" ? text : trimXmlWhitespace(text);
|
|
127
148
|
}
|
|
128
|
-
function
|
|
149
|
+
function codePointFromHex(hex) {
|
|
150
|
+
const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
|
|
151
|
+
return cp <= 1114111 ? String.fromCodePoint(cp) : "\uFFFD";
|
|
152
|
+
}
|
|
153
|
+
function extractInlineText(nodes) {
|
|
129
154
|
let text = "";
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
155
|
+
for (const node of nodes) {
|
|
156
|
+
if ("#text" in node) {
|
|
157
|
+
text += String(node["#text"]);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const tag = Object.keys(node).find((k) => k !== ":@");
|
|
161
|
+
const attrs = node[":@"] ?? {};
|
|
162
|
+
switch (tag) {
|
|
163
|
+
case "ph":
|
|
164
|
+
case "sc":
|
|
165
|
+
case "ec":
|
|
166
|
+
text += attrs["@_equiv"] ?? attrs["@_disp"] ?? "";
|
|
167
|
+
break;
|
|
168
|
+
case "cp":
|
|
169
|
+
text += codePointFromHex(attrs["@_hex"]);
|
|
170
|
+
break;
|
|
171
|
+
case "sm":
|
|
172
|
+
case "em":
|
|
173
|
+
break;
|
|
174
|
+
default:
|
|
175
|
+
if (tag) text += extractInlineText(node[tag]);
|
|
143
176
|
}
|
|
144
177
|
}
|
|
145
178
|
return text;
|
|
146
179
|
}
|
|
147
|
-
function processUnitsFromItems(units, fileAttrs) {
|
|
180
|
+
function processUnitsFromItems(units, fileAttrs, space) {
|
|
148
181
|
const messages = [];
|
|
149
182
|
for (const u of units) {
|
|
150
|
-
const msg = processUnit(u, fileAttrs);
|
|
183
|
+
const msg = processUnit(u, fileAttrs, space);
|
|
151
184
|
if (msg) messages.push(msg);
|
|
152
185
|
}
|
|
153
186
|
return messages;
|
|
154
187
|
}
|
|
155
|
-
function processGroupsForMessages(groups, fileAttrs) {
|
|
188
|
+
function processGroupsForMessages(groups, fileAttrs, space) {
|
|
156
189
|
const messages = [];
|
|
157
190
|
for (const item of groups) {
|
|
158
191
|
const obj = item;
|
|
192
|
+
const groupSpace = resolveXmlSpace(obj, space);
|
|
159
193
|
if (obj.unit) {
|
|
160
194
|
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
161
|
-
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
195
|
+
messages.push(...processUnitsFromItems(units, fileAttrs, groupSpace));
|
|
162
196
|
}
|
|
163
197
|
if (obj.group) {
|
|
164
198
|
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
165
|
-
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
199
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs, groupSpace));
|
|
166
200
|
}
|
|
167
201
|
}
|
|
168
202
|
return messages;
|
|
169
203
|
}
|
|
170
|
-
function processUnit(unit, fileAttrs) {
|
|
204
|
+
function processUnit(unit, fileAttrs, inheritedSpace) {
|
|
205
|
+
const space = resolveXmlSpace(unit, inheritedSpace);
|
|
171
206
|
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
172
207
|
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
173
208
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
@@ -189,14 +224,14 @@ function processUnit(unit, fileAttrs) {
|
|
|
189
224
|
}
|
|
190
225
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
191
226
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
192
|
-
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
227
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg, space)).join("");
|
|
193
228
|
let value;
|
|
194
229
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
195
230
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
196
231
|
)) {
|
|
197
232
|
const importSegments = segments.map((seg) => ({
|
|
198
233
|
caseAttr: seg["@_pgs:case"],
|
|
199
|
-
body: extractSegmentText(seg)
|
|
234
|
+
body: extractSegmentText(seg, space)
|
|
200
235
|
}));
|
|
201
236
|
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
202
237
|
value = rebuilt ?? joinSegmentTexts();
|
|
@@ -215,7 +250,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
215
250
|
notes
|
|
216
251
|
};
|
|
217
252
|
}
|
|
218
|
-
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
253
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales, xliffSpace = "default") {
|
|
219
254
|
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
220
255
|
if (!trgLang) return null;
|
|
221
256
|
if (!targetLocales.includes(trgLang)) return null;
|
|
@@ -245,14 +280,17 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
245
280
|
trgDir,
|
|
246
281
|
translate
|
|
247
282
|
};
|
|
283
|
+
const fileSpace = resolveXmlSpace(fileEl, xliffSpace);
|
|
248
284
|
const allMessages = [
|
|
249
285
|
...processUnitsFromItems(
|
|
250
286
|
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
251
|
-
fileAttrs
|
|
287
|
+
fileAttrs,
|
|
288
|
+
fileSpace
|
|
252
289
|
),
|
|
253
290
|
...processGroupsForMessages(
|
|
254
291
|
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
255
|
-
fileAttrs
|
|
292
|
+
fileAttrs,
|
|
293
|
+
fileSpace
|
|
256
294
|
)
|
|
257
295
|
];
|
|
258
296
|
const resourceData = {
|
|
@@ -286,13 +324,15 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
286
324
|
if (!xliffTrgLang) return null;
|
|
287
325
|
const fileEls = xliffRoot.file;
|
|
288
326
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
327
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
289
328
|
const resources = [];
|
|
290
329
|
for (const fileEl of files) {
|
|
291
330
|
const resource = extractResourceFromXliffFile(
|
|
292
331
|
fileEl,
|
|
293
332
|
xliffTrgLang,
|
|
294
333
|
project,
|
|
295
|
-
targetLocales
|
|
334
|
+
targetLocales,
|
|
335
|
+
xliffSpace
|
|
296
336
|
);
|
|
297
337
|
if (resource) {
|
|
298
338
|
const json = resource.toJSON(true);
|
|
@@ -4,10 +4,10 @@ import {
|
|
|
4
4
|
} from "./chunk-Q775ZGC5.mjs";
|
|
5
5
|
import {
|
|
6
6
|
mf1MessageToPgsExport
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-QMSV2PP2.mjs";
|
|
8
8
|
import {
|
|
9
9
|
selectMessageToPgsExport
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-Z7H6FUD6.mjs";
|
|
11
11
|
import {
|
|
12
12
|
dynamicImportFromUrl
|
|
13
13
|
} from "./chunk-KRQA4FRY.mjs";
|
|
@@ -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) {
|
|
@@ -39,9 +39,7 @@ function getFunctionName(expr) {
|
|
|
39
39
|
return expr.functionRef?.name;
|
|
40
40
|
}
|
|
41
41
|
function getOption(opts, key) {
|
|
42
|
-
|
|
43
|
-
if (opts instanceof Map) return opts.get(key);
|
|
44
|
-
return opts[key];
|
|
42
|
+
return opts?.[key];
|
|
45
43
|
}
|
|
46
44
|
function getSelectOptionOrdinal(expr) {
|
|
47
45
|
const sel = getOption(expr.functionRef?.options, "select");
|
|
@@ -183,9 +181,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
183
181
|
}
|
|
184
182
|
});
|
|
185
183
|
} else if (kind === "ordinal") {
|
|
186
|
-
const opts =
|
|
187
|
-
["select", { type: "literal", value: "ordinal" }]
|
|
188
|
-
]);
|
|
184
|
+
const opts = { select: { type: "literal", value: "ordinal" } };
|
|
189
185
|
declarations.push({
|
|
190
186
|
type: "input",
|
|
191
187
|
name,
|
package/dist/commands/export.cjs
CHANGED
|
@@ -171,9 +171,7 @@ function getFunctionName(expr) {
|
|
|
171
171
|
return expr.functionRef?.name;
|
|
172
172
|
}
|
|
173
173
|
function getOption(opts, key) {
|
|
174
|
-
|
|
175
|
-
if (opts instanceof Map) return opts.get(key);
|
|
176
|
-
return opts[key];
|
|
174
|
+
return opts?.[key];
|
|
177
175
|
}
|
|
178
176
|
function getSelectOptionOrdinal(expr) {
|
|
179
177
|
const sel = getOption(expr.functionRef?.options, "select");
|
|
@@ -412,7 +410,10 @@ function filterResourceGroupsByProject(groups, projectName) {
|
|
|
412
410
|
return groups.filter((g) => g.project === projectName);
|
|
413
411
|
}
|
|
414
412
|
function escapeXml(s) {
|
|
415
|
-
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);
|
|
416
417
|
}
|
|
417
418
|
function sanitizeForXmlId(s) {
|
|
418
419
|
const sanitized = s.replace(/[^a-zA-Z0-9._:-]/g, "_").replace(/_+/g, "_");
|
|
@@ -506,6 +507,10 @@ function resourceGroupToXliff22(group) {
|
|
|
506
507
|
if (pgsExport) {
|
|
507
508
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
508
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
|
+
}
|
|
509
514
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
510
515
|
const msgNotes = msg.notes ?? [];
|
|
511
516
|
if (msgNotes.length > 0) {
|
package/dist/commands/export.mjs
CHANGED
|
@@ -5,10 +5,10 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-XH4RUUAJ.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-QMSV2PP2.mjs";
|
|
11
|
+
import "../chunk-Z7H6FUD6.mjs";
|
|
12
12
|
import "../chunk-KRQA4FRY.mjs";
|
|
13
13
|
import {
|
|
14
14
|
loadPackageJsonForMsg
|
package/dist/commands/import.cjs
CHANGED
|
@@ -200,9 +200,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
200
200
|
}
|
|
201
201
|
});
|
|
202
202
|
} else if (kind === "ordinal") {
|
|
203
|
-
const opts =
|
|
204
|
-
["select", { type: "literal", value: "ordinal" }]
|
|
205
|
-
]);
|
|
203
|
+
const opts = { select: { type: "literal", value: "ordinal" } };
|
|
206
204
|
declarations.push({
|
|
207
205
|
type: "input",
|
|
208
206
|
name,
|
|
@@ -443,7 +441,21 @@ async function importMsgProject(projectsDir, projectName) {
|
|
|
443
441
|
}
|
|
444
442
|
var xmlParser = new import_fast_xml_parser.XMLParser({
|
|
445
443
|
ignoreAttributes: false,
|
|
446
|
-
attributeNamePrefix: "@_"
|
|
444
|
+
attributeNamePrefix: "@_",
|
|
445
|
+
// Keep segment content as raw XML: the default (non-order-preserving) mode
|
|
446
|
+
// groups mixed content by tag, scrambling text around inline elements.
|
|
447
|
+
// extractSegmentText re-parses it in document order.
|
|
448
|
+
stopNodes: ["*.source", "*.target"]
|
|
449
|
+
});
|
|
450
|
+
var inlineContentParser = new import_fast_xml_parser.XMLParser({
|
|
451
|
+
ignoreAttributes: false,
|
|
452
|
+
attributeNamePrefix: "@_",
|
|
453
|
+
preserveOrder: true,
|
|
454
|
+
trimValues: 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
|
|
447
459
|
});
|
|
448
460
|
function parseXliff20(xml) {
|
|
449
461
|
try {
|
|
@@ -469,59 +481,80 @@ function categoryToNoteType(category) {
|
|
|
469
481
|
};
|
|
470
482
|
return map[lower] ?? category.toUpperCase();
|
|
471
483
|
}
|
|
472
|
-
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) {
|
|
473
492
|
if (segment == null) return "";
|
|
474
493
|
const seg = segment;
|
|
475
|
-
const
|
|
476
|
-
|
|
477
|
-
if (
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
return "";
|
|
481
|
-
}
|
|
482
|
-
function
|
|
494
|
+
const content = seg.target ?? seg.source;
|
|
495
|
+
const raw = content && typeof content === "object" ? content["#text"] : content;
|
|
496
|
+
if (typeof raw !== "string" || raw === "") return "";
|
|
497
|
+
const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
|
|
498
|
+
const text = extractInlineText(root.c);
|
|
499
|
+
return resolveXmlSpace(content, space) === "preserve" ? text : trimXmlWhitespace(text);
|
|
500
|
+
}
|
|
501
|
+
function codePointFromHex(hex) {
|
|
502
|
+
const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
|
|
503
|
+
return cp <= 1114111 ? String.fromCodePoint(cp) : "\uFFFD";
|
|
504
|
+
}
|
|
505
|
+
function extractInlineText(nodes) {
|
|
483
506
|
let text = "";
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
507
|
+
for (const node of nodes) {
|
|
508
|
+
if ("#text" in node) {
|
|
509
|
+
text += String(node["#text"]);
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
512
|
+
const tag = Object.keys(node).find((k) => k !== ":@");
|
|
513
|
+
const attrs = node[":@"] ?? {};
|
|
514
|
+
switch (tag) {
|
|
515
|
+
case "ph":
|
|
516
|
+
case "sc":
|
|
517
|
+
case "ec":
|
|
518
|
+
text += attrs["@_equiv"] ?? attrs["@_disp"] ?? "";
|
|
519
|
+
break;
|
|
520
|
+
case "cp":
|
|
521
|
+
text += codePointFromHex(attrs["@_hex"]);
|
|
522
|
+
break;
|
|
523
|
+
case "sm":
|
|
524
|
+
case "em":
|
|
525
|
+
break;
|
|
526
|
+
default:
|
|
527
|
+
if (tag) text += extractInlineText(node[tag]);
|
|
497
528
|
}
|
|
498
529
|
}
|
|
499
530
|
return text;
|
|
500
531
|
}
|
|
501
|
-
function processUnitsFromItems(units, fileAttrs) {
|
|
532
|
+
function processUnitsFromItems(units, fileAttrs, space) {
|
|
502
533
|
const messages = [];
|
|
503
534
|
for (const u of units) {
|
|
504
|
-
const msg = processUnit(u, fileAttrs);
|
|
535
|
+
const msg = processUnit(u, fileAttrs, space);
|
|
505
536
|
if (msg) messages.push(msg);
|
|
506
537
|
}
|
|
507
538
|
return messages;
|
|
508
539
|
}
|
|
509
|
-
function processGroupsForMessages(groups, fileAttrs) {
|
|
540
|
+
function processGroupsForMessages(groups, fileAttrs, space) {
|
|
510
541
|
const messages = [];
|
|
511
542
|
for (const item of groups) {
|
|
512
543
|
const obj = item;
|
|
544
|
+
const groupSpace = resolveXmlSpace(obj, space);
|
|
513
545
|
if (obj.unit) {
|
|
514
546
|
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
515
|
-
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
547
|
+
messages.push(...processUnitsFromItems(units, fileAttrs, groupSpace));
|
|
516
548
|
}
|
|
517
549
|
if (obj.group) {
|
|
518
550
|
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
519
|
-
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
551
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs, groupSpace));
|
|
520
552
|
}
|
|
521
553
|
}
|
|
522
554
|
return messages;
|
|
523
555
|
}
|
|
524
|
-
function processUnit(unit, fileAttrs) {
|
|
556
|
+
function processUnit(unit, fileAttrs, inheritedSpace) {
|
|
557
|
+
const space = resolveXmlSpace(unit, inheritedSpace);
|
|
525
558
|
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
526
559
|
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
527
560
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
@@ -543,14 +576,14 @@ function processUnit(unit, fileAttrs) {
|
|
|
543
576
|
}
|
|
544
577
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
545
578
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
546
|
-
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
579
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg, space)).join("");
|
|
547
580
|
let value;
|
|
548
581
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
549
582
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
550
583
|
)) {
|
|
551
584
|
const importSegments = segments.map((seg) => ({
|
|
552
585
|
caseAttr: seg["@_pgs:case"],
|
|
553
|
-
body: extractSegmentText(seg)
|
|
586
|
+
body: extractSegmentText(seg, space)
|
|
554
587
|
}));
|
|
555
588
|
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
556
589
|
value = rebuilt ?? joinSegmentTexts();
|
|
@@ -569,7 +602,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
569
602
|
notes
|
|
570
603
|
};
|
|
571
604
|
}
|
|
572
|
-
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
605
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales, xliffSpace = "default") {
|
|
573
606
|
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
574
607
|
if (!trgLang) return null;
|
|
575
608
|
if (!targetLocales.includes(trgLang)) return null;
|
|
@@ -599,14 +632,17 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
599
632
|
trgDir,
|
|
600
633
|
translate
|
|
601
634
|
};
|
|
635
|
+
const fileSpace = resolveXmlSpace(fileEl, xliffSpace);
|
|
602
636
|
const allMessages = [
|
|
603
637
|
...processUnitsFromItems(
|
|
604
638
|
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
605
|
-
fileAttrs
|
|
639
|
+
fileAttrs,
|
|
640
|
+
fileSpace
|
|
606
641
|
),
|
|
607
642
|
...processGroupsForMessages(
|
|
608
643
|
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
609
|
-
fileAttrs
|
|
644
|
+
fileAttrs,
|
|
645
|
+
fileSpace
|
|
610
646
|
)
|
|
611
647
|
];
|
|
612
648
|
const resourceData = {
|
|
@@ -640,13 +676,15 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
640
676
|
if (!xliffTrgLang) return null;
|
|
641
677
|
const fileEls = xliffRoot.file;
|
|
642
678
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
679
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
643
680
|
const resources = [];
|
|
644
681
|
for (const fileEl of files) {
|
|
645
682
|
const resource = extractResourceFromXliffFile(
|
|
646
683
|
fileEl,
|
|
647
684
|
xliffTrgLang,
|
|
648
685
|
project,
|
|
649
|
-
targetLocales
|
|
686
|
+
targetLocales,
|
|
687
|
+
xliffSpace
|
|
650
688
|
);
|
|
651
689
|
if (resource) {
|
|
652
690
|
const json = resource.toJSON(true);
|
package/dist/commands/import.mjs
CHANGED
|
@@ -5,10 +5,10 @@ import {
|
|
|
5
5
|
parseXliffFilename,
|
|
6
6
|
processXliffFile,
|
|
7
7
|
writeTranslationFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-KOTPQTLG.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-QMSV2PP2.mjs";
|
|
11
|
+
import "../chunk-Z7H6FUD6.mjs";
|
|
12
12
|
import "../chunk-KRQA4FRY.mjs";
|
|
13
13
|
import {
|
|
14
14
|
loadPackageJsonForMsg
|
|
@@ -108,9 +108,7 @@ function getFunctionName(expr) {
|
|
|
108
108
|
return expr.functionRef?.name;
|
|
109
109
|
}
|
|
110
110
|
function getOption(opts, key) {
|
|
111
|
-
|
|
112
|
-
if (opts instanceof Map) return opts.get(key);
|
|
113
|
-
return opts[key];
|
|
111
|
+
return opts?.[key];
|
|
114
112
|
}
|
|
115
113
|
function getSelectOptionOrdinal(expr) {
|
|
116
114
|
const sel = getOption(expr.functionRef?.options, "select");
|
|
@@ -349,7 +347,10 @@ function filterResourceGroupsByProject(groups, projectName) {
|
|
|
349
347
|
return groups.filter((g) => g.project === projectName);
|
|
350
348
|
}
|
|
351
349
|
function escapeXml(s) {
|
|
352
|
-
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);
|
|
353
354
|
}
|
|
354
355
|
function sanitizeForXmlId(s) {
|
|
355
356
|
const sanitized = s.replace(/[^a-zA-Z0-9._:-]/g, "_").replace(/_+/g, "_");
|
|
@@ -443,6 +444,10 @@ function resourceGroupToXliff22(group) {
|
|
|
443
444
|
if (pgsExport) {
|
|
444
445
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
445
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
|
+
}
|
|
446
451
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
447
452
|
const msgNotes = msg.notes ?? [];
|
|
448
453
|
if (msgNotes.length > 0) {
|
|
@@ -5,10 +5,10 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-XH4RUUAJ.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-QMSV2PP2.mjs";
|
|
11
|
+
import "../chunk-Z7H6FUD6.mjs";
|
|
12
12
|
import "../chunk-KRQA4FRY.mjs";
|
|
13
13
|
import "../chunk-F7XZRKP7.mjs";
|
|
14
14
|
export {
|
|
@@ -141,9 +141,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
141
141
|
}
|
|
142
142
|
});
|
|
143
143
|
} else if (kind === "ordinal") {
|
|
144
|
-
const opts =
|
|
145
|
-
["select", { type: "literal", value: "ordinal" }]
|
|
146
|
-
]);
|
|
144
|
+
const opts = { select: { type: "literal", value: "ordinal" } };
|
|
147
145
|
declarations.push({
|
|
148
146
|
type: "input",
|
|
149
147
|
name,
|
|
@@ -384,7 +382,21 @@ async function importMsgProject(projectsDir, projectName) {
|
|
|
384
382
|
}
|
|
385
383
|
var xmlParser = new import_fast_xml_parser.XMLParser({
|
|
386
384
|
ignoreAttributes: false,
|
|
387
|
-
attributeNamePrefix: "@_"
|
|
385
|
+
attributeNamePrefix: "@_",
|
|
386
|
+
// Keep segment content as raw XML: the default (non-order-preserving) mode
|
|
387
|
+
// groups mixed content by tag, scrambling text around inline elements.
|
|
388
|
+
// extractSegmentText re-parses it in document order.
|
|
389
|
+
stopNodes: ["*.source", "*.target"]
|
|
390
|
+
});
|
|
391
|
+
var inlineContentParser = new import_fast_xml_parser.XMLParser({
|
|
392
|
+
ignoreAttributes: false,
|
|
393
|
+
attributeNamePrefix: "@_",
|
|
394
|
+
preserveOrder: true,
|
|
395
|
+
trimValues: 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
|
|
388
400
|
});
|
|
389
401
|
function parseXliff20(xml) {
|
|
390
402
|
try {
|
|
@@ -410,59 +422,80 @@ function categoryToNoteType(category) {
|
|
|
410
422
|
};
|
|
411
423
|
return map[lower] ?? category.toUpperCase();
|
|
412
424
|
}
|
|
413
|
-
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) {
|
|
414
433
|
if (segment == null) return "";
|
|
415
434
|
const seg = segment;
|
|
416
|
-
const
|
|
417
|
-
|
|
418
|
-
if (
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
return "";
|
|
435
|
+
const content = seg.target ?? seg.source;
|
|
436
|
+
const raw = content && typeof content === "object" ? content["#text"] : content;
|
|
437
|
+
if (typeof raw !== "string" || raw === "") return "";
|
|
438
|
+
const [root] = inlineContentParser.parse(`<c>${raw}</c>`);
|
|
439
|
+
const text = extractInlineText(root.c);
|
|
440
|
+
return resolveXmlSpace(content, space) === "preserve" ? text : trimXmlWhitespace(text);
|
|
422
441
|
}
|
|
423
|
-
function
|
|
442
|
+
function codePointFromHex(hex) {
|
|
443
|
+
const cp = /^[0-9a-f]{1,6}$/i.test(hex ?? "") ? parseInt(hex, 16) : NaN;
|
|
444
|
+
return cp <= 1114111 ? String.fromCodePoint(cp) : "\uFFFD";
|
|
445
|
+
}
|
|
446
|
+
function extractInlineText(nodes) {
|
|
424
447
|
let text = "";
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
const
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
448
|
+
for (const node of nodes) {
|
|
449
|
+
if ("#text" in node) {
|
|
450
|
+
text += String(node["#text"]);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const tag = Object.keys(node).find((k) => k !== ":@");
|
|
454
|
+
const attrs = node[":@"] ?? {};
|
|
455
|
+
switch (tag) {
|
|
456
|
+
case "ph":
|
|
457
|
+
case "sc":
|
|
458
|
+
case "ec":
|
|
459
|
+
text += attrs["@_equiv"] ?? attrs["@_disp"] ?? "";
|
|
460
|
+
break;
|
|
461
|
+
case "cp":
|
|
462
|
+
text += codePointFromHex(attrs["@_hex"]);
|
|
463
|
+
break;
|
|
464
|
+
case "sm":
|
|
465
|
+
case "em":
|
|
466
|
+
break;
|
|
467
|
+
default:
|
|
468
|
+
if (tag) text += extractInlineText(node[tag]);
|
|
438
469
|
}
|
|
439
470
|
}
|
|
440
471
|
return text;
|
|
441
472
|
}
|
|
442
|
-
function processUnitsFromItems(units, fileAttrs) {
|
|
473
|
+
function processUnitsFromItems(units, fileAttrs, space) {
|
|
443
474
|
const messages = [];
|
|
444
475
|
for (const u of units) {
|
|
445
|
-
const msg = processUnit(u, fileAttrs);
|
|
476
|
+
const msg = processUnit(u, fileAttrs, space);
|
|
446
477
|
if (msg) messages.push(msg);
|
|
447
478
|
}
|
|
448
479
|
return messages;
|
|
449
480
|
}
|
|
450
|
-
function processGroupsForMessages(groups, fileAttrs) {
|
|
481
|
+
function processGroupsForMessages(groups, fileAttrs, space) {
|
|
451
482
|
const messages = [];
|
|
452
483
|
for (const item of groups) {
|
|
453
484
|
const obj = item;
|
|
485
|
+
const groupSpace = resolveXmlSpace(obj, space);
|
|
454
486
|
if (obj.unit) {
|
|
455
487
|
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
456
|
-
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
488
|
+
messages.push(...processUnitsFromItems(units, fileAttrs, groupSpace));
|
|
457
489
|
}
|
|
458
490
|
if (obj.group) {
|
|
459
491
|
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
460
|
-
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
492
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs, groupSpace));
|
|
461
493
|
}
|
|
462
494
|
}
|
|
463
495
|
return messages;
|
|
464
496
|
}
|
|
465
|
-
function processUnit(unit, fileAttrs) {
|
|
497
|
+
function processUnit(unit, fileAttrs, inheritedSpace) {
|
|
498
|
+
const space = resolveXmlSpace(unit, inheritedSpace);
|
|
466
499
|
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
467
500
|
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
468
501
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
@@ -484,14 +517,14 @@ function processUnit(unit, fileAttrs) {
|
|
|
484
517
|
}
|
|
485
518
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
486
519
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
487
|
-
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
520
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg, space)).join("");
|
|
488
521
|
let value;
|
|
489
522
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
490
523
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
491
524
|
)) {
|
|
492
525
|
const importSegments = segments.map((seg) => ({
|
|
493
526
|
caseAttr: seg["@_pgs:case"],
|
|
494
|
-
body: extractSegmentText(seg)
|
|
527
|
+
body: extractSegmentText(seg, space)
|
|
495
528
|
}));
|
|
496
529
|
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
497
530
|
value = rebuilt ?? joinSegmentTexts();
|
|
@@ -510,7 +543,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
510
543
|
notes
|
|
511
544
|
};
|
|
512
545
|
}
|
|
513
|
-
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
546
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales, xliffSpace = "default") {
|
|
514
547
|
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
515
548
|
if (!trgLang) return null;
|
|
516
549
|
if (!targetLocales.includes(trgLang)) return null;
|
|
@@ -540,14 +573,17 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
540
573
|
trgDir,
|
|
541
574
|
translate
|
|
542
575
|
};
|
|
576
|
+
const fileSpace = resolveXmlSpace(fileEl, xliffSpace);
|
|
543
577
|
const allMessages = [
|
|
544
578
|
...processUnitsFromItems(
|
|
545
579
|
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
546
|
-
fileAttrs
|
|
580
|
+
fileAttrs,
|
|
581
|
+
fileSpace
|
|
547
582
|
),
|
|
548
583
|
...processGroupsForMessages(
|
|
549
584
|
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
550
|
-
fileAttrs
|
|
585
|
+
fileAttrs,
|
|
586
|
+
fileSpace
|
|
551
587
|
)
|
|
552
588
|
];
|
|
553
589
|
const resourceData = {
|
|
@@ -581,13 +617,15 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
581
617
|
if (!xliffTrgLang) return null;
|
|
582
618
|
const fileEls = xliffRoot.file;
|
|
583
619
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
620
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
584
621
|
const resources = [];
|
|
585
622
|
for (const fileEl of files) {
|
|
586
623
|
const resource = extractResourceFromXliffFile(
|
|
587
624
|
fileEl,
|
|
588
625
|
xliffTrgLang,
|
|
589
626
|
project,
|
|
590
|
-
targetLocales
|
|
627
|
+
targetLocales,
|
|
628
|
+
xliffSpace
|
|
591
629
|
);
|
|
592
630
|
if (resource) {
|
|
593
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 };
|
|
@@ -9,10 +9,10 @@ import {
|
|
|
9
9
|
processXliffFile,
|
|
10
10
|
writeTranslationFiles,
|
|
11
11
|
xliffDirSegments
|
|
12
|
-
} from "../chunk-
|
|
12
|
+
} from "../chunk-KOTPQTLG.mjs";
|
|
13
13
|
import "../chunk-Q775ZGC5.mjs";
|
|
14
|
-
import "../chunk-
|
|
15
|
-
import "../chunk-
|
|
14
|
+
import "../chunk-QMSV2PP2.mjs";
|
|
15
|
+
import "../chunk-Z7H6FUD6.mjs";
|
|
16
16
|
import "../chunk-KRQA4FRY.mjs";
|
|
17
17
|
import "../chunk-F7XZRKP7.mjs";
|
|
18
18
|
export {
|
package/dist/lib/pgs-mf1.cjs
CHANGED
|
@@ -62,9 +62,7 @@ function getFunctionName(expr) {
|
|
|
62
62
|
return expr.functionRef?.name;
|
|
63
63
|
}
|
|
64
64
|
function getOption(opts, key) {
|
|
65
|
-
|
|
66
|
-
if (opts instanceof Map) return opts.get(key);
|
|
67
|
-
return opts[key];
|
|
65
|
+
return opts?.[key];
|
|
68
66
|
}
|
|
69
67
|
function getSelectOptionOrdinal(expr) {
|
|
70
68
|
const sel = getOption(expr.functionRef?.options, "select");
|
package/dist/lib/pgs-mf1.mjs
CHANGED
package/dist/lib/pgs-mf2.cjs
CHANGED
|
@@ -63,9 +63,7 @@ function getFunctionName(expr) {
|
|
|
63
63
|
return expr.functionRef?.name;
|
|
64
64
|
}
|
|
65
65
|
function getOption(opts, key) {
|
|
66
|
-
|
|
67
|
-
if (opts instanceof Map) return opts.get(key);
|
|
68
|
-
return opts[key];
|
|
66
|
+
return opts?.[key];
|
|
69
67
|
}
|
|
70
68
|
function getSelectOptionOrdinal(expr) {
|
|
71
69
|
const sel = getOption(expr.functionRef?.options, "select");
|
|
@@ -207,9 +205,7 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
207
205
|
}
|
|
208
206
|
});
|
|
209
207
|
} else if (kind === "ordinal") {
|
|
210
|
-
const opts =
|
|
211
|
-
["select", { type: "literal", value: "ordinal" }]
|
|
212
|
-
]);
|
|
208
|
+
const opts = { select: { type: "literal", value: "ordinal" } };
|
|
213
209
|
declarations.push({
|
|
214
210
|
type: "input",
|
|
215
211
|
name,
|
package/dist/lib/pgs-mf2.d.mts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* (`pgs:switch` / `pgs:case`, urn:oasis:names:tc:xliff:pgs:1.0).
|
|
4
4
|
*
|
|
5
5
|
* Ordinal vs cardinal :number uses the `select` option passed to PluralRules
|
|
6
|
-
* (`select=ordinal` in MF2 syntax → options
|
|
6
|
+
* (`select=ordinal` in MF2 syntax → options key `select`, literal `ordinal`).
|
|
7
7
|
*/
|
|
8
8
|
/** PGS selector keywords (XLIFF 2.2 Extended Part 2 §4.9). */
|
|
9
9
|
type PgsSelectorKind = "plural" | "ordinal" | "gender" | "select";
|
package/dist/lib/pgs-mf2.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* (`pgs:switch` / `pgs:case`, urn:oasis:names:tc:xliff:pgs:1.0).
|
|
4
4
|
*
|
|
5
5
|
* Ordinal vs cardinal :number uses the `select` option passed to PluralRules
|
|
6
|
-
* (`select=ordinal` in MF2 syntax → options
|
|
6
|
+
* (`select=ordinal` in MF2 syntax → options key `select`, literal `ordinal`).
|
|
7
7
|
*/
|
|
8
8
|
/** PGS selector keywords (XLIFF 2.2 Extended Part 2 §4.9). */
|
|
9
9
|
type PgsSelectorKind = "plural" | "ordinal" | "gender" | "select";
|
package/dist/lib/pgs-mf2.mjs
CHANGED