@worldware/msg-cli 0.2.2 → 0.3.0

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.
@@ -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.0 string.
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
  */
@@ -5,7 +5,8 @@ import {
5
5
  importMsgResourcesFromPaths,
6
6
  serializeResourceGroupsToXliff,
7
7
  writeXliffFiles
8
- } from "../chunk-BM3O7ZJW.mjs";
8
+ } from "../chunk-XHD7F45F.mjs";
9
+ import "../chunk-DPUF7YUH.mjs";
9
10
  import "../chunk-LNCUPVEF.mjs";
10
11
  import "../chunk-F7XZRKP7.mjs";
11
12
  export {
@@ -64,6 +64,129 @@ async function dynamicImportFromUrl(url) {
64
64
  }
65
65
  }
66
66
 
67
+ // src/lib/pgs-mf2.ts
68
+ var import_messageformat = require("messageformat");
69
+ var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
70
+ function parsePgsSwitch(value) {
71
+ const trimmed = value.trim();
72
+ if (!trimmed) return null;
73
+ const parts = trimmed.split(/\s+/);
74
+ const out = [];
75
+ for (const p of parts) {
76
+ const m = p.match(PGS_SWITCH_ITEM);
77
+ if (!m) return null;
78
+ out.push({ kind: m[1], name: m[2] });
79
+ }
80
+ return out.length > 0 ? out : null;
81
+ }
82
+ function pgsTokenToMf2Key(token, kind) {
83
+ if (kind === "plural" || kind === "ordinal") {
84
+ if (token === "other") {
85
+ return { type: "*" };
86
+ }
87
+ return { type: "literal", value: token };
88
+ }
89
+ return { type: "literal", value: token };
90
+ }
91
+ function parsePatternFromSegmentBody(body) {
92
+ const trimmed = body.trim();
93
+ if (!trimmed) return [];
94
+ try {
95
+ const m = (0, import_messageformat.parseMessage)(trimmed);
96
+ if (m.type !== "message") return [];
97
+ return m.pattern;
98
+ } catch {
99
+ return [];
100
+ }
101
+ }
102
+ function ensureFallbackVariant(keysCount, variants) {
103
+ const hasAllStars = variants.some(
104
+ (v) => v.keys.length === keysCount && v.keys.every((k) => (0, import_messageformat.isCatchallKey)(k))
105
+ );
106
+ if (hasAllStars) return;
107
+ const fallbackPattern = variants.length > 0 ? variants[variants.length - 1].value : [];
108
+ const starKeys = Array.from({ length: keysCount }, () => ({
109
+ type: "*"
110
+ }));
111
+ variants.push({ keys: starKeys, value: fallbackPattern });
112
+ }
113
+ function pgsImportToSelectMessage(switchAttr, segments) {
114
+ const parsedSwitch = parsePgsSwitch(switchAttr);
115
+ if (!parsedSwitch || parsedSwitch.length === 0) return null;
116
+ const n = parsedSwitch.length;
117
+ const declarations = [];
118
+ for (const { kind, name } of parsedSwitch) {
119
+ const varRef = { type: "variable", name };
120
+ if (kind === "plural") {
121
+ declarations.push({
122
+ type: "input",
123
+ name,
124
+ value: {
125
+ type: "expression",
126
+ arg: varRef,
127
+ functionRef: { type: "function", name: "number" }
128
+ }
129
+ });
130
+ } else if (kind === "ordinal") {
131
+ const opts = /* @__PURE__ */ new Map([
132
+ ["select", { type: "literal", value: "ordinal" }]
133
+ ]);
134
+ declarations.push({
135
+ type: "input",
136
+ name,
137
+ value: {
138
+ type: "expression",
139
+ arg: varRef,
140
+ functionRef: { type: "function", name: "number", options: opts }
141
+ }
142
+ });
143
+ } else {
144
+ declarations.push({
145
+ type: "input",
146
+ name,
147
+ value: {
148
+ type: "expression",
149
+ arg: varRef,
150
+ functionRef: { type: "function", name: "string" }
151
+ }
152
+ });
153
+ }
154
+ }
155
+ const selectors = parsedSwitch.map(
156
+ ({ name }) => ({ type: "variable", name })
157
+ );
158
+ const variants = [];
159
+ for (const seg of segments) {
160
+ const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
161
+ if (tokens.length !== n) return null;
162
+ const keys = tokens.map(
163
+ (t, i) => pgsTokenToMf2Key(t, parsedSwitch[i].kind)
164
+ );
165
+ variants.push({
166
+ keys,
167
+ value: parsePatternFromSegmentBody(seg.body)
168
+ });
169
+ }
170
+ if (variants.length === 0) return null;
171
+ const msg = {
172
+ type: "select",
173
+ declarations,
174
+ selectors,
175
+ variants
176
+ };
177
+ ensureFallbackVariant(n, msg.variants);
178
+ try {
179
+ (0, import_messageformat.validate)(
180
+ msg
181
+ );
182
+ } catch {
183
+ return null;
184
+ }
185
+ return (0, import_messageformat.stringifyMessage)(
186
+ msg
187
+ );
188
+ }
189
+
67
190
  // src/lib/import-helpers.ts
