@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 CHANGED
@@ -200,12 +200,22 @@ msg export -p myApp
200
200
  - **Resource attributes** — `dir` → file `srcDir`; `dnt` → file `translate="no"`.
201
201
  - **Message notes** — Emitted as unit-level `<notes>` with category (e.g. `description`, `context`, `parameters`).
202
202
  - **Message attributes** — `dnt` → unit `translate="no"`; message `dir` is serialized as the unit’s `srcDir` attribute (XLIFF text direction for the segment).
203
+ - **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`.
203
204
 
204
- **Plural, gender, select (PGS) and MessageFormat 2:** Messages written in Unicode MessageFormat 2 using `.match` 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) when the selectors classify as `plural`, `ordinal`, `gender`, or `select` (see implementation in `pgs-mf2`). Such units use `xmlns:pgs="urn:oasis:names:tc:xliff:pgs:1.0"`, `pgs:switch` on the `<unit>`, and one `<segment>` per variant with `pgs:case`. Other messages stay a single segment with the full MF2 string in `<source>`. Unsupported `.match` shapes fall back to that single-segment form.
205
+ **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`.
206
+
207
+ - **MF2** — Unicode MessageFormat 2 `.match` messages (see `pgs-mf2`). Segment bodies keep MF2 pattern text. Unsupported `.match` shapes fall back to a single segment with the full string in `<source>`.
208
+ - **MF1** — ICU MessageFormat 1 `plural` / `selectordinal` / `select` messages (see `pgs-mf1`). Segment bodies use ICU-friendly text (`#` for the plural/ordinal variable, `{name}` for others).
209
+ - **NONE** — Always a single segment with the raw string (no PGS).
205
210
 
206
211
  ### import
207
212
 
208
- Import translations from bilingual XLIFF 2.0 or 2.2 files in `l10n/xliff` to JSON files in `l10n/translations`. Expects XLIFF files with `trgLang` (target language) and translated content in `<target>` elements. Writes JSON files without notes for minimal size. Requires `package.json` with `directories.i18n` and `directories.l10n` (run `msg init` first). Units with `pgs:switch` are reassembled into a single MessageFormat 2 string per message key from the segment bodies (targets when present).
213
+ Import translations from bilingual XLIFF 2.0 or 2.2 files in `l10n/xliff` to JSON files in `l10n/translations`. Expects XLIFF files with `trgLang` (target language) and translated content in `<target>` elements. Writes JSON files without notes for minimal size. Requires `package.json` with `directories.i18n` and `directories.l10n` (run `msg init` first).
214
+
215
+ **Format and PGS on import:**
216
+
217
+ - Unit `type` of `msg:NONE` / `msg:MF1` / `msg:MF2` (or bare `NONE` / `MF1` / `MF2`) is restored as the message `attributes.format`.
218
+ - 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.
209
219
 
210
220
  ```bash
211
221
  msg import [-p <projectName>] [-l <locale>]
@@ -246,9 +256,9 @@ msg import -l zh
246
256
 
247
257
  ### Example: plural messages (XLIFF 2.2 PGS)
248
258
 
249
- Plural (and other classifiable `.match`) messages use the [XLIFF 2.2 PGS module](https://docs.oasis-open.org/xliff/xliff-core/v2.2/xliff-extended-v2.2-part2.html): export splits them into one `<segment>` per variant (`pgs:case`), and import merges `<target>` text back into a single MessageFormat 2 string per message key.
259
+ Plural (and other classifiable select) messages use the [XLIFF 2.2 PGS module](https://docs.oasis-open.org/xliff/xliff-core/v2.2/xliff-extended-v2.2-part2.html): export splits them into one `<segment>` per variant (`pgs:case`), and import merges `<target>` text back into a single message string per key. The unit `type` (`msg:MF1` or `msg:MF2`) selects ICU MessageFormat 1 vs MessageFormat 2 reconstruction.
250
260
 
251
- **1. Message in the MsgResource** (Unicode MessageFormat 2 with `.input` / `.match` on a plural):
261
+ **1. Message in the MsgResource** (MF2 `.match`, or MF1 `{count, plural, ...}` with `format: "MF1"`):
252
262
 
253
263
  ```js
