@worldware/msg-cli 0.6.2 → 0.6.4
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 +24 -6
- package/dist/{chunk-WOGVKE3R.mjs → chunk-A6XUN4LX.mjs} +50 -23
- 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 +53 -29
- package/dist/commands/import.mjs +4 -7
- package/dist/lib/export-helpers.cjs +8 -1
- package/dist/lib/export-helpers.mjs +1 -1
- package/dist/lib/import-helpers.cjs +50 -23
- package/dist/lib/import-helpers.d.mts +14 -6
- package/dist/lib/import-helpers.d.ts +14 -6
- 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,22 @@ 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
|
+
**Inline elements on import:** segment text is rebuilt in document order, keeping the text and spacing around inline elements (e.g. `Hello <pc id="1">bold</pc>!` → `Hello bold!`).
|
|
268
|
+
|
|
269
|
+
- `<pc>`, `<mrk>`, and unrecognized elements contribute their content (nested elements included).
|
|
270
|
+
- `<ph>`, `<sc>`, `<ec>` contribute their `equiv` attribute, else `disp`, else nothing (e.g. `<ph id="1" equiv="{name}"/>` → `{name}`).
|
|
271
|
+
- `<cp hex="…"/>` becomes that character, or U+FFFD if the hex value is not a valid code point.
|
|
272
|
+
- `<sm>` and `<em>` contribute nothing.
|
|
273
|
+
- CDATA text is kept in place.
|
|
274
|
+
|
|
275
|
+
**Whitespace on import:**
|
|
276
|
+
|
|
277
|
+
- `xml:space` is inherited per XLIFF 2: `<xliff>` → `<file>` → `<group>` → `<unit>` → `<source>`/`<target>`, defaulting to `default`. Invalid values are ignored.
|
|
278
|
+
- `preserve` — segment text is kept exactly, including leading/trailing whitespace and whitespace-only values.
|
|
279
|
+
- `default` — leading and trailing XML whitespace (space, tab, CR, LF) is trimmed; non-breaking and other Unicode spaces are kept.
|
|
280
|
+
- Character references such as ` ` and ` ` are decoded.
|
|
281
|
+
- Notes are not affected: import writes translation files without notes.
|
|
282
|
+
|
|
266
283
|
```bash
|
|
267
284
|
msg import [-p <projectName>] [-l <locale>]
|
|
268
285
|
```
|
|
@@ -291,14 +308,15 @@ msg import -l zh
|
|
|
291
308
|
**Behavior:**
|
|
292
309
|
|
|
293
310
|
- Recursively finds all `.xliff` and `.xlf` files under `l10n/xliff`.
|
|
294
|
-
-
|
|
295
|
-
-
|
|
296
|
-
-
|
|
311
|
+
- The project is taken from the filename (`myApp.zh.xliff` or `myApp.xliff` → `myApp`).
|
|
312
|
+
- The target locale is the file's `trgLang`, wherever the file sits under `l10n/xliff` (e.g. `l10n/xliff/zh/myApp.xliff`, `l10n/xliff/vendor/zh/myApp.xliff`, or `l10n/xliff/myApp.xliff`).
|
|
313
|
+
- A locale in the filename must match `trgLang` (case-insensitive). Otherwise the file is skipped with a warning, e.g. `Skipping …/myApp.fr.xliff: file name locale 'fr' does not match trgLang 'de'`. Directory names are not checked against `trgLang`.
|
|
314
|
+
- `--project` and `--language` match the filename or a directory under `l10n/xliff`. Directories above `l10n/xliff` are ignored, so a path like `/var/…` or `/tmp/…` never matches.
|
|
315
|
+
- Skips (with a warning) monolingual files (no `trgLang`), such as the `myApp.xliff` written by `msg export`.
|
|
297
316
|
- Dynamically imports `MsgProject` from `i18n/projects` to validate target locales.
|
|
298
|
-
- Skips
|
|
299
|
-
- Skips files whose target locale is not in the project's `targetLocales`.
|
|
317
|
+
- Skips files whose `trgLang` is not in the project's `targetLocales`.
|
|
300
318
|
- Skips files when the matching project file does not exist or cannot be loaded.
|
|
301
|
-
- Writes JSON to `l10n/translations/<project>/<
|
|
319
|
+
- Writes JSON to `l10n/translations/<project>/<trgLang>/<title>.json`.
|
|
302
320
|
- Preserves existing translation files for other projects or locales when filtering.
|
|
303
321
|
- Errors on malformed XLIFF and logs each step.
|
|
304
322
|
|
|
@@ -63,7 +63,7 @@ function parseXliffFilename(filePath, xliffDir) {
|
|
|
63
63
|
const match = filename.match(XLIFF_FILENAME_PATTERN);
|
|
64
64
|
const project = match?.[1] ?? basename(filename, extname(filename));
|
|
65
65
|
const locale = match?.[2] ?? xliffDirSegments(xliffDir, filePath).find((part) => LOCALE_PATTERN.test(part));
|
|
66
|
-
return { project, locale };
|
|
66
|
+
return { project, locale, filenameLocale: match?.[2] };
|
|
67
67
|
}
|
|
68
68
|
function isMsgProjectLike(value) {
|
|
69
69
|
if (!value || typeof value !== "object") return false;
|
|
@@ -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 = {
|
|
@@ -291,7 +307,22 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
291
307
|
const proj = project;
|
|
292
308
|
return MsgResource.create(resourceData, proj);
|
|
293
309
|
}
|
|
294
|
-
async function processXliffFile(xliffPath, projectsDir, projectName,
|
|
310
|
+
async function processXliffFile(xliffPath, projectsDir, projectName, filenameLocale, warn = () => {
|
|
311
|
+
}) {
|
|
312
|
+
const xml = await readFile(xliffPath, "utf-8");
|
|
313
|
+
const parsed = parseXliff20(xml);
|
|
314
|
+
const xliffRoot = parsed.xliff;
|
|
315
|
+
if (!xliffRoot) return null;
|
|
316
|
+
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
317
|
+
if (!xliffTrgLang) {
|
|
318
|
+
warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
if (filenameLocale && filenameLocale.toLowerCase() !== xliffTrgLang.toLowerCase()) {
|
|
322
|
+
warn(`Skipping ${xliffPath}: file name locale '${filenameLocale}' does not match trgLang '${xliffTrgLang}'`);
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
325
|
+
const locale = xliffTrgLang;
|
|
295
326
|
let project;
|
|
296
327
|
try {
|
|
297
328
|
project = await importMsgProject(projectsDir, projectName);
|
|
@@ -300,21 +331,17 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
300
331
|
}
|
|
301
332
|
const targetLocales = Object.keys(project.locales.targetLocales);
|
|
302
333
|
if (!targetLocales.includes(locale)) return null;
|
|
303
|
-
const xml = await readFile(xliffPath, "utf-8");
|
|
304
|
-
const parsed = parseXliff20(xml);
|
|
305
|
-
const xliffRoot = parsed.xliff;
|
|
306
|
-
if (!xliffRoot) return null;
|
|
307
|
-
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
308
|
-
if (!xliffTrgLang) return null;
|
|
309
334
|
const fileEls = xliffRoot.file;
|
|
310
335
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
336
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
311
337
|
const resources = [];
|
|
312
338
|
for (const fileEl of files) {
|
|
313
339
|
const resource = extractResourceFromXliffFile(
|
|
314
340
|
fileEl,
|
|
315
341
|
xliffTrgLang,
|
|
316
342
|
project,
|
|
317
|
-
targetLocales
|
|
343
|
+
targetLocales,
|
|
344
|
+
xliffSpace
|
|
318
345
|
);
|
|
319
346
|
if (resource) {
|
|
320
347
|
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
|
@@ -415,7 +415,7 @@ function parseXliffFilename(filePath, xliffDir) {
|
|
|
415
415
|
const match = filename.match(XLIFF_FILENAME_PATTERN);
|
|
416
416
|
const project = match?.[1] ?? (0, import_path2.basename)(filename, (0, import_path2.extname)(filename));
|
|
417
417
|
const locale = match?.[2] ?? xliffDirSegments(xliffDir, filePath).find((part) => LOCALE_PATTERN.test(part));
|
|
418
|
-
return { project, locale };
|
|
418
|
+
return { project, locale, filenameLocale: match?.[2] };
|
|
419
419
|
}
|
|
420
420
|
function isMsgProjectLike(value) {
|
|
421
421
|
if (!value || typeof value !== "object") return false;
|
|
@@ -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 = {
|
|
@@ -643,7 +659,22 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
643
659
|
const proj = project;
|
|
644
660
|
return import_msg2.MsgResource.create(resourceData, proj);
|
|
645
661
|
}
|
|
646
|
-
async function processXliffFile(xliffPath, projectsDir, projectName,
|
|
662
|
+
async function processXliffFile(xliffPath, projectsDir, projectName, filenameLocale, warn = () => {
|
|
663
|
+
}) {
|
|
664
|
+
const xml = await (0, import_promises.readFile)(xliffPath, "utf-8");
|
|
665
|
+
const parsed = parseXliff20(xml);
|
|
666
|
+
const xliffRoot = parsed.xliff;
|
|
667
|
+
if (!xliffRoot) return null;
|
|
668
|
+
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
669
|
+
if (!xliffTrgLang) {
|
|
670
|
+
warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
if (filenameLocale && filenameLocale.toLowerCase() !== xliffTrgLang.toLowerCase()) {
|
|
674
|
+
warn(`Skipping ${xliffPath}: file name locale '${filenameLocale}' does not match trgLang '${xliffTrgLang}'`);
|
|
675
|
+
return null;
|
|
676
|
+
}
|
|
677
|
+
const locale = xliffTrgLang;
|
|
647
678
|
let project;
|
|
648
679
|
try {
|
|
649
680
|
project = await importMsgProject(projectsDir, projectName);
|
|
@@ -652,21 +683,17 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
652
683
|
}
|
|
653
684
|
const targetLocales = Object.keys(project.locales.targetLocales);
|
|
654
685
|
if (!targetLocales.includes(locale)) return null;
|
|
655
|
-
const xml = await (0, import_promises.readFile)(xliffPath, "utf-8");
|
|
656
|
-
const parsed = parseXliff20(xml);
|
|
657
|
-
const xliffRoot = parsed.xliff;
|
|
658
|
-
if (!xliffRoot) return null;
|
|
659
|
-
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
660
|
-
if (!xliffTrgLang) return null;
|
|
661
686
|
const fileEls = xliffRoot.file;
|
|
662
687
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
688
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
663
689
|
const resources = [];
|
|
664
690
|
for (const fileEl of files) {
|
|
665
691
|
const resource = extractResourceFromXliffFile(
|
|
666
692
|
fileEl,
|
|
667
693
|
xliffTrgLang,
|
|
668
694
|
project,
|
|
669
|
-
targetLocales
|
|
695
|
+
targetLocales,
|
|
696
|
+
xliffSpace
|
|
670
697
|
);
|
|
671
698
|
if (resource) {
|
|
672
699
|
const json = resource.toJSON(true);
|
|
@@ -752,17 +779,14 @@ var Import = class _Import extends import_core.Command {
|
|
|
752
779
|
let wroteCount = 0;
|
|
753
780
|
let errorCount = 0;
|
|
754
781
|
for (const xliffPath of filePaths) {
|
|
755
|
-
const { project: projectName,
|
|
756
|
-
if (!locale) {
|
|
757
|
-
this.warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
758
|
-
continue;
|
|
759
|
-
}
|
|
782
|
+
const { project: projectName, filenameLocale } = parseXliffFilename(xliffPath, xliffDir);
|
|
760
783
|
try {
|
|
761
784
|
const result = await processXliffFile(
|
|
762
785
|
xliffPath,
|
|
763
786
|
projectsDir,
|
|
764
787
|
projectName,
|
|
765
|
-
|
|
788
|
+
filenameLocale,
|
|
789
|
+
(message) => this.warn(message)
|
|
766
790
|
);
|
|
767
791
|
if (result) {
|
|
768
792
|
await writeTranslationFiles(translationsDir, result);
|
package/dist/commands/import.mjs
CHANGED
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
parseXliffFilename,
|
|
6
6
|
processXliffFile,
|
|
7
7
|
writeTranslationFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-A6XUN4LX.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
10
|
import "../chunk-QMSV2PP2.mjs";
|
|
11
11
|
import "../chunk-Z7H6FUD6.mjs";
|
|
@@ -82,17 +82,14 @@ var Import = class _Import extends Command {
|
|
|
82
82
|
let wroteCount = 0;
|
|
83
83
|
let errorCount = 0;
|
|
84
84
|
for (const xliffPath of filePaths) {
|
|
85
|
-
const { project: projectName,
|
|
86
|
-
if (!locale) {
|
|
87
|
-
this.warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
88
|
-
continue;
|
|
89
|
-
}
|
|
85
|
+
const { project: projectName, filenameLocale } = parseXliffFilename(xliffPath, xliffDir);
|
|
90
86
|
try {
|
|
91
87
|
const result = await processXliffFile(
|
|
92
88
|
xliffPath,
|
|
93
89
|
projectsDir,
|
|
94
90
|
projectName,
|
|
95
|
-
|
|
91
|
+
filenameLocale,
|
|
92
|
+
(message) => this.warn(message)
|
|
96
93
|
);
|
|
97
94
|
if (result) {
|
|
98
95
|
await writeTranslationFiles(translationsDir, result);
|
|
@@ -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) {
|
|
@@ -356,7 +356,7 @@ function parseXliffFilename(filePath, xliffDir) {
|
|
|
356
356
|
const match = filename.match(XLIFF_FILENAME_PATTERN);
|
|
357
357
|
const project = match?.[1] ?? (0, import_path.basename)(filename, (0, import_path.extname)(filename));
|
|
358
358
|
const locale = match?.[2] ?? xliffDirSegments(xliffDir, filePath).find((part) => LOCALE_PATTERN.test(part));
|
|
359
|
-
return { project, locale };
|
|
359
|
+
return { project, locale, filenameLocale: match?.[2] };
|
|
360
360
|
}
|
|
361
361
|
function isMsgProjectLike(value) {
|
|
362
362
|
if (!value || typeof value !== "object") return false;
|
|
@@ -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 = {
|
|
@@ -584,7 +600,22 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
584
600
|
const proj = project;
|
|
585
601
|
return import_msg2.MsgResource.create(resourceData, proj);
|
|
586
602
|
}
|
|
587
|
-
async function processXliffFile(xliffPath, projectsDir, projectName,
|
|
603
|
+
async function processXliffFile(xliffPath, projectsDir, projectName, filenameLocale, warn = () => {
|
|
604
|
+
}) {
|
|
605
|
+
const xml = await (0, import_promises.readFile)(xliffPath, "utf-8");
|
|
606
|
+
const parsed = parseXliff20(xml);
|
|
607
|
+
const xliffRoot = parsed.xliff;
|
|
608
|
+
if (!xliffRoot) return null;
|
|
609
|
+
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
610
|
+
if (!xliffTrgLang) {
|
|
611
|
+
warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
612
|
+
return null;
|
|
613
|
+
}
|
|
614
|
+
if (filenameLocale && filenameLocale.toLowerCase() !== xliffTrgLang.toLowerCase()) {
|
|
615
|
+
warn(`Skipping ${xliffPath}: file name locale '${filenameLocale}' does not match trgLang '${xliffTrgLang}'`);
|
|
616
|
+
return null;
|
|
617
|
+
}
|
|
618
|
+
const locale = xliffTrgLang;
|
|
588
619
|
let project;
|
|
589
620
|
try {
|
|
590
621
|
project = await importMsgProject(projectsDir, projectName);
|
|
@@ -593,21 +624,17 @@ async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
|
593
624
|
}
|
|
594
625
|
const targetLocales = Object.keys(project.locales.targetLocales);
|
|
595
626
|
if (!targetLocales.includes(locale)) return null;
|
|
596
|
-
const xml = await (0, import_promises.readFile)(xliffPath, "utf-8");
|
|
597
|
-
const parsed = parseXliff20(xml);
|
|
598
|
-
const xliffRoot = parsed.xliff;
|
|
599
|
-
if (!xliffRoot) return null;
|
|
600
|
-
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
601
|
-
if (!xliffTrgLang) return null;
|
|
602
627
|
const fileEls = xliffRoot.file;
|
|
603
628
|
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
629
|
+
const xliffSpace = resolveXmlSpace(xliffRoot, "default");
|
|
604
630
|
const resources = [];
|
|
605
631
|
for (const fileEl of files) {
|
|
606
632
|
const resource = extractResourceFromXliffFile(
|
|
607
633
|
fileEl,
|
|
608
634
|
xliffTrgLang,
|
|
609
635
|
project,
|
|
610
|
-
targetLocales
|
|
636
|
+
targetLocales,
|
|
637
|
+
xliffSpace
|
|
611
638
|
);
|
|
612
639
|
if (resource) {
|
|
613
640
|
const json = resource.toJSON(true);
|
|
@@ -42,11 +42,14 @@ declare function filterXliffPathsByLocale(filePaths: string[], locale: string, x
|
|
|
42
42
|
* xliffDir may also indicate project/locale (e.g. xliff/zh/project.xliff).
|
|
43
43
|
* @param filePath - Absolute path to XLIFF file
|
|
44
44
|
* @param xliffDir - Absolute path to the xliff directory the file was found in
|
|
45
|
-
* @returns Object with project
|
|
45
|
+
* @returns Object with project, a path-based locale hint, and `filenameLocale`
|
|
46
|
+
* (the locale from the file name alone, `project.locale.xliff`). Import takes
|
|
47
|
+
* the locale from the file's `trgLang` and checks only `filenameLocale` against it.
|
|
46
48
|
*/
|
|
47
49
|
declare function parseXliffFilename(filePath: string, xliffDir: string): {
|
|
48
50
|
project: string;
|
|
49
51
|
locale?: string;
|
|
52
|
+
filenameLocale?: string;
|
|
50
53
|
};
|
|
51
54
|
/**
|
|
52
55
|
* Dynamically imports MsgProject from i18n/projects by project name.
|
|
@@ -67,19 +70,22 @@ declare function importMsgProject(projectsDir: string, projectName: string): Pro
|
|
|
67
70
|
* @throws Error if XML is malformed
|
|
68
71
|
*/
|
|
69
72
|
declare function parseXliff20(xml: string): object;
|
|
73
|
+
/** XLIFF `xml:space` value: `preserve` keeps all whitespace; `default` lets the importer trim. */
|
|
74
|
+
type XmlSpace = "default" | "preserve";
|
|
70
75
|
/**
|
|
71
76
|
* Extracts MsgResource from parsed XLIFF 2.x object for a single file element.
|
|
72
77
|
* @param fileEl - Parsed file element
|
|
73
78
|
* @param xliffTrgLang - trgLang from xliff root (may be undefined for monolingual)
|
|
74
79
|
* @param project - MsgProject instance
|
|
75
80
|
* @param targetLocales - Array of supported target locale keys
|
|
81
|
+
* @param xliffSpace - `xml:space` from the xliff root, inherited by the file (default `default`)
|
|
76
82
|
* @returns MsgResource or null if file should be skipped (monolingual or unsupported locale)
|
|
77
83
|
*/
|
|
78
84
|
declare function extractResourceFromXliffFile(fileEl: Record<string, unknown>, xliffTrgLang: string | undefined, project: {
|
|
79
85
|
locales: {
|
|
80
86
|
targetLocales: Record<string, string[]>;
|
|
81
87
|
};
|
|
82
|
-
}, targetLocales: string[]): MsgResource | null;
|
|
88
|
+
}, targetLocales: string[], xliffSpace?: XmlSpace): MsgResource | null;
|
|
83
89
|
/** Result of processing one XLIFF file: project, locale, and resources to write. */
|
|
84
90
|
interface ImportResult {
|
|
85
91
|
project: string;
|
|
@@ -94,10 +100,12 @@ interface ImportResult {
|
|
|
94
100
|
* @param xliffPath - Absolute path to XLIFF file
|
|
95
101
|
* @param projectsDir - Absolute path to i18n/projects
|
|
96
102
|
* @param projectName - Project name (from filename, for loading MsgProject)
|
|
97
|
-
* @param
|
|
98
|
-
* @
|
|
103
|
+
* @param filenameLocale - Locale from the file name, if any; must match the file's `trgLang`
|
|
104
|
+
* @param warn - Receives a message for each skip the user should hear about
|
|
105
|
+
* (monolingual file, file name locale that disagrees with `trgLang`)
|
|
106
|
+
* @returns ImportResult (locale = the file's `trgLang`) or null if the file should be skipped
|
|
99
107
|
*/
|
|
100
|
-
declare function processXliffFile(xliffPath: string, projectsDir: string, projectName: string,
|
|
108
|
+
declare function processXliffFile(xliffPath: string, projectsDir: string, projectName: string, filenameLocale: string | undefined, warn?: (message: string) => void): Promise<ImportResult | null>;
|
|
101
109
|
/**
|
|
102
110
|
* Writes translation JSON files to l10n/translations/project/locale/.
|
|
103
111
|
* @param translationsDir - Absolute path to l10n/translations
|
|
@@ -105,4 +113,4 @@ declare function processXliffFile(xliffPath: string, projectsDir: string, projec
|
|
|
105
113
|
*/
|
|
106
114
|
declare function writeTranslationFiles(translationsDir: string, result: ImportResult): Promise<void>;
|
|
107
115
|
|
|
108
|
-
export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|
|
116
|
+
export { type ImportResult, type XmlSpace, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|
|
@@ -42,11 +42,14 @@ declare function filterXliffPathsByLocale(filePaths: string[], locale: string, x
|
|
|
42
42
|
* xliffDir may also indicate project/locale (e.g. xliff/zh/project.xliff).
|
|
43
43
|
* @param filePath - Absolute path to XLIFF file
|
|
44
44
|
* @param xliffDir - Absolute path to the xliff directory the file was found in
|
|
45
|
-
* @returns Object with project
|
|
45
|
+
* @returns Object with project, a path-based locale hint, and `filenameLocale`
|
|
46
|
+
* (the locale from the file name alone, `project.locale.xliff`). Import takes
|
|
47
|
+
* the locale from the file's `trgLang` and checks only `filenameLocale` against it.
|
|
46
48
|
*/
|
|
47
49
|
declare function parseXliffFilename(filePath: string, xliffDir: string): {
|
|
48
50
|
project: string;
|
|
49
51
|
locale?: string;
|
|
52
|
+
filenameLocale?: string;
|
|
50
53
|
};
|
|
51
54
|
/**
|
|
52
55
|
* Dynamically imports MsgProject from i18n/projects by project name.
|
|
@@ -67,19 +70,22 @@ declare function importMsgProject(projectsDir: string, projectName: string): Pro
|
|
|
67
70
|
* @throws Error if XML is malformed
|
|
68
71
|
*/
|
|
69
72
|
declare function parseXliff20(xml: string): object;
|
|
73
|
+
/** XLIFF `xml:space` value: `preserve` keeps all whitespace; `default` lets the importer trim. */
|
|
74
|
+
type XmlSpace = "default" | "preserve";
|
|
70
75
|
/**
|
|
71
76
|
* Extracts MsgResource from parsed XLIFF 2.x object for a single file element.
|
|
72
77
|
* @param fileEl - Parsed file element
|
|
73
78
|
* @param xliffTrgLang - trgLang from xliff root (may be undefined for monolingual)
|
|
74
79
|
* @param project - MsgProject instance
|
|
75
80
|
* @param targetLocales - Array of supported target locale keys
|
|
81
|
+
* @param xliffSpace - `xml:space` from the xliff root, inherited by the file (default `default`)
|
|
76
82
|
* @returns MsgResource or null if file should be skipped (monolingual or unsupported locale)
|
|
77
83
|
*/
|
|
78
84
|
declare function extractResourceFromXliffFile(fileEl: Record<string, unknown>, xliffTrgLang: string | undefined, project: {
|
|
79
85
|
locales: {
|
|
80
86
|
targetLocales: Record<string, string[]>;
|
|
81
87
|
};
|
|
82
|
-
}, targetLocales: string[]): MsgResource | null;
|
|
88
|
+
}, targetLocales: string[], xliffSpace?: XmlSpace): MsgResource | null;
|
|
83
89
|
/** Result of processing one XLIFF file: project, locale, and resources to write. */
|
|
84
90
|
interface ImportResult {
|
|
85
91
|
project: string;
|
|
@@ -94,10 +100,12 @@ interface ImportResult {
|
|
|
94
100
|
* @param xliffPath - Absolute path to XLIFF file
|
|
95
101
|
* @param projectsDir - Absolute path to i18n/projects
|
|
96
102
|
* @param projectName - Project name (from filename, for loading MsgProject)
|
|
97
|
-
* @param
|
|
98
|
-
* @
|
|
103
|
+
* @param filenameLocale - Locale from the file name, if any; must match the file's `trgLang`
|
|
104
|
+
* @param warn - Receives a message for each skip the user should hear about
|
|
105
|
+
* (monolingual file, file name locale that disagrees with `trgLang`)
|
|
106
|
+
* @returns ImportResult (locale = the file's `trgLang`) or null if the file should be skipped
|
|
99
107
|
*/
|
|
100
|
-
declare function processXliffFile(xliffPath: string, projectsDir: string, projectName: string,
|
|
108
|
+
declare function processXliffFile(xliffPath: string, projectsDir: string, projectName: string, filenameLocale: string | undefined, warn?: (message: string) => void): Promise<ImportResult | null>;
|
|
101
109
|
/**
|
|
102
110
|
* Writes translation JSON files to l10n/translations/project/locale/.
|
|
103
111
|
* @param translationsDir - Absolute path to l10n/translations
|
|
@@ -105,4 +113,4 @@ declare function processXliffFile(xliffPath: string, projectsDir: string, projec
|
|
|
105
113
|
*/
|
|
106
114
|
declare function writeTranslationFiles(translationsDir: string, result: ImportResult): Promise<void>;
|
|
107
115
|
|
|
108
|
-
export { type ImportResult, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|
|
116
|
+
export { type ImportResult, type XmlSpace, extractResourceFromXliffFile, filterXliffPathsByLocale, filterXliffPathsByProject, findXliffFilePaths, importMsgProject, parseXliff20, parseXliffFilename, processXliffFile, writeTranslationFiles, xliffDirSegments };
|