68
191
  var XLIFF_EXTENSIONS = [".xliff", ".xlf"];
69
192
  var XLIFF_FILENAME_PATTERN = /^(.+?)(?:\.([a-zA-Z]{2,}(?:-[a-zA-Z0-9]+)*))?\.(?:xliff|xlf)$/i;
@@ -166,7 +289,7 @@ function parseXliff20(xml) {
166
289
  return parsed;
167
290
  } catch (err) {
168
291
  const message = err instanceof Error ? err.message : "Failed to parse XLIFF";
169
- throw new Error(`Malformed XLIFF 2.0: ${message}`);
292
+ throw new Error(`Malformed XLIFF: ${message}`);
170
293
  }
171
294
  }
172
295
  function categoryToNoteType(category) {
@@ -253,11 +376,32 @@ function processUnit(unit, fileAttrs) {
253
376
  }
254
377
  }
255
378
  const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
256
- const targetParts = [];
257
- for (const seg of segments) {
258
- targetParts.push(extractSegmentText(seg));
379
+ const pgsSwitch = unit["@_pgs:switch"];
380
+ let value;
381
+ if (pgsSwitch && segments.length > 0 && segments.every(
382
+ (seg) => typeof seg["@_pgs:case"] === "string"
383
+ )) {
384
+ const importSegments = segments.map((seg) => ({
385
+ caseAttr: seg["@_pgs:case"],
386
+ body: extractSegmentText(seg)
387
+ }));
388
+ const rebuilt = pgsImportToSelectMessage(pgsSwitch, importSegments);
389
+ if (rebuilt) {
390
+ value = rebuilt;
391
+ } else {
392
+ const targetParts = [];
393
+ for (const seg of segments) {
394
+ targetParts.push(extractSegmentText(seg));
395
+ }
396
+ value = targetParts.join("");
397
+ }
398
+ } else {
399
+ const targetParts = [];
400
+ for (const seg of segments) {
401
+ targetParts.push(extractSegmentText(seg));
402
+ }
403
+ value = targetParts.join("");
259
404
  }
260
- const value = targetParts.join("");
261
405
  const attributes = {};
262
406
  if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
263
407
  if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
@@ -45,14 +45,14 @@ declare function importMsgProject(projectsDir: string, projectName: string): Pro
45
45
  };
46
46
  }>;
47
47
  /**
48
- * Parses XLIFF 2.0 XML string to a JavaScript object.
49
- * @param xml - XLIFF 2.0 XML string
48
+ * Parses XLIFF 2.x XML string to a JavaScript object (2.0 and 2.2 supported).
49
+ * @param xml - XLIFF XML string
50
50
  * @returns Parsed object
51
51
  * @throws Error if XML is malformed
52
52
  */
53
53
  declare function parseXliff20(xml: string): object;
54
54
  /**
55
- * Extracts MsgResource from parsed XLIFF 2.0 object for a single file element.
55
+ * Extracts MsgResource from parsed XLIFF 2.x object for a single file element.
56
56
  * @param fileEl - Parsed file element
57
57
  * @param xliffTrgLang - trgLang from xliff root (may be undefined for monolingual)
58
58
  * @param project - MsgProject instance
@@ -45,14 +45,14 @@ declare function importMsgProject(projectsDir: string, projectName: string): Pro
45
45
  };
46
46
  }>;
47
47
  /**
48
- * Parses XLIFF 2.0 XML string to a JavaScript object.
49
- * @param xml - XLIFF 2.0 XML string
48
+ * Parses XLIFF 2.x XML string to a JavaScript object (2.0 and 2.2 supported).
49
+ * @param xml - XLIFF XML string
50
50
  * @returns Parsed object
51
51
  * @throws Error if XML is malformed
52
52
  */