254
264
  // i18n/resources/messages.msg.js — excerpt
@@ -271,7 +281,7 @@ The plural becomes a `pgs:switch` unit with one segment per case (abbreviated):
271
281
  <?xml version="1.0" encoding="UTF-8"?>
272
282
  <xliff xmlns="urn:oasis:names:tc:xliff:document:2.2" xmlns:pgs="urn:oasis:names:tc:xliff:pgs:1.0" version="2.2" srcLang="en">
273
283
  <file id="f1" original="messages.json" srcDir="ltr">
274
- <unit id="itemsCount" name="itemsCount" pgs:switch="plural:n">
284
+ <unit id="itemsCount" name="itemsCount" type="msg:MF2" pgs:switch="plural:n">
275
285
  <segment id="itemsCount_s1" pgs:case="one">
276
286
  <source>One item</source>
277
287
  </segment>
@@ -288,7 +298,7 @@ The plural becomes a `pgs:switch` unit with one segment per case (abbreviated):
288
298
  ```xml
289
299
  <xliff xmlns="urn:oasis:names:tc:xliff:document:2.2" xmlns:pgs="urn:oasis:names:tc:xliff:pgs:1.0" version="2.2" srcLang="en" trgLang="fr">
290
300
  <file id="f1" original="messages.json" srcDir="ltr">
291
- <unit id="itemsCount" name="itemsCount" pgs:switch="plural:n">
301
+ <unit id="itemsCount" name="itemsCount" type="msg:MF2" pgs:switch="plural:n">
292
302
  <segment id="itemsCount_s1" pgs:case="one">
293
303
  <source>One item</source>
294
304
  <target>Un élément</target>
@@ -302,7 +312,7 @@ The plural becomes a `pgs:switch` unit with one segment per case (abbreviated):
302
312
  </xliff>
303
313
  ```
304
314
 
305
- **4. Import** — writes minimal JSON under `l10n/translations/<project>/<locale>/<resource>.json`, with one MF2 message per key rebuilt from the PGS segments:
315
+ **4. Import** — writes minimal JSON under `l10n/translations/<project>/<locale>/<resource>.json`, with one message per key rebuilt from the PGS segments (MF2 `.match` when `type` is `msg:MF2` or omitted; ICU MF1 when `type` is `msg:MF1`):
306
316
 
307
317
  ```bash
308
318
  msg import
@@ -343,7 +353,7 @@ The CLI does not expose a programmatic API. For library usage, see [@worldware/m
343
353
  Source layout:
344
354
 
345
355
  - `src/commands/` — CLI commands (init, export, import, create/project, create/resource).
346
- - `src/lib/` — Shared utilities: init-helpers, export-helpers, import-helpers, create-project-helpers, create-resource-helpers.
356
+ - `src/lib/` — Shared utilities: init-helpers, export-helpers, import-helpers, create-project-helpers, create-resource-helpers, msg-format, pgs-mf1, pgs-mf2.
347
357
  - `src/tests/` — Vitest tests and fixtures.
348
358
 
349
359
  ## License
@@ -1,6 +1,12 @@
1
+ import {
2
+ unitTypeToFormat
3
+ } from "./chunk-Q775ZGC5.mjs";
4
+ import {
5
+ pgsImportToMf1Message
6
+ } from "./chunk-XID5J2F7.mjs";
1
7
  import {
2
8
  pgsImportToSelectMessage
3
- } from "./chunk-DPUF7YUH.mjs";
9
+ } from "./chunk-MGHC4AYZ.mjs";
4
10
  import {
5
11
  dynamicImportFromUrl
6
12
  } from "./chunk-LNCUPVEF.mjs";
@@ -186,6 +192,7 @@ function processUnit(unit, fileAttrs) {
186
192
  const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
187
193
  const translate = (unit["@_translate"] ?? "yes").toLowerCase();
188
194
  const dnt = translate === "no" || translate === "false";
195
+ const format = unitTypeToFormat(unit["@_type"]);
189
196
  const fileTranslate = (fileAttrs.translate ?? "yes").toLowerCase();
190
197
  const fileDnt = fileTranslate === "no" || fileTranslate === "false";
191
198
  const notes = [];
@@ -201,6 +208,7 @@ function processUnit(unit, fileAttrs) {
201
208
  }
202
209
  const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
203
210
  const pgsSwitch = unit["@_pgs:switch"];
211
+ const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
204
212
  let value;
205
213
  if (pgsSwitch && segments.length > 0 && segments.every(
206
214
  (seg) => typeof seg["@_pgs:case"] === "string"
@@ -209,27 +217,16 @@ function processUnit(unit, fileAttrs) {
209
217
  caseAttr: seg["@_pgs:case"],
210
218
  body: extractSegmentText(seg)
211
219
  }));
212
- const rebuilt = pgsImportToSelectMessage(pgsSwitch, importSegments);
213
- if (rebuilt) {
214
- value = rebuilt;
215
- } else {
216
- const targetParts = [];
217
- for (const seg of segments) {
218
- targetParts.push(extractSegmentText(seg));
219
- }
220
- value = targetParts.join("");
221
- }
220
+ const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
221
+ value = rebuilt ?? joinSegmentTexts();
222
222
  } else {
223
- const targetParts = [];
224
- for (const seg of segments) {
225
- targetParts.push(extractSegmentText(seg));
226
- }
227
- value = targetParts.join("");
223
+ value = joinSegmentTexts();
228
224
  }
229
225
  const attributes = {};
230
226
  if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
231
227
  if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
232
228
  if (dnt !== fileDnt) attributes.dnt = dnt;
229
+ if (format) attributes.format = format;
233
230
  return {
234
231
  key: name,
235
232
  value,
@@ -1,11 +1,19 @@
1
+ import {
2
+ formatToUnitType,
3
+ resolveMessageFormat
4
+ } from "./chunk-Q775ZGC5.mjs";
5
+ import {
6
+ mf1MessageToPgsExport
7
+ } from "./chunk-XID5J2F7.mjs";
1
8
  import {
2
9
  selectMessageToPgsExport
3
- } from "./chunk-DPUF7YUH.mjs";
10
+ } from "./chunk-MGHC4AYZ.mjs";
4
11
  import {
5
12
  dynamicImportFromUrl
6
13
  } from "./chunk-LNCUPVEF.mjs";
7
14
 
8
15
  // src/lib/export-helpers.ts
16
+ import { MSG_DEFAULT_FORMAT } from "@worldware/msg";
9
17
  import { readdir, writeFile, mkdir } from "fs/promises";
10
18
  import { join } from "path";
11
19
  import { pathToFileURL } from "url";
@@ -97,6 +105,11 @@ function renderNotes(notes, idPrefix, indent = " ") {
97
105
  parts.push(`${indent}</notes>`);
98
106
  return parts;
99
107
  }
108
+ function pgsExportForFormat(format, value) {
109
+ if (format === "MF1") return mf1MessageToPgsExport(value);
110
+ if (format === "MF2") return selectMessageToPgsExport(value);
111
+ return null;
112
+ }
100
113
  function resourceGroupToXliff22(group) {
101
114
  const srcLang = group.resources[0]?.attributes?.lang ?? "en";
102
115
  const parts = [
@@ -109,6 +122,12 @@ function resourceGroupToXliff22(group) {
109
122
  fileIndex += 1;
110
123
  const fileId = `f${fileIndex}`;
111
124
  const attrs = resource.attributes ?? {};
125
+ const projectFormat = resource.getProject().format ?? MSG_DEFAULT_FORMAT;
126
+ const resourceFormat = resolveMessageFormat(
127
+ void 0,
128
+ attrs.format,
129
+ projectFormat
130
+ );
112
131
  const fileAttrs = [
113
132
  `id="${fileId}"`,
114
133
  `original="${escapeXml(orig)}"`
@@ -139,7 +158,13 @@ function resourceGroupToXliff22(group) {
139
158
  if (msgAttr?.dir) {
140
159
  msgAttrs.push(`srcDir="${escapeXml(msgAttr.dir)}"`);
141
160
  }
142
- const pgsExport = selectMessageToPgsExport(msg.value);
161
+ const msgFormat = resolveMessageFormat(
162
+ msgAttr?.format,
163
+ resourceFormat,
164
+ projectFormat
165
+ );
166
+ msgAttrs.push(`type="${escapeXml(formatToUnitType(msgFormat))}"`);
167
+ const pgsExport = pgsExportForFormat(msgFormat, msg.value);
143
168
  if (pgsExport) {
144
169
  msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
145
170
  }
@@ -38,10 +38,13 @@ function findInputDeclaration(msg, varName) {
38
38
  function getFunctionName(expr) {
39
39
  return expr.functionRef?.name;
40
40
  }
41
+ function getOption(opts, key) {
42
+ if (!opts) return void 0;
43
+ if (opts instanceof Map) return opts.get(key);
44
+ return opts[key];
45
+ }
41
46
  function getSelectOptionOrdinal(expr) {
42
- const opts = expr.functionRef?.options;
43
- if (!opts || !(opts instanceof Map)) return false;
44
- const sel = opts.get("select");
47
+ const sel = getOption(expr.functionRef?.options, "select");
45
48
  if (!sel || typeof sel !== "object" || sel === null) return false;
46
49
  if (sel.type !== "literal") return false;
47
50
  return sel.value === "ordinal";
@@ -118,13 +121,7 @@ function stringifyPattern(pattern) {
118
121
  wrap
119
122
  );
120
123
  }
121
- function selectMessageToPgsExport(source) {
122
- let msg;
123
- try {
124
- msg = parseMessage(source);
125
- } catch {
126
- return null;
127
- }
124
+ function selectMessageDataToPgsExport(msg) {
128
125
  if (!isSelectMessage(msg)) return null;
129
126
  const classification = classifySelectMessageForPgs(
130
127
  msg
@@ -139,9 +136,7 @@ function selectMessageToPgsExport(source) {
139
136
  for (let ki = 0; ki < classification.length; ki++) {
140
137
  const key = variant.keys[ki];
141
138
  if (!key) return null;
142
- caseParts.push(
143
- mf2KeyToPgsToken(key, classification[ki].kind)
144
- );
139
+ caseParts.push(mf2KeyToPgsToken(key, classification[ki].kind));
145
140
  }
146
141
  segments.push({
147
142
  caseAttr: caseParts.join(" "),
@@ -150,6 +145,15 @@ function selectMessageToPgsExport(source) {
150
145
  }
151
146
  return { switchAttr, segments };
152
147
  }
148
+ function selectMessageToPgsExport(source) {
149
+ let msg;
150
+ try {
151
+ msg = parseMessage(source);
152
+ } catch {
153
+ return null;
154
+ }
155
+ return selectMessageDataToPgsExport(msg);
156
+ }
153
157
  function parsePatternFromSegmentBody(body) {
154
158
  const trimmed = body.trim();
155
159
  if (!trimmed) return [];
@@ -263,6 +267,7 @@ export {
263
267
  classifySelectMessageForPgs,
264
268
  mf2KeyToPgsToken,
265
269
  pgsTokenToMf2Key,
270
+ selectMessageDataToPgsExport,
266
271
  selectMessageToPgsExport,
267
272
  pgsImportToSelectMessage,
268
273
  messagesStructurallyEqual
@@ -0,0 +1,29 @@
1
+ // src/lib/msg-format.ts
2
+ import { MSG_DEFAULT_FORMAT } from "@worldware/msg";
3
+ var MSG_TYPE_PREFIX = "msg";
4
+ var FORMAT_VALUES = /* @__PURE__ */ new Set(["NONE", "MF1", "MF2"]);
5
+ function formatToUnitType(format) {
6
+ return `${MSG_TYPE_PREFIX}:${format}`;
7
+ }
8
+ function unitTypeToFormat(type) {
9
+ if (type == null || type === "") return void 0;
10
+ if (FORMAT_VALUES.has(type)) {
11
+ return type;
12
+ }
13
+ const prefixed = new RegExp(
14
+ `^${MSG_TYPE_PREFIX}:(NONE|MF1|MF2)$`
15
+ ).exec(type);
16
+ if (prefixed?.[1] && FORMAT_VALUES.has(prefixed[1])) {
17
+ return prefixed[1];
18
+ }
19
+ return void 0;
20
+ }
21
+ function resolveMessageFormat(messageFormat, resourceFormat, projectFormat) {
22
+ return messageFormat ?? resourceFormat ?? projectFormat ?? MSG_DEFAULT_FORMAT;
23
+ }
24
+
25
+ export {
26
+ formatToUnitType,
27
+ unitTypeToFormat,
28
+ resolveMessageFormat
29
+ };
@@ -0,0 +1,135 @@
1
+ import {
2
+ parsePgsSwitch,
3
+ selectMessageDataToPgsExport
4
+ } from "./chunk-MGHC4AYZ.mjs";
5
+
6
+ // src/lib/pgs-mf1.ts
7
+ import { parse } from "@messageformat/parser";
8
+ import { mf1ToMessageData } from "@messageformat/icu-messageformat-1";
9
+ import { parseMessage } from "messageformat";
10
+ function mf1MessageToPgsExport(source) {
11
+ try {
12
+ const data = mf1ToMessageData(parse(source));
13
+ const exp = selectMessageDataToPgsExport(data);
14
+ if (!exp) return null;
15
+ const parsedSwitch = parsePgsSwitch(exp.switchAttr);
16
+ if (!parsedSwitch) return null;
17
+ const hashVars = pluralVarNames(parsedSwitch);
18
+ return {
19
+ switchAttr: exp.switchAttr,
20
+ segments: exp.segments.map((seg) => ({
21
+ caseAttr: seg.caseAttr,
22
+ sourcePattern: segmentBodyToMf1(seg.sourcePattern, hashVars)
23
+ }))
24
+ };
25
+ } catch {
26
+ return null;
27
+ }
28
+ }
29
+ function kindToIcuType(kind) {
30
+ if (kind === "plural") return "plural";
31
+ if (kind === "ordinal") return "selectordinal";
32
+ return "select";
33
+ }
34
+ function pluralVarNames(parsedSwitch) {
35
+ const names = /* @__PURE__ */ new Set();
36
+ for (const { kind, name } of parsedSwitch) {
37
+ if (kind === "plural" || kind === "ordinal") names.add(name);
38
+ }
39
+ return names;
40
+ }
41
+ function segmentBodyToMf1(body, hashVars) {
42
+ const trimmed = body.trim();
43
+ if (!trimmed) return "";
44
+ try {
45
+ const msg = parseMessage(trimmed);
46
+ if (msg.type !== "message") {
47
+ return escapeIcuLiteral(trimmed);
48
+ }
49
+ return patternPartsToMf1(
50
+ msg.pattern,
51
+ hashVars
52
+ );
53
+ } catch {
54
+ return escapeIcuLiteral(trimmed);
55
+ }
56
+ }
57
+ function patternPartsToMf1(pattern, hashVars) {
58
+ let out = "";
59
+ for (const part of pattern) {
60
+ if (typeof part === "string") {
61
+ out += escapeIcuLiteral(part);
62
+ continue;
63
+ }
64
+ if (!part || typeof part !== "object") continue;
65
+ const expr = part;
66
+ if (expr.type === "expression" && expr.arg?.type === "variable" && expr.arg.name) {
67
+ const name = expr.arg.name;
68
+ out += hashVars.has(name) ? "#" : `{${name}}`;
69
+ continue;
70
+ }
71
+ }
72
+ return out;
73
+ }
74
+ function escapeIcuLiteral(s) {
75
+ return s.replace(/'/g, "''").replace(/([{}])/g, "'$1'");
76
+ }
77
+ function insertCase(tree, tokens, body, depth) {
78
+ if (tokens.length === 0) return false;
79
+ const token = tokens[depth];
80
+ if (token === void 0) return false;
81
+ if (depth === tokens.length - 1) {
82
+ tree.set(token, body);
83
+ return true;
84
+ }
85
+ let child = tree.get(token);
86
+ if (typeof child === "string") return false;
87
+ if (!child) {
88
+ child = /* @__PURE__ */ new Map();
89
+ tree.set(token, child);
90
+ }
91
+ return insertCase(child, tokens, body, depth + 1);
92
+ }
93
+ function serializeCaseTree(tree, parsedSwitch, depth, hashVars) {
94
+ const item = parsedSwitch[depth];
95
+ if (!item) return null;
96
+ const icuType = kindToIcuType(item.kind);
97
+ const parts = [];
98
+ for (const [key, child] of tree) {
99
+ let content;
100
+ if (typeof child === "string") {
101
+ content = segmentBodyToMf1(child, hashVars);
102
+ } else {
103
+ const nested = serializeCaseTree(
104
+ child,
105
+ parsedSwitch,
106
+ depth + 1,
107
+ hashVars
108
+ );
109
+ if (nested == null) return null;
110
+ content = nested;
111
+ }
112
+ parts.push(`${key} {${content}}`);
113
+ }
114
+ if (parts.length === 0) return null;
115
+ return `{${item.name}, ${icuType}, ${parts.join(" ")}}`;
116
+ }
117
+ function pgsImportToMf1Message(switchAttr, segments) {
118
+ const parsedSwitch = parsePgsSwitch(switchAttr);
119
+ if (!parsedSwitch || parsedSwitch.length === 0) return null;
120
+ const n = parsedSwitch.length;
121
+ if (segments.length === 0) return null;
122
+ const hashVars = pluralVarNames(parsedSwitch);
123
+ const tree = /* @__PURE__ */ new Map();
124
+ for (const seg of segments) {
125
+ const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
126
+ if (tokens.length !== n) return null;
127
+ if (!insertCase(tree, tokens, seg.body, 0)) return null;
128
+ }
129
+ return serializeCaseTree(tree, parsedSwitch, 0, hashVars);
130
+ }
131
+
132
+ export {
133
+ mf1MessageToPgsExport,
134
+ pgsImportToMf1Message
135
+ };
@@ -91,6 +91,7 @@ function loadPackageJsonForMsg(cwd, options) {
91
91
  }
92
92
 
93
93
  // src/lib/export-helpers.ts
94
+ var import_msg2 = require("@worldware/msg");
94
95
  var import_promises = require("fs/promises");
95
96
  var import_path2 = require("path");
96
97
  var import_url2 = require("url");
@@ -121,14 +122,42 @@ async function dynamicImportFromUrl(url) {
121
122
  }
122
123
  }
123
124
 
125
+ // src/lib/msg-format.ts
126
+ var import_msg = require("@worldware/msg");
127
+ var MSG_TYPE_PREFIX = "msg";
128
+ function formatToUnitType(format) {
129
+ return `${MSG_TYPE_PREFIX}:${format}`;
130
+ }
131
+ function resolveMessageFormat(messageFormat, resourceFormat, projectFormat) {
132
+ return messageFormat ?? resourceFormat ?? projectFormat ?? import_msg.MSG_DEFAULT_FORMAT;
133
+ }
134
+
135
+ // src/lib/pgs-mf1.ts
136
+ var import_parser = require("@messageformat/parser");
137
+ var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
138
+ var import_messageformat2 = require("messageformat");
139
+
124
140
  // src/lib/pgs-mf2.ts
125
141
  var import_messageformat = require("messageformat");
142
+ var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
126
143
  var GENDER_LABELS = /* @__PURE__ */ new Set([
127
144
  "feminine",
128
145
  "masculine",
129
146
  "neuter",
130
147
  "other"
131
148
  ]);
149
+ function parsePgsSwitch(value) {
150
+ const trimmed = value.trim();
151
+ if (!trimmed) return null;
152
+ const parts = trimmed.split(/\s+/);
153
+ const out = [];
154
+ for (const p of parts) {
155
+ const m = p.match(PGS_SWITCH_ITEM);
156
+ if (!m) return null;
157
+ out.push({ kind: m[1], name: m[2] });
158
+ }
159
+ return out.length > 0 ? out : null;
160
+ }
132
161
  function findInputDeclaration(msg, varName) {
133
162
  for (const d of msg.declarations) {
134
163
  const decl = d;
@@ -141,10 +170,13 @@ function findInputDeclaration(msg, varName) {
141
170
  function getFunctionName(expr) {
142
171
  return expr.functionRef?.name;
143
172
  }
173
+ function getOption(opts, key) {
174
+ if (!opts) return void 0;
175
+ if (opts instanceof Map) return opts.get(key);
176
+ return opts[key];
177
+ }
144
178
  function getSelectOptionOrdinal(expr) {
145
- const opts = expr.functionRef?.options;
146
- if (!opts || !(opts instanceof Map)) return false;
147
- const sel = opts.get("select");
179
+ const sel = getOption(expr.functionRef?.options, "select");
148
180
  if (!sel || typeof sel !== "object" || sel === null) return false;
149
181
  if (sel.type !== "literal") return false;
150
182
  return sel.value === "ordinal";
@@ -212,13 +244,7 @@ function stringifyPattern(pattern) {
212
244
  wrap
213
245
  );
214
246
  }
215
- function selectMessageToPgsExport(source) {
216
- let msg;
217
- try {
218
- msg = (0, import_messageformat.parseMessage)(source);
219
- } catch {
220
- return null;
221
- }
247
+ function selectMessageDataToPgsExport(msg) {
222
248
  if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
223
249
  const classification = classifySelectMessageForPgs(
224
250
  msg
@@ -233,9 +259,7 @@ function selectMessageToPgsExport(source) {
233
259
  for (let ki = 0; ki < classification.length; ki++) {
234
260
  const key = variant.keys[ki];
235
261
  if (!key) return null;
236
- caseParts.push(
237
- mf2KeyToPgsToken(key, classification[ki].kind)
238
- );
262
+ caseParts.push(mf2KeyToPgsToken(key, classification[ki].kind));
239
263
  }
240
264
  segments.push({
241
265
  caseAttr: caseParts.join(" "),
@@ -244,6 +268,79 @@ function selectMessageToPgsExport(source) {
244
268
  }
245
269
  return { switchAttr, segments };
246
270
  }
271
+ function selectMessageToPgsExport(source) {
272
+ let msg;
273
+ try {
274
+ msg = (0, import_messageformat.parseMessage)(source);
275
+ } catch {
276
+ return null;
277
+ }
278
+ return selectMessageDataToPgsExport(msg);
279
+ }
280
+
281
+ // src/lib/pgs-mf1.ts
282
+ function mf1MessageToPgsExport(source) {
283
+ try {
284
+ const data = (0, import_icu_messageformat_1.mf1ToMessageData)((0, import_parser.parse)(source));
285
+ const exp = selectMessageDataToPgsExport(data);
286
+ if (!exp) return null;
287
+ const parsedSwitch = parsePgsSwitch(exp.switchAttr);
288
+ if (!parsedSwitch) return null;
289
+ const hashVars = pluralVarNames(parsedSwitch);
290
+ return {
291
+ switchAttr: exp.switchAttr,
292
+ segments: exp.segments.map((seg) => ({
293
+ caseAttr: seg.caseAttr,
294
+ sourcePattern: segmentBodyToMf1(seg.sourcePattern, hashVars)
295
+ }))
296
+ };
297
+ } catch {
298
+ return null;
299
+ }
300
+ }
301
+ function pluralVarNames(parsedSwitch) {
302
+ const names = /* @__PURE__ */ new Set();
303
+ for (const { kind, name } of parsedSwitch) {
304
+ if (kind === "plural" || kind === "ordinal") names.add(name);
305
+ }
306
+ return names;
307
+ }
308
+ function segmentBodyToMf1(body, hashVars) {
309
+ const trimmed = body.trim();
310
+ if (!trimmed) return "";
311
+ try {
312
+ const msg = (0, import_messageformat2.parseMessage)(trimmed);
313
+ if (msg.type !== "message") {
314
+ return escapeIcuLiteral(trimmed);
315
+ }
316
+ return patternPartsToMf1(
317
+ msg.pattern,
318
+ hashVars
319
+ );
320
+ } catch {
321
+ return escapeIcuLiteral(trimmed);
322
+ }
323
+ }
324
+ function patternPartsToMf1(pattern, hashVars) {
325
+ let out = "";
326
+ for (const part of pattern) {
327
+ if (typeof part === "string") {
328
+ out += escapeIcuLiteral(part);
329
+ continue;
330
+ }
331
+ if (!part || typeof part !== "object") continue;
332
+ const expr = part;
333
+ if (expr.type === "expression" && expr.arg?.type === "variable" && expr.arg.name) {
334
+ const name = expr.arg.name;
335
+ out += hashVars.has(name) ? "#" : `{${name}}`;
336
+ continue;
337
+ }
338
+ }
339
+ return out;
340
+ }
341
+ function escapeIcuLiteral(s) {
342
+ return s.replace(/'/g, "''").replace(/([{}])/g, "'$1'");
343
+ }
247
344
 
248
345
  // src/lib/export-helpers.ts
249
346
  var MSG_PATTERN = /\.msg\.(ts|js)$/i;
@@ -334,6 +431,11 @@ function renderNotes(notes, idPrefix, indent = " ") {
334
431
  parts.push(`${indent}</notes>`);
335
432
  return parts;
336
433
  }
434
+ function pgsExportForFormat(format, value) {
435
+ if (format === "MF1") return mf1MessageToPgsExport(value);
436
+ if (format === "MF2") return selectMessageToPgsExport(value);
437
+ return null;
438
+ }
337
439
  function resourceGroupToXliff22(group) {
338
440
  const srcLang = group.resources[0]?.attributes?.lang ?? "en";
339
441
  const parts = [
@@ -346,6 +448,12 @@ function resourceGroupToXliff22(group) {
346
448
  fileIndex += 1;
347
449
  const fileId = `f${fileIndex}`;
348
450
  const attrs = resource.attributes ?? {};
451
+ const projectFormat = resource.getProject().format ?? import_msg2.MSG_DEFAULT_FORMAT;
452
+ const resourceFormat = resolveMessageFormat(
453
+ void 0,
454
+ attrs.format,
455
+ projectFormat
456
+ );
349
457
  const fileAttrs = [
350
458
  `id="${fileId}"`,
351
459
  `original="${escapeXml(orig)}"`
@@ -376,7 +484,13 @@ function resourceGroupToXliff22(group) {
376
484
  if (msgAttr?.dir) {
377
485
  msgAttrs.push(`srcDir="${escapeXml(msgAttr.dir)}"`);
378
486
  }
379
- const pgsExport = selectMessageToPgsExport(msg.value);
487
+ const msgFormat = resolveMessageFormat(
488
+ msgAttr?.format,
489
+ resourceFormat,
490
+ projectFormat
491
+ );
492
+ msgAttrs.push(`type="${escapeXml(formatToUnitType(msgFormat))}"`);
493
+ const pgsExport = pgsExportForFormat(msgFormat, msg.value);
380
494
  if (pgsExport) {
381
495
  msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
382
496
  }
@@ -5,8 +5,10 @@ import {
5
5
  importMsgResourcesFromPaths,
6
6
  serializeResourceGroupsToXliff,
7
7
  writeXliffFiles
8
- } from "../chunk-XHD7F45F.mjs";
9
- import "../chunk-DPUF7YUH.mjs";
8
+ } from "../chunk-6BLQESHW.mjs";
9
+ import "../chunk-Q775ZGC5.mjs";
10
+ import "../chunk-XID5J2F7.mjs";
11
+ import "../chunk-MGHC4AYZ.mjs";
10
12
  import "../chunk-LNCUPVEF.mjs";
11
13
  import {
12
14
  loadPackageJsonForMsg