@worldware/msg-cli 0.2.1 → 0.2.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 +99 -11
- package/dist/{chunk-KKATA5YE.mjs → chunk-DIY2WO3R.mjs} +29 -5
- package/dist/chunk-DPUF7YUH.mjs +269 -0
- package/dist/{chunk-BM3O7ZJW.mjs → chunk-XHD7F45F.mjs} +26 -7
- package/dist/commands/create/project.cjs +1 -1
- package/dist/commands/create/project.mjs +1 -1
- package/dist/commands/create/resource.cjs +1 -1
- package/dist/commands/create/resource.mjs +1 -1
- package/dist/commands/export.cjs +149 -9
- package/dist/commands/export.d.mts +1 -1
- package/dist/commands/export.d.ts +1 -1
- package/dist/commands/export.mjs +4 -3
- package/dist/commands/import.cjs +150 -6
- package/dist/commands/import.d.mts +1 -1
- package/dist/commands/import.d.ts +1 -1
- package/dist/commands/import.mjs +3 -2
- package/dist/lib/export-helpers.cjs +147 -7
- package/dist/lib/export-helpers.d.mts +1 -1
- package/dist/lib/export-helpers.d.ts +1 -1
- package/dist/lib/export-helpers.mjs +2 -1
- package/dist/lib/import-helpers.cjs +149 -5
- package/dist/lib/import-helpers.d.mts +3 -3
- package/dist/lib/import-helpers.d.ts +3 -3
- package/dist/lib/import-helpers.mjs +2 -1
- package/dist/lib/pgs-mf2.cjs +292 -0
- package/dist/lib/pgs-mf2.d.mts +73 -0
- package/dist/lib/pgs-mf2.d.ts +73 -0
- package/dist/lib/pgs-mf2.mjs +18 -0
- package/package.json +14 -5
package/dist/commands/export.cjs
CHANGED
|
@@ -121,12 +121,137 @@ async function dynamicImportFromUrl(url) {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
|
|
124
|
+
// src/lib/pgs-mf2.ts
|
|
125
|
+
var import_messageformat = require("messageformat");
|
|
126
|
+
var GENDER_LABELS = /* @__PURE__ */ new Set([
|
|
127
|
+
"feminine",
|
|
128
|
+
"masculine",
|
|
129
|
+
"neuter",
|
|
130
|
+
"other"
|
|
131
|
+
]);
|
|
132
|
+
function findInputDeclaration(msg, varName) {
|
|
133
|
+
for (const d of msg.declarations) {
|
|
134
|
+
const decl = d;
|
|
135
|
+
if (decl.type === "input" && decl.name === varName) {
|
|
136
|
+
return decl;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function getFunctionName(expr) {
|
|
142
|
+
return expr.functionRef?.name;
|
|
143
|
+
}
|
|
144
|
+
function getSelectOptionOrdinal(expr) {
|
|
145
|
+
const opts = expr.functionRef?.options;
|
|
146
|
+
if (!opts || !(opts instanceof Map)) return false;
|
|
147
|
+
const sel = opts.get("select");
|
|
148
|
+
if (!sel || typeof sel !== "object" || sel === null) return false;
|
|
149
|
+
if (sel.type !== "literal") return false;
|
|
150
|
+
return sel.value === "ordinal";
|
|
151
|
+
}
|
|
152
|
+
function variantKeyAt(msg, variantIndex, keyIndex) {
|
|
153
|
+
const v = msg.variants[variantIndex];
|
|
154
|
+
return v?.keys[keyIndex];
|
|
155
|
+
}
|
|
156
|
+
function allKeysAtIndexAreGenderLike(msg, keyIndex) {
|
|
157
|
+
for (let vi = 0; vi < msg.variants.length; vi++) {
|
|
158
|
+
const k = variantKeyAt(msg, vi, keyIndex);
|
|
159
|
+
if (!k) return false;
|
|
160
|
+
if ((0, import_messageformat.isCatchallKey)(k)) continue;
|
|
161
|
+
if ((0, import_messageformat.isLiteral)(k)) {
|
|
162
|
+
if (!GENDER_LABELS.has(k.value)) return false;
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
return true;
|
|
168
|
+
}
|
|
169
|
+
function classifySelectMessageForPgs(msg) {
|
|
170
|
+
const out = [];
|
|
171
|
+
for (let si = 0; si < msg.selectors.length; si++) {
|
|
172
|
+
const sel = msg.selectors[si];
|
|
173
|
+
if (!sel || sel.type !== "variable" || !sel.name) return null;
|
|
174
|
+
const varName = sel.name;
|
|
175
|
+
const decl = findInputDeclaration(msg, varName);
|
|
176
|
+
if (!decl) return null;
|
|
177
|
+
const expr = decl.value;
|
|
178
|
+
if (expr.type !== "expression") return null;
|
|
179
|
+
const fname = getFunctionName(expr);
|
|
180
|
+
if (fname === "number") {
|
|
181
|
+
out.push({
|
|
182
|
+
kind: getSelectOptionOrdinal(expr) ? "ordinal" : "plural",
|
|
183
|
+
name: varName
|
|
184
|
+
});
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (fname === "string") {
|
|
188
|
+
const kind = allKeysAtIndexAreGenderLike(msg, si) ? "gender" : "select";
|
|
189
|
+
out.push({ kind, name: varName });
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
return null;
|
|
193
|
+
}
|
|
194
|
+
return out.length === msg.selectors.length ? out : null;
|
|
195
|
+
}
|
|
196
|
+
function mf2KeyToPgsToken(key, kind) {
|
|
197
|
+
if ((0, import_messageformat.isCatchallKey)(key)) {
|
|
198
|
+
return "other";
|
|
199
|
+
}
|
|
200
|
+
if ((0, import_messageformat.isLiteral)(key)) {
|
|
201
|
+
return key.value;
|
|
202
|
+
}
|
|
203
|
+
return "other";
|
|
204
|
+
}
|
|
205
|
+
function stringifyPattern(pattern) {
|
|
206
|
+
const wrap = {
|
|
207
|
+
type: "message",
|
|
208
|
+
declarations: [],
|
|
209
|
+
pattern
|
|
210
|
+
};
|
|
211
|
+
return (0, import_messageformat.stringifyMessage)(
|
|
212
|
+
wrap
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
function selectMessageToPgsExport(source) {
|
|
216
|
+
let msg;
|
|
217
|
+
try {
|
|
218
|
+
msg = (0, import_messageformat.parseMessage)(source);
|
|
219
|
+
} catch {
|
|
220
|
+
return null;
|
|
221
|
+
}
|
|
222
|
+
if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
|
|
223
|
+
const classification = classifySelectMessageForPgs(
|
|
224
|
+
msg
|
|
225
|
+
);
|
|
226
|
+
if (!classification) return null;
|
|
227
|
+
const switchAttr = classification.map((c) => `${c.kind}:${c.name}`).join(" ");
|
|
228
|
+
const segments = [];
|
|
229
|
+
const sel = msg;
|
|
230
|
+
for (let vi = 0; vi < sel.variants.length; vi++) {
|
|
231
|
+
const variant = sel.variants[vi];
|
|
232
|
+
const caseParts = [];
|
|
233
|
+
for (let ki = 0; ki < classification.length; ki++) {
|
|
234
|
+
const key = variant.keys[ki];
|
|
235
|
+
if (!key) return null;
|
|
236
|
+
caseParts.push(
|
|
237
|
+
mf2KeyToPgsToken(key, classification[ki].kind)
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
segments.push({
|
|
241
|
+
caseAttr: caseParts.join(" "),
|
|
242
|
+
sourcePattern: stringifyPattern(variant.value)
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
return { switchAttr, segments };
|
|
246
|
+
}
|
|
247
|
+
|
|
124
248
|
// src/lib/export-helpers.ts
|
|
125
249
|
var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
126
250
|
function isMsgResourceLike(value) {
|
|
127
251
|
return Boolean(value) && typeof value.getProject === "function";
|
|
128
252
|
}
|
|
129
|
-
var
|
|
253
|
+
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
254
|
+
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
130
255
|
async function findMsgResourceFilePaths(directory) {
|
|
131
256
|
const result = [];
|
|
132
257
|
const entries = await (0, import_promises.readdir)(directory, { withFileTypes: true });
|
|
@@ -209,11 +334,11 @@ function renderNotes(notes, idPrefix, indent = " ") {
|
|
|
209
334
|
parts.push(`${indent}</notes>`);
|
|
210
335
|
return parts;
|
|
211
336
|
}
|
|
212
|
-
function
|
|
337
|
+
function resourceGroupToXliff22(group) {
|
|
213
338
|
const srcLang = group.resources[0]?.attributes?.lang ?? "en";
|
|
214
339
|
const parts = [
|
|
215
340
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
216
|
-
`<xliff xmlns="${
|
|
341
|
+
`<xliff xmlns="${XLIFF22_NS}" xmlns:pgs="${PGS_NS}" version="2.2" srcLang="${escapeXml(srcLang)}">`
|
|
217
342
|
];
|
|
218
343
|
let fileIndex = 0;
|
|
219
344
|
for (const resource of group.resources) {
|
|
@@ -251,6 +376,10 @@ function resourceGroupToXliff20(group) {
|
|
|
251
376
|
if (msgAttr?.dir) {
|
|
252
377
|
msgAttrs.push(`srcDir="${escapeXml(msgAttr.dir)}"`);
|
|
253
378
|
}
|
|
379
|
+
const pgsExport = selectMessageToPgsExport(msg.value);
|
|
380
|
+
if (pgsExport) {
|
|
381
|
+
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
382
|
+
}
|
|
254
383
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
255
384
|
const msgNotes = msg.notes ?? [];
|
|
256
385
|
if (msgNotes.length > 0) {
|
|
@@ -258,9 +387,20 @@ function resourceGroupToXliff20(group) {
|
|
|
258
387
|
...renderNotes(msgNotes, `${fileId}-${unitId}`, " ")
|
|
259
388
|
);
|
|
260
389
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
390
|
+
if (pgsExport) {
|
|
391
|
+
pgsExport.segments.forEach((seg, segIdx) => {
|
|
392
|
+
const segId = `${unitId}_s${segIdx + 1}`;
|
|
393
|
+
parts.push(
|
|
394
|
+
` <segment id="${escapeXml(segId)}" pgs:case="${escapeXml(seg.caseAttr)}">`
|
|
395
|
+
);
|
|
396
|
+
parts.push(` <source>${escapeXml(seg.sourcePattern)}</source>`);
|
|
397
|
+
parts.push(" </segment>");
|
|
398
|
+
});
|
|
399
|
+
} else {
|
|
400
|
+
parts.push(" <segment>");
|
|
401
|
+
parts.push(` <source>${escapeXml(msg.value)}</source>`);
|
|
402
|
+
parts.push(" </segment>");
|
|
403
|
+
}
|
|
264
404
|
parts.push(" </unit>");
|
|
265
405
|
});
|
|
266
406
|
parts.push(" </file>");
|
|
@@ -271,7 +411,7 @@ function resourceGroupToXliff20(group) {
|
|
|
271
411
|
function serializeResourceGroupsToXliff(groups) {
|
|
272
412
|
return groups.map((group) => ({
|
|
273
413
|
project: group.project,
|
|
274
|
-
xliff:
|
|
414
|
+
xliff: resourceGroupToXliff22(group)
|
|
275
415
|
}));
|
|
276
416
|
}
|
|
277
417
|
async function writeXliffFiles(xliffDir, xliffGroups) {
|
|
@@ -284,7 +424,7 @@ async function writeXliffFiles(xliffDir, xliffGroups) {
|
|
|
284
424
|
|
|
285
425
|
// src/commands/export.ts
|
|
286
426
|
var Export = class _Export extends import_core.Command {
|
|
287
|
-
static description = "Serialize msg resources in i18n/resources to XLIFF 2.
|
|
427
|
+
static description = "Serialize msg resources in i18n/resources to XLIFF 2.2 files in l10n/xliff (per project)";
|
|
288
428
|
static flags = {
|
|
289
429
|
help: import_core.Flags.help({ char: "h" }),
|
|
290
430
|
project: import_core.Flags.string({
|
|
@@ -340,7 +480,7 @@ var Export = class _Export extends import_core.Command {
|
|
|
340
480
|
}
|
|
341
481
|
this.log(`Filtered to project '${flags.project}'.`);
|
|
342
482
|
}
|
|
343
|
-
this.log("Serializing to XLIFF 2.
|
|
483
|
+
this.log("Serializing to XLIFF 2.2...");
|
|
344
484
|
const xliffGroups = serializeResourceGroupsToXliff(groups);
|
|
345
485
|
this.log(`Writing ${xliffGroups.length} XLIFF file(s) to ${xliffDir}...`);
|
|
346
486
|
try {
|
|
@@ -2,7 +2,7 @@ import * as _oclif_core_lib_interfaces_parser_js from '@oclif/core/lib/interface
|
|
|
2
2
|
import { Command } from '@oclif/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Serializes MsgResource objects to XLIFF 2.
|
|
5
|
+
* Serializes MsgResource objects to XLIFF 2.2 files in l10n/xliff on a per-project basis.
|
|
6
6
|
*/
|
|
7
7
|
declare class Export extends Command {
|
|
8
8
|
static description: string;
|
|
@@ -2,7 +2,7 @@ import * as _oclif_core_lib_interfaces_parser_js from '@oclif/core/lib/interface
|
|
|
2
2
|
import { Command } from '@oclif/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Serializes MsgResource objects to XLIFF 2.
|
|
5
|
+
* Serializes MsgResource objects to XLIFF 2.2 files in l10n/xliff on a per-project basis.
|
|
6
6
|
*/
|
|
7
7
|
declare class Export extends Command {
|
|
8
8
|
static description: string;
|
package/dist/commands/export.mjs
CHANGED
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-XHD7F45F.mjs";
|
|
9
|
+
import "../chunk-DPUF7YUH.mjs";
|
|
9
10
|
import "../chunk-LNCUPVEF.mjs";
|
|
10
11
|
import {
|
|
11
12
|
loadPackageJsonForMsg
|
|
@@ -16,7 +17,7 @@ import { Command, Flags } from "@oclif/core";
|
|
|
16
17
|
import { existsSync } from "fs";
|
|
17
18
|
import { join } from "path";
|
|
18
19
|
var Export = class _Export extends Command {
|
|
19
|
-
static description = "Serialize msg resources in i18n/resources to XLIFF 2.
|
|
20
|
+
static description = "Serialize msg resources in i18n/resources to XLIFF 2.2 files in l10n/xliff (per project)";
|
|
20
21
|
static flags = {
|
|
21
22
|
help: Flags.help({ char: "h" }),
|
|
22
23
|
project: Flags.string({
|
|
@@ -72,7 +73,7 @@ var Export = class _Export extends Command {
|
|
|
72
73
|
}
|
|
73
74
|
this.log(`Filtered to project '${flags.project}'.`);
|
|
74
75
|
}
|
|
75
|
-
this.log("Serializing to XLIFF 2.
|
|
76
|
+
this.log("Serializing to XLIFF 2.2...");
|
|
76
77
|
const xliffGroups = serializeResourceGroupsToXliff(groups);
|
|
77
78
|
this.log(`Writing ${xliffGroups.length} XLIFF file(s) to ${xliffDir}...`);
|
|
78
79
|
try {
|
package/dist/commands/import.cjs
CHANGED
|
@@ -124,6 +124,129 @@ async function dynamicImportFromUrl(url) {
|
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
+
// src/lib/pgs-mf2.ts
|
|
128
|
+
var import_messageformat = require("messageformat");
|
|
129
|
+
var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
|
|
130
|
+
function parsePgsSwitch(value) {
|
|
131
|
+
const trimmed = value.trim();
|
|
132
|
+
if (!trimmed) return null;
|
|
133
|
+
const parts = trimmed.split(/\s+/);
|
|
134
|
+
const out = [];
|
|
135
|
+
for (const p of parts) {
|
|
136
|
+
const m = p.match(PGS_SWITCH_ITEM);
|
|
137
|
+
if (!m) return null;
|
|
138
|
+
out.push({ kind: m[1], name: m[2] });
|
|
139
|
+
}
|
|
140
|
+
return out.length > 0 ? out : null;
|
|
141
|
+
}
|
|
142
|
+
function pgsTokenToMf2Key(token, kind) {
|
|
143
|
+
if (kind === "plural" || kind === "ordinal") {
|
|
144
|
+
if (token === "other") {
|
|
145
|
+
return { type: "*" };
|
|
146
|
+
}
|
|
147
|
+
return { type: "literal", value: token };
|
|
148
|
+
}
|
|
149
|
+
return { type: "literal", value: token };
|
|
150
|
+
}
|
|
151
|
+
function parsePatternFromSegmentBody(body) {
|
|
152
|
+
const trimmed = body.trim();
|
|
153
|
+
if (!trimmed) return [];
|
|
154
|
+
try {
|
|
155
|
+
const m = (0, import_messageformat.parseMessage)(trimmed);
|
|
156
|
+
if (m.type !== "message") return [];
|
|
157
|
+
return m.pattern;
|
|
158
|
+
} catch {
|
|
159
|
+
return [];
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function ensureFallbackVariant(keysCount, variants) {
|
|
163
|
+
const hasAllStars = variants.some(
|
|
164
|
+
(v) => v.keys.length === keysCount && v.keys.every((k) => (0, import_messageformat.isCatchallKey)(k))
|
|
165
|
+
);
|
|
166
|
+
if (hasAllStars) return;
|
|
167
|
+
const fallbackPattern = variants.length > 0 ? variants[variants.length - 1].value : [];
|
|
168
|
+
const starKeys = Array.from({ length: keysCount }, () => ({
|
|
169
|
+
type: "*"
|
|
170
|
+
}));
|
|
171
|
+
variants.push({ keys: starKeys, value: fallbackPattern });
|
|
172
|
+
}
|
|
173
|
+
function pgsImportToSelectMessage(switchAttr, segments) {
|
|
174
|
+
const parsedSwitch = parsePgsSwitch(switchAttr);
|
|
175
|
+
if (!parsedSwitch || parsedSwitch.length === 0) return null;
|
|
176
|
+
const n = parsedSwitch.length;
|
|
177
|
+
const declarations = [];
|
|
178
|
+
for (const { kind, name } of parsedSwitch) {
|
|
179
|
+
const varRef = { type: "variable", name };
|
|
180
|
+
if (kind === "plural") {
|
|
181
|
+
declarations.push({
|
|
182
|
+
type: "input",
|
|
183
|
+
name,
|
|
184
|
+
value: {
|
|
185
|
+
type: "expression",
|
|
186
|
+
arg: varRef,
|
|
187
|
+
functionRef: { type: "function", name: "number" }
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
} else if (kind === "ordinal") {
|
|
191
|
+
const opts = /* @__PURE__ */ new Map([
|
|
192
|
+
["select", { type: "literal", value: "ordinal" }]
|
|
193
|
+
]);
|
|
194
|
+
declarations.push({
|
|
195
|
+
type: "input",
|
|
196
|
+
name,
|
|
197
|
+
value: {
|
|
198
|
+
type: "expression",
|
|
199
|
+
arg: varRef,
|
|
200
|
+
functionRef: { type: "function", name: "number", options: opts }
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
} else {
|
|
204
|
+
declarations.push({
|
|
205
|
+
type: "input",
|
|
206
|
+
name,
|
|
207
|
+
value: {
|
|
208
|
+
type: "expression",
|
|
209
|
+
arg: varRef,
|
|
210
|
+
functionRef: { type: "function", name: "string" }
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
const selectors = parsedSwitch.map(
|
|
216
|
+
({ name }) => ({ type: "variable", name })
|
|
217
|
+
);
|
|
218
|
+
const variants = [];
|
|
219
|
+
for (const seg of segments) {
|
|
220
|
+
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
221
|
+
if (tokens.length !== n) return null;
|
|
222
|
+
const keys = tokens.map(
|
|
223
|
+
(t, i) => pgsTokenToMf2Key(t, parsedSwitch[i].kind)
|
|
224
|
+
);
|
|
225
|
+
variants.push({
|
|
226
|
+
keys,
|
|
227
|
+
value: parsePatternFromSegmentBody(seg.body)
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (variants.length === 0) return null;
|
|
231
|
+
const msg = {
|
|
232
|
+
type: "select",
|
|
233
|
+
declarations,
|
|
234
|
+
selectors,
|
|
235
|
+
variants
|
|
236
|
+
};
|
|
237
|
+
ensureFallbackVariant(n, msg.variants);
|
|
238
|
+
try {
|
|
239
|
+
(0, import_messageformat.validate)(
|
|
240
|
+
msg
|
|
241
|
+
);
|
|
242
|
+
} catch {
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
return (0, import_messageformat.stringifyMessage)(
|
|
246
|
+
msg
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
|
|
127
250
|
// src/lib/import-helpers.ts
|
|
128
251
|
var XLIFF_EXTENSIONS = [".xliff", ".xlf"];
|
|
129
252
|
var XLIFF_FILENAME_PATTERN = /^(.+?)(?:\.([a-zA-Z]{2,}(?:-[a-zA-Z0-9]+)*))?\.(?:xliff|xlf)$/i;
|
|
@@ -226,7 +349,7 @@ function parseXliff20(xml) {
|
|
|
226
349
|
return parsed;
|
|
227
350
|
} catch (err) {
|
|
228
351
|
const message = err instanceof Error ? err.message : "Failed to parse XLIFF";
|
|
229
|
-
throw new Error(`Malformed XLIFF
|
|
352
|
+
throw new Error(`Malformed XLIFF: ${message}`);
|
|
230
353
|
}
|
|
231
354
|
}
|
|
232
355
|
function categoryToNoteType(category) {
|
|
@@ -313,11 +436,32 @@ function processUnit(unit, fileAttrs) {
|
|
|
313
436
|
}
|
|
314
437
|
}
|
|
315
438
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
439
|
+
const pgsSwitch = unit["@_pgs:switch"];
|
|
440
|
+
let value;
|
|
441
|
+
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
442
|
+
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
443
|
+
)) {
|
|
444
|
+
const importSegments = segments.map((seg) => ({
|
|
445
|
+
caseAttr: seg["@_pgs:case"],
|
|
446
|
+
body: extractSegmentText(seg)
|
|
447
|
+
}));
|
|
448
|
+
const rebuilt = pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
449
|
+
if (rebuilt) {
|
|
450
|
+
value = rebuilt;
|
|
451
|
+
} else {
|
|
452
|
+
const targetParts = [];
|
|
453
|
+
for (const seg of segments) {
|
|
454
|
+
targetParts.push(extractSegmentText(seg));
|
|
455
|
+
}
|
|
456
|
+
value = targetParts.join("");
|
|
457
|
+
}
|
|
458
|
+
} else {
|
|
459
|
+
const targetParts = [];
|
|
460
|
+
for (const seg of segments) {
|
|
461
|
+
targetParts.push(extractSegmentText(seg));
|
|
462
|
+
}
|
|
463
|
+
value = targetParts.join("");
|
|
319
464
|
}
|
|
320
|
-
const value = targetParts.join("");
|
|
321
465
|
const attributes = {};
|
|
322
466
|
if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
|
|
323
467
|
if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
|
|
@@ -429,7 +573,7 @@ async function writeTranslationFiles(translationsDir, result) {
|
|
|
429
573
|
|
|
430
574
|
// src/commands/import.ts
|
|
431
575
|
var Import = class _Import extends import_core.Command {
|
|
432
|
-
static description = "Import translations from XLIFF 2.0 files in l10n/xliff to JSON in l10n/translations";
|
|
576
|
+
static description = "Import translations from XLIFF 2.0/2.2 files in l10n/xliff to JSON in l10n/translations";
|
|
433
577
|
static flags = {
|
|
434
578
|
help: import_core.Flags.help({ char: "h" }),
|
|
435
579
|
project: import_core.Flags.string({
|
|
@@ -2,7 +2,7 @@ import * as _oclif_core_lib_interfaces_parser_js from '@oclif/core/lib/interface
|
|
|
2
2
|
import { Command } from '@oclif/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Imports translations from XLIFF 2.0 files into l10n/translations as JSON.
|
|
5
|
+
* Imports translations from XLIFF 2.0/2.2 files into l10n/translations as JSON.
|
|
6
6
|
*/
|
|
7
7
|
declare class Import extends Command {
|
|
8
8
|
static description: string;
|
|
@@ -2,7 +2,7 @@ import * as _oclif_core_lib_interfaces_parser_js from '@oclif/core/lib/interface
|
|
|
2
2
|
import { Command } from '@oclif/core';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Imports translations from XLIFF 2.0 files into l10n/translations as JSON.
|
|
5
|
+
* Imports translations from XLIFF 2.0/2.2 files into l10n/translations as JSON.
|
|
6
6
|
*/
|
|
7
7
|
declare class Import extends Command {
|
|
8
8
|
static description: string;
|
package/dist/commands/import.mjs
CHANGED
|
@@ -5,7 +5,8 @@ import {
|
|
|
5
5
|
parseXliffFilename,
|
|
6
6
|
processXliffFile,
|
|
7
7
|
writeTranslationFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-DIY2WO3R.mjs";
|
|
9
|
+
import "../chunk-DPUF7YUH.mjs";
|
|
9
10
|
import "../chunk-LNCUPVEF.mjs";
|
|
10
11
|
import {
|
|
11
12
|
loadPackageJsonForMsg
|
|
@@ -16,7 +17,7 @@ import { Command, Flags } from "@oclif/core";
|
|
|
16
17
|
import { existsSync } from "fs";
|
|
17
18
|
import { join } from "path";
|
|
18
19
|
var Import = class _Import extends Command {
|
|
19
|
-
static description = "Import translations from XLIFF 2.0 files in l10n/xliff to JSON in l10n/translations";
|
|
20
|
+
static description = "Import translations from XLIFF 2.0/2.2 files in l10n/xliff to JSON in l10n/translations";
|
|
20
21
|
static flags = {
|
|
21
22
|
help: Flags.help({ char: "h" }),
|
|
22
23
|
project: Flags.string({
|
|
@@ -58,12 +58,137 @@ async function dynamicImportFromUrl(url) {
|
|
|
58
58
|
}
|
|
59
59
|
}
|
|
60
60
|
|
|
61
|
+
// src/lib/pgs-mf2.ts
|
|
62
|
+
var import_messageformat = require("messageformat");
|
|
63
|
+
var GENDER_LABELS = /* @__PURE__ */ new Set([
|
|
64
|
+
"feminine",
|
|
65
|
+
"masculine",
|
|
66
|
+
"neuter",
|
|
67
|
+
"other"
|
|
68
|
+
]);
|
|
69
|
+
function findInputDeclaration(msg, varName) {
|
|
70
|
+
for (const d of msg.declarations) {
|
|
71
|
+
const decl = d;
|
|
72
|
+
if (decl.type === "input" && decl.name === varName) {
|
|
73
|
+
return decl;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
function getFunctionName(expr) {
|
|
79
|
+
return expr.functionRef?.name;
|
|
80
|
+
}
|
|
81
|
+
function getSelectOptionOrdinal(expr) {
|
|
82
|
+
const opts = expr.functionRef?.options;
|
|
83
|
+
if (!opts || !(opts instanceof Map)) return false;
|
|
84
|
+
const sel = opts.get("select");
|
|
85
|
+
if (!sel || typeof sel !== "object" || sel === null) return false;
|
|
86
|
+
if (sel.type !== "literal") return false;
|
|
87
|
+
return sel.value === "ordinal";
|
|
88
|
+
}
|
|
89
|
+
function variantKeyAt(msg, variantIndex, keyIndex) {
|
|
90
|
+
const v = msg.variants[variantIndex];
|
|
91
|
+
return v?.keys[keyIndex];
|
|
92
|
+
}
|
|
93
|
+
function allKeysAtIndexAreGenderLike(msg, keyIndex) {
|
|
94
|
+
for (let vi = 0; vi < msg.variants.length; vi++) {
|
|
95
|
+
const k = variantKeyAt(msg, vi, keyIndex);
|
|
96
|
+
if (!k) return false;
|
|
97
|
+
if ((0, import_messageformat.isCatchallKey)(k)) continue;
|
|
98
|
+
if ((0, import_messageformat.isLiteral)(k)) {
|
|
99
|
+
if (!GENDER_LABELS.has(k.value)) return false;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
return false;
|
|
103
|
+
}
|
|
104
|
+
return true;
|
|
105
|
+
}
|
|
106
|
+
function classifySelectMessageForPgs(msg) {
|
|
107
|
+
const out = [];
|
|
108
|
+
for (let si = 0; si < msg.selectors.length; si++) {
|
|
109
|
+
const sel = msg.selectors[si];
|
|
110
|
+
if (!sel || sel.type !== "variable" || !sel.name) return null;
|
|
111
|
+
const varName = sel.name;
|
|
112
|
+
const decl = findInputDeclaration(msg, varName);
|
|
113
|
+
if (!decl) return null;
|
|
114
|
+
const expr = decl.value;
|
|
115
|
+
if (expr.type !== "expression") return null;
|
|
116
|
+
const fname = getFunctionName(expr);
|
|
117
|
+
if (fname === "number") {
|
|
118
|
+
out.push({
|
|
119
|
+
kind: getSelectOptionOrdinal(expr) ? "ordinal" : "plural",
|
|
120
|
+
name: varName
|
|
121
|
+
});
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
if (fname === "string") {
|
|
125
|
+
const kind = allKeysAtIndexAreGenderLike(msg, si) ? "gender" : "select";
|
|
126
|
+
out.push({ kind, name: varName });
|
|
127
|
+
continue;
|
|
128
|
+
}
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
return out.length === msg.selectors.length ? out : null;
|
|
132
|
+
}
|
|
133
|
+
function mf2KeyToPgsToken(key, kind) {
|
|
134
|
+
if ((0, import_messageformat.isCatchallKey)(key)) {
|
|
135
|
+
return "other";
|
|
136
|
+
}
|
|
137
|
+
if ((0, import_messageformat.isLiteral)(key)) {
|
|
138
|
+
return key.value;
|
|
139
|
+
}
|
|
140
|
+
return "other";
|
|
141
|
+
}
|
|
142
|
+
function stringifyPattern(pattern) {
|
|
143
|
+
const wrap = {
|
|
144
|
+
type: "message",
|
|
145
|
+
declarations: [],
|
|
146
|
+
pattern
|
|
147
|
+
};
|
|
148
|
+
return (0, import_messageformat.stringifyMessage)(
|
|
149
|
+
wrap
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
function selectMessageToPgsExport(source) {
|
|
153
|
+
let msg;
|
|
154
|
+
try {
|
|
155
|
+
msg = (0, import_messageformat.parseMessage)(source);
|
|
156
|
+
} catch {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
|
|
160
|
+
const classification = classifySelectMessageForPgs(
|
|
161
|
+
msg
|
|
162
|
+
);
|
|
163
|
+
if (!classification) return null;
|
|
164
|
+
const switchAttr = classification.map((c) => `${c.kind}:${c.name}`).join(" ");
|
|
165
|
+
const segments = [];
|
|
166
|
+
const sel = msg;
|
|
167
|
+
for (let vi = 0; vi < sel.variants.length; vi++) {
|
|
168
|
+
const variant = sel.variants[vi];
|
|
169
|
+
const caseParts = [];
|
|
170
|
+
for (let ki = 0; ki < classification.length; ki++) {
|
|
171
|
+
const key = variant.keys[ki];
|
|
172
|
+
if (!key) return null;
|
|
173
|
+
caseParts.push(
|
|
174
|
+
mf2KeyToPgsToken(key, classification[ki].kind)
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
segments.push({
|
|
178
|
+
caseAttr: caseParts.join(" "),
|
|
179
|
+
sourcePattern: stringifyPattern(variant.value)
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
return { switchAttr, segments };
|
|
183
|
+
}
|
|
184
|
+
|
|
61
185
|
// src/lib/export-helpers.ts
|
|
62
186
|
var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
63
187
|
function isMsgResourceLike(value) {
|
|
64
188
|
return Boolean(value) && typeof value.getProject === "function";
|
|
65
189
|
}
|
|
66
|
-
var
|
|
190
|
+
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
191
|
+
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
67
192
|
async function findMsgResourceFilePaths(directory) {
|
|
68
193
|
const result = [];
|
|
69
194
|
const entries = await (0, import_promises.readdir)(directory, { withFileTypes: true });
|
|
@@ -146,11 +271,11 @@ function renderNotes(notes, idPrefix, indent = " ") {
|
|
|
146
271
|
parts.push(`${indent}</notes>`);
|
|
147
272
|
return parts;
|
|
148
273
|
}
|
|
149
|
-
function
|
|
274
|
+
function resourceGroupToXliff22(group) {
|
|
150
275
|
const srcLang = group.resources[0]?.attributes?.lang ?? "en";
|
|
151
276
|
const parts = [
|
|
152
277
|
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
153
|
-
`<xliff xmlns="${
|
|
278
|
+
`<xliff xmlns="${XLIFF22_NS}" xmlns:pgs="${PGS_NS}" version="2.2" srcLang="${escapeXml(srcLang)}">`
|
|
154
279
|
];
|
|
155
280
|
let fileIndex = 0;
|
|
156
281
|
for (const resource of group.resources) {
|
|
@@ -188,6 +313,10 @@ function resourceGroupToXliff20(group) {
|
|
|
188
313
|
if (msgAttr?.dir) {
|
|
189
314
|
msgAttrs.push(`srcDir="${escapeXml(msgAttr.dir)}"`);
|
|
190
315
|
}
|
|
316
|
+
const pgsExport = selectMessageToPgsExport(msg.value);
|
|
317
|
+
if (pgsExport) {
|
|
318
|
+
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
319
|
+
}
|
|
191
320
|
parts.push(` <unit ${msgAttrs.join(" ")}>`);
|
|
192
321
|
const msgNotes = msg.notes ?? [];
|
|
193
322
|
if (msgNotes.length > 0) {
|
|
@@ -195,9 +324,20 @@ function resourceGroupToXliff20(group) {
|
|
|
195
324
|
...renderNotes(msgNotes, `${fileId}-${unitId}`, " ")
|
|
196
325
|
);
|
|
197
326
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
327
|
+
if (pgsExport) {
|
|
328
|
+
pgsExport.segments.forEach((seg, segIdx) => {
|
|
329
|
+
const segId = `${unitId}_s${segIdx + 1}`;
|
|
330
|
+
parts.push(
|
|
331
|
+
` <segment id="${escapeXml(segId)}" pgs:case="${escapeXml(seg.caseAttr)}">`
|
|
332
|
+
);
|
|
333
|
+
parts.push(` <source>${escapeXml(seg.sourcePattern)}</source>`);
|
|
334
|
+
parts.push(" </segment>");
|
|
335
|
+
});
|
|
336
|
+
} else {
|
|
337
|
+
parts.push(" <segment>");
|
|
338
|
+
parts.push(` <source>${escapeXml(msg.value)}</source>`);
|
|
339
|
+
parts.push(" </segment>");
|
|
340
|
+
}
|
|
201
341
|
parts.push(" </unit>");
|
|
202
342
|
});
|
|
203
343
|
parts.push(" </file>");
|
|
@@ -208,7 +348,7 @@ function resourceGroupToXliff20(group) {
|
|
|
208
348
|
function serializeResourceGroupsToXliff(groups) {
|
|
209
349
|
return groups.map((group) => ({
|
|
210
350
|
project: group.project,
|
|
211
|
-
xliff:
|
|
351
|
+
xliff: resourceGroupToXliff22(group)
|
|
212
352
|
}));
|
|
213
353
|
}
|
|
214
354
|
async function writeXliffFiles(xliffDir, xliffGroups) {
|
|
@@ -39,7 +39,7 @@ declare function groupResourcesByProject(resources: MsgResource[]): ResourceGrou
|
|
|
39
39
|
*/
|
|
40
40
|
declare function filterResourceGroupsByProject(groups: ResourceGroup[], projectName: string): ResourceGroup[];
|
|
41
41
|
/**
|
|
42
|
-
* Serializes each resource group to an XLIFF 2.
|
|
42
|
+
* Serializes each resource group to an XLIFF 2.2 string.
|
|
43
43
|
* @param groups - Array of resource group objects
|
|
44
44
|
* @returns Array of xliff group objects { project, xliff }
|
|
45
45
|
*/
|