@worldware/msg-cli 0.3.0 → 0.3.1
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 +18 -8
- package/dist/{chunk-DIY2WO3R.mjs → chunk-4Z65MYW7.mjs} +13 -16
- package/dist/{chunk-XHD7F45F.mjs → chunk-6BLQESHW.mjs} +27 -2
- package/dist/{chunk-DPUF7YUH.mjs → chunk-MGHC4AYZ.mjs} +18 -13
- package/dist/chunk-Q775ZGC5.mjs +29 -0
- package/dist/chunk-XID5J2F7.mjs +135 -0
- package/dist/commands/export.cjs +128 -14
- package/dist/commands/export.mjs +4 -2
- package/dist/commands/import.cjs +135 -17
- package/dist/commands/import.mjs +4 -2
- package/dist/lib/export-helpers.cjs +128 -14
- package/dist/lib/export-helpers.mjs +4 -2
- package/dist/lib/import-helpers.cjs +135 -17
- package/dist/lib/import-helpers.mjs +4 -2
- package/dist/lib/msg-format.cjs +55 -0
- package/dist/lib/msg-format.d.mts +28 -0
- package/dist/lib/msg-format.d.ts +28 -0
- package/dist/lib/msg-format.mjs +10 -0
- package/dist/lib/pgs-mf1.cjs +289 -0
- package/dist/lib/pgs-mf1.d.mts +28 -0
- package/dist/lib/pgs-mf1.d.ts +28 -0
- package/dist/lib/pgs-mf1.mjs +9 -0
- package/dist/lib/pgs-mf2.cjs +19 -13
- package/dist/lib/pgs-mf2.d.mts +9 -1
- package/dist/lib/pgs-mf2.d.ts +9 -1
- package/dist/lib/pgs-mf2.mjs +3 -1
- package/package.json +13 -3
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/msg-format.ts
|
|
21
|
+
var msg_format_exports = {};
|
|
22
|
+
__export(msg_format_exports, {
|
|
23
|
+
formatToUnitType: () => formatToUnitType,
|
|
24
|
+
resolveMessageFormat: () => resolveMessageFormat,
|
|
25
|
+
unitTypeToFormat: () => unitTypeToFormat
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(msg_format_exports);
|
|
28
|
+
var import_msg = require("@worldware/msg");
|
|
29
|
+
var MSG_TYPE_PREFIX = "msg";
|
|
30
|
+
var FORMAT_VALUES = /* @__PURE__ */ new Set(["NONE", "MF1", "MF2"]);
|
|
31
|
+
function formatToUnitType(format) {
|
|
32
|
+
return `${MSG_TYPE_PREFIX}:${format}`;
|
|
33
|
+
}
|
|
34
|
+
function unitTypeToFormat(type) {
|
|
35
|
+
if (type == null || type === "") return void 0;
|
|
36
|
+
if (FORMAT_VALUES.has(type)) {
|
|
37
|
+
return type;
|
|
38
|
+
}
|
|
39
|
+
const prefixed = new RegExp(
|
|
40
|
+
`^${MSG_TYPE_PREFIX}:(NONE|MF1|MF2)$`
|
|
41
|
+
).exec(type);
|
|
42
|
+
if (prefixed?.[1] && FORMAT_VALUES.has(prefixed[1])) {
|
|
43
|
+
return prefixed[1];
|
|
44
|
+
}
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
function resolveMessageFormat(messageFormat, resourceFormat, projectFormat) {
|
|
48
|
+
return messageFormat ?? resourceFormat ?? projectFormat ?? import_msg.MSG_DEFAULT_FORMAT;
|
|
49
|
+
}
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
formatToUnitType,
|
|
53
|
+
resolveMessageFormat,
|
|
54
|
+
unitTypeToFormat
|
|
55
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { MsgFormat } from '@worldware/msg';
|
|
2
|
+
export { MsgFormat } from '@worldware/msg';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maps `@worldware/msg` `format` values to XLIFF 2.2 `unit@type` and back.
|
|
6
|
+
*
|
|
7
|
+
* XLIFF requires custom `type` values on `<unit>` to use `prefix:value` form
|
|
8
|
+
* (see XLIFF 2.2 Core §3.3.1.40). We use the `msg:` authority prefix.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a format as an XLIFF 2.2 unit `type` attribute value.
|
|
13
|
+
* @example formatToUnitType("MF1") → "msg:MF1"
|
|
14
|
+
*/
|
|
15
|
+
declare function formatToUnitType(format: MsgFormat): string;
|
|
16
|
+
/**
|
|
17
|
+
* Decodes an XLIFF unit `type` into a MsgFormat.
|
|
18
|
+
* Accepts `msg:MF1` (canonical) and bare `MF1` for resilience.
|
|
19
|
+
* @returns undefined when type is missing or not a recognized format
|
|
20
|
+
*/
|
|
21
|
+
declare function unitTypeToFormat(type: string | undefined | null): MsgFormat | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the effective format for a message using inheritance
|
|
24
|
+
* (message → resource → project → library default).
|
|
25
|
+
*/
|
|
26
|
+
declare function resolveMessageFormat(messageFormat?: MsgFormat, resourceFormat?: MsgFormat, projectFormat?: MsgFormat): MsgFormat;
|
|
27
|
+
|
|
28
|
+
export { formatToUnitType, resolveMessageFormat, unitTypeToFormat };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { MsgFormat } from '@worldware/msg';
|
|
2
|
+
export { MsgFormat } from '@worldware/msg';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Maps `@worldware/msg` `format` values to XLIFF 2.2 `unit@type` and back.
|
|
6
|
+
*
|
|
7
|
+
* XLIFF requires custom `type` values on `<unit>` to use `prefix:value` form
|
|
8
|
+
* (see XLIFF 2.2 Core §3.3.1.40). We use the `msg:` authority prefix.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Encodes a format as an XLIFF 2.2 unit `type` attribute value.
|
|
13
|
+
* @example formatToUnitType("MF1") → "msg:MF1"
|
|
14
|
+
*/
|
|
15
|
+
declare function formatToUnitType(format: MsgFormat): string;
|
|
16
|
+
/**
|
|
17
|
+
* Decodes an XLIFF unit `type` into a MsgFormat.
|
|
18
|
+
* Accepts `msg:MF1` (canonical) and bare `MF1` for resilience.
|
|
19
|
+
* @returns undefined when type is missing or not a recognized format
|
|
20
|
+
*/
|
|
21
|
+
declare function unitTypeToFormat(type: string | undefined | null): MsgFormat | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* Resolves the effective format for a message using inheritance
|
|
24
|
+
* (message → resource → project → library default).
|
|
25
|
+
*/
|
|
26
|
+
declare function resolveMessageFormat(messageFormat?: MsgFormat, resourceFormat?: MsgFormat, projectFormat?: MsgFormat): MsgFormat;
|
|
27
|
+
|
|
28
|
+
export { formatToUnitType, resolveMessageFormat, unitTypeToFormat };
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/pgs-mf1.ts
|
|
21
|
+
var pgs_mf1_exports = {};
|
|
22
|
+
__export(pgs_mf1_exports, {
|
|
23
|
+
mf1MessageToPgsExport: () => mf1MessageToPgsExport,
|
|
24
|
+
pgsImportToMf1Message: () => pgsImportToMf1Message
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(pgs_mf1_exports);
|
|
27
|
+
var import_parser = require("@messageformat/parser");
|
|
28
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
29
|
+
var import_messageformat2 = require("messageformat");
|
|
30
|
+
|
|
31
|
+
// src/lib/pgs-mf2.ts
|
|
32
|
+
var import_messageformat = require("messageformat");
|
|
33
|
+
var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
|
|
34
|
+
var GENDER_LABELS = /* @__PURE__ */ new Set([
|
|
35
|
+
"feminine",
|
|
36
|
+
"masculine",
|
|
37
|
+
"neuter",
|
|
38
|
+
"other"
|
|
39
|
+
]);
|
|
40
|
+
function parsePgsSwitch(value) {
|
|
41
|
+
const trimmed = value.trim();
|
|
42
|
+
if (!trimmed) return null;
|
|
43
|
+
const parts = trimmed.split(/\s+/);
|
|
44
|
+
const out = [];
|
|
45
|
+
for (const p of parts) {
|
|
46
|
+
const m = p.match(PGS_SWITCH_ITEM);
|
|
47
|
+
if (!m) return null;
|
|
48
|
+
out.push({ kind: m[1], name: m[2] });
|
|
49
|
+
}
|
|
50
|
+
return out.length > 0 ? out : null;
|
|
51
|
+
}
|
|
52
|
+
function findInputDeclaration(msg, varName) {
|
|
53
|
+
for (const d of msg.declarations) {
|
|
54
|
+
const decl = d;
|
|
55
|
+
if (decl.type === "input" && decl.name === varName) {
|
|
56
|
+
return decl;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
function getFunctionName(expr) {
|
|
62
|
+
return expr.functionRef?.name;
|
|
63
|
+
}
|
|
64
|
+
function getOption(opts, key) {
|
|
65
|
+
if (!opts) return void 0;
|
|
66
|
+
if (opts instanceof Map) return opts.get(key);
|
|
67
|
+
return opts[key];
|
|
68
|
+
}
|
|
69
|
+
function getSelectOptionOrdinal(expr) {
|
|
70
|
+
const sel = getOption(expr.functionRef?.options, "select");
|
|
71
|
+
if (!sel || typeof sel !== "object" || sel === null) return false;
|
|
72
|
+
if (sel.type !== "literal") return false;
|
|
73
|
+
return sel.value === "ordinal";
|
|
74
|
+
}
|
|
75
|
+
function variantKeyAt(msg, variantIndex, keyIndex) {
|
|
76
|
+
const v = msg.variants[variantIndex];
|
|
77
|
+
return v?.keys[keyIndex];
|
|
78
|
+
}
|
|
79
|
+
function allKeysAtIndexAreGenderLike(msg, keyIndex) {
|
|
80
|
+
for (let vi = 0; vi < msg.variants.length; vi++) {
|
|
81
|
+
const k = variantKeyAt(msg, vi, keyIndex);
|
|
82
|
+
if (!k) return false;
|
|
83
|
+
if ((0, import_messageformat.isCatchallKey)(k)) continue;
|
|
84
|
+
if ((0, import_messageformat.isLiteral)(k)) {
|
|
85
|
+
if (!GENDER_LABELS.has(k.value)) return false;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
return true;
|
|
91
|
+
}
|
|
92
|
+
function classifySelectMessageForPgs(msg) {
|
|
93
|
+
const out = [];
|
|
94
|
+
for (let si = 0; si < msg.selectors.length; si++) {
|
|
95
|
+
const sel = msg.selectors[si];
|
|
96
|
+
if (!sel || sel.type !== "variable" || !sel.name) return null;
|
|
97
|
+
const varName = sel.name;
|
|
98
|
+
const decl = findInputDeclaration(msg, varName);
|
|
99
|
+
if (!decl) return null;
|
|
100
|
+
const expr = decl.value;
|
|
101
|
+
if (expr.type !== "expression") return null;
|
|
102
|
+
const fname = getFunctionName(expr);
|
|
103
|
+
if (fname === "number") {
|
|
104
|
+
out.push({
|
|
105
|
+
kind: getSelectOptionOrdinal(expr) ? "ordinal" : "plural",
|
|
106
|
+
name: varName
|
|
107
|
+
});
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (fname === "string") {
|
|
111
|
+
const kind = allKeysAtIndexAreGenderLike(msg, si) ? "gender" : "select";
|
|
112
|
+
out.push({ kind, name: varName });
|
|
113
|
+
continue;
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
return out.length === msg.selectors.length ? out : null;
|
|
118
|
+
}
|
|
119
|
+
function mf2KeyToPgsToken(key, kind) {
|
|
120
|
+
if ((0, import_messageformat.isCatchallKey)(key)) {
|
|
121
|
+
return "other";
|
|
122
|
+
}
|
|
123
|
+
if ((0, import_messageformat.isLiteral)(key)) {
|
|
124
|
+
return key.value;
|
|
125
|
+
}
|
|
126
|
+
return "other";
|
|
127
|
+
}
|
|
128
|
+
function stringifyPattern(pattern) {
|
|
129
|
+
const wrap = {
|
|
130
|
+
type: "message",
|
|
131
|
+
declarations: [],
|
|
132
|
+
pattern
|
|
133
|
+
};
|
|
134
|
+
return (0, import_messageformat.stringifyMessage)(
|
|
135
|
+
wrap
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
function selectMessageDataToPgsExport(msg) {
|
|
139
|
+
if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
|
|
140
|
+
const classification = classifySelectMessageForPgs(
|
|
141
|
+
msg
|
|
142
|
+
);
|
|
143
|
+
if (!classification) return null;
|
|
144
|
+
const switchAttr = classification.map((c) => `${c.kind}:${c.name}`).join(" ");
|
|
145
|
+
const segments = [];
|
|
146
|
+
const sel = msg;
|
|
147
|
+
for (let vi = 0; vi < sel.variants.length; vi++) {
|
|
148
|
+
const variant = sel.variants[vi];
|
|
149
|
+
const caseParts = [];
|
|
150
|
+
for (let ki = 0; ki < classification.length; ki++) {
|
|
151
|
+
const key = variant.keys[ki];
|
|
152
|
+
if (!key) return null;
|
|
153
|
+
caseParts.push(mf2KeyToPgsToken(key, classification[ki].kind));
|
|
154
|
+
}
|
|
155
|
+
segments.push({
|
|
156
|
+
caseAttr: caseParts.join(" "),
|
|
157
|
+
sourcePattern: stringifyPattern(variant.value)
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
return { switchAttr, segments };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/lib/pgs-mf1.ts
|
|
164
|
+
function mf1MessageToPgsExport(source) {
|
|
165
|
+
try {
|
|
166
|
+
const data = (0, import_icu_messageformat_1.mf1ToMessageData)((0, import_parser.parse)(source));
|
|
167
|
+
const exp = selectMessageDataToPgsExport(data);
|
|
168
|
+
if (!exp) return null;
|
|
169
|
+
const parsedSwitch = parsePgsSwitch(exp.switchAttr);
|
|
170
|
+
if (!parsedSwitch) return null;
|
|
171
|
+
const hashVars = pluralVarNames(parsedSwitch);
|
|
172
|
+
return {
|
|
173
|
+
switchAttr: exp.switchAttr,
|
|
174
|
+
segments: exp.segments.map((seg) => ({
|
|
175
|
+
caseAttr: seg.caseAttr,
|
|
176
|
+
sourcePattern: segmentBodyToMf1(seg.sourcePattern, hashVars)
|
|
177
|
+
}))
|
|
178
|
+
};
|
|
179
|
+
} catch {
|
|
180
|
+
return null;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
function kindToIcuType(kind) {
|
|
184
|
+
if (kind === "plural") return "plural";
|
|
185
|
+
if (kind === "ordinal") return "selectordinal";
|
|
186
|
+
return "select";
|
|
187
|
+
}
|
|
188
|
+
function pluralVarNames(parsedSwitch) {
|
|
189
|
+
const names = /* @__PURE__ */ new Set();
|
|
190
|
+
for (const { kind, name } of parsedSwitch) {
|
|
191
|
+
if (kind === "plural" || kind === "ordinal") names.add(name);
|
|
192
|
+
}
|
|
193
|
+
return names;
|
|
194
|
+
}
|
|
195
|
+
function segmentBodyToMf1(body, hashVars) {
|
|
196
|
+
const trimmed = body.trim();
|
|
197
|
+
if (!trimmed) return "";
|
|
198
|
+
try {
|
|
199
|
+
const msg = (0, import_messageformat2.parseMessage)(trimmed);
|
|
200
|
+
if (msg.type !== "message") {
|
|
201
|
+
return escapeIcuLiteral(trimmed);
|
|
202
|
+
}
|
|
203
|
+
return patternPartsToMf1(
|
|
204
|
+
msg.pattern,
|
|
205
|
+
hashVars
|
|
206
|
+
);
|
|
207
|
+
} catch {
|
|
208
|
+
return escapeIcuLiteral(trimmed);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function patternPartsToMf1(pattern, hashVars) {
|
|
212
|
+
let out = "";
|
|
213
|
+
for (const part of pattern) {
|
|
214
|
+
if (typeof part === "string") {
|
|
215
|
+
out += escapeIcuLiteral(part);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (!part || typeof part !== "object") continue;
|
|
219
|
+
const expr = part;
|
|
220
|
+
if (expr.type === "expression" && expr.arg?.type === "variable" && expr.arg.name) {
|
|
221
|
+
const name = expr.arg.name;
|
|
222
|
+
out += hashVars.has(name) ? "#" : `{${name}}`;
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return out;
|
|
227
|
+
}
|
|
228
|
+
function escapeIcuLiteral(s) {
|
|
229
|
+
return s.replace(/'/g, "''").replace(/([{}])/g, "'$1'");
|
|
230
|
+
}
|
|
231
|
+
function insertCase(tree, tokens, body, depth) {
|
|
232
|
+
if (tokens.length === 0) return false;
|
|
233
|
+
const token = tokens[depth];
|
|
234
|
+
if (token === void 0) return false;
|
|
235
|
+
if (depth === tokens.length - 1) {
|
|
236
|
+
tree.set(token, body);
|
|
237
|
+
return true;
|
|
238
|
+
}
|
|
239
|
+
let child = tree.get(token);
|
|
240
|
+
if (typeof child === "string") return false;
|
|
241
|
+
if (!child) {
|
|
242
|
+
child = /* @__PURE__ */ new Map();
|
|
243
|
+
tree.set(token, child);
|
|
244
|
+
}
|
|
245
|
+
return insertCase(child, tokens, body, depth + 1);
|
|
246
|
+
}
|
|
247
|
+
function serializeCaseTree(tree, parsedSwitch, depth, hashVars) {
|
|
248
|
+
const item = parsedSwitch[depth];
|
|
249
|
+
if (!item) return null;
|
|
250
|
+
const icuType = kindToIcuType(item.kind);
|
|
251
|
+
const parts = [];
|
|
252
|
+
for (const [key, child] of tree) {
|
|
253
|
+
let content;
|
|
254
|
+
if (typeof child === "string") {
|
|
255
|
+
content = segmentBodyToMf1(child, hashVars);
|
|
256
|
+
} else {
|
|
257
|
+
const nested = serializeCaseTree(
|
|
258
|
+
child,
|
|
259
|
+
parsedSwitch,
|
|
260
|
+
depth + 1,
|
|
261
|
+
hashVars
|
|
262
|
+
);
|
|
263
|
+
if (nested == null) return null;
|
|
264
|
+
content = nested;
|
|
265
|
+
}
|
|
266
|
+
parts.push(`${key} {${content}}`);
|
|
267
|
+
}
|
|
268
|
+
if (parts.length === 0) return null;
|
|
269
|
+
return `{${item.name}, ${icuType}, ${parts.join(" ")}}`;
|
|
270
|
+
}
|
|
271
|
+
function pgsImportToMf1Message(switchAttr, segments) {
|
|
272
|
+
const parsedSwitch = parsePgsSwitch(switchAttr);
|
|
273
|
+
if (!parsedSwitch || parsedSwitch.length === 0) return null;
|
|
274
|
+
const n = parsedSwitch.length;
|
|
275
|
+
if (segments.length === 0) return null;
|
|
276
|
+
const hashVars = pluralVarNames(parsedSwitch);
|
|
277
|
+
const tree = /* @__PURE__ */ new Map();
|
|
278
|
+
for (const seg of segments) {
|
|
279
|
+
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
280
|
+
if (tokens.length !== n) return null;
|
|
281
|
+
if (!insertCase(tree, tokens, seg.body, 0)) return null;
|
|
282
|
+
}
|
|
283
|
+
return serializeCaseTree(tree, parsedSwitch, 0, hashVars);
|
|
284
|
+
}
|
|
285
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
286
|
+
0 && (module.exports = {
|
|
287
|
+
mf1MessageToPgsExport,
|
|
288
|
+
pgsImportToMf1Message
|
|
289
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { PgsSegmentExport, PgsSegmentImport } from './pgs-mf2.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Maps between ICU MessageFormat 1 plural/selectordinal/select messages and
|
|
5
|
+
* XLIFF 2.2 PGS (`pgs:switch` / `pgs:case`).
|
|
6
|
+
*
|
|
7
|
+
* Export path: MF1 → MF2 data model (via `@messageformat/icu-messageformat-1`)
|
|
8
|
+
* → shared PGS classification used by `pgs-mf2.ts`, with segment bodies
|
|
9
|
+
* rewritten to ICU-friendly text (`#` / `{name}`).
|
|
10
|
+
* Import path: PGS → nested ICU MF1 string.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* If `source` is an MF1 plural/selectordinal/select message that maps to PGS,
|
|
15
|
+
* returns switch/case segments; otherwise null (caller falls back to plain XLIFF).
|
|
16
|
+
* Segment bodies use ICU MF1 conventions (`#` for plural/ordinal vars).
|
|
17
|
+
*/
|
|
18
|
+
declare function mf1MessageToPgsExport(source: string): {
|
|
19
|
+
switchAttr: string;
|
|
20
|
+
segments: PgsSegmentExport[];
|
|
21
|
+
} | null;
|
|
22
|
+
/**
|
|
23
|
+
* Builds an ICU MessageFormat 1 string from PGS `pgs:switch` and segment bodies.
|
|
24
|
+
* Multi-selector switches are nested as ICU select/plural trees.
|
|
25
|
+
*/
|
|
26
|
+
declare function pgsImportToMf1Message(switchAttr: string, segments: PgsSegmentImport[]): string | null;
|
|
27
|
+
|
|
28
|
+
export { mf1MessageToPgsExport, pgsImportToMf1Message };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { PgsSegmentExport, PgsSegmentImport } from './pgs-mf2.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Maps between ICU MessageFormat 1 plural/selectordinal/select messages and
|
|
5
|
+
* XLIFF 2.2 PGS (`pgs:switch` / `pgs:case`).
|
|
6
|
+
*
|
|
7
|
+
* Export path: MF1 → MF2 data model (via `@messageformat/icu-messageformat-1`)
|
|
8
|
+
* → shared PGS classification used by `pgs-mf2.ts`, with segment bodies
|
|
9
|
+
* rewritten to ICU-friendly text (`#` / `{name}`).
|
|
10
|
+
* Import path: PGS → nested ICU MF1 string.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* If `source` is an MF1 plural/selectordinal/select message that maps to PGS,
|
|
15
|
+
* returns switch/case segments; otherwise null (caller falls back to plain XLIFF).
|
|
16
|
+
* Segment bodies use ICU MF1 conventions (`#` for plural/ordinal vars).
|
|
17
|
+
*/
|
|
18
|
+
declare function mf1MessageToPgsExport(source: string): {
|
|
19
|
+
switchAttr: string;
|
|
20
|
+
segments: PgsSegmentExport[];
|
|
21
|
+
} | null;
|
|
22
|
+
/**
|
|
23
|
+
* Builds an ICU MessageFormat 1 string from PGS `pgs:switch` and segment bodies.
|
|
24
|
+
* Multi-selector switches are nested as ICU select/plural trees.
|
|
25
|
+
*/
|
|
26
|
+
declare function pgsImportToMf1Message(switchAttr: string, segments: PgsSegmentImport[]): string | null;
|
|
27
|
+
|
|
28
|
+
export { mf1MessageToPgsExport, pgsImportToMf1Message };
|
package/dist/lib/pgs-mf2.cjs
CHANGED
|
@@ -26,6 +26,7 @@ __export(pgs_mf2_exports, {
|
|
|
26
26
|
parsePgsSwitch: () => parsePgsSwitch,
|
|
27
27
|
pgsImportToSelectMessage: () => pgsImportToSelectMessage,
|
|
28
28
|
pgsTokenToMf2Key: () => pgsTokenToMf2Key,
|
|
29
|
+
selectMessageDataToPgsExport: () => selectMessageDataToPgsExport,
|
|
29
30
|
selectMessageToPgsExport: () => selectMessageToPgsExport
|
|
30
31
|
});
|
|
31
32
|
module.exports = __toCommonJS(pgs_mf2_exports);
|
|
@@ -61,10 +62,13 @@ function findInputDeclaration(msg, varName) {
|
|
|
61
62
|
function getFunctionName(expr) {
|
|
62
63
|
return expr.functionRef?.name;
|
|
63
64
|
}
|
|
65
|
+
function getOption(opts, key) {
|
|
66
|
+
if (!opts) return void 0;
|
|
67
|
+
if (opts instanceof Map) return opts.get(key);
|
|
68
|
+
return opts[key];
|
|
69
|
+
}
|
|
64
70
|
function getSelectOptionOrdinal(expr) {
|
|
65
|
-
const
|
|
66
|
-
if (!opts || !(opts instanceof Map)) return false;
|
|
67
|
-
const sel = opts.get("select");
|
|
71
|
+
const sel = getOption(expr.functionRef?.options, "select");
|
|
68
72
|
if (!sel || typeof sel !== "object" || sel === null) return false;
|
|
69
73
|
if (sel.type !== "literal") return false;
|
|
70
74
|
return sel.value === "ordinal";
|
|
@@ -141,13 +145,7 @@ function stringifyPattern(pattern) {
|
|
|
141
145
|
wrap
|
|
142
146
|
);
|
|
143
147
|
}
|
|
144
|
-
function
|
|
145
|
-
let msg;
|
|
146
|
-
try {
|
|
147
|
-
msg = (0, import_messageformat.parseMessage)(source);
|
|
148
|
-
} catch {
|
|
149
|
-
return null;
|
|
150
|
-
}
|
|
148
|
+
function selectMessageDataToPgsExport(msg) {
|
|
151
149
|
if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
|
|
152
150
|
const classification = classifySelectMessageForPgs(
|
|
153
151
|
msg
|
|
@@ -162,9 +160,7 @@ function selectMessageToPgsExport(source) {
|
|
|
162
160
|
for (let ki = 0; ki < classification.length; ki++) {
|
|
163
161
|
const key = variant.keys[ki];
|
|
164
162
|
if (!key) return null;
|
|
165
|
-
caseParts.push(
|
|
166
|
-
mf2KeyToPgsToken(key, classification[ki].kind)
|
|
167
|
-
);
|
|
163
|
+
caseParts.push(mf2KeyToPgsToken(key, classification[ki].kind));
|
|
168
164
|
}
|
|
169
165
|
segments.push({
|
|
170
166
|
caseAttr: caseParts.join(" "),
|
|
@@ -173,6 +169,15 @@ function selectMessageToPgsExport(source) {
|
|
|
173
169
|
}
|
|
174
170
|
return { switchAttr, segments };
|
|
175
171
|
}
|
|
172
|
+
function selectMessageToPgsExport(source) {
|
|
173
|
+
let msg;
|
|
174
|
+
try {
|
|
175
|
+
msg = (0, import_messageformat.parseMessage)(source);
|
|
176
|
+
} catch {
|
|
177
|
+
return null;
|
|
178
|
+
}
|
|
179
|
+
return selectMessageDataToPgsExport(msg);
|
|
180
|
+
}
|
|
176
181
|
function parsePatternFromSegmentBody(body) {
|
|
177
182
|
const trimmed = body.trim();
|
|
178
183
|
if (!trimmed) return [];
|
|
@@ -288,5 +293,6 @@ function messagesStructurallyEqual(a, b) {
|
|
|
288
293
|
parsePgsSwitch,
|
|
289
294
|
pgsImportToSelectMessage,
|
|
290
295
|
pgsTokenToMf2Key,
|
|
296
|
+
selectMessageDataToPgsExport,
|
|
291
297
|
selectMessageToPgsExport
|
|
292
298
|
});
|
package/dist/lib/pgs-mf2.d.mts
CHANGED
|
@@ -50,6 +50,14 @@ interface PgsSegmentExport {
|
|
|
50
50
|
/** MF2 pattern body for this variant (not necessarily wrapped in {{ }}). */
|
|
51
51
|
sourcePattern: string;
|
|
52
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* If `msg` is a classifiable MF2 select (`.match`) data model, returns PGS
|
|
55
|
+
* attributes and per-variant segments; otherwise null.
|
|
56
|
+
*/
|
|
57
|
+
declare function selectMessageDataToPgsExport(msg: unknown): {
|
|
58
|
+
switchAttr: string;
|
|
59
|
+
segments: PgsSegmentExport[];
|
|
60
|
+
} | null;
|
|
53
61
|
/**
|
|
54
62
|
* If `source` is a classifiable `.match` message, returns PGS attributes and
|
|
55
63
|
* per-variant segments; otherwise null (caller falls back to plain XLIFF).
|
|
@@ -70,4 +78,4 @@ declare function pgsImportToSelectMessage(switchAttr: string, segments: PgsSegme
|
|
|
70
78
|
/** Compare two MF2 strings by parsing and re-stringifying both (for tests). */
|
|
71
79
|
declare function messagesStructurallyEqual(a: string, b: string): boolean;
|
|
72
80
|
|
|
73
|
-
export { type ParsedPgsSwitch, type PgsSegmentExport, type PgsSegmentImport, type PgsSelectorKind, type VariantKey, classifySelectMessageForPgs, messagesStructurallyEqual, mf2KeyToPgsToken, parsePgsSwitch, pgsImportToSelectMessage, pgsTokenToMf2Key, selectMessageToPgsExport };
|
|
81
|
+
export { type ParsedPgsSwitch, type PgsSegmentExport, type PgsSegmentImport, type PgsSelectorKind, type SelectMessage, type VariantKey, classifySelectMessageForPgs, messagesStructurallyEqual, mf2KeyToPgsToken, parsePgsSwitch, pgsImportToSelectMessage, pgsTokenToMf2Key, selectMessageDataToPgsExport, selectMessageToPgsExport };
|
package/dist/lib/pgs-mf2.d.ts
CHANGED
|
@@ -50,6 +50,14 @@ interface PgsSegmentExport {
|
|
|
50
50
|
/** MF2 pattern body for this variant (not necessarily wrapped in {{ }}). */
|
|
51
51
|
sourcePattern: string;
|
|
52
52
|
}
|
|
53
|
+
/**
|
|
54
|
+
* If `msg` is a classifiable MF2 select (`.match`) data model, returns PGS
|
|
55
|
+
* attributes and per-variant segments; otherwise null.
|
|
56
|
+
*/
|
|
57
|
+
declare function selectMessageDataToPgsExport(msg: unknown): {
|
|
58
|
+
switchAttr: string;
|
|
59
|
+
segments: PgsSegmentExport[];
|
|
60
|
+
} | null;
|
|
53
61
|
/**
|
|
54
62
|
* If `source` is a classifiable `.match` message, returns PGS attributes and
|
|
55
63
|
* per-variant segments; otherwise null (caller falls back to plain XLIFF).
|
|
@@ -70,4 +78,4 @@ declare function pgsImportToSelectMessage(switchAttr: string, segments: PgsSegme
|
|
|
70
78
|
/** Compare two MF2 strings by parsing and re-stringifying both (for tests). */
|
|
71
79
|
declare function messagesStructurallyEqual(a: string, b: string): boolean;
|
|
72
80
|
|
|
73
|
-
export { type ParsedPgsSwitch, type PgsSegmentExport, type PgsSegmentImport, type PgsSelectorKind, type VariantKey, classifySelectMessageForPgs, messagesStructurallyEqual, mf2KeyToPgsToken, parsePgsSwitch, pgsImportToSelectMessage, pgsTokenToMf2Key, selectMessageToPgsExport };
|
|
81
|
+
export { type ParsedPgsSwitch, type PgsSegmentExport, type PgsSegmentImport, type PgsSelectorKind, type SelectMessage, type VariantKey, classifySelectMessageForPgs, messagesStructurallyEqual, mf2KeyToPgsToken, parsePgsSwitch, pgsImportToSelectMessage, pgsTokenToMf2Key, selectMessageDataToPgsExport, selectMessageToPgsExport };
|
package/dist/lib/pgs-mf2.mjs
CHANGED
|
@@ -5,8 +5,9 @@ import {
|
|
|
5
5
|
parsePgsSwitch,
|
|
6
6
|
pgsImportToSelectMessage,
|
|
7
7
|
pgsTokenToMf2Key,
|
|
8
|
+
selectMessageDataToPgsExport,
|
|
8
9
|
selectMessageToPgsExport
|
|
9
|
-
} from "../chunk-
|
|
10
|
+
} from "../chunk-MGHC4AYZ.mjs";
|
|
10
11
|
export {
|
|
11
12
|
classifySelectMessageForPgs,
|
|
12
13
|
messagesStructurallyEqual,
|
|
@@ -14,5 +15,6 @@ export {
|
|
|
14
15
|
parsePgsSwitch,
|
|
15
16
|
pgsImportToSelectMessage,
|
|
16
17
|
pgsTokenToMf2Key,
|
|
18
|
+
selectMessageDataToPgsExport,
|
|
17
19
|
selectMessageToPgsExport
|
|
18
20
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldware/msg-cli",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "CLI for msg library",
|
|
5
5
|
"main": "dist/commands/init.cjs",
|
|
6
6
|
"module": "dist/commands/init.mjs",
|
|
@@ -8,7 +8,9 @@
|
|
|
8
8
|
"scripts": {
|
|
9
9
|
"test": "vitest run",
|
|
10
10
|
"build": "tsup",
|
|
11
|
-
"
|
|
11
|
+
"prepack": "npm run build",
|
|
12
|
+
"coverage": "vitest run --coverage",
|
|
13
|
+
"docs": "typedoc --entryPointStrategy expand src/commands src/lib"
|
|
12
14
|
},
|
|
13
15
|
"repository": {
|
|
14
16
|
"type": "git",
|
|
@@ -20,6 +22,10 @@
|
|
|
20
22
|
"url": "https://github.com/worldware-studios/msg-cli/issues"
|
|
21
23
|
},
|
|
22
24
|
"homepage": "https://github.com/worldware-studios/msg-cli#readme",
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public",
|
|
27
|
+
"provenance": true
|
|
28
|
+
},
|
|
23
29
|
"bin": {
|
|
24
30
|
"msg": "./bin/run.js"
|
|
25
31
|
},
|
|
@@ -30,16 +36,20 @@
|
|
|
30
36
|
"topicSeparator": " "
|
|
31
37
|
},
|
|
32
38
|
"dependencies": {
|
|
39
|
+
"@messageformat/icu-messageformat-1": "^0.12.0",
|
|
40
|
+
"@messageformat/parser": "^5.1.1",
|
|
33
41
|
"@oclif/core": "^3.21.2",
|
|
34
|
-
"@worldware/msg": "^0.
|
|
42
|
+
"@worldware/msg": "^0.8.1",
|
|
35
43
|
"fast-xml-parser": "^5.3.4",
|
|
36
44
|
"messageformat": "^4.0.0-10"
|
|
37
45
|
},
|
|
38
46
|
"devDependencies": {
|
|
39
47
|
"@types/node": "^22.0.0",
|
|
40
48
|
"@vitest/coverage-v8": "^4.0.18",
|
|
49
|
+
"semantic-release": "^25.0.8",
|
|
41
50
|
"ts-node": "^10.9.2",
|
|
42
51
|
"tsup": "^8.5.1",
|
|
52
|
+
"typedoc": "^0.28.20",
|
|
43
53
|
"typescript": "^5.9.3",
|
|
44
54
|
"vitest": "^4.0.18"
|
|
45
55
|
},
|