@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
|
@@ -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
|
@@ -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
|
|
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
|
|
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,12 +268,97 @@ 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;
|
|
250
347
|
function isMsgResourceLike(value) {
|
|
251
348
|
return Boolean(value) && typeof value.getProject === "function";
|
|
252
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
|
+
}
|
|
253
362
|
var XLIFF22_NS = "urn:oasis:names:tc:xliff:document:2.2";
|
|
254
363
|
var PGS_NS = "urn:oasis:names:tc:xliff:pgs:1.0";
|
|
255
364
|
async function findMsgResourceFilePaths(directory) {
|
|
@@ -273,8 +382,8 @@ async function importMsgResourcesFromPaths(filePaths) {
|
|
|
273
382
|
}
|
|
274
383
|
const url = (0, import_url2.pathToFileURL)(filePath).href;
|
|
275
384
|
const mod = await dynamicImportFromUrl(url);
|
|
276
|
-
const resource = mod
|
|
277
|
-
if (!
|
|
385
|
+
const resource = resolveMsgResourceExport(mod);
|
|
386
|
+
if (!resource) {
|
|
278
387
|
throw new Error(
|
|
279
388
|
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
280
389
|
);
|
|
@@ -334,6 +443,11 @@ function renderNotes(notes, idPrefix, indent = " ") {
|
|
|
334
443
|
parts.push(`${indent}</notes>`);
|
|
335
444
|
return parts;
|
|
336
445
|
}
|
|
446
|
+
function pgsExportForFormat(format, value) {
|
|
447
|
+
if (format === "MF1") return mf1MessageToPgsExport(value);
|
|
448
|
+
if (format === "MF2") return selectMessageToPgsExport(value);
|
|
449
|
+
return null;
|
|
450
|
+
}
|
|
337
451
|
function resourceGroupToXliff22(group) {
|
|
338
452
|
const srcLang = group.resources[0]?.attributes?.lang ?? "en";
|
|
339
453
|
const parts = [
|
|
@@ -346,6 +460,12 @@ function resourceGroupToXliff22(group) {
|
|
|
346
460
|
fileIndex += 1;
|
|
347
461
|
const fileId = `f${fileIndex}`;
|
|
348
462
|
const attrs = resource.attributes ?? {};
|
|
463
|
+
const projectFormat = resource.getProject().format ?? import_msg2.MSG_DEFAULT_FORMAT;
|
|
464
|
+
const resourceFormat = resolveMessageFormat(
|
|
465
|
+
void 0,
|
|
466
|
+
attrs.format,
|
|
467
|
+
projectFormat
|
|
468
|
+
);
|
|
349
469
|
const fileAttrs = [
|
|
350
470
|
`id="${fileId}"`,
|
|
351
471
|
`original="${escapeXml(orig)}"`
|
|
@@ -376,7 +496,13 @@ function resourceGroupToXliff22(group) {
|
|
|
376
496
|
if (msgAttr?.dir) {
|
|
377
497
|
msgAttrs.push(`srcDir="${escapeXml(msgAttr.dir)}"`);
|
|
378
498
|
}
|
|
379
|
-
const
|
|
499
|
+
const msgFormat = resolveMessageFormat(
|
|
500
|
+
msgAttr?.format,
|
|
501
|
+
resourceFormat,
|
|
502
|
+
projectFormat
|
|
503
|
+
);
|
|
504
|
+
msgAttrs.push(`type="${escapeXml(formatToUnitType(msgFormat))}"`);
|
|
505
|
+
const pgsExport = pgsExportForFormat(msgFormat, msg.value);
|
|
380
506
|
if (pgsExport) {
|
|
381
507
|
msgAttrs.push(`pgs:switch="${escapeXml(pgsExport.switchAttr)}"`);
|
|
382
508
|
}
|
package/dist/commands/export.mjs
CHANGED
|
@@ -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 {
|
|
12
14
|
loadPackageJsonForMsg
|
|
13
15
|
} from "../chunk-F7XZRKP7.mjs";
|
package/dist/commands/import.cjs
CHANGED
|
@@ -91,7 +91,7 @@ function loadPackageJsonForMsg(cwd, options) {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
// src/lib/import-helpers.ts
|
|
94
|
-
var
|
|
94
|
+
var import_msg2 = require("@worldware/msg");
|
|
95
95
|
var import_promises = require("fs/promises");
|
|
96
96
|
var import_path2 = require("path");
|
|
97
97
|
var import_url2 = require("url");
|
|
@@ -124,6 +124,29 @@ async function dynamicImportFromUrl(url) {
|
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
+
// src/lib/msg-format.ts
|
|
128
|
+
var import_msg = require("@worldware/msg");
|
|
129
|
+
var MSG_TYPE_PREFIX = "msg";
|
|
130
|
+
var FORMAT_VALUES = /* @__PURE__ */ new Set(["NONE", "MF1", "MF2"]);
|
|
131
|
+
function unitTypeToFormat(type) {
|
|
132
|
+
if (type == null || type === "") return void 0;
|
|
133
|
+
if (FORMAT_VALUES.has(type)) {
|
|
134
|
+
return type;
|
|
135
|
+
}
|
|
136
|
+
const prefixed = new RegExp(
|
|
137
|
+
`^${MSG_TYPE_PREFIX}:(NONE|MF1|MF2)$`
|
|
138
|
+
).exec(type);
|
|
139
|
+
if (prefixed?.[1] && FORMAT_VALUES.has(prefixed[1])) {
|
|
140
|
+
return prefixed[1];
|
|
141
|
+
}
|
|
142
|
+
return void 0;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// src/lib/pgs-mf1.ts
|
|
146
|
+
var import_parser = require("@messageformat/parser");
|
|
147
|
+
var import_icu_messageformat_1 = require("@messageformat/icu-messageformat-1");
|
|
148
|
+
var import_messageformat2 = require("messageformat");
|
|
149
|
+
|
|
127
150
|
// src/lib/pgs-mf2.ts
|
|
128
151
|
var import_messageformat = require("messageformat");
|
|
129
152
|
var PGS_SWITCH_ITEM = /^(plural|ordinal|gender|select):([a-zA-Z_][a-zA-Z0-9_]*)$/;
|
|
@@ -247,6 +270,110 @@ function pgsImportToSelectMessage(switchAttr, segments) {
|
|
|
247
270
|
);
|
|
248
271
|
}
|
|
249
272
|
|
|
273
|
+
// src/lib/pgs-mf1.ts
|
|
274
|
+
function kindToIcuType(kind) {
|
|
275
|
+
if (kind === "plural") return "plural";
|
|
276
|
+
if (kind === "ordinal") return "selectordinal";
|
|
277
|
+
return "select";
|
|
278
|
+
}
|
|
279
|
+
function pluralVarNames(parsedSwitch) {
|
|
280
|
+
const names = /* @__PURE__ */ new Set();
|
|
281
|
+
for (const { kind, name } of parsedSwitch) {
|
|
282
|
+
if (kind === "plural" || kind === "ordinal") names.add(name);
|
|
283
|
+
}
|
|
284
|
+
return names;
|
|
285
|
+
}
|
|
286
|
+
function segmentBodyToMf1(body, hashVars) {
|
|
287
|
+
const trimmed = body.trim();
|
|
288
|
+
if (!trimmed) return "";
|
|
289
|
+
try {
|
|
290
|
+
const msg = (0, import_messageformat2.parseMessage)(trimmed);
|
|
291
|
+
if (msg.type !== "message") {
|
|
292
|
+
return escapeIcuLiteral(trimmed);
|
|
293
|
+
}
|
|
294
|
+
return patternPartsToMf1(
|
|
295
|
+
msg.pattern,
|
|
296
|
+
hashVars
|
|
297
|
+
);
|
|
298
|
+
} catch {
|
|
299
|
+
return escapeIcuLiteral(trimmed);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
function patternPartsToMf1(pattern, hashVars) {
|
|
303
|
+
let out = "";
|
|
304
|
+
for (const part of pattern) {
|
|
305
|
+
if (typeof part === "string") {
|
|
306
|
+
out += escapeIcuLiteral(part);
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (!part || typeof part !== "object") continue;
|
|
310
|
+
const expr = part;
|
|
311
|
+
if (expr.type === "expression" && expr.arg?.type === "variable" && expr.arg.name) {
|
|
312
|
+
const name = expr.arg.name;
|
|
313
|
+
out += hashVars.has(name) ? "#" : `{${name}}`;
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return out;
|
|
318
|
+
}
|
|
319
|
+
function escapeIcuLiteral(s) {
|
|
320
|
+
return s.replace(/'/g, "''").replace(/([{}])/g, "'$1'");
|
|
321
|
+
}
|
|
322
|
+
function insertCase(tree, tokens, body, depth) {
|
|
323
|
+
if (tokens.length === 0) return false;
|
|
324
|
+
const token = tokens[depth];
|
|
325
|
+
if (token === void 0) return false;
|
|
326
|
+
if (depth === tokens.length - 1) {
|
|
327
|
+
tree.set(token, body);
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
let child = tree.get(token);
|
|
331
|
+
if (typeof child === "string") return false;
|
|
332
|
+
if (!child) {
|
|
333
|
+
child = /* @__PURE__ */ new Map();
|
|
334
|
+
tree.set(token, child);
|
|
335
|
+
}
|
|
336
|
+
return insertCase(child, tokens, body, depth + 1);
|
|
337
|
+
}
|
|
338
|
+
function serializeCaseTree(tree, parsedSwitch, depth, hashVars) {
|
|
339
|
+
const item = parsedSwitch[depth];
|
|
340
|
+
if (!item) return null;
|
|
341
|
+
const icuType = kindToIcuType(item.kind);
|
|
342
|
+
const parts = [];
|
|
343
|
+
for (const [key, child] of tree) {
|
|
344
|
+
let content;
|
|
345
|
+
if (typeof child === "string") {
|
|
346
|
+
content = segmentBodyToMf1(child, hashVars);
|
|
347
|
+
} else {
|
|
348
|
+
const nested = serializeCaseTree(
|
|
349
|
+
child,
|
|
350
|
+
parsedSwitch,
|
|
351
|
+
depth + 1,
|
|
352
|
+
hashVars
|
|
353
|
+
);
|
|
354
|
+
if (nested == null) return null;
|
|
355
|
+
content = nested;
|
|
356
|
+
}
|
|
357
|
+
parts.push(`${key} {${content}}`);
|
|
358
|
+
}
|
|
359
|
+
if (parts.length === 0) return null;
|
|
360
|
+
return `{${item.name}, ${icuType}, ${parts.join(" ")}}`;
|
|
361
|
+
}
|
|
362
|
+
function pgsImportToMf1Message(switchAttr, segments) {
|
|
363
|
+
const parsedSwitch = parsePgsSwitch(switchAttr);
|
|
364
|
+
if (!parsedSwitch || parsedSwitch.length === 0) return null;
|
|
365
|
+
const n = parsedSwitch.length;
|
|
366
|
+
if (segments.length === 0) return null;
|
|
367
|
+
const hashVars = pluralVarNames(parsedSwitch);
|
|
368
|
+
const tree = /* @__PURE__ */ new Map();
|
|
369
|
+
for (const seg of segments) {
|
|
370
|
+
const tokens = seg.caseAttr.trim().split(/\s+/).filter(Boolean);
|
|
371
|
+
if (tokens.length !== n) return null;
|
|
372
|
+
if (!insertCase(tree, tokens, seg.body, 0)) return null;
|
|
373
|
+
}
|
|
374
|
+
return serializeCaseTree(tree, parsedSwitch, 0, hashVars);
|
|
375
|
+
}
|
|
376
|
+
|
|
250
377
|
// src/lib/import-helpers.ts
|
|
251
378
|
var XLIFF_EXTENSIONS = [".xliff", ".xlf"];
|
|
252
379
|
var XLIFF_FILENAME_PATTERN = /^(.+?)(?:\.([a-zA-Z]{2,}(?:-[a-zA-Z0-9]+)*))?\.(?:xliff|xlf)$/i;
|
|
@@ -422,6 +549,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
422
549
|
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
423
550
|
const translate = (unit["@_translate"] ?? "yes").toLowerCase();
|
|
424
551
|
const dnt = translate === "no" || translate === "false";
|
|
552
|
+
const format = unitTypeToFormat(unit["@_type"]);
|
|
425
553
|
const fileTranslate = (fileAttrs.translate ?? "yes").toLowerCase();
|
|
426
554
|
const fileDnt = fileTranslate === "no" || fileTranslate === "false";
|
|
427
555
|
const notes = [];
|
|
@@ -437,6 +565,7 @@ function processUnit(unit, fileAttrs) {
|
|
|
437
565
|
}
|
|
438
566
|
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
439
567
|
const pgsSwitch = unit["@_pgs:switch"];
|
|
568
|
+
const joinSegmentTexts = () => segments.map((seg) => extractSegmentText(seg)).join("");
|
|
440
569
|
let value;
|
|
441
570
|
if (pgsSwitch && segments.length > 0 && segments.every(
|
|
442
571
|
(seg) => typeof seg["@_pgs:case"] === "string"
|
|
@@ -445,27 +574,16 @@ function processUnit(unit, fileAttrs) {
|
|
|
445
574
|
caseAttr: seg["@_pgs:case"],
|
|
446
575
|
body: extractSegmentText(seg)
|
|
447
576
|
}));
|
|
448
|
-
const rebuilt = pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
449
|
-
|
|
450
|
-
value = rebuilt;
|
|
451
|
-
} else {
|
|
452
|
-
const targetParts = [];
|
|
453
|
-
for (const seg of segments) {
|
|
454
|
-
targetParts.push(extractSegmentText(seg));
|
|
455
|
-
}
|
|
456
|
-
value = targetParts.join("");
|
|
457
|
-
}
|
|
577
|
+
const rebuilt = format === "MF1" ? pgsImportToMf1Message(pgsSwitch, importSegments) : pgsImportToSelectMessage(pgsSwitch, importSegments);
|
|
578
|
+
value = rebuilt ?? joinSegmentTexts();
|
|
458
579
|
} else {
|
|
459
|
-
|
|
460
|
-
for (const seg of segments) {
|
|
461
|
-
targetParts.push(extractSegmentText(seg));
|
|
462
|
-
}
|
|
463
|
-
value = targetParts.join("");
|
|
580
|
+
value = joinSegmentTexts();
|
|
464
581
|
}
|
|
465
582
|
const attributes = {};
|
|
466
583
|
if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
|
|
467
584
|
if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
|
|
468
585
|
if (dnt !== fileDnt) attributes.dnt = dnt;
|
|
586
|
+
if (format) attributes.format = format;
|
|
469
587
|
return {
|
|
470
588
|
key: name,
|
|
471
589
|
value,
|
|
@@ -525,7 +643,7 @@ function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocal
|
|
|
525
643
|
}))
|
|
526
644
|
};
|
|
527
645
|
const proj = project;
|
|
528
|
-
return
|
|
646
|
+
return import_msg2.MsgResource.create(resourceData, proj);
|
|
529
647
|
}
|
|
530
648
|
async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
531
649
|
let project;
|
package/dist/commands/import.mjs
CHANGED
|
@@ -5,9 +5,11 @@ import {
|
|
|
5
5
|
parseXliffFilename,
|
|
6
6
|
processXliffFile,
|
|
7
7
|
writeTranslationFiles
|
|
8
|
-
} from "../chunk-
|
|
9
|
-
import "../chunk-
|
|
10
|
-
import "../chunk-
|
|
8
|
+
} from "../chunk-77PP26JZ.mjs";
|
|
9
|
+
import "../chunk-Q775ZGC5.mjs";
|
|
10
|
+
import "../chunk-XID5J2F7.mjs";
|
|
11
|
+
import "../chunk-MGHC4AYZ.mjs";
|
|
12
|
+
import "../chunk-JQKAEOL7.mjs";
|
|
11
13
|
import {
|
|
12
14
|
loadPackageJsonForMsg
|
|
13
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,
|