53
53
  declare function parseXliff20(xml: string): object;
54
54
  /**
55
- * Extracts MsgResource from parsed XLIFF 2.0 object for a single file element.
55
+ * Extracts MsgResource from parsed XLIFF 2.x object for a single file element.
56
56
  * @param fileEl - Parsed file element
57
57
  * @param xliffTrgLang - trgLang from xliff root (may be undefined for monolingual)
58
58
  * @param project - MsgProject instance
@@ -8,7 +8,8 @@ import {
8
8
  parseXliffFilename,
9
9
  processXliffFile,
10
10
  writeTranslationFiles
11
- } from "../chunk-KKATA5YE.mjs";
11
+ } from "../chunk-DIY2WO3R.mjs";
12
+ import "../chunk-DPUF7YUH.mjs";
12
13
  import "../chunk-LNCUPVEF.mjs";
13
14
  import "../chunk-F7XZRKP7.mjs";
14
15
  export {
@@ -0,0 +1,292 @@
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-mf2.ts
21
+ var pgs_mf2_exports = {};
22
+ __export(pgs_mf2_exports, {
23
+ classifySelectMessageForPgs: () => classifySelectMessageForPgs,
24
+ messagesStructurallyEqual: () => messagesStructurallyEqual,
25
+ mf2KeyToPgsToken: () => mf2KeyToPgsToken,
26
+ parsePgsSwitch: () => parsePgsSwitch,
27
+ pgsImportToSelectMessage: () => pgsImportToSelectMessage,
28
+ pgsTokenToMf2Key: () => pgsTokenToMf2Key,
29
+ selectMessageToPgsExport: () => selectMessageToPgsExport
30
+ });
31
+ module.exports = __toCommonJS(pgs_mf2_exports);
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 getSelectOptionOrdinal(expr) {
65
+ const opts = expr.functionRef?.options;
66
+ if (!opts || !(opts instanceof Map)) return false;
67
+ const sel = opts.get("select");
68
+ if (!sel || typeof sel !== "object" || sel === null) return false;
69
+ if (sel.type !== "literal") return false;
70
+ return sel.value === "ordinal";
71
+ }
72
+ function variantKeyAt(msg, variantIndex, keyIndex) {
73
+ const v = msg.variants[variantIndex];
74
+ return v?.keys[keyIndex];
75
+ }
76
+ function allKeysAtIndexAreGenderLike(msg, keyIndex) {
77
+ for (let vi = 0; vi < msg.variants.length; vi++) {
78
+ const k = variantKeyAt(msg, vi, keyIndex);
79
+ if (!k) return false;
80
+ if ((0, import_messageformat.isCatchallKey)(k)) continue;
81
+ if ((0, import_messageformat.isLiteral)(k)) {
82
+ if (!GENDER_LABELS.has(k.value)) return false;
83
+ continue;
84
+ }
85
+ return false;
86
+ }
87
+ return true;
88
+ }
89
+ function classifySelectMessageForPgs(msg) {
90
+ const out = [];
91
+ for (let si = 0; si < msg.selectors.length; si++) {
92
+ const sel = msg.selectors[si];
93
+ if (!sel || sel.type !== "variable" || !sel.name) return null;
94
+ const varName = sel.name;
95
+ const decl = findInputDeclaration(msg, varName);
96
+ if (!decl) return null;
97
+ const expr = decl.value;
98
+ if (expr.type !== "expression") return null;
99
+ const fname = getFunctionName(expr);
100
+ if (fname === "number") {
101
+ out.push({
102
+ kind: getSelectOptionOrdinal(expr) ? "ordinal" : "plural",
103
+ name: varName
104
+ });
105
+ continue;
106
+ }
107
+ if (fname === "string") {
108
+ const kind = allKeysAtIndexAreGenderLike(msg, si) ? "gender" : "select";
109
+ out.push({ kind, name: varName });
110
+ continue;
111
+ }
112
+ return null;
113
+ }
114
+ return out.length === msg.selectors.length ? out : null;
115
+ }
116
+ function mf2KeyToPgsToken(key, kind) {
117
+ if ((0, import_messageformat.isCatchallKey)(key)) {
118
+ return "other";
119
+ }
120
+ if ((0, import_messageformat.isLiteral)(key)) {
121
+ return key.value;
122
+ }
123
+ return "other";
124
+ }
125
+ function pgsTokenToMf2Key(token, kind) {
126
+ if (kind === "plural" || kind === "ordinal") {
127
+ if (token === "other") {
128
+ return { type: "*" };
129
+ }
130
+ return { type: "literal", value: token };
131
+ }
132
+ return { type: "literal", value: token };
133
+ }
134
+ function stringifyPattern(pattern) {
135
+ const wrap = {
136
+ type: "message",
137
+ declarations: [],
138
+ pattern
139
+ };
140
+ return (0, import_messageformat.stringifyMessage)(
141
+ wrap
142
+ );
143
+ }
144
+ function selectMessageToPgsExport(source) {
145
+ let msg;
146
+ try {
147
+ msg = (0, import_messageformat.parseMessage)(source);
148
+ } catch {
149
+ return null;
150
+ }
151
+ if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
152
+ const classification = classifySelectMessageForPgs(
153
+ msg
154
+ );
155
+ if (!classification) return null;
156
+ const switchAttr = classification.map((c) => `${c.kind}:${c.name}`).join(" ");
157
+ const segments = [];
158
+ const sel = msg;
159
+ for (let vi = 0; vi < sel.variants.length; vi++) {
160
+ const variant = sel.variants[vi];
161
+ const caseParts = [];
162
+ for (let ki = 0; ki < classification.length; ki++) {
163
+ const key = variant.keys[ki];
164
+ if (!key) return null;
165
+ caseParts.push(
166
+ mf2KeyToPgsToken(key, classification[ki].kind)
167
+ );
168
+ }
169
+ segments.push({
170
+ caseAttr: caseParts.join(" "),
171
+ sourcePattern: stringifyPattern(variant.value)
172
+ });
173
+ }
174
+ return { switchAttr, segments };
175
+ }
176
+ function parsePatternFromSegmentBody(body) {
177
+ const trimmed = body.trim();
178
+ if (!trimmed) return [];
179
+ try {
180
+ const m = (0, import_messageformat.parseMessage)(trimmed);
181
+ if (m.type !== "message") return [];
182
+ return m.pattern;
183
+ } catch {
184
+ return [];
185
+ }
186
+ }
187
+ function ensureFallbackVariant(keysCount, variants) {
188
+ const hasAllStars = variants.some(
189
+ (v) => v.keys.length === keysCount && v.keys.every((k) => (0, import_messageformat.isCatchallKey)(k))
190
+ );
191
+ if (hasAllStars) return;
192
+ const fallbackPattern = variants.length > 0 ? variants[variants.length - 1].value : [];
193
+ const starKeys = Array.from({ length: keysCount }, () => ({
194
+ type: "*"
195
+ }));
196
+ variants.push({ keys: starKeys, value: fallbackPattern });
197
+ }
198
+ function pgsImportToSelectMessage(switchAttr, segments) {
199
+ const parsedSwitch = parsePgsSwitch(switchAttr);
200
+ if (!parsedSwitch || parsedSwitch.length === 0) return null;
201
+ const n = parsedSwitch.length;
202
+ const declarations = [];
203
+ for (const { kind, name } of parsedSwitch) {
204
+ const varRef = { type: "variable", name };
205
+ if (kind === "plural") {
206
+ declarations.push({
207
+ type: "input",
208
+ name,
209
+ value: {
210
+ type: "expression",
211
+ arg: varRef,
212
+ functionRef: { type: "function", name: "number" }
213
+ }
214
+ });
215
+ } else if (kind === "ordinal") {
216
+ const opts = /* @__PURE__ */ new Map([
217
+ ["select", { type: "literal", value: "ordinal" }]
218
+ ]);
219
+ declarations.push({
220
+ type: "input",
221
+ name,
222
+ value: {
223
+ type: "expression",
224
+ arg: varRef,
225
+ functionRef: { type: "function", name: "number", options: opts }
226
+ }
227
+ });
228
+ } else {
229
+ declarations.push({
230
+ type: "input",
231
+ name,
232
+ value: {
233
+ type: "expression",
234
+ arg: varRef,
235
+ functionRef: { type: "function", name: "string" }
236
+ }
237
+ });
238
+ }
239
+ }
240
+ const selectors = parsedSwitch.map(
241
+ ({ name }) => ({ type: "variable", name })
242
+ );
243
+ const variants = [];
244
+ for (const seg of segments) {
245
+ const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
246
+ if (tokens.length !== n) return null;
247
+ const keys = tokens.map(
248
+ (t, i) => pgsTokenToMf2Key(t, parsedSwitch[i].kind)
249
+ );
250
+ variants.push({
251
+ keys,
252
+ value: parsePatternFromSegmentBody(seg.body)
253
+ });
254
+ }
255
+ if (variants.length === 0) return null;
256
+ const msg = {
257
+ type: "select",
258
+ declarations,
259
+ selectors,
260
+ variants
261
+ };
262
+ ensureFallbackVariant(n, msg.variants);
263
+ try {
264
+ (0, import_messageformat.validate)(
265
+ msg
266
+ );
267
+ } catch {
268
+ return null;
269
+ }
270
+ return (0, import_messageformat.stringifyMessage)(
271
+ msg
272
+ );
273
+ }
274
+ function messagesStructurallyEqual(a, b) {
275
+ try {
276
+ const ma = (0, import_messageformat.parseMessage)(a);
277
+ const mb = (0, import_messageformat.parseMessage)(b);
278
+ return (0, import_messageformat.stringifyMessage)(ma) === (0, import_messageformat.stringifyMessage)(mb);
279
+ } catch {
280
+ return false;
281
+ }
282
+ }
283
+ // Annotate the CommonJS export names for ESM import in node:
284
+ 0 && (module.exports = {
285
+ classifySelectMessageForPgs,
286
+ messagesStructurallyEqual,
287
+ mf2KeyToPgsToken,
288
+ parsePgsSwitch,
289
+ pgsImportToSelectMessage,
290
+ pgsTokenToMf2Key,
291
+ selectMessageToPgsExport
292
+ });
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Maps between Unicode MessageFormat 2 `.match` messages and XLIFF 2.2 PGS
3
+ * (`pgs:switch` / `pgs:case`, urn:oasis:names:tc:xliff:pgs:1.0).
4
+ *
5
+ * Ordinal vs cardinal :number uses the `select` option passed to PluralRules
6
+ * (`select=ordinal` in MF2 syntax → options Map key `select`, literal `ordinal`).
7
+ */
8
+ /** PGS selector keywords (XLIFF 2.2 Extended Part 2 §4.9). */
9
+ type PgsSelectorKind = "plural" | "ordinal" | "gender" | "select";
10
+ type ParsedPgsSwitch = Array<{
11
+ kind: PgsSelectorKind;
12
+ name: string;
13
+ }>;
14
+ declare function parsePgsSwitch(value: string): ParsedPgsSwitch | null;
15
+ /** MF2 variant key node from `messageformat` (minimal structural typing). */
16
+ type VariantKey = {
17
+ type: "*";
18
+ value?: never;
19
+ } | {
20
+ type: "literal";
21
+ value: string;
22
+ };
23
+ /** Parsed MF2 select (`.match`) message shape from `messageformat`. */
24
+ type SelectMessage = {
25
+ type: "select";
26
+ declarations: unknown[];
27
+ selectors: Array<{
28
+ type: string;
29
+ name?: string;
30
+ }>;
31
+ variants: Array<{
32
+ keys: VariantKey[];
33
+ value: unknown[];
34
+ }>;
35
+ };
36
+ /**
37
+ * Classifies MF2 selectors into PGS kinds. Returns null if unsupported.
38
+ */
39
+ declare function classifySelectMessageForPgs(msg: SelectMessage): ParsedPgsSwitch | null;
40
+ /**
41
+ * Maps an MF2 variant key to a single PGS case token (one selector position).
42
+ */
43
+ declare function mf2KeyToPgsToken(key: VariantKey, kind: PgsSelectorKind): string;
44
+ /**
45
+ * Maps a PGS case token back to an MF2 variant key for one selector.
46
+ */
47
+ declare function pgsTokenToMf2Key(token: string, kind: PgsSelectorKind): VariantKey;
48
+ interface PgsSegmentExport {
49
+ caseAttr: string;
50
+ /** MF2 pattern body for this variant (not necessarily wrapped in {{ }}). */
51
+ sourcePattern: string;
52
+ }
53
+ /**
54
+ * If `source` is a classifiable `.match` message, returns PGS attributes and
55
+ * per-variant segments; otherwise null (caller falls back to plain XLIFF).
56
+ */
57
+ declare function selectMessageToPgsExport(source: string): {
58
+ switchAttr: string;
59
+ segments: PgsSegmentExport[];
60
+ } | null;
61
+ interface PgsSegmentImport {
62
+ caseAttr: string;
63
+ /** Text for target locale (or source when building monolingual). */
64
+ body: string;
65
+ }
66
+ /**
67
+ * Builds MF2 source string from PGS `pgs:switch` and segment bodies.
68
+ */
69
+ declare function pgsImportToSelectMessage(switchAttr: string, segments: PgsSegmentImport[]): string | null;
70
+ /** Compare two MF2 strings by parsing and re-stringifying both (for tests). */
71
+ declare function messagesStructurallyEqual(a: string, b: string): boolean;
72
+
73
+ export { type ParsedPgsSwitch, type PgsSegmentExport, type PgsSegmentImport, type PgsSelectorKind, type VariantKey, classifySelectMessageForPgs, messagesStructurallyEqual, mf2KeyToPgsToken, parsePgsSwitch, pgsImportToSelectMessage, pgsTokenToMf2Key, selectMessageToPgsExport };
@@ -0,0 +1,73 @@
1
+ /**
2
+ * Maps between Unicode MessageFormat 2 `.match` messages and XLIFF 2.2 PGS
3
+ * (`pgs:switch` / `pgs:case`, urn:oasis:names:tc:xliff:pgs:1.0).
4
+ *
5
+ * Ordinal vs cardinal :number uses the `select` option passed to PluralRules
6
+ * (`select=ordinal` in MF2 syntax → options Map key `select`, literal `ordinal`).
7
+ */
8
+ /** PGS selector keywords (XLIFF 2.2 Extended Part 2 §4.9). */
9
+ type PgsSelectorKind = "plural" | "ordinal" | "gender" | "select";
10
+ type ParsedPgsSwitch = Array<{
11
+ kind: PgsSelectorKind;
12
+ name: string;
13
+ }>;
14
+ declare function parsePgsSwitch(value: string): ParsedPgsSwitch | null;
15
+ /** MF2 variant key node from `messageformat` (minimal structural typing). */
16
+ type VariantKey = {
17
+ type: "*";
18
+ value?: never;
19
+ } | {
20
+ type: "literal";
21
+ value: string;
22
+ };
23
+ /** Parsed MF2 select (`.match`) message shape from `messageformat`. */
24
+ type SelectMessage = {
25
+ type: "select";
26
+ declarations: unknown[];
27
+ selectors: Array<{
28
+ type: string;
29
+ name?: string;
30
+ }>;
31
+ variants: Array<{
32
+ keys: VariantKey[];
33
+ value: unknown[];
34
+ }>;
35
+ };
36
+ /**
37
+ * Classifies MF2 selectors into PGS kinds. Returns null if unsupported.
38
+ */
39
+ declare function classifySelectMessageForPgs(msg: SelectMessage): ParsedPgsSwitch | null;
40
+ /**
41
+ * Maps an MF2 variant key to a single PGS case token (one selector position).
42
+ */
43
+ declare function mf2KeyToPgsToken(key: VariantKey, kind: PgsSelectorKind): string;
44
+ /**
45
+ * Maps a PGS case token back to an MF2 variant key for one selector.
46
+ */
47
+ declare function pgsTokenToMf2Key(token: string, kind: PgsSelectorKind): VariantKey;
48
+ interface PgsSegmentExport {
49
+ caseAttr: string;
50
+ /** MF2 pattern body for this variant (not necessarily wrapped in {{ }}). */
51
+ sourcePattern: string;
52
+ }
53
+ /**
54
+ * If `source` is a classifiable `.match` message, returns PGS attributes and
55
+ * per-variant segments; otherwise null (caller falls back to plain XLIFF).
56
+ */
57
+ declare function selectMessageToPgsExport(source: string): {
58
+ switchAttr: string;
59
+ segments: PgsSegmentExport[];
60
+ } | null;
61
+ interface PgsSegmentImport {
62
+ caseAttr: string;
63
+ /** Text for target locale (or source when building monolingual). */
64
+ body: string;
65
+ }
66
+ /**
67
+ * Builds MF2 source string from PGS `pgs:switch` and segment bodies.
68
+ */
69
+ declare function pgsImportToSelectMessage(switchAttr: string, segments: PgsSegmentImport[]): string | null;
70
+ /** Compare two MF2 strings by parsing and re-stringifying both (for tests). */
71
+ declare function messagesStructurallyEqual(a: string, b: string): boolean;
72
+
73
+ export { type ParsedPgsSwitch, type PgsSegmentExport, type PgsSegmentImport, type PgsSelectorKind, type VariantKey, classifySelectMessageForPgs, messagesStructurallyEqual, mf2KeyToPgsToken, parsePgsSwitch, pgsImportToSelectMessage, pgsTokenToMf2Key, selectMessageToPgsExport };