@worldware/msg-cli 0.3.1 → 0.4.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 +31 -4
- package/dist/{chunk-6A6JGF7U.mjs → chunk-5VZRQBSH.mjs} +1 -1
- package/dist/{chunk-4Z65MYW7.mjs → chunk-77PP26JZ.mjs} +1 -1
- package/dist/{chunk-LNCUPVEF.mjs → chunk-JQKAEOL7.mjs} +64 -36
- package/dist/{chunk-6BLQESHW.mjs → chunk-KZ47S4SH.mjs} +15 -3
- package/dist/commands/create/project.mjs +2 -2
- package/dist/commands/create/resource.cjs +64 -36
- package/dist/commands/create/resource.mjs +1 -1
- package/dist/commands/export.cjs +14 -2
- package/dist/commands/export.mjs +2 -2
- package/dist/commands/import.mjs +2 -2
- package/dist/lib/create-project-helpers.mjs +2 -2
- package/dist/lib/create-resource-helpers.cjs +64 -36
- package/dist/lib/create-resource-helpers.d.mts +4 -0
- package/dist/lib/create-resource-helpers.d.ts +4 -0
- package/dist/lib/create-resource-helpers.mjs +1 -1
- package/dist/lib/export-helpers.cjs +14 -2
- package/dist/lib/export-helpers.mjs +2 -2
- package/dist/lib/import-helpers.mjs +2 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -154,12 +154,39 @@ msg create resource myProject messages --edit
|
|
|
154
154
|
|
|
155
155
|
**Behavior:**
|
|
156
156
|
|
|
157
|
-
- Writes the file to `i18n/resources/<title>.msg.js` (always `.js
|
|
158
|
-
- Uses ES module or CommonJS
|
|
157
|
+
- Writes the file to `i18n/resources/<title>.msg.js` (always `.js`, including TypeScript projects).
|
|
158
|
+
- Uses ES module or CommonJS syntax based on `package.json` `"type"` or presence of `tsconfig.json`.
|
|
159
|
+
- Imports the project via the `#i18n/projects/<projectName>` alias (added by `msg init`).
|
|
159
160
|
- Sets `lang` from the project's `sourceLocale` and `dir` to `rtl` for Arabic/Hebrew, `ltr` otherwise.
|
|
160
|
-
-
|
|
161
|
+
- 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.
|
|
162
|
+
- Includes sample messages added via chainable `.add()`. Validates that the generated file is importable.
|
|
161
163
|
- Errors if i18n/projects or i18n/resources does not exist, the project is not found, or the resource file already exists (unless `--force`).
|
|
162
164
|
|
|
165
|
+
**Generated file (ESM excerpt):**
|
|
166
|
+
|
|
167
|
+
```javascript
|
|
168
|
+
import { MsgResource, getLang } from '@worldware/msg';
|
|
169
|
+
import project from '#i18n/projects/myProject';
|
|
170
|
+
|
|
171
|
+
export const resource = MsgResource.create({ /* title, attributes, notes */ }, project);
|
|
172
|
+
|
|
173
|
+
resource
|
|
174
|
+
.add('sampleKey', 'Sample value.', {}, [/* notes */])
|
|
175
|
+
.add('sampleKey2', 'Hi, {name}', { dnt: true }, [/* notes */]);
|
|
176
|
+
|
|
177
|
+
export async function getMessages() {
|
|
178
|
+
return await resource.getTranslation(getLang());
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Usage:**
|
|
183
|
+
|
|
184
|
+
```javascript
|
|
185
|
+
import { getMessages } from '#i18n/resources/messages.msg.js';
|
|
186
|
+
|
|
187
|
+
const messages = await getMessages();
|
|
188
|
+
```
|
|
189
|
+
|
|
163
190
|
### export
|
|
164
191
|
|
|
165
192
|
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).
|
|
@@ -187,7 +214,7 @@ msg export -p myApp
|
|
|
187
214
|
**Behavior:**
|
|
188
215
|
|
|
189
216
|
- Recursively finds all `.msg.js` and `.msg.ts` files under `i18n/resources`.
|
|
190
|
-
- Imports each file as a MsgResource; errors if any file is invalid.
|
|
217
|
+
- Imports each file as a MsgResource (default export, or named `resource` / `MsgResource`, including CJS `module.exports = { resource }`); errors if any file is invalid.
|
|
191
218
|
- Groups resources by project name and writes one XLIFF 2.2 file per project to `l10n/xliff` (e.g. `myApp.xliff`).
|
|
192
219
|
- With `--project`, only that project is exported; existing other files in `l10n/xliff` are not removed.
|
|
193
220
|
- If no MsgResource files are found, exits with an informational message (no error).
|
|
@@ -71,52 +71,80 @@ async function importMsgProjectForResource(projectsDir, projectName) {
|
|
|
71
71
|
}
|
|
72
72
|
return void 0;
|
|
73
73
|
}
|
|
74
|
+
function escapeSingleQuoted(value) {
|
|
75
|
+
return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
76
|
+
}
|
|
74
77
|
function generateMsgResourceContent(params) {
|
|
75
78
|
const { title, projectName, sourceLocale, dir, isEsm } = params;
|
|
76
|
-
const
|
|
77
|
-
const
|
|
78
|
-
{
|
|
79
|
-
key: 'example.message',
|
|
80
|
-
value: 'Example message.',
|
|
81
|
-
notes: [
|
|
82
|
-
{ type: 'DESCRIPTION', content: 'This is an example message. You can delete it.' }
|
|
83
|
-
]
|
|
84
|
-
}
|
|
85
|
-
]`;
|
|
86
|
-
const titleStr = `'${title.replace(/'/g, "\\'")}'`;
|
|
87
|
-
const langStr = `'${sourceLocale.replace(/'/g, "\\'")}'`;
|
|
79
|
+
const titleStr = `'${escapeSingleQuoted(title)}'`;
|
|
80
|
+
const langStr = `'${escapeSingleQuoted(sourceLocale)}'`;
|
|
88
81
|
const dirStr = `'${dir}'`;
|
|
82
|
+
const resourceNote = `This is the ${escapeSingleQuoted(title)} resource.`;
|
|
83
|
+
const projectImport = `#i18n/projects/${escapeSingleQuoted(projectName)}`;
|
|
84
|
+
const createAndAdd = `MsgResource.create({
|
|
85
|
+
title: ${titleStr},
|
|
86
|
+
attributes: {
|
|
87
|
+
lang: ${langStr},
|
|
88
|
+
dir: ${dirStr}
|
|
89
|
+
},
|
|
90
|
+
notes: [
|
|
91
|
+
{type: 'DESCRIPTION', content: '${resourceNote}'}
|
|
92
|
+
]
|
|
93
|
+
}, project);
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Add messages to the resource using add(key, value, attributes, notes)
|
|
97
|
+
* The add method is chainable.
|
|
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
|
+
]);`;
|
|
89
108
|
if (isEsm) {
|
|
90
|
-
return
|
|
109
|
+
return `/** ESM module **/
|
|
110
|
+
|
|
111
|
+
import { MsgResource, getLang } from '@worldware/msg';
|
|
91
112
|
import project from '${projectImport}';
|
|
92
113
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
114
|
+
/** Create a MsgResource object */
|
|
115
|
+
|
|
116
|
+
export const resource = ${createAndAdd}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* An async function to get a translated version of the resource
|
|
120
|
+
* If the runtime language has not been set using \`setLang()\`,
|
|
121
|
+
* it will return the original resource
|
|
122
|
+
*/
|
|
123
|
+
export async function getMessages() {
|
|
124
|
+
return await resource.getTranslation(getLang());
|
|
125
|
+
}
|
|
104
126
|
`;
|
|
105
127
|
}
|
|
106
|
-
return
|
|
128
|
+
return `/** CJS implementation **/
|
|
129
|
+
|
|
130
|
+
const { MsgResource, getLang } = require('@worldware/msg');
|
|
107
131
|
const project = require('${projectImport}');
|
|
108
132
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
133
|
+
const resource = ${createAndAdd}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* An async function to get a translated version of the resource
|
|
137
|
+
* If a runtime language has not been set using \`setLang()\`,
|
|
138
|
+
* it will return the original resource
|
|
139
|
+
*/
|
|
140
|
+
async function getMessages() {
|
|
141
|
+
return await resource.getTranslation(getLang());
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
module.exports = {
|
|
145
|
+
resource,
|
|
146
|
+
getMessages
|
|
147
|
+
}
|
|
120
148
|
`;
|
|
121
149
|
}
|
|
122
150
|
function writeMsgResourceFile(filePath, content) {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from "./chunk-MGHC4AYZ.mjs";
|
|
11
11
|
import {
|
|
12
12
|
dynamicImportFromUrl
|
|
13
|
-
} from "./chunk-
|
|
13
|
+
} from "./chunk-JQKAEOL7.mjs";
|
|
14
14
|
|
|
15
15
|
// src/lib/export-helpers.ts
|
|
16
16
|
import { MSG_DEFAULT_FORMAT } from "@worldware/msg";
|
|
@@ -21,6 +21,18 @@ var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
|
21
21
|
function isMsgResourceLike(value) {
|
|
22
22
|
return Boolean(value) && typeof value.getProject === "function";
|
|
23
23
|
}
|
|
24
|
+
function resolveMsgResourceExport(mod) {
|
|
25
|
+
const defaultExport = mod.default;
|
|
26
|
+
const defaultRecord = defaultExport && typeof defaultExport === "object" ? defaultExport : void 0;
|
|
27
|
+
const candidates = [
|
|
28
|
+
defaultExport,
|
|
29
|
+
mod.resource,
|
|
30
|
+
mod.MsgResource,
|
|
31
|
+
defaultRecord?.resource,
|
|
32
|
+
defaultRecord?.MsgResource
|
|
33
|
+
];
|
|
34
|
+
return candidates.find(isMsgResourceLike);
|
|
35
|
+
}
|
|
24
36
|
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
25
37
|
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
26
38
|
async function findMsgResourceFilePaths(directory) {
|
|
@@ -44,8 +56,8 @@ async function importMsgResourcesFromPaths(filePaths) {
|
|
|
44
56
|
}
|
|
45
57
|
const url = pathToFileURL(filePath).href;
|
|
46
58
|
const mod = await dynamicImportFromUrl(url);
|
|
47
|
-
const resource = mod
|
|
48
|
-
if (!
|
|
59
|
+
const resource = resolveMsgResourceExport(mod);
|
|
60
|
+
if (!resource) {
|
|
49
61
|
throw new Error(
|
|
50
62
|
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
51
63
|
);
|
|
@@ -3,8 +3,8 @@ import {
|
|
|
3
3
|
importMsgProjectFile,
|
|
4
4
|
loadPackageJsonForCreateProject,
|
|
5
5
|
writeMsgProjectFile
|
|
6
|
-
} from "../../chunk-
|
|
7
|
-
import "../../chunk-
|
|
6
|
+
} from "../../chunk-5VZRQBSH.mjs";
|
|
7
|
+
import "../../chunk-JQKAEOL7.mjs";
|
|
8
8
|
import {
|
|
9
9
|
findPackageJsonPath
|
|
10
10
|
} from "../../chunk-F7XZRKP7.mjs";
|
|
@@ -172,52 +172,80 @@ async function importMsgProjectForResource(projectsDir, projectName) {
|
|
|
172
172
|
}
|
|
173
173
|
return void 0;
|
|
174
174
|
}
|
|
175
|
+
function escapeSingleQuoted(value) {
|
|
176
|
+
return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
177
|
+
}
|
|
175
178
|
function generateMsgResourceContent(params) {
|
|
176
179
|
const { title, projectName, sourceLocale, dir, isEsm } = params;
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
{
|
|
180
|
-
key: 'example.message',
|
|
181
|
-
value: 'Example message.',
|
|
182
|
-
notes: [
|
|
183
|
-
{ type: 'DESCRIPTION', content: 'This is an example message. You can delete it.' }
|
|
184
|
-
]
|
|
185
|
-
}
|
|
186
|
-
]`;
|
|
187
|
-
const titleStr = `'${title.replace(/'/g, "\\'")}'`;
|
|
188
|
-
const langStr = `'${sourceLocale.replace(/'/g, "\\'")}'`;
|
|
180
|
+
const titleStr = `'${escapeSingleQuoted(title)}'`;
|
|
181
|
+
const langStr = `'${escapeSingleQuoted(sourceLocale)}'`;
|
|
189
182
|
const dirStr = `'${dir}'`;
|
|
183
|
+
const resourceNote = `This is the ${escapeSingleQuoted(title)} resource.`;
|
|
184
|
+
const projectImport = `#i18n/projects/${escapeSingleQuoted(projectName)}`;
|
|
185
|
+
const createAndAdd = `MsgResource.create({
|
|
186
|
+
title: ${titleStr},
|
|
187
|
+
attributes: {
|
|
188
|
+
lang: ${langStr},
|
|
189
|
+
dir: ${dirStr}
|
|
190
|
+
},
|
|
191
|
+
notes: [
|
|
192
|
+
{type: 'DESCRIPTION', content: '${resourceNote}'}
|
|
193
|
+
]
|
|
194
|
+
}, project);
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Add messages to the resource using add(key, value, attributes, notes)
|
|
198
|
+
* The add method is chainable.
|
|
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
|
+
]);`;
|
|
190
209
|
if (isEsm) {
|
|
191
|
-
return
|
|
210
|
+
return `/** ESM module **/
|
|
211
|
+
|
|
212
|
+
import { MsgResource, getLang } from '@worldware/msg';
|
|
192
213
|
import project from '${projectImport}';
|
|
193
214
|
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
215
|
+
/** Create a MsgResource object */
|
|
216
|
+
|
|
217
|
+
export const resource = ${createAndAdd}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* An async function to get a translated version of the resource
|
|
221
|
+
* If the runtime language has not been set using \`setLang()\`,
|
|
222
|
+
* it will return the original resource
|
|
223
|
+
*/
|
|
224
|
+
export async function getMessages() {
|
|
225
|
+
return await resource.getTranslation(getLang());
|
|
226
|
+
}
|
|
205
227
|
`;
|
|
206
228
|
}
|
|
207
|
-
return
|
|
229
|
+
return `/** CJS implementation **/
|
|
230
|
+
|
|
231
|
+
const { MsgResource, getLang } = require('@worldware/msg');
|
|
208
232
|
const project = require('${projectImport}');
|
|
209
233
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
234
|
+
const resource = ${createAndAdd}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* An async function to get a translated version of the resource
|
|
238
|
+
* If a runtime language has not been set using \`setLang()\`,
|
|
239
|
+
* it will return the original resource
|
|
240
|
+
*/
|
|
241
|
+
async function getMessages() {
|
|
242
|
+
return await resource.getTranslation(getLang());
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
module.exports = {
|
|
246
|
+
resource,
|
|
247
|
+
getMessages
|
|
248
|
+
}
|
|
221
249
|
`;
|
|
222
250
|
}
|
|
223
251
|
function writeMsgResourceFile(filePath, content) {
|
package/dist/commands/export.cjs
CHANGED
|
@@ -347,6 +347,18 @@ var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
|
347
347
|
function isMsgResourceLike(value) {
|
|
348
348
|
return Boolean(value) && typeof value.getProject === "function";
|
|
349
349
|
}
|
|
350
|
+
function resolveMsgResourceExport(mod) {
|
|
351
|
+
const defaultExport = mod.default;
|
|
352
|
+
const defaultRecord = defaultExport && typeof defaultExport === "object" ? defaultExport : void 0;
|
|
353
|
+
const candidates = [
|
|
354
|
+
defaultExport,
|
|
355
|
+
mod.resource,
|
|
356
|
+
mod.MsgResource,
|
|
357
|
+
defaultRecord?.resource,
|
|
358
|
+
defaultRecord?.MsgResource
|
|
359
|
+
];
|
|
360
|
+
return candidates.find(isMsgResourceLike);
|
|
361
|
+
}
|
|
350
362
|
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
351
363
|
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
352
364
|
async function findMsgResourceFilePaths(directory) {
|
|
@@ -370,8 +382,8 @@ async function importMsgResourcesFromPaths(filePaths) {
|
|
|
370
382
|
}
|
|
371
383
|
const url = (0, import_url2.pathToFileURL)(filePath).href;
|
|
372
384
|
const mod = await dynamicImportFromUrl(url);
|
|
373
|
-
const resource = mod
|
|
374
|
-
if (!
|
|
385
|
+
const resource = resolveMsgResourceExport(mod);
|
|
386
|
+
if (!resource) {
|
|
375
387
|
throw new Error(
|
|
376
388
|
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
377
389
|
);
|
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-KZ47S4SH.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
10
|
import "../chunk-XID5J2F7.mjs";
|
|
11
11
|
import "../chunk-MGHC4AYZ.mjs";
|
|
12
|
-
import "../chunk-
|
|
12
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
13
13
|
import {
|
|
14
14
|
loadPackageJsonForMsg
|
|
15
15
|
} from "../chunk-F7XZRKP7.mjs";
|
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-77PP26JZ.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
10
|
import "../chunk-XID5J2F7.mjs";
|
|
11
11
|
import "../chunk-MGHC4AYZ.mjs";
|
|
12
|
-
import "../chunk-
|
|
12
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
13
13
|
import {
|
|
14
14
|
loadPackageJsonForMsg
|
|
15
15
|
} from "../chunk-F7XZRKP7.mjs";
|
|
@@ -3,8 +3,8 @@ import {
|
|
|
3
3
|
importMsgProjectFile,
|
|
4
4
|
loadPackageJsonForCreateProject,
|
|
5
5
|
writeMsgProjectFile
|
|
6
|
-
} from "../chunk-
|
|
7
|
-
import "../chunk-
|
|
6
|
+
} from "../chunk-5VZRQBSH.mjs";
|
|
7
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
8
8
|
import "../chunk-F7XZRKP7.mjs";
|
|
9
9
|
export {
|
|
10
10
|
calculateRelativePath,
|
|
@@ -160,52 +160,80 @@ async function importMsgProjectForResource(projectsDir, projectName) {
|
|
|
160
160
|
}
|
|
161
161
|
return void 0;
|
|
162
162
|
}
|
|
163
|
+
function escapeSingleQuoted(value) {
|
|
164
|
+
return value.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
|
|
165
|
+
}
|
|
163
166
|
function generateMsgResourceContent(params) {
|
|
164
167
|
const { title, projectName, sourceLocale, dir, isEsm } = params;
|
|
165
|
-
const
|
|
166
|
-
const
|
|
167
|
-
{
|
|
168
|
-
key: 'example.message',
|
|
169
|
-
value: 'Example message.',
|
|
170
|
-
notes: [
|
|
171
|
-
{ type: 'DESCRIPTION', content: 'This is an example message. You can delete it.' }
|
|
172
|
-
]
|
|
173
|
-
}
|
|
174
|
-
]`;
|
|
175
|
-
const titleStr = `'${title.replace(/'/g, "\\'")}'`;
|
|
176
|
-
const langStr = `'${sourceLocale.replace(/'/g, "\\'")}'`;
|
|
168
|
+
const titleStr = `'${escapeSingleQuoted(title)}'`;
|
|
169
|
+
const langStr = `'${escapeSingleQuoted(sourceLocale)}'`;
|
|
177
170
|
const dirStr = `'${dir}'`;
|
|
171
|
+
const resourceNote = `This is the ${escapeSingleQuoted(title)} resource.`;
|
|
172
|
+
const projectImport = `#i18n/projects/${escapeSingleQuoted(projectName)}`;
|
|
173
|
+
const createAndAdd = `MsgResource.create({
|
|
174
|
+
title: ${titleStr},
|
|
175
|
+
attributes: {
|
|
176
|
+
lang: ${langStr},
|
|
177
|
+
dir: ${dirStr}
|
|
178
|
+
},
|
|
179
|
+
notes: [
|
|
180
|
+
{type: 'DESCRIPTION', content: '${resourceNote}'}
|
|
181
|
+
]
|
|
182
|
+
}, project);
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Add messages to the resource using add(key, value, attributes, notes)
|
|
186
|
+
* The add method is chainable.
|
|
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
|
+
]);`;
|
|
178
197
|
if (isEsm) {
|
|
179
|
-
return
|
|
198
|
+
return `/** ESM module **/
|
|
199
|
+
|
|
200
|
+
import { MsgResource, getLang } from '@worldware/msg';
|
|
180
201
|
import project from '${projectImport}';
|
|
181
202
|
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
203
|
+
/** Create a MsgResource object */
|
|
204
|
+
|
|
205
|
+
export const resource = ${createAndAdd}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* An async function to get a translated version of the resource
|
|
209
|
+
* If the runtime language has not been set using \`setLang()\`,
|
|
210
|
+
* it will return the original resource
|
|
211
|
+
*/
|
|
212
|
+
export async function getMessages() {
|
|
213
|
+
return await resource.getTranslation(getLang());
|
|
214
|
+
}
|
|
193
215
|
`;
|
|
194
216
|
}
|
|
195
|
-
return
|
|
217
|
+
return `/** CJS implementation **/
|
|
218
|
+
|
|
219
|
+
const { MsgResource, getLang } = require('@worldware/msg');
|
|
196
220
|
const project = require('${projectImport}');
|
|
197
221
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
222
|
+
const resource = ${createAndAdd}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* An async function to get a translated version of the resource
|
|
226
|
+
* If a runtime language has not been set using \`setLang()\`,
|
|
227
|
+
* it will return the original resource
|
|
228
|
+
*/
|
|
229
|
+
async function getMessages() {
|
|
230
|
+
return await resource.getTranslation(getLang());
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
module.exports = {
|
|
234
|
+
resource,
|
|
235
|
+
getMessages
|
|
236
|
+
}
|
|
209
237
|
`;
|
|
210
238
|
}
|
|
211
239
|
function writeMsgResourceFile(filePath, content) {
|
|
@@ -35,6 +35,10 @@ 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, adds sample
|
|
39
|
+
* messages via chainable `.add()`, and exports `resource` plus an async
|
|
40
|
+
* `getMessages` loader that calls `resource.getTranslation(getLang())`.
|
|
41
|
+
* Project import uses the `#i18n/projects/<name>` alias from `msg init`.
|
|
38
42
|
* @param params - Title, projectName, sourceLocale, dir, and isEsm
|
|
39
43
|
* @returns The generated file content
|
|
40
44
|
*/
|
|
@@ -35,6 +35,10 @@ 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, adds sample
|
|
39
|
+
* messages via chainable `.add()`, and exports `resource` plus an async
|
|
40
|
+
* `getMessages` loader that calls `resource.getTranslation(getLang())`.
|
|
41
|
+
* Project import uses the `#i18n/projects/<name>` alias from `msg init`.
|
|
38
42
|
* @param params - Title, projectName, sourceLocale, dir, and isEsm
|
|
39
43
|
* @returns The generated file content
|
|
40
44
|
*/
|
|
@@ -284,6 +284,18 @@ var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
|
284
284
|
function isMsgResourceLike(value) {
|
|
285
285
|
return Boolean(value) && typeof value.getProject === "function";
|
|
286
286
|
}
|
|
287
|
+
function resolveMsgResourceExport(mod) {
|
|
288
|
+
const defaultExport = mod.default;
|
|
289
|
+
const defaultRecord = defaultExport && typeof defaultExport === "object" ? defaultExport : void 0;
|
|
290
|
+
const candidates = [
|
|
291
|
+
defaultExport,
|
|
292
|
+
mod.resource,
|
|
293
|
+
mod.MsgResource,
|
|
294
|
+
defaultRecord?.resource,
|
|
295
|
+
defaultRecord?.MsgResource
|
|
296
|
+
];
|
|
297
|
+
return candidates.find(isMsgResourceLike);
|
|
298
|
+
}
|
|
287
299
|
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
288
300
|
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
289
301
|
async function findMsgResourceFilePaths(directory) {
|
|
@@ -307,8 +319,8 @@ async function importMsgResourcesFromPaths(filePaths) {
|
|
|
307
319
|
}
|
|
308
320
|
const url = (0, import_url2.pathToFileURL)(filePath).href;
|
|
309
321
|
const mod = await dynamicImportFromUrl(url);
|
|
310
|
-
const resource = mod
|
|
311
|
-
if (!
|
|
322
|
+
const resource = resolveMsgResourceExport(mod);
|
|
323
|
+
if (!resource) {
|
|
312
324
|
throw new Error(
|
|
313
325
|
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
314
326
|
);
|
|
@@ -5,11 +5,11 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
8
|
+
} from "../chunk-KZ47S4SH.mjs";
|
|
9
9
|
import "../chunk-Q775ZGC5.mjs";
|
|
10
10
|
import "../chunk-XID5J2F7.mjs";
|
|
11
11
|
import "../chunk-MGHC4AYZ.mjs";
|
|
12
|
-
import "../chunk-
|
|
12
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
13
13
|
import "../chunk-F7XZRKP7.mjs";
|
|
14
14
|
export {
|
|
15
15
|
filterResourceGroupsByProject,
|
|
@@ -8,11 +8,11 @@ import {
|
|
|
8
8
|
parseXliffFilename,
|
|
9
9
|
processXliffFile,
|
|
10
10
|
writeTranslationFiles
|
|
11
|
-
} from "../chunk-
|
|
11
|
+
} from "../chunk-77PP26JZ.mjs";
|
|
12
12
|
import "../chunk-Q775ZGC5.mjs";
|
|
13
13
|
import "../chunk-XID5J2F7.mjs";
|
|
14
14
|
import "../chunk-MGHC4AYZ.mjs";
|
|
15
|
-
import "../chunk-
|
|
15
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
16
16
|
import "../chunk-F7XZRKP7.mjs";
|
|
17
17
|
export {
|
|
18
18
|
extractResourceFromXliffFile,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@worldware/msg-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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.10.0",
|
|
43
43
|
"fast-xml-parser": "^5.3.4",
|
|
44
44
|
"messageformat": "^4.0.0-10"
|
|
45
45
|
},
|