@worldware/msg-cli 0.5.1 → 0.6.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.
- package/README.md +17 -6
- package/dist/{chunk-MGHC4AYZ.mjs → chunk-HH6ZOHO4.mjs} +11 -14
- package/dist/{chunk-PO2Z7EWJ.mjs → chunk-KRQA4FRY.mjs} +30 -17
- package/dist/{chunk-XID5J2F7.mjs → chunk-PQJA3H4Z.mjs} +1 -1
- package/dist/{chunk-MPDE5BUV.mjs → chunk-VJKOG7EA.mjs} +3 -3
- package/dist/{chunk-QOEKYM6W.mjs → chunk-W33MA4DJ.mjs} +1 -1
- package/dist/{chunk-PYIBLOWY.mjs → chunk-WGI2EFOS.mjs} +3 -3
- package/dist/commands/create/project.mjs +2 -2
- package/dist/commands/create/resource.cjs +30 -17
- package/dist/commands/create/resource.mjs +1 -1
- package/dist/commands/export.mjs +4 -4
- package/dist/commands/import.cjs +11 -14
- package/dist/commands/import.mjs +4 -4
- package/dist/lib/create-project-helpers.mjs +2 -2
- package/dist/lib/create-resource-helpers.cjs +30 -17
- package/dist/lib/create-resource-helpers.d.mts +5 -3
- package/dist/lib/create-resource-helpers.d.ts +5 -3
- package/dist/lib/create-resource-helpers.mjs +1 -1
- package/dist/lib/export-helpers.mjs +4 -4
- package/dist/lib/import-helpers.cjs +11 -14
- package/dist/lib/import-helpers.mjs +4 -4
- package/dist/lib/pgs-mf1.mjs +2 -2
- package/dist/lib/pgs-mf2.cjs +11 -14
- package/dist/lib/pgs-mf2.d.mts +3 -0
- package/dist/lib/pgs-mf2.d.ts +3 -0
- package/dist/lib/pgs-mf2.mjs +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -167,7 +167,7 @@ msg create resource myProject messages --edit
|
|
|
167
167
|
- Imports the project via the `#i18n/projects/<projectName>.js` alias (added by `msg init`).
|
|
168
168
|
- Sets `lang` from the project's `sourceLocale` and `dir` to `rtl` for Arabic/Hebrew, `ltr` otherwise.
|
|
169
169
|
- Exports a named `resource` plus an async `getMessages()` helper that returns `resource.getTranslation(getLang())`, so the resource can be pre-translated for the runtime locale.
|
|
170
|
-
-
|
|
170
|
+
- Declares sample messages in `MsgResource.create({ messages })`, wrapped in a JSDoc const assertion (`/** @type {const} */ (…)`, the `.js` form of `as const`), so TypeScript and editors infer the resource's message keys. Validates that the generated file is importable.
|
|
171
171
|
- Errors if i18n/projects or i18n/resources does not exist, the project is not found, or the resource file already exists (unless `--force`).
|
|
172
172
|
|
|
173
173
|
**Generated file (ESM excerpt):**
|
|
@@ -176,11 +176,13 @@ msg create resource myProject messages --edit
|
|
|
176
176
|
import { MsgResource, getLang } from '@worldware/msg';
|
|
177
177
|
import project from '#i18n/projects/myProject.js';
|
|
178
178
|
|
|
179
|
-
export const resource = MsgResource.create({
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
179
|
+
export const resource = MsgResource.create({
|
|
180
|
+
/* title, attributes, notes */
|
|
181
|
+
messages: /** @type {const} */ ([
|
|
182
|
+
{ key: 'sampleKey', value: 'Sample value.', notes: [/* … */] },
|
|
183
|
+
{ key: 'sampleKey2', value: 'Hi, {name}', attributes: { dnt: true }, notes: [/* … */] }
|
|
184
|
+
])
|
|
185
|
+
}, project);
|
|
184
186
|
|
|
185
187
|
export async function getMessages() {
|
|
186
188
|
return await resource.getTranslation(getLang());
|
|
@@ -195,6 +197,15 @@ import { getMessages } from '#i18n/resources/messages.msg.js';
|
|
|
195
197
|
const messages = await getMessages();
|
|
196
198
|
```
|
|
197
199
|
|
|
200
|
+
**Typed message keys:** with `@worldware/msg` 0.13+, the const assertion makes `resource` a `MsgResource<'sampleKey' | 'sampleKey2'>`. In a TypeScript project, or a JS project with `checkJs` / `// @ts-check`, a misspelled key is a compile error:
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
resource.get('sampleKey'); // ok
|
|
204
|
+
resource.get('sampleKye'); // error: not assignable to 'sampleKey' | 'sampleKey2'
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Add your own messages to the `messages` array to keep them typed. Messages added later with the chainable `resource.add(key, value, attributes, notes)` still work at runtime, but their keys are not part of the inferred type. Remove the `/** @type {const} */` cast to opt out of typed keys.
|
|
208
|
+
|
|
198
209
|
### export
|
|
199
210
|
|
|
200
211
|
Serialize all MsgResource files in `i18n/resources` to XLIFF 2.2 files in `l10n/xliff`, one file per project. Does not send files for translation; use your own translation workflow with the generated XLIFF. Requires `package.json` with `directories.i18n` and `directories.l10n` (run `msg init` first).
|
|
@@ -154,17 +154,6 @@ function selectMessageToPgsExport(source) {
|
|
|
154
154
|
}
|
|
155
155
|
return selectMessageDataToPgsExport(msg);
|
|
156
156
|
}
|
|
157
|
-
function parsePatternFromSegmentBody(body) {
|
|
158
|
-
const trimmed = body.trim();
|
|
159
|
-
if (!trimmed) return [];
|
|
160
|
-
try {
|
|
161
|
-
const m = parseMessage(trimmed);
|
|
162
|
-
if (m.type !== "message") return [];
|
|
163
|
-
return m.pattern;
|
|
164
|
-
} catch {
|
|
165
|
-
return [];
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
157
|
function ensureFallbackVariant(keysCount, variants) {
|
|
169
158
|
const hasAllStars = variants.some(
|
|
170
159
|
(v) => v.keys.length === keysCount && v.keys.every((k) => isCatchallKey(k))
|
|
@@ -226,11 +215,13 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
226
215
|
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
227
216
|
if (tokens.length !== n) return null;
|
|
228
217
|
const keys = tokens.map(
|
|
229
|
-
(t,
|
|
218
|
+
(t, i2) => pgsTokenToMf2Key(t, parsedSwitch[i2].kind)
|
|
230
219
|
);
|
|
231
220
|
variants.push({
|
|
232
221
|
keys,
|
|
233
|
-
|
|
222
|
+
// Empty pattern: stringify emits `{{}}`, then we splice the raw XLIFF
|
|
223
|
+
// body so `\t` / `\n` / `\{` are not dropped or double-escaped.
|
|
224
|
+
value: []
|
|
234
225
|
});
|
|
235
226
|
}
|
|
236
227
|
if (variants.length === 0) return null;
|
|
@@ -248,9 +239,15 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
248
239
|
} catch {
|
|
249
240
|
return null;
|
|
250
241
|
}
|
|
251
|
-
|
|
242
|
+
const bodies = segments.map((seg) => seg.body);
|
|
243
|
+
while (bodies.length < msg.variants.length) {
|
|
244
|
+
bodies.push(segments[segments.length - 1]?.body ?? "");
|
|
245
|
+
}
|
|
246
|
+
const skeleton = stringifyMessage(
|
|
252
247
|
msg
|
|
253
248
|
);
|
|
249
|
+
let i = 0;
|
|
250
|
+
return skeleton.replace(/\{\{\}\}/g, () => `{{${bodies[i++] ?? ""}}}`);
|
|
254
251
|
}
|
|
255
252
|
function messagesStructurallyEqual(a, b) {
|
|
256
253
|
try {
|
|
@@ -81,7 +81,7 @@ function generateMsgResourceContent(params) {
|
|
|
81
81
|
const dirStr = `'${dir}'`;
|
|
82
82
|
const resourceNote = `This is the ${escapeSingleQuoted(title)} resource.`;
|
|
83
83
|
const projectImport = `#i18n/projects/${escapeSingleQuoted(projectName)}.js`;
|
|
84
|
-
const
|
|
84
|
+
const createWithMessages = `MsgResource.create({
|
|
85
85
|
title: ${titleStr},
|
|
86
86
|
attributes: {
|
|
87
87
|
lang: ${langStr},
|
|
@@ -89,22 +89,35 @@ function generateMsgResourceContent(params) {
|
|
|
89
89
|
},
|
|
90
90
|
notes: [
|
|
91
91
|
{type: 'DESCRIPTION', content: '${resourceNote}'}
|
|
92
|
-
]
|
|
92
|
+
],
|
|
93
|
+
/**
|
|
94
|
+
* The JSDoc const assertion lets TypeScript infer the message keys, so
|
|
95
|
+
* resource.get('unknownKey') is a compile error. Remove it to opt out.
|
|
96
|
+
*/
|
|
97
|
+
messages: /** @type {const} */ ([
|
|
98
|
+
{
|
|
99
|
+
key: 'sampleKey',
|
|
100
|
+
value: 'Sample value.',
|
|
101
|
+
notes: [
|
|
102
|
+
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
103
|
+
]
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
key: 'sampleKey2',
|
|
107
|
+
value: 'Hi, {name}',
|
|
108
|
+
attributes: { dnt: true },
|
|
109
|
+
notes: [
|
|
110
|
+
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
111
|
+
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
112
|
+
]
|
|
113
|
+
}
|
|
114
|
+
])
|
|
93
115
|
}, project);
|
|
94
116
|
|
|
95
|
-
/**
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
resource
|
|
101
|
-
.add('sampleKey', 'Sample value.', {}, [
|
|
102
|
-
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
103
|
-
])
|
|
104
|
-
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [
|
|
105
|
-
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
106
|
-
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
107
|
-
]);`;
|
|
117
|
+
/**
|
|
118
|
+
* More messages can be added with the chainable add(key, value, attributes, notes),
|
|
119
|
+
* but keys added that way are not part of the inferred key type.
|
|
120
|
+
*/`;
|
|
108
121
|
if (isEsm) {
|
|
109
122
|
return `/** ESM module **/
|
|
110
123
|
|
|
@@ -113,7 +126,7 @@ import project from '${projectImport}';
|
|
|
113
126
|
|
|
114
127
|
/** Create a MsgResource object */
|
|
115
128
|
|
|
116
|
-
export const resource = ${
|
|
129
|
+
export const resource = ${createWithMessages}
|
|
117
130
|
|
|
118
131
|
/**
|
|
119
132
|
* An async function to get a translated version of the resource
|
|
@@ -130,7 +143,7 @@ export async function getMessages() {
|
|
|
130
143
|
const { MsgResource, getLang } = require('@worldware/msg');
|
|
131
144
|
const project = require('${projectImport}');
|
|
132
145
|
|
|
133
|
-
const resource = ${
|
|
146
|
+
const resource = ${createWithMessages}
|
|
134
147
|
|
|
135
148
|
/**
|
|
136
149
|
* An async function to get a translated version of the resource
|
|
@@ -3,13 +3,13 @@ import {
|
|
|
3
3
|
} from "./chunk-Q775ZGC5.mjs";
|
|
4
4
|
import {
|
|
5
5
|
pgsImportToMf1Message
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-PQJA3H4Z.mjs";
|
|
7
7
|
import {
|
|
8
8
|
pgsImportToSelectMessage
|
|
9
|
-
} from "./chunk-
|
|
9
|
+
} from "./chunk-HH6ZOHO4.mjs";
|
|
10
10
|
import {
|
|
11
11
|
dynamicImportFromUrl
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-KRQA4FRY.mjs";
|
|
13
13
|
|
|
14
14
|
// src/lib/import-helpers.ts
|
|
15
15
|
import { MsgResource } from "@worldware/msg";
|
|
@@ -4,13 +4,13 @@ import {
|
|
|
4
4
|
} from "./chunk-Q775ZGC5.mjs";
|
|
5
5
|
import {
|
|
6
6
|
mf1MessageToPgsExport
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-PQJA3H4Z.mjs";
|
|
8
8
|
import {
|
|
9
9
|
selectMessageToPgsExport
|
|
10
|
-
} from "./chunk-
|
|
10
|
+
} from "./chunk-HH6ZOHO4.mjs";
|
|
11
11
|
import {
|
|
12
12
|
dynamicImportFromUrl
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-KRQA4FRY.mjs";
|
|
14
14
|
|
|
15
15
|
// src/lib/export-helpers.ts
|
|
16
16
|
import { MSG_DEFAULT_FORMAT } from "@worldware/msg";
|
|
@@ -5,8 +5,8 @@ import {
|
|
|
5
5
|
loadPackageJsonForCreateProject,
|
|
6
6
|
resolveCreateProjectFormat,
|
|
7
7
|
writeMsgProjectFile
|
|
8
|
-
} from "../../chunk-
|
|
9
|
-
import "../../chunk-
|
|
8
|
+
} from "../../chunk-W33MA4DJ.mjs";
|
|
9
|
+
import "../../chunk-KRQA4FRY.mjs";
|
|
10
10
|
import {
|
|
11
11
|
findPackageJsonPath
|
|
12
12
|
} from "../../chunk-F7XZRKP7.mjs";
|
|
@@ -182,7 +182,7 @@ function generateMsgResourceContent(params) {
|
|
|
182
182
|
const dirStr = `'${dir}'`;
|
|
183
183
|
const resourceNote = `This is the ${escapeSingleQuoted(title)} resource.`;
|
|
184
184
|
const projectImport = `#i18n/projects/${escapeSingleQuoted(projectName)}.js`;
|
|
185
|
-
const
|
|
185
|
+
const createWithMessages = `MsgResource.create({
|
|
186
186
|
title: ${titleStr},
|
|
187
187
|
attributes: {
|
|
188
188
|
lang: ${langStr},
|
|
@@ -190,22 +190,35 @@ function generateMsgResourceContent(params) {
|
|
|
190
190
|
},
|
|
191
191
|
notes: [
|
|
192
192
|
{type: 'DESCRIPTION', content: '${resourceNote}'}
|
|
193
|
-
]
|
|
193
|
+
],
|
|
194
|
+
/**
|
|
195
|
+
* The JSDoc const assertion lets TypeScript infer the message keys, so
|
|
196
|
+
* resource.get('unknownKey') is a compile error. Remove it to opt out.
|
|
197
|
+
*/
|
|
198
|
+
messages: /** @type {const} */ ([
|
|
199
|
+
{
|
|
200
|
+
key: 'sampleKey',
|
|
201
|
+
value: 'Sample value.',
|
|
202
|
+
notes: [
|
|
203
|
+
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
204
|
+
]
|
|
205
|
+
},
|
|
206
|
+
{
|
|
207
|
+
key: 'sampleKey2',
|
|
208
|
+
value: 'Hi, {name}',
|
|
209
|
+
attributes: { dnt: true },
|
|
210
|
+
notes: [
|
|
211
|
+
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
212
|
+
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
213
|
+
]
|
|
214
|
+
}
|
|
215
|
+
])
|
|
194
216
|
}, project);
|
|
195
217
|
|
|
196
|
-
/**
|
|
197
|
-
*
|
|
198
|
-
*
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
resource
|
|
202
|
-
.add('sampleKey', 'Sample value.', {}, [
|
|
203
|
-
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
204
|
-
])
|
|
205
|
-
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [
|
|
206
|
-
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
207
|
-
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
208
|
-
]);`;
|
|
218
|
+
/**
|
|
219
|
+
* More messages can be added with the chainable add(key, value, attributes, notes),
|
|
220
|
+
* but keys added that way are not part of the inferred key type.
|
|
221
|
+
*/`;
|
|
209
222
|
if (isEsm) {
|
|
210
223
|
return `/** ESM module **/
|
|
211
224
|
|
|
@@ -214,7 +227,7 @@ import project from '${projectImport}';
|
|
|
214
227
|
|
|
215
228
|
/** Create a MsgResource object */
|
|
216
229
|
|
|
217
|
-
export const resource = ${
|
|
230
|
+
export const resource = ${createWithMessages}
|
|
218
231
|
|
|
219
232
|
/**
|
|
220
233
|
* An async function to get a translated version of the resource
|
|
@@ -231,7 +244,7 @@ export async function getMessages() {
|
|
|
231
244
|
const { MsgResource, getLang } = require('@worldware/msg');
|
|
232
245
|
const project = require('${projectImport}');
|
|
233
246
|
|
|
234
|
-
const resource = ${
|
|
247
|
+
const resource = ${createWithMessages}
|
|
235
248
|
|
|
236
249
|
/**
|
|
237
250
|
* An async function to get a translated version of the resource
|
package/dist/commands/export.mjs
CHANGED
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-WGI2EFOS.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-PQJA3H4Z.mjs";
|
|
11
|
+
import "../chunk-HH6ZOHO4.mjs";
|
|
12
|
+
import "../chunk-KRQA4FRY.mjs";
|
|
13
13
|
import {
|
|
14
14
|
loadPackageJsonForMsg
|
|
15
15
|
} from "../chunk-F7XZRKP7.mjs";
|
package/dist/commands/import.cjs
CHANGED
|
@@ -171,17 +171,6 @@ function pgsTokenToMf2Key(token, kind) {
|
|
|
171
171
|
}
|
|
172
172
|
return { type: "literal", value: token };
|
|
173
173
|
}
|
|
174
|
-
function parsePatternFromSegmentBody(body) {
|
|
175
|
-
const trimmed = body.trim();
|
|
176
|
-
if (!trimmed) return [];
|
|
177
|
-
try {
|
|
178
|
-
const m = (0, import_messageformat.parseMessage)(trimmed);
|
|
179
|
-
if (m.type !== "message") return [];
|
|
180
|
-
return m.pattern;
|
|
181
|
-
} catch {
|
|
182
|
-
return [];
|
|
183
|
-
}
|
|
184
|
-
}
|
|
185
174
|
function ensureFallbackVariant(keysCount, variants) {
|
|
186
175
|
const hasAllStars = variants.some(
|
|
187
176
|
(v) => v.keys.length === keysCount && v.keys.every((k) => (0, import_messageformat.isCatchallKey)(k))
|
|
@@ -243,11 +232,13 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
243
232
|
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
244
233
|
if (tokens.length !== n) return null;
|
|
245
234
|
const keys = tokens.map(
|
|
246
|
-
(t,
|
|
235
|
+
(t, i2) => pgsTokenToMf2Key(t, parsedSwitch[i2].kind)
|
|
247
236
|
);
|
|
248
237
|
variants.push({
|
|
249
238
|
keys,
|
|
250
|
-
|
|
239
|
+
// Empty pattern: stringify emits `{{}}`, then we splice the raw XLIFF
|
|
240
|
+
// body so `\t` / `\n` / `\{` are not dropped or double-escaped.
|
|
241
|
+
value: []
|
|
251
242
|
});
|
|
252
243
|
}
|
|
253
244
|
if (variants.length === 0) return null;
|
|
@@ -265,9 +256,15 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
265
256
|
} catch {
|
|
266
257
|
return null;
|
|
267
258
|
}
|
|
268
|
-
|
|
259
|
+
const bodies = segments.map((seg) => seg.body);
|
|
260
|
+
while (bodies.length < msg.variants.length) {
|
|
261
|
+
bodies.push(segments[segments.length - 1]?.body ?? "");
|
|
262
|
+
}
|
|
263
|
+
const skeleton = (0, import_messageformat.stringifyMessage)(
|
|
269
264
|
msg
|
|
270
265
|
);
|
|
266
|
+
let i = 0;
|
|
267
|
+
return skeleton.replace(/\{\{\}\}/g, () => `{{${bodies[i++] ?? ""}}}`);
|
|
271
268
|
}
|
|
272
269
|
|
|
273
270
|
// src/lib/pgs-mf1.ts
|
package/dist/commands/import.mjs
CHANGED
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
parseXliffFilename,
|
|
6
6
|
processXliffFile,
|
|
7
7
|
writeTranslationFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-VJKOG7EA.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-PQJA3H4Z.mjs";
|
|
11
|
+
import "../chunk-HH6ZOHO4.mjs";
|
|
12
|
+
import "../chunk-KRQA4FRY.mjs";
|
|
13
13
|
import {
|
|
14
14
|
loadPackageJsonForMsg
|
|
15
15
|
} from "../chunk-F7XZRKP7.mjs";
|
|
@@ -5,8 +5,8 @@ import {
|
|
|
5
5
|
loadPackageJsonForCreateProject,
|
|
6
6
|
resolveCreateProjectFormat,
|
|
7
7
|
writeMsgProjectFile
|
|
8
|
-
} from "../chunk-
|
|
9
|
-
import "../chunk-
|
|
8
|
+
} from "../chunk-W33MA4DJ.mjs";
|
|
9
|
+
import "../chunk-KRQA4FRY.mjs";
|
|
10
10
|
import "../chunk-F7XZRKP7.mjs";
|
|
11
11
|
export {
|
|
12
12
|
CREATE_PROJECT_FORMATS,
|
|
@@ -170,7 +170,7 @@ function generateMsgResourceContent(params) {
|
|
|
170
170
|
const dirStr = `'${dir}'`;
|
|
171
171
|
const resourceNote = `This is the ${escapeSingleQuoted(title)} resource.`;
|
|
172
172
|
const projectImport = `#i18n/projects/${escapeSingleQuoted(projectName)}.js`;
|
|
173
|
-
const
|
|
173
|
+
const createWithMessages = `MsgResource.create({
|
|
174
174
|
title: ${titleStr},
|
|
175
175
|
attributes: {
|
|
176
176
|
lang: ${langStr},
|
|
@@ -178,22 +178,35 @@ function generateMsgResourceContent(params) {
|
|
|
178
178
|
},
|
|
179
179
|
notes: [
|
|
180
180
|
{type: 'DESCRIPTION', content: '${resourceNote}'}
|
|
181
|
-
]
|
|
181
|
+
],
|
|
182
|
+
/**
|
|
183
|
+
* The JSDoc const assertion lets TypeScript infer the message keys, so
|
|
184
|
+
* resource.get('unknownKey') is a compile error. Remove it to opt out.
|
|
185
|
+
*/
|
|
186
|
+
messages: /** @type {const} */ ([
|
|
187
|
+
{
|
|
188
|
+
key: 'sampleKey',
|
|
189
|
+
value: 'Sample value.',
|
|
190
|
+
notes: [
|
|
191
|
+
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
192
|
+
]
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
key: 'sampleKey2',
|
|
196
|
+
value: 'Hi, {name}',
|
|
197
|
+
attributes: { dnt: true },
|
|
198
|
+
notes: [
|
|
199
|
+
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
200
|
+
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
201
|
+
]
|
|
202
|
+
}
|
|
203
|
+
])
|
|
182
204
|
}, project);
|
|
183
205
|
|
|
184
|
-
/**
|
|
185
|
-
*
|
|
186
|
-
*
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
resource
|
|
190
|
-
.add('sampleKey', 'Sample value.', {}, [
|
|
191
|
-
{ type: 'DESCRIPTION', content: 'This is first message.' }
|
|
192
|
-
])
|
|
193
|
-
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [
|
|
194
|
-
{ type: 'DESCRIPTION', content: 'This is the second message.' },
|
|
195
|
-
{ type: 'PARAMETERS', content: 'The {name} parameter holds the user name.' }
|
|
196
|
-
]);`;
|
|
206
|
+
/**
|
|
207
|
+
* More messages can be added with the chainable add(key, value, attributes, notes),
|
|
208
|
+
* but keys added that way are not part of the inferred key type.
|
|
209
|
+
*/`;
|
|
197
210
|
if (isEsm) {
|
|
198
211
|
return `/** ESM module **/
|
|
199
212
|
|
|
@@ -202,7 +215,7 @@ import project from '${projectImport}';
|
|
|
202
215
|
|
|
203
216
|
/** Create a MsgResource object */
|
|
204
217
|
|
|
205
|
-
export const resource = ${
|
|
218
|
+
export const resource = ${createWithMessages}
|
|
206
219
|
|
|
207
220
|
/**
|
|
208
221
|
* An async function to get a translated version of the resource
|
|
@@ -219,7 +232,7 @@ export async function getMessages() {
|
|
|
219
232
|
const { MsgResource, getLang } = require('@worldware/msg');
|
|
220
233
|
const project = require('${projectImport}');
|
|
221
234
|
|
|
222
|
-
const resource = ${
|
|
235
|
+
const resource = ${createWithMessages}
|
|
223
236
|
|
|
224
237
|
/**
|
|
225
238
|
* An async function to get a translated version of the resource
|
|
@@ -35,9 +35,11 @@ declare function importMsgProjectForResource(projectsDir: string, projectName: s
|
|
|
35
35
|
} | undefined>;
|
|
36
36
|
/**
|
|
37
37
|
* Generates the MsgResource file content as a string.
|
|
38
|
-
* Emits ESM or CJS boilerplate that creates a MsgResource
|
|
39
|
-
* messages
|
|
40
|
-
* `
|
|
38
|
+
* Emits ESM or CJS boilerplate that creates a MsgResource with sample
|
|
39
|
+
* messages passed to `create()` inside a JSDoc `@type {const}` assertion
|
|
40
|
+
* (the `.js` form of `as const`), so TypeScript infers the message key union for
|
|
41
|
+
* `.msg.js` files. Exports `resource` plus an async `getMessages` loader that
|
|
42
|
+
* calls `resource.getTranslation(getLang())`.
|
|
41
43
|
* Project import uses the `#i18n/projects/<name>.js` alias from `msg init`.
|
|
42
44
|
* @param params - Title, projectName, sourceLocale, dir, and isEsm
|
|
43
45
|
* @returns The generated file content
|
|
@@ -35,9 +35,11 @@ declare function importMsgProjectForResource(projectsDir: string, projectName: s
|
|
|
35
35
|
} | undefined>;
|
|
36
36
|
/**
|
|
37
37
|
* Generates the MsgResource file content as a string.
|
|
38
|
-
* Emits ESM or CJS boilerplate that creates a MsgResource
|
|
39
|
-
* messages
|
|
40
|
-
* `
|
|
38
|
+
* Emits ESM or CJS boilerplate that creates a MsgResource with sample
|
|
39
|
+
* messages passed to `create()` inside a JSDoc `@type {const}` assertion
|
|
40
|
+
* (the `.js` form of `as const`), so TypeScript infers the message key union for
|
|
41
|
+
* `.msg.js` files. Exports `resource` plus an async `getMessages` loader that
|
|
42
|
+
* calls `resource.getTranslation(getLang())`.
|
|
41
43
|
* Project import uses the `#i18n/projects/<name>.js` alias from `msg init`.
|
|
42
44
|
* @param params - Title, projectName, sourceLocale, dir, and isEsm
|
|
43
45
|
* @returns The generated file content
|
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-WGI2EFOS.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
-
import "../chunk-
|
|
11
|
-
import "../chunk-
|
|
12
|
-
import "../chunk-
|
|
10
|
+
import "../chunk-PQJA3H4Z.mjs";
|
|
11
|
+
import "../chunk-HH6ZOHO4.mjs";
|
|
12
|
+
import "../chunk-KRQA4FRY.mjs";
|
|
13
13
|
import "../chunk-F7XZRKP7.mjs";
|
|
14
14
|
export {
|
|
15
15
|
filterResourceGroupsByProject,
|
|
@@ -111,17 +111,6 @@ function pgsTokenToMf2Key(token, kind) {
|
|
|
111
111
|
}
|
|
112
112
|
return { type: "literal", value: token };
|
|
113
113
|
}
|
|
114
|
-
function parsePatternFromSegmentBody(body) {
|
|
115
|
-
const trimmed = body.trim();
|
|
116
|
-
if (!trimmed) return [];
|
|
117
|
-
try {
|
|
118
|
-
const m = (0, import_messageformat.parseMessage)(trimmed);
|
|
119
|
-
if (m.type !== "message") return [];
|
|
120
|
-
return m.pattern;
|
|
121
|
-
} catch {
|
|
122
|
-
return [];
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
114
|
function ensureFallbackVariant(keysCount, variants) {
|
|
126
115
|
const hasAllStars = variants.some(
|
|
127
116
|
(v) => v.keys.length === keysCount && v.keys.every((k) => (0, import_messageformat.isCatchallKey)(k))
|
|
@@ -183,11 +172,13 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
183
172
|
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
184
173
|
if (tokens.length !== n) return null;
|
|
185
174
|
const keys = tokens.map(
|
|
186
|
-
(t,
|
|
175
|
+
(t, i2) => pgsTokenToMf2Key(t, parsedSwitch[i2].kind)
|
|
187
176
|
);
|
|
188
177
|
variants.push({
|
|
189
178
|
keys,
|
|
190
|
-
|
|
179
|
+
// Empty pattern: stringify emits `{{}}`, then we splice the raw XLIFF
|
|
180
|
+
// body so `\t` / `\n` / `\{` are not dropped or double-escaped.
|
|
181
|
+
value: []
|
|
191
182
|
});
|
|
192
183
|
}
|
|
193
184
|
if (variants.length === 0) return null;
|
|
@@ -205,9 +196,15 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
205
196
|
} catch {
|
|
206
197
|
return null;
|
|
207
198
|
}
|
|
208
|
-
|
|
199
|
+
const bodies = segments.map((seg) => seg.body);
|
|
200
|
+
while (bodies.length < msg.variants.length) {
|
|
201
|
+
bodies.push(segments[segments.length - 1]?.body ?? "");
|
|
202
|
+
}
|
|
203
|
+
const skeleton = (0, import_messageformat.stringifyMessage)(
|
|
209
204
|
msg
|
|
210
205
|
);
|
|
206
|
+
let i = 0;
|
|
207
|
+
return skeleton.replace(/\{\{\}\}/g, () => `{{${bodies[i++] ?? ""}}}`);
|
|
211
208
|
}
|
|
212
209
|
|
|
213
210
|
// src/lib/pgs-mf1.ts
|
|
@@ -8,11 +8,11 @@ import {
|
|
|
8
8
|
parseXliffFilename,
|
|
9
9
|
processXliffFile,
|
|
10
10
|
writeTranslationFiles
|
|
11
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-VJKOG7EA.mjs";
|
|
12
12
|
import "../chunk-Q775ZGC5.mjs";
|
|
13
|
-
import "../chunk-
|
|
14
|
-
import "../chunk-
|
|
15
|
-
import "../chunk-
|
|
13
|
+
import "../chunk-PQJA3H4Z.mjs";
|
|
14
|
+
import "../chunk-HH6ZOHO4.mjs";
|
|
15
|
+
import "../chunk-KRQA4FRY.mjs";
|
|
16
16
|
import "../chunk-F7XZRKP7.mjs";
|
|
17
17
|
export {
|
|
18
18
|
extractResourceFromXliffFile,
|
package/dist/lib/pgs-mf1.mjs
CHANGED
package/dist/lib/pgs-mf2.cjs
CHANGED
|
@@ -178,17 +178,6 @@ function selectMessageToPgsExport(source) {
|
|
|
178
178
|
}
|
|
179
179
|
return selectMessageDataToPgsExport(msg);
|
|
180
180
|
}
|
|
181
|
-
function parsePatternFromSegmentBody(body) {
|
|
182
|
-
const trimmed = body.trim();
|
|
183
|
-
if (!trimmed) return [];
|
|
184
|
-
try {
|
|
185
|
-
const m = (0, import_messageformat.parseMessage)(trimmed);
|
|
186
|
-
if (m.type !== "message") return [];
|
|
187
|
-
return m.pattern;
|
|
188
|
-
} catch {
|
|
189
|
-
return [];
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
181
|
function ensureFallbackVariant(keysCount, variants) {
|
|
193
182
|
const hasAllStars = variants.some(
|
|
194
183
|
(v) => v.keys.length === keysCount && v.keys.every((k) => (0, import_messageformat.isCatchallKey)(k))
|
|
@@ -250,11 +239,13 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
250
239
|
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
251
240
|
if (tokens.length !== n) return null;
|
|
252
241
|
const keys = tokens.map(
|
|
253
|
-
(t,
|
|
242
|
+
(t, i2) => pgsTokenToMf2Key(t, parsedSwitch[i2].kind)
|
|
254
243
|
);
|
|
255
244
|
variants.push({
|
|
256
245
|
keys,
|
|
257
|
-
|
|
246
|
+
// Empty pattern: stringify emits `{{}}`, then we splice the raw XLIFF
|
|
247
|
+
// body so `\t` / `\n` / `\{` are not dropped or double-escaped.
|
|
248
|
+
value: []
|
|
258
249
|
});
|
|
259
250
|
}
|
|
260
251
|
if (variants.length === 0) return null;
|
|
@@ -272,9 +263,15 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
272
263
|
} catch {
|
|
273
264
|
return null;
|
|
274
265
|
}
|
|
275
|
-
|
|
266
|
+
const bodies = segments.map((seg) => seg.body);
|
|
267
|
+
while (bodies.length < msg.variants.length) {
|
|
268
|
+
bodies.push(segments[segments.length - 1]?.body ?? "");
|
|
269
|
+
}
|
|
270
|
+
const skeleton = (0, import_messageformat.stringifyMessage)(
|
|
276
271
|
msg
|
|
277
272
|
);
|
|
273
|
+
let i = 0;
|
|
274
|
+
return skeleton.replace(/\{\{\}\}/g, () => `{{${bodies[i++] ?? ""}}}`);
|
|
278
275
|
}
|
|
279
276
|
function messagesStructurallyEqual(a, b) {
|
|
280
277
|
try {
|
package/dist/lib/pgs-mf2.d.mts
CHANGED
|
@@ -73,6 +73,9 @@ interface PgsSegmentImport {
|
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Builds MF2 source string from PGS `pgs:switch` and segment bodies.
|
|
76
|
+
*
|
|
77
|
+
* Segment bodies are spliced into quoted patterns as-is so XLIFF `\` sequences
|
|
78
|
+
* are not dropped (`\t`/`\n` are invalid MF2 escapes) or double-escaped.
|
|
76
79
|
*/
|
|
77
80
|
declare function pgsImportToSelectMessage(switchAttr: string, segments: PgsSegmentImport[]): string | null;
|
|
78
81
|
/** Compare two MF2 strings by parsing and re-stringifying both (for tests). */
|
package/dist/lib/pgs-mf2.d.ts
CHANGED
|
@@ -73,6 +73,9 @@ interface PgsSegmentImport {
|
|
|
73
73
|
}
|
|
74
74
|
/**
|
|
75
75
|
* Builds MF2 source string from PGS `pgs:switch` and segment bodies.
|
|
76
|
+
*
|
|
77
|
+
* Segment bodies are spliced into quoted patterns as-is so XLIFF `\` sequences
|
|
78
|
+
* are not dropped (`\t`/`\n` are invalid MF2 escapes) or double-escaped.
|
|
76
79
|
*/
|
|
77
80
|
declare function pgsImportToSelectMessage(switchAttr: string, segments: PgsSegmentImport[]): string | null;
|
|
78
81
|
/** Compare two MF2 strings by parsing and re-stringifying both (for tests). */
|
package/dist/lib/pgs-mf2.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldware/msg-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "CLI for msg library",
|
|
5
5
|
"main": "dist/commands/init.cjs",
|
|
6
6
|
"module": "dist/commands/init.mjs",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"@messageformat/icu-messageformat-1": "^0.12.0",
|
|
40
40
|
"@messageformat/parser": "^5.1.1",
|
|
41
41
|
"@oclif/core": "^3.21.2",
|
|
42
|
-
"@worldware/msg": "^0.
|
|
42
|
+
"@worldware/msg": "^0.13.0",
|
|
43
43
|
"fast-xml-parser": "^5.3.4",
|
|
44
44
|
"messageformat": "^4.0.0-10"
|
|
45
45
|
},
|