@worldware/msg-cli 0.3.0 → 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 +49 -12
- package/dist/{chunk-6A6JGF7U.mjs → chunk-5VZRQBSH.mjs} +1 -1
- package/dist/{chunk-DIY2WO3R.mjs → chunk-77PP26JZ.mjs} +14 -17
- package/dist/{chunk-LNCUPVEF.mjs → chunk-JQKAEOL7.mjs} +64 -36
- package/dist/{chunk-XHD7F45F.mjs → chunk-KZ47S4SH.mjs} +42 -5
- package/dist/{chunk-DPUF7YUH.mjs → chunk-MGHC4AYZ.mjs} +18 -13
- package/dist/chunk-Q775ZGC5.mjs +29 -0
- package/dist/chunk-XID5J2F7.mjs +135 -0
- package/dist/commands/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 +142 -16
- package/dist/commands/export.mjs +5 -3
- package/dist/commands/import.cjs +135 -17
- package/dist/commands/import.mjs +5 -3
- 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 +142 -16
- package/dist/lib/export-helpers.mjs +5 -3
- package/dist/lib/import-helpers.cjs +135 -17
- package/dist/lib/import-helpers.mjs +5 -3
- package/dist/lib/msg-format.cjs +55 -0
- package/dist/lib/msg-format.d.mts +28 -0
- package/dist/lib/msg-format.d.ts +28 -0
- package/dist/lib/msg-format.mjs +10 -0
- package/dist/lib/pgs-mf1.cjs +289 -0
- package/dist/lib/pgs-mf1.d.mts +28 -0
- package/dist/lib/pgs-mf1.d.ts +28 -0
- package/dist/lib/pgs-mf1.mjs +9 -0
- package/dist/lib/pgs-mf2.cjs +19 -13
- package/dist/lib/pgs-mf2.d.mts +9 -1
- package/dist/lib/pgs-mf2.d.ts +9 -1
- package/dist/lib/pgs-mf2.mjs +3 -1
- package/package.json +13 -3
|
@@ -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
|
*/
|
|
@@ -28,6 +28,7 @@ __export(export_helpers_exports, {
|
|
|
28
28
|
writeXliffFiles: () => writeXliffFiles
|
|
29
29
|
});
|
|
30
30
|
module.exports = __toCommonJS(export_helpers_exports);
|
|
31
|
+
var import_msg2 = require("@worldware/msg");
|
|
31
32
|
var import_promises = require("fs/promises");
|
|
32
33
|
var import_path = require("path");
|
|
33
34
|
var import_url2 = require("url");
|
|
@@ -58,14 +59,42 @@ async function dynamicImportFromUrl(url) {
|
|
|
58
59
|
}
|
|
59
60
|
}
|
|
60
61
|
|
|
62
|
+
// src/lib/msg-format.ts
|
|
63
|
+
var import_msg = require("@worldware/msg");
|
|
64
|
+
var MSG_TYPE_PREFIX = "msg";
|
|
65
|
+
function formatToUnitType(format) {
|
|
66
|
+
return `${MSG_TYPE_PREFIX}:${format}`;
|
|
67
|
+
}
|
|
68
|
+
function resolveMessageFormat(messageFormat, resourceFormat, projectFormat) {
|
|
69
|
+
return messageFormat ?? resourceFormat ?? projectFormat ?? import_msg.MSG_DEFAULT_FORMAT;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// src/lib/pgs-mf1.ts
|
|
73
|
+
var import_parser = require("@messageformat/parser");
|
|
74
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
75
|
+
var import_messageformat2 = require("messageformat");
|
|
76
|
+
|
|
61
77
|
// src/lib/pgs-mf2.ts
|
|
62
78
|
var import_messageformat = require("messageformat");
|
|
79
|
+
var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
|
|
63
80
|
var GENDER_LABELS = /* @__PURE__ */ new Set([
|
|
64
81
|
"feminine",
|
|
65
82
|
"masculine",
|
|
66
83
|
"neuter",
|
|
67
84
|
"other"
|
|
68
85
|
]);
|
|
86
|
+
function parsePgsSwitch(value) {
|
|
87
|
+
const trimmed = value.trim();
|
|
88
|
+
if (!trimmed) return null;
|
|
89
|
+
const parts = trimmed.split(/\s+/);
|
|
90
|
+
const out = [];
|
|
91
|
+
for (const p of parts) {
|
|
92
|
+
const m = p.match(PGS_SWITCH_ITEM);
|
|
93
|
+
if (!m) return null;
|
|
94
|
+
out.push({ kind: m[1], name: m[2] });
|
|
95
|
+
}
|
|
96
|
+
return out.length > 0 ? out : null;
|
|
97
|
+
}
|
|
69
98
|
function findInputDeclaration(msg, varName) {
|
|
70
99
|
for (const d of msg.declarations) {
|
|
71
100
|
const decl = d;
|
|
@@ -78,10 +107,13 @@ function findInputDeclaration(msg, varName) {
|
|
|
78
107
|
function getFunctionName(expr) {
|
|
79
108
|
return expr.functionRef?.name;
|
|
80
109
|
}
|
|
110
|
+
function getOption(opts, key) {
|
|
111
|
+
if (!opts) return void 0;
|
|
112
|
+
if (opts instanceof Map) return opts.get(key);
|
|
113
|
+
return opts[key];
|
|
114
|
+
}
|
|
81
115
|
function getSelectOptionOrdinal(expr) {
|
|
82
|
-
const
|
|
83
|
-
if (!opts || !(opts instanceof Map)) return false;
|
|
84
|
-
const sel = opts.get("select");
|
|
116
|
+
const sel = getOption(expr.functionRef?.options, "select");
|
|
85
117
|
if (!sel || typeof sel !== "object" || sel === null) return false;
|
|
86
118
|
if (sel.type !== "literal") return false;
|
|
87
119
|
return sel.value === "ordinal";
|
|
@@ -149,13 +181,7 @@ function stringifyPattern(pattern) {
|
|
|
149
181
|
wrap
|
|
150
182
|
);
|
|
151
183
|
}
|
|
152
|
-
function
|
|
153
|
-
let msg;
|
|
154
|
-
try {
|
|
155
|
-
msg = (0, import_messageformat.parseMessage)(source);
|
|
156
|
-
} catch {
|
|
157
|
-
return null;
|
|
158
|
-
}
|
|
184
|
+
function selectMessageDataToPgsExport(msg) {
|
|
159
185
|
if (!(0, import_messageformat.isSelectMessage)(msg)) return null;
|
|
160
186
|
const classification = classifySelectMessageForPgs(
|
|
161
187
|
msg
|
|
@@ -170,9 +196,7 @@ function selectMessageToPgsExport(source) {
|
|
|
170
196
|
for (let ki = 0; ki < classification.length; ki++) {
|
|
171
197
|
const key = variant.keys[ki];
|
|
172
198
|
if (!key) return null;
|
|
173
|
-
caseParts.push(
|
|
174
|
-
mf2KeyToPgsToken(key, classification[ki].kind)
|
|
175
|
-
);
|
|
199
|
+
caseParts.push(mf2KeyToPgsToken(key, classification[ki].kind));
|
|
176
200
|
}
|
|
177
201
|
segments.push({
|
|
178
202
|
caseAttr: caseParts.join(" "),
|
|
@@ -181,12 +205,97 @@ function selectMessageToPgsExport(source) {
|
|
|
181
205
|
}
|
|
182
206
|
return { switchAttr, segments };
|
|
183
207
|
}
|
|
208
|
+
function selectMessageToPgsExport(source) {
|
|
209
|
+
let msg;
|
|
210
|
+
try {
|
|
211
|
+
msg = (0, import_messageformat.parseMessage)(source);
|
|
212
|
+
} catch {
|
|
213
|
+
return null;
|
|
214
|
+
}
|
|
215
|
+
return selectMessageDataToPgsExport(msg);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// src/lib/pgs-mf1.ts
|
|
219
|
+
function mf1MessageToPgsExport(source) {
|
|
220
|
+
try {
|
|
221
|
+
const data = (0, import_icu_messageformat_1.mf1ToMessageData)((0, import_parser.parse)(source));
|
|
222
|
+
const exp = selectMessageDataToPgsExport(data);
|
|
223
|
+
if (!exp) return null;
|
|
224
|
+
const parsedSwitch = parsePgsSwitch(exp.switchAttr);
|
|
225
|
+
if (!parsedSwitch) return null;
|
|
226
|
+
const hashVars = pluralVarNames(parsedSwitch);
|
|
227
|
+
return {
|
|
228
|
+
switchAttr: exp.switchAttr,
|
|
229
|
+
segments: exp.segments.map((seg) => ({
|
|
230
|
+
caseAttr: seg.caseAttr,
|
|
231
|
+
sourcePattern: segmentBodyToMf1(seg.sourcePattern, hashVars)
|
|
232
|
+
}))
|
|
233
|
+
};
|
|
234
|
+
} catch {
|
|
235
|
+
return null;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function pluralVarNames(parsedSwitch) {
|
|
239
|
+
const names = /* @__PURE__ */ new Set();
|
|
240
|
+
for (const { kind, name } of parsedSwitch) {
|
|
241
|
+
if (kind === "plural" || kind === "ordinal") names.add(name);
|
|
242
|
+
}
|
|
243
|
+
return names;
|
|
244
|
+
}
|
|
245
|
+
function segmentBodyToMf1(body, hashVars) {
|
|
246
|
+
const trimmed = body.trim();
|
|
247
|
+
if (!trimmed) return "";
|
|
248
|
+
try {
|
|
249
|
+
const msg = (0, import_messageformat2.parseMessage)(trimmed);
|
|
250
|
+
if (msg.type !== "message") {
|
|
251
|
+
return escapeIcuLiteral(trimmed);
|
|
252
|
+
}
|
|
253
|
+
return patternPartsToMf1(
|
|
254
|
+
msg.pattern,
|
|
255
|
+
hashVars
|
|
256
|
+
);
|
|
257
|
+
} catch {
|
|
258
|
+
return escapeIcuLiteral(trimmed);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function patternPartsToMf1(pattern, hashVars) {
|
|
262
|
+
let out = "";
|
|
263
|
+
for (const part of pattern) {
|
|
264
|
+
if (typeof part === "string") {
|
|
265
|
+
out += escapeIcuLiteral(part);
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
if (!part || typeof part !== "object") continue;
|
|
269
|
+
const expr = part;
|
|
270
|
+
if (expr.type === "expression" && expr.arg?.type === "variable" && expr.arg.name) {
|
|
271
|
+
const name = expr.arg.name;
|
|
272
|
+
out += hashVars.has(name) ? "#" : `{${name}}`;
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
function escapeIcuLiteral(s) {
|
|
279
|
+
return s.replace(/'/g, "''").replace(/([{}])/g, "'$1'");
|
|
280
|
+
}
|
|
184
281
|
|
|
185
282
|
// src/lib/export-helpers.ts
|
|
186
283
|
var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
187
284
|
function isMsgResourceLike(value) {
|
|
188
285
|
return Boolean(value) && typeof value.getProject === "function";
|
|
189
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
|
+
}
|
|
190
299
|
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
191
300
|
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
192
301
|
async function findMsgResourceFilePaths(directory) {
|
|
@@ -210,8 +319,8 @@ async function importMsgResourcesFromPaths(filePaths) {
|
|
|
210
319
|
}
|
|
211
320
|
const url = (0, import_url2.pathToFileURL)(filePath).href;
|
|
212
321
|
const mod = await dynamicImportFromUrl(url);
|
|
213
|
-
const resource = mod
|
|
214
|
-
if (!
|
|
322
|
+
const resource = resolveMsgResourceExport(mod);
|
|
323
|
+
if (!resource) {
|
|
215
324
|
throw new Error(
|
|
216
325
|
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
217
326
|
);
|
|
@@ -271,6 +380,11 @@ function renderNotes(notes, idPrefix, indent = " ") {
|
|
|
271
380
|
parts.push(`${indent}</notes>`);
|
|
272
381
|
return parts;
|
|
273
382
|
}
|
|
383
|
+
function pgsExportForFormat(format, value) {
|
|
384
|
+
if (format === "MF1") return mf1MessageToPgsExport(value);
|
|
385
|
+
if (format === "MF2") return selectMessageToPgsExport(value);
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
274
388
|
function resourceGroupToXliff22(group) {
|
|
275
389
|
const srcLang = group.resources[0]?.attributes?.lang ?? "en";
|
|
276
390
|
const parts = [
|
|
@@ -283,6 +397,12 @@ function resourceGroupToXliff22(group) {
|
|
|
283
397
|
fileIndex += 1;
|
|
284
398
|
const fileId = `f${fileIndex}`;
|
|
285
399
|
const attrs = resource.attributes ?? {};
|
|
400
|
+
const projectFormat = resource.getProject().format ?? import_msg2.MSG_DEFAULT_FORMAT;
|
|
401
|
+
const resourceFormat = resolveMessageFormat(
|
|
402
|
+
void 0,
|
|
403
|
+
attrs.format,
|
|
404
|
+
projectFormat
|
|
405
|
+
);
|
|
286
406
|
const fileAttrs = [
|
|
287
407
|
`id="${fileId}"`,
|
|
288
408
|
`original="${escapeXml(orig)}"`
|
|
@@ -313,7 +433,13 @@ function resourceGroupToXliff22(group) {
|
|
|
313
433
|
if (msgAttr?.dir) {
|
|
314
434
|
msgAttrs.push(`srcDir="${escapeXml(msgAttr.dir)}"`);
|
|
315
435
|
}
|
|
316
|
-
const
|
|
436
|
+
const msgFormat = resolveMessageFormat(
|
|
437
|
+
msgAttr?.format,
|
|
438
|
+
resourceFormat,
|
|
439
|
+
projectFormat
|
|
440
|
+
);
|
|
441
|
+
msgAttrs.push(`type="${escapeXml(formatToUnitType(msgFormat))}"`);
|
|
442
|
+
const pgsExport = pgsExportForFormat(msgFormat, msg.value);
|
|
317
443
|
if (pgsExport) {
|
|
318
444
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
319
445
|
}
|
|
@@ -5,9 +5,11 @@ import {
|
|
|
5
5
|
importMsgResourcesFromPaths,
|
|
6
6
|
serializeResourceGroupsToXliff,
|
|
7
7
|
writeXliffFiles
|
|
8
|
-
} from "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
8
|
+
} from "../chunk-KZ47S4SH.mjs";
|
|
9
|
+
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
+
import "../chunk-XID5J2F7.mjs";
|
|
11
|
+
import "../chunk-MGHC4AYZ.mjs";
|
|
12
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
11
13
|
import "../chunk-F7XZRKP7.mjs";
|
|
12
14
|
export {
|
|
13
15
|
filterResourceGroupsByProject,
|
|
@@ -31,7 +31,7 @@ __export(import_helpers_exports, {
|
|
|
31
31
|
writeTranslationFiles: () => writeTranslationFiles
|
|
32
32
|
});
|
|
33
33
|
module.exports = __toCommonJS(import_helpers_exports);
|
|
34
|
-
var
|
|
34
|
+
var import_msg2 = require("@worldware/msg");
|
|
35
35
|
var import_promises = require("fs/promises");
|
|
36
36
|
var import_path = require("path");
|
|
37
37
|
var import_url2 = require("url");
|
|
@@ -64,6 +64,29 @@ async function dynamicImportFromUrl(url) {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
// src/lib/msg-format.ts
|
|
68
|
+
var import_msg = require("@worldware/msg");
|
|
69
|
+
var MSG_TYPE_PREFIX = "msg";
|
|
70
|
+
var FORMAT_VALUES = /* @__PURE__ */ new Set(["NONE", "MF1", "MF2"]);
|
|
71
|
+
function unitTypeToFormat(type) {
|
|
72
|
+
if (type == null || type === "") return void 0;
|
|
73
|
+
if (FORMAT_VALUES.has(type)) {
|
|
74
|
+
return type;
|
|
75
|
+
}
|
|
76
|
+
const prefixed = new RegExp(
|
|
77
|
+
`^${MSG_TYPE_PREFIX}:(NONE|MF1|MF2)$`
|
|
78
|
+
).exec(type);
|
|
79
|
+
if (prefixed?.[1] && FORMAT_VALUES.has(prefixed[1])) {
|
|
80
|
+
return prefixed[1];
|
|
81
|
+
}
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// src/lib/pgs-mf1.ts
|
|
86
|
+
var import_parser = require("@messageformat/parser");
|
|
87
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
88
|
+
var import_messageformat2 = require("messageformat");
|
|
89
|
+
|
|
67
90
|
// src/lib/pgs-mf2.ts
|
|
68
91
|
var import_messageformat = require("messageformat");
|
|
69
92
|
var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
|
|
@@ -187,6 +210,110 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
187
210
|
);
|
|
188
211
|
}
|
|
189
212
|
|
|
213
|
+
// src/lib/pgs-mf1.ts
|
|
214
|
+
function kindToIcuType(kind) {
|
|
215
|
+
if (kind === "plural") return "plural";
|
|
216
|
+
if (kind === "ordinal") return "selectordinal";
|
|
217
|
+
return "select";
|
|
218
|
+
}
|
|
219
|
+
function pluralVarNames(parsedSwitch) {
|
|
220
|
+
const names = /* @__PURE__ */ new Set();
|
|
221
|
+
for (const { kind, name } of parsedSwitch) {
|
|
222
|
+
if (kind === "plural" || kind === "ordinal") names.add(name);
|
|
223
|
+
}
|
|
224
|
+
return names;
|
|
225
|
+
}
|
|
226
|
+
function segmentBodyToMf1(body, hashVars) {
|
|
227
|
+
const trimmed = body.trim();
|
|
228
|
+
if (!trimmed) return "";
|
|
229
|
+
try {
|
|
230
|
+
const msg = (0, import_messageformat2.parseMessage)(trimmed);
|
|
231
|
+
if (msg.type !== "message") {
|
|
232
|
+
return escapeIcuLiteral(trimmed);
|
|
233
|
+
}
|
|
234
|
+
return patternPartsToMf1(
|
|
235
|
+
msg.pattern,
|
|
236
|
+
hashVars
|
|
237
|
+
);
|
|
238
|
+
} catch {
|
|
239
|
+
return escapeIcuLiteral(trimmed);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
function patternPartsToMf1(pattern, hashVars) {
|
|
243
|
+
let out = "";
|
|
244
|
+
for (const part of pattern) {
|
|
245
|
+
if (typeof part === "string") {
|
|
246
|
+
out += escapeIcuLiteral(part);
|
|
247
|
+
continue;
|
|
248
|
+
}
|
|
249
|
+
if (!part || typeof part !== "object") continue;
|
|
250
|
+
const expr = part;
|
|
251
|
+
if (expr.type === "expression" && expr.arg?.type === "variable" && expr.arg.name) {
|
|
252
|
+
const name = expr.arg.name;
|
|
253
|
+
out += hashVars.has(name) ? "#" : `{${name}}`;
|
|
254
|
+
continue;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return out;
|
|
258
|
+
}
|
|
259
|
+
function escapeIcuLiteral(s) {
|
|
260
|
+
return s.replace(/'/g, "''").replace(/([{}])/g, "'$1'");
|
|
261
|
+
}
|
|
262
|
+
function insertCase(tree, tokens, body, depth) {
|
|
263
|
+
if (tokens.length === 0) return false;
|
|
264
|
+
const token = tokens[depth];
|
|
265
|
+
if (token === void 0) return false;
|
|
266
|
+
if (depth === tokens.length - 1) {
|
|
267
|
+
tree.set(token, body);
|
|
268
|
+
return true;
|
|
269
|
+
}
|
|
270
|
+
let child = tree.get(token);
|
|
271
|
+
if (typeof child === "string") return false;
|
|
272
|
+
if (!child) {
|
|
273
|
+
child = /* @__PURE__ */ new Map();
|
|
274
|
+
tree.set(token, child);
|
|
275
|
+
}
|
|
276
|
+
return insertCase(child, tokens, body, depth + 1);
|
|
277
|
+
}
|
|
278
|
+
function serializeCaseTree(tree, parsedSwitch, depth, hashVars) {
|
|
279
|
+
const item = parsedSwitch[depth];
|
|
280
|
+
if (!item) return null;
|
|
281
|
+
const icuType = kindToIcuType(item.kind);
|
|
282
|
+
const parts = [];
|
|
283
|
+
for (const [key, child] of tree) {
|
|
284
|
+
let content;
|
|
285
|
+
if (typeof child === "string") {
|
|
286
|
+
content = segmentBodyToMf1(child, hashVars);
|
|
287
|
+
} else {
|
|
288
|
+
const nested = serializeCaseTree(
|
|
289
|
+
child,
|
|
290
|
+
parsedSwitch,
|
|
291
|
+
depth + 1,
|
|
292
|
+
hashVars
|
|
293
|
+
);
|
|
294
|
+
if (nested == null) return null;
|
|
295
|
+
content = nested;
|
|
296
|
+
}
|
|
297
|
+
parts.push(`${key} {${content}}`);
|
|
298
|
+
}
|
|
299
|
+
if (parts.length === 0) return null;
|
|
300
|
+
return `{${item.name}, ${icuType}, ${parts.join(" ")}}`;
|
|
301
|
+
}
|
|
302
|
+
function pgsImportToMf1Message(switchAttr, segments) {
|
|
303
|
+
const parsedSwitch = parsePgsSwitch(switchAttr);
|
|
304
|
+
if (!parsedSwitch || parsedSwitch.length === 0) return null;
|
|
305
|
+
const n = parsedSwitch.length;
|
|
306
|
+
if (segments.length === 0) return null;
|
|
307
|
+
const hashVars = pluralVarNames(parsedSwitch);
|
|
308
|
+
const tree = /* @__PURE__ */ new Map();
|
|
309
|
+
for (const seg of segments) {
|
|
310
|
+
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
311
|
+
if (tokens.length !== n) return null;
|
|
312
|
+
if (!insertCase(tree, tokens, seg.body, 0)) return null;
|
|
313
|
+
}
|
|
314
|
+
return serializeCaseTree(tree, parsedSwitch, 0, hashVars);
|
|
315
|
+
}
|
|
316
|
+
|
|
190
317
|
// src/lib/import-helpers.ts
|
|
191
318
|
var XLIFF_EXTENSIONS = [".xliff", ".xlf"];
|
|
192
319
|
var XLIFF_FILENAME_PATTERN = /^(.+?)(?:\.([a-zA-Z]{2,}(?:-[a-zA-Z0-9]+)*))?\.(?:xliff|xlf)$/i;
|
|
@@ -362,6 +489,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
362
489
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
363
490
|
const translate = (unit["@_translate"] ?? "yes").toLowerCase();
|
|
364
491
|
const dnt = translate === "no" || translate === "false";
|
|
492
|
+
const format = unitTypeToFormat(unit["@_type"]);
|
|
365
493
|
const fileTranslate = (fileAttrs.translate ?? "yes").toLowerCase();
|
|
366
494
|
const fileDnt = fileTranslate === "no" || fileTranslate === "false";
|
|
367
495
|
const notes = [];
|
|
@@ -377,6 +505,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
377
505
|
}
|
|
378
506
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
379
507
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
508
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
380
509
|
let value;
|
|
381
510
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
382
511
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
@@ -385,27 +514,16 @@ function processUnit(unit, fileAttrs) {
|
|
|
385
514
|
caseAttr: seg["@_pgs:case"],
|
|
386
515
|
body: extractSegmentText(seg)
|
|
387
516
|
}));
|
|
388
|
-
const rebuilt = pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
389
|
-
|
|
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
|
-
}
|
|
517
|
+
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
518
|
+
value = rebuilt ?? joinSegmentTexts();
|
|
398
519
|
} else {
|
|
399
|
-
|
|
400
|
-
for (const seg of segments) {
|
|
401
|
-
targetParts.push(extractSegmentText(seg));
|
|
402
|
-
}
|
|
403
|
-
value = targetParts.join("");
|
|
520
|
+
value = joinSegmentTexts();
|
|
404
521
|
}
|
|
405
522
|
const attributes = {};
|
|
406
523
|
if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
|
|
407
524
|
if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
|
|
408
525
|
if (dnt !== fileDnt) attributes.dnt = dnt;
|
|
526
|
+
if (format) attributes.format = format;
|
|
409
527
|
return {
|
|
410
528
|
key: name,
|
|
411
529
|
value,
|
|
@@ -465,7 +583,7 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
465
583
|
}))
|
|
466
584
|
};
|
|
467
585
|
const proj = project;
|
|
468
|
-
return
|
|
586
|
+
return import_msg2.MsgResource.create(resourceData, proj);
|
|
469
587
|
}
|
|
470
588
|
async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
471
589
|
let project;
|
|
@@ -8,9 +8,11 @@ import {
|
|
|
8
8
|
parseXliffFilename,
|
|
9
9
|
processXliffFile,
|
|
10
10
|
writeTranslationFiles
|
|
11
|
-
} from "../chunk-
|
|
12
|
-
import "../chunk-
|
|
13
|
-
import "../chunk-
|
|
11
|
+
} from "../chunk-77PP26JZ.mjs";
|
|
12
|
+
import "../chunk-Q775ZGC5.mjs";
|
|
13
|
+
import "../chunk-XID5J2F7.mjs";
|
|
14
|
+
import "../chunk-MGHC4AYZ.mjs";
|
|
15
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
14
16
|
import "../chunk-F7XZRKP7.mjs";
|
|
15
17
|
export {
|
|
16
18
|
extractResourceFromXliffFile,
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/lib/msg-format.ts
|
|
21
|
+
var msg_format_exports = {};
|
|
22
|
+
__export(msg_format_exports, {
|
|
23
|
+
formatToUnitType: () => formatToUnitType,
|
|
24
|
+
resolveMessageFormat: () => resolveMessageFormat,
|
|
25
|
+
unitTypeToFormat: () => unitTypeToFormat
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(msg_format_exports);
|
|
28
|
+
var import_msg = require("@worldware/msg");
|
|
29
|
+
var MSG_TYPE_PREFIX = "msg";
|
|
30
|
+
var FORMAT_VALUES = /* @__PURE__ */ new Set(["NONE", "MF1", "MF2"]);
|
|
31
|
+
function formatToUnitType(format) {
|
|
32
|
+
return `${MSG_TYPE_PREFIX}:${format}`;
|
|
33
|
+
}
|
|
34
|
+
function unitTypeToFormat(type) {
|
|
35
|
+
if (type == null || type === "") return void 0;
|
|
36
|
+
if (FORMAT_VALUES.has(type)) {
|
|
37
|
+
return type;
|
|
38
|
+
}
|
|
39
|
+
const prefixed = new RegExp(
|
|
40
|
+
`^${MSG_TYPE_PREFIX}:(NONE|MF1|MF2)$`
|
|
41
|
+
).exec(type);
|
|
42
|
+
if (prefixed?.[1] && FORMAT_VALUES.has(prefixed[1])) {
|
|
43
|
+
return prefixed[1];
|
|
44
|
+
}
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
function resolveMessageFormat(messageFormat, resourceFormat, projectFormat) {
|
|
48
|
+
return messageFormat ?? resourceFormat ?? projectFormat ?? import_msg.MSG_DEFAULT_FORMAT;
|
|
49
|
+
}
|
|
50
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
51
|
+
0 && (module.exports = {
|
|
52
|
+
formatToUnitType,
|
|
53
|
+
resolveMessageFormat,
|
|
54
|
+
unitTypeToFormat
|
|
55
|
+
});
|