@worldware/msg-cli 0.1.1 → 0.2.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 +99 -13
- package/dist/{chunk-4Q2252JQ.mjs → chunk-6A6JGF7U.mjs} +2 -2
- package/dist/chunk-BM3O7ZJW.mjs +176 -0
- package/dist/{chunk-DBC2Y6VQ.mjs → chunk-F7XZRKP7.mjs} +6 -6
- package/dist/chunk-KKATA5YE.mjs +324 -0
- package/dist/{chunk-KKYCVD5M.mjs → chunk-LNCUPVEF.mjs} +2 -2
- package/dist/commands/create/project.cjs +23 -10
- package/dist/commands/create/project.mjs +26 -13
- package/dist/commands/create/resource.cjs +1 -1
- package/dist/commands/create/resource.mjs +2 -2
- package/dist/commands/export.cjs +356 -0
- package/dist/commands/export.d.mts +16 -0
- package/dist/commands/export.d.ts +16 -0
- package/dist/commands/export.mjs +91 -0
- package/dist/commands/import.cjs +526 -0
- package/dist/commands/import.d.mts +17 -0
- package/dist/commands/import.d.ts +17 -0
- package/dist/commands/import.mjs +116 -0
- package/dist/commands/init.cjs +6 -6
- package/dist/commands/init.mjs +1 -1
- package/dist/lib/create-project-helpers.mjs +3 -3
- package/dist/lib/create-resource-helpers.cjs +1 -1
- package/dist/lib/create-resource-helpers.mjs +2 -2
- package/dist/lib/export-helpers.cjs +229 -0
- package/dist/lib/export-helpers.d.mts +55 -0
- package/dist/lib/export-helpers.d.ts +55 -0
- package/dist/lib/export-helpers.mjs +18 -0
- package/dist/lib/import-helpers.cjs +380 -0
- package/dist/lib/import-helpers.d.mts +92 -0
- package/dist/lib/import-helpers.d.ts +92 -0
- package/dist/lib/import-helpers.mjs +24 -0
- package/dist/lib/init-helpers.cjs +6 -6
- package/dist/lib/init-helpers.mjs +1 -1
- package/package.json +2 -2
- package/dist/lib/utilities.cjs +0 -203
- package/dist/lib/utilities.d.mts +0 -59
- package/dist/lib/utilities.d.ts +0 -59
- package/dist/lib/utilities.mjs +0 -172
|
@@ -0,0 +1,526 @@
|
|
|
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/commands/import.ts
|
|
21
|
+
var import_exports = {};
|
|
22
|
+
__export(import_exports, {
|
|
23
|
+
default: () => Import
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(import_exports);
|
|
26
|
+
var import_core = require("@oclif/core");
|
|
27
|
+
var import_fs3 = require("fs");
|
|
28
|
+
var import_path3 = require("path");
|
|
29
|
+
|
|
30
|
+
// src/lib/init-helpers.ts
|
|
31
|
+
var import_fs = require("fs");
|
|
32
|
+
var import_path = require("path");
|
|
33
|
+
function findPackageJsonPath(cwd) {
|
|
34
|
+
let dir = cwd;
|
|
35
|
+
for (; ; ) {
|
|
36
|
+
const p = (0, import_path.join)(dir, "package.json");
|
|
37
|
+
if ((0, import_fs.existsSync)(p)) return p;
|
|
38
|
+
const parent = (0, import_path.dirname)(dir);
|
|
39
|
+
if (parent === dir) return null;
|
|
40
|
+
dir = parent;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
function readPackageJson(pkgPath) {
|
|
44
|
+
const raw = (0, import_fs.readFileSync)(pkgPath, "utf-8");
|
|
45
|
+
try {
|
|
46
|
+
const parsed = JSON.parse(raw);
|
|
47
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
48
|
+
throw new Error("package.json must be a JSON object");
|
|
49
|
+
}
|
|
50
|
+
return parsed;
|
|
51
|
+
} catch (err) {
|
|
52
|
+
if (err instanceof SyntaxError) {
|
|
53
|
+
throw new Error("Invalid package.json: " + err.message);
|
|
54
|
+
}
|
|
55
|
+
throw err;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function loadPackageJsonForMsg(cwd, options) {
|
|
59
|
+
const pkgPath = findPackageJsonPath(cwd);
|
|
60
|
+
if (!pkgPath) {
|
|
61
|
+
throw new Error("package.json not found. Run this command from the project root.");
|
|
62
|
+
}
|
|
63
|
+
let pkg;
|
|
64
|
+
try {
|
|
65
|
+
pkg = readPackageJson(pkgPath);
|
|
66
|
+
} catch (err) {
|
|
67
|
+
const msg = err instanceof Error ? err.message : "package.json could not be parsed.";
|
|
68
|
+
throw new Error(msg);
|
|
69
|
+
}
|
|
70
|
+
const dirs = pkg.directories;
|
|
71
|
+
if (!dirs || typeof dirs !== "object" || !dirs.i18n) {
|
|
72
|
+
throw new Error("package.json must contain directories.i18n. Run 'msg init' first.");
|
|
73
|
+
}
|
|
74
|
+
if (options?.requireL10n && !dirs.l10n) {
|
|
75
|
+
throw new Error(
|
|
76
|
+
"package.json must contain directories.i18n and directories.l10n. Run 'msg init' first."
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
const rootDir = (0, import_path.dirname)(pkgPath);
|
|
80
|
+
const useTypeScript = (0, import_fs.existsSync)((0, import_path.join)(rootDir, "tsconfig.json"));
|
|
81
|
+
const isEsm = pkg.type === "module";
|
|
82
|
+
return {
|
|
83
|
+
pkgPath,
|
|
84
|
+
rootDir,
|
|
85
|
+
pkg,
|
|
86
|
+
i18nDir: dirs.i18n,
|
|
87
|
+
l10nDir: dirs.l10n,
|
|
88
|
+
isEsm,
|
|
89
|
+
useTypeScript
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// src/lib/import-helpers.ts
|
|
94
|
+
var import_msg = require("@worldware/msg");
|
|
95
|
+
var import_promises = require("fs/promises");
|
|
96
|
+
var import_path2 = require("path");
|
|
97
|
+
var import_url2 = require("url");
|
|
98
|
+
var import_fast_xml_parser = require("fast-xml-parser");
|
|
99
|
+
var import_fs2 = require("fs");
|
|
100
|
+
|
|
101
|
+
// src/lib/create-resource-helpers.ts
|
|
102
|
+
var import_module = require("module");
|
|
103
|
+
var import_url = require("url");
|
|
104
|
+
async function dynamicImportFromUrl(url) {
|
|
105
|
+
try {
|
|
106
|
+
return await import(
|
|
107
|
+
/* @vite-ignore */
|
|
108
|
+
url
|
|
109
|
+
);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
112
|
+
const useRequire = msg.includes("dynamic import callback") || msg.includes("ERR_VM_DYNAMIC_IMPORT");
|
|
113
|
+
if (useRequire) {
|
|
114
|
+
try {
|
|
115
|
+
const path = url.startsWith("file:") ? (0, import_url.fileURLToPath)(url) : url;
|
|
116
|
+
const base = (0, import_url.pathToFileURL)(process.cwd()).href;
|
|
117
|
+
const req = (0, import_module.createRequire)(base);
|
|
118
|
+
const mod = req(path);
|
|
119
|
+
return Promise.resolve(mod ?? {});
|
|
120
|
+
} catch {
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// src/lib/import-helpers.ts
|
|
128
|
+
var XLIFF_EXTENSIONS = [".xliff", ".xlf"];
|
|
129
|
+
var XLIFF_FILENAME_PATTERN = /^(.+?)(?:\.([a-zA-Z]{2,}(?:-[a-zA-Z0-9]+)*))?\.(?:xliff|xlf)$/i;
|
|
130
|
+
async function findXliffFilePaths(directory) {
|
|
131
|
+
const result = [];
|
|
132
|
+
const entries = await (0, import_promises.readdir)(directory, { withFileTypes: true });
|
|
133
|
+
for (const entry of entries) {
|
|
134
|
+
const fullPath = (0, import_path2.join)(directory, entry.name);
|
|
135
|
+
if (entry.isFile()) {
|
|
136
|
+
const ext = (0, import_path2.extname)(entry.name).toLowerCase();
|
|
137
|
+
if (XLIFF_EXTENSIONS.includes(ext)) {
|
|
138
|
+
result.push(fullPath);
|
|
139
|
+
}
|
|
140
|
+
} else if (entry.isDirectory()) {
|
|
141
|
+
result.push(...await findXliffFilePaths(fullPath));
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
function filterXliffPathsByProject(filePaths, projectName) {
|
|
147
|
+
return filePaths.filter((p) => {
|
|
148
|
+
const filename = (0, import_path2.basename)(p);
|
|
149
|
+
if (filename.startsWith(projectName)) return true;
|
|
150
|
+
const pathParts = p.split(/[/\\]/);
|
|
151
|
+
return pathParts.some((part) => part === projectName);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
function filterXliffPathsByLocale(filePaths, locale) {
|
|
155
|
+
return filePaths.filter((p) => {
|
|
156
|
+
const filename = (0, import_path2.basename)(p);
|
|
157
|
+
if (filename.includes(`.${locale}.`)) return true;
|
|
158
|
+
const pathParts = p.split(/[/\\]/);
|
|
159
|
+
return pathParts.some((part) => part === locale);
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
var LOCALE_PATTERN = /^[a-zA-Z]{2,3}(-[a-zA-Z0-9]+)*$/;
|
|
163
|
+
function parseXliffFilename(filePath) {
|
|
164
|
+
const filename = (0, import_path2.basename)(filePath);
|
|
165
|
+
const match = filename.match(XLIFF_FILENAME_PATTERN);
|
|
166
|
+
const pathParts = filePath.split(/[/\\]/);
|
|
167
|
+
let project;
|
|
168
|
+
let locale;
|
|
169
|
+
if (match) {
|
|
170
|
+
const [, proj, loc] = match;
|
|
171
|
+
project = proj ?? (0, import_path2.basename)(filename, (0, import_path2.extname)(filename));
|
|
172
|
+
locale = loc;
|
|
173
|
+
} else {
|
|
174
|
+
project = (0, import_path2.basename)(filename, (0, import_path2.extname)(filename));
|
|
175
|
+
}
|
|
176
|
+
if (!locale) {
|
|
177
|
+
for (const part of pathParts) {
|
|
178
|
+
if (LOCALE_PATTERN.test(part) && part.length >= 2) {
|
|
179
|
+
locale = part;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
if (!project && pathParts.length >= 2) {
|
|
185
|
+
for (const part of pathParts) {
|
|
186
|
+
if (part && !XLIFF_EXTENSIONS.some((e) => part.endsWith(e))) {
|
|
187
|
+
project = part;
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return { project, locale };
|
|
193
|
+
}
|
|
194
|
+
function isMsgProjectLike(value) {
|
|
195
|
+
if (!value || typeof value !== "object") return false;
|
|
196
|
+
const loc = value.locales?.targetLocales;
|
|
197
|
+
return typeof loc === "object" && loc !== null && !Array.isArray(loc);
|
|
198
|
+
}
|
|
199
|
+
async function importMsgProject(projectsDir, projectName) {
|
|
200
|
+
const basePath = (0, import_path2.join)(projectsDir, projectName);
|
|
201
|
+
const exts = [".ts", ".js"];
|
|
202
|
+
for (const ext of exts) {
|
|
203
|
+
const p = `${basePath}${ext}`;
|
|
204
|
+
if ((0, import_fs2.existsSync)(p)) {
|
|
205
|
+
const url = (0, import_url2.pathToFileURL)(p).href;
|
|
206
|
+
const mod = await dynamicImportFromUrl(url);
|
|
207
|
+
const project = mod?.default ?? mod;
|
|
208
|
+
if (!isMsgProjectLike(project)) {
|
|
209
|
+
throw new Error(`Project file '${projectName}${ext}' does not export a valid MsgProject.`);
|
|
210
|
+
}
|
|
211
|
+
return project;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
throw new Error(`Project file not found: ${projectName}`);
|
|
215
|
+
}
|
|
216
|
+
var xmlParser = new import_fast_xml_parser.XMLParser({
|
|
217
|
+
ignoreAttributes: false,
|
|
218
|
+
attributeNamePrefix: "@_"
|
|
219
|
+
});
|
|
220
|
+
function parseXliff20(xml) {
|
|
221
|
+
try {
|
|
222
|
+
const parsed = xmlParser.parse(xml);
|
|
223
|
+
if (!parsed || typeof parsed !== "object") {
|
|
224
|
+
throw new Error("Failed to parse XLIFF");
|
|
225
|
+
}
|
|
226
|
+
return parsed;
|
|
227
|
+
} catch (err) {
|
|
228
|
+
const message = err instanceof Error ? err.message : "Failed to parse XLIFF";
|
|
229
|
+
throw new Error(`Malformed XLIFF 2.0: ${message}`);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
function categoryToNoteType(category) {
|
|
233
|
+
const lower = category.toLowerCase();
|
|
234
|
+
if (lower.startsWith("x-")) return category.toUpperCase();
|
|
235
|
+
const map = {
|
|
236
|
+
description: "DESCRIPTION",
|
|
237
|
+
authorship: "AUTHORSHIP",
|
|
238
|
+
parameters: "PARAMETERS",
|
|
239
|
+
context: "CONTEXT",
|
|
240
|
+
comment: "COMMENT"
|
|
241
|
+
};
|
|
242
|
+
return map[lower] ?? category.toUpperCase();
|
|
243
|
+
}
|
|
244
|
+
function extractSegmentText(segment) {
|
|
245
|
+
if (segment == null) return "";
|
|
246
|
+
const seg = segment;
|
|
247
|
+
const target = seg.target ?? seg.source;
|
|
248
|
+
if (typeof target === "string") return target;
|
|
249
|
+
if (target && typeof target === "object") {
|
|
250
|
+
return extractInlineText(target);
|
|
251
|
+
}
|
|
252
|
+
return "";
|
|
253
|
+
}
|
|
254
|
+
function extractInlineText(node) {
|
|
255
|
+
let text = "";
|
|
256
|
+
if ("#text" in node && typeof node["#text"] === "string") {
|
|
257
|
+
text += node["#text"];
|
|
258
|
+
}
|
|
259
|
+
const children = ["pc", "sc", "ec", "ph", "mrk", "sm", "em", "cp", "target", "source"];
|
|
260
|
+
for (const tag of children) {
|
|
261
|
+
const child = node[tag];
|
|
262
|
+
if (Array.isArray(child)) {
|
|
263
|
+
for (const c of child) {
|
|
264
|
+
const childObj = typeof c === "object" && c !== null ? c : {};
|
|
265
|
+
text += extractInlineText(childObj);
|
|
266
|
+
}
|
|
267
|
+
} else if (child && typeof child === "object") {
|
|
268
|
+
text += extractInlineText(child);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return text;
|
|
272
|
+
}
|
|
273
|
+
function processUnitsFromItems(units, fileAttrs) {
|
|
274
|
+
const messages = [];
|
|
275
|
+
for (const u of units) {
|
|
276
|
+
const msg = processUnit(u, fileAttrs);
|
|
277
|
+
if (msg) messages.push(msg);
|
|
278
|
+
}
|
|
279
|
+
return messages;
|
|
280
|
+
}
|
|
281
|
+
function processGroupsForMessages(groups, fileAttrs) {
|
|
282
|
+
const messages = [];
|
|
283
|
+
for (const item of groups) {
|
|
284
|
+
const obj = item;
|
|
285
|
+
if (obj.unit) {
|
|
286
|
+
const units = Array.isArray(obj.unit) ? obj.unit : [obj.unit];
|
|
287
|
+
messages.push(...processUnitsFromItems(units, fileAttrs));
|
|
288
|
+
}
|
|
289
|
+
if (obj.group) {
|
|
290
|
+
const nestedGroups = Array.isArray(obj.group) ? obj.group : [obj.group];
|
|
291
|
+
messages.push(...processGroupsForMessages(nestedGroups, fileAttrs));
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
return messages;
|
|
295
|
+
}
|
|
296
|
+
function processUnit(unit, fileAttrs) {
|
|
297
|
+
const name = unit["@_name"] ?? unit["@_id"] ?? "";
|
|
298
|
+
const trgLang = unit["@_trgLang"] ?? fileAttrs.trgLang;
|
|
299
|
+
const trgDir = unit["@_trgDir"] ?? fileAttrs.trgDir;
|
|
300
|
+
const translate = (unit["@_translate"] ?? "yes").toLowerCase();
|
|
301
|
+
const dnt = translate === "no" || translate === "false";
|
|
302
|
+
const fileTranslate = (fileAttrs.translate ?? "yes").toLowerCase();
|
|
303
|
+
const fileDnt = fileTranslate === "no" || fileTranslate === "false";
|
|
304
|
+
const notes = [];
|
|
305
|
+
const notesEl = unit.notes;
|
|
306
|
+
if (notesEl?.note) {
|
|
307
|
+
const noteArr = Array.isArray(notesEl.note) ? notesEl.note : [notesEl.note];
|
|
308
|
+
for (const n of noteArr) {
|
|
309
|
+
const note = n;
|
|
310
|
+
const category = note["@_category"] ?? "comment";
|
|
311
|
+
const content = note["#text"] ?? "";
|
|
312
|
+
notes.push({ type: categoryToNoteType(category), content });
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const segments = unit.segment ? Array.isArray(unit.segment) ? unit.segment : [unit.segment] : [];
|
|
316
|
+
const targetParts = [];
|
|
317
|
+
for (const seg of segments) {
|
|
318
|
+
targetParts.push(extractSegmentText(seg));
|
|
319
|
+
}
|
|
320
|
+
const value = targetParts.join("");
|
|
321
|
+
const attributes = {};
|
|
322
|
+
if (trgLang !== fileAttrs.trgLang) attributes.lang = trgLang || void 0;
|
|
323
|
+
if (trgDir !== fileAttrs.trgDir) attributes.dir = trgDir || void 0;
|
|
324
|
+
if (dnt !== fileDnt) attributes.dnt = dnt;
|
|
325
|
+
return {
|
|
326
|
+
key: name,
|
|
327
|
+
value,
|
|
328
|
+
attributes,
|
|
329
|
+
notes
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
function extractResourceFromXliffFile(fileEl, xliffTrgLang, project, targetLocales) {
|
|
333
|
+
const trgLang = fileEl["@_trgLang"] ?? xliffTrgLang;
|
|
334
|
+
if (!trgLang) return null;
|
|
335
|
+
if (!targetLocales.includes(trgLang)) return null;
|
|
336
|
+
const original = fileEl["@_original"] ?? "unknown.json";
|
|
337
|
+
const title = (0, import_path2.basename)(original, (0, import_path2.extname)(original)) || original;
|
|
338
|
+
const trgDir = fileEl["@_trgDir"];
|
|
339
|
+
const translate = (fileEl["@_translate"] ?? "yes").toLowerCase();
|
|
340
|
+
const dnt = translate === "no" || translate === "false";
|
|
341
|
+
const attributes = {
|
|
342
|
+
lang: trgLang,
|
|
343
|
+
dir: trgDir ?? "",
|
|
344
|
+
dnt
|
|
345
|
+
};
|
|
346
|
+
const notes = [];
|
|
347
|
+
const notesEl = fileEl.notes;
|
|
348
|
+
if (notesEl?.note) {
|
|
349
|
+
const noteArr = Array.isArray(notesEl.note) ? notesEl.note : [notesEl.note];
|
|
350
|
+
for (const n of noteArr) {
|
|
351
|
+
const note = n;
|
|
352
|
+
const category = note["@_category"] ?? "comment";
|
|
353
|
+
const content = note["#text"] ?? "";
|
|
354
|
+
notes.push({ type: categoryToNoteType(category), content });
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
const fileAttrs = {
|
|
358
|
+
trgLang,
|
|
359
|
+
trgDir,
|
|
360
|
+
translate
|
|
361
|
+
};
|
|
362
|
+
const allMessages = [
|
|
363
|
+
...processUnitsFromItems(
|
|
364
|
+
fileEl.unit ? Array.isArray(fileEl.unit) ? fileEl.unit : [fileEl.unit] : [],
|
|
365
|
+
fileAttrs
|
|
366
|
+
),
|
|
367
|
+
...processGroupsForMessages(
|
|
368
|
+
fileEl.group ? Array.isArray(fileEl.group) ? fileEl.group : [fileEl.group] : [],
|
|
369
|
+
fileAttrs
|
|
370
|
+
)
|
|
371
|
+
];
|
|
372
|
+
const resourceData = {
|
|
373
|
+
title,
|
|
374
|
+
attributes,
|
|
375
|
+
notes,
|
|
376
|
+
messages: allMessages.map((m) => ({
|
|
377
|
+
key: m.key,
|
|
378
|
+
value: m.value,
|
|
379
|
+
attributes: m.attributes,
|
|
380
|
+
notes: m.notes.length > 0 ? m.notes : void 0
|
|
381
|
+
}))
|
|
382
|
+
};
|
|
383
|
+
const proj = project;
|
|
384
|
+
return import_msg.MsgResource.create(resourceData, proj);
|
|
385
|
+
}
|
|
386
|
+
async function processXliffFile(xliffPath, projectsDir, projectName, locale) {
|
|
387
|
+
let project;
|
|
388
|
+
try {
|
|
389
|
+
project = await importMsgProject(projectsDir, projectName);
|
|
390
|
+
} catch {
|
|
391
|
+
return null;
|
|
392
|
+
}
|
|
393
|
+
const targetLocales = Object.keys(project.locales.targetLocales);
|
|
394
|
+
if (!targetLocales.includes(locale)) return null;
|
|
395
|
+
const xml = await (0, import_promises.readFile)(xliffPath, "utf-8");
|
|
396
|
+
const parsed = parseXliff20(xml);
|
|
397
|
+
const xliffRoot = parsed.xliff;
|
|
398
|
+
if (!xliffRoot) return null;
|
|
399
|
+
const xliffTrgLang = xliffRoot["@_trgLang"];
|
|
400
|
+
if (!xliffTrgLang) return null;
|
|
401
|
+
const fileEls = xliffRoot.file;
|
|
402
|
+
const files = Array.isArray(fileEls) ? fileEls : fileEls ? [fileEls] : [];
|
|
403
|
+
const resources = [];
|
|
404
|
+
for (const fileEl of files) {
|
|
405
|
+
const resource = extractResourceFromXliffFile(
|
|
406
|
+
fileEl,
|
|
407
|
+
xliffTrgLang,
|
|
408
|
+
project,
|
|
409
|
+
targetLocales
|
|
410
|
+
);
|
|
411
|
+
if (resource) {
|
|
412
|
+
const json = resource.toJSON(true);
|
|
413
|
+
resources.push({ title: resource.title, json });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
if (resources.length === 0) return null;
|
|
417
|
+
return { project: projectName, locale, resources };
|
|
418
|
+
}
|
|
419
|
+
async function writeTranslationFiles(translationsDir, result) {
|
|
420
|
+
const { project, locale, resources } = result;
|
|
421
|
+
const projectDir = (0, import_path2.join)(translationsDir, project);
|
|
422
|
+
const localeDir = (0, import_path2.join)(projectDir, locale);
|
|
423
|
+
await (0, import_promises.mkdir)(localeDir, { recursive: true });
|
|
424
|
+
for (const { title, json } of resources) {
|
|
425
|
+
const filePath = (0, import_path2.join)(localeDir, `${title}.json`);
|
|
426
|
+
await (0, import_promises.writeFile)(filePath, json, "utf-8");
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
// src/commands/import.ts
|
|
431
|
+
var Import = class _Import extends import_core.Command {
|
|
432
|
+
static description = "Import translations from XLIFF 2.0 files in l10n/xliff to JSON in l10n/translations";
|
|
433
|
+
static flags = {
|
|
434
|
+
help: import_core.Flags.help({ char: "h" }),
|
|
435
|
+
project: import_core.Flags.string({
|
|
436
|
+
char: "p",
|
|
437
|
+
description: "Filter import to the single named project"
|
|
438
|
+
}),
|
|
439
|
+
language: import_core.Flags.string({
|
|
440
|
+
char: "l",
|
|
441
|
+
description: "Filter import to the single locale"
|
|
442
|
+
})
|
|
443
|
+
};
|
|
444
|
+
async run() {
|
|
445
|
+
const { flags } = await this.parse(_Import);
|
|
446
|
+
const cwd = process.cwd();
|
|
447
|
+
let ctx;
|
|
448
|
+
try {
|
|
449
|
+
ctx = loadPackageJsonForMsg(cwd, { requireL10n: true });
|
|
450
|
+
} catch (err) {
|
|
451
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
452
|
+
this.error(msg, { exit: 1 });
|
|
453
|
+
}
|
|
454
|
+
const xliffDir = (0, import_path3.join)(ctx.rootDir, ctx.l10nDir ?? "l10n", "xliff");
|
|
455
|
+
const translationsDir = (0, import_path3.join)(ctx.rootDir, ctx.l10nDir ?? "l10n", "translations");
|
|
456
|
+
const projectsDir = (0, import_path3.join)(ctx.rootDir, ctx.i18nDir, "projects");
|
|
457
|
+
if (!(0, import_fs3.existsSync)(xliffDir)) {
|
|
458
|
+
this.warn(
|
|
459
|
+
`l10n/xliff directory does not exist at ${xliffDir}. Run 'msg init' first.`
|
|
460
|
+
);
|
|
461
|
+
return;
|
|
462
|
+
}
|
|
463
|
+
this.log("Finding XLIFF files...");
|
|
464
|
+
let filePaths;
|
|
465
|
+
try {
|
|
466
|
+
filePaths = await findXliffFilePaths(xliffDir);
|
|
467
|
+
} catch (err) {
|
|
468
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
469
|
+
this.error(`Could not read xliff directory: ${msg}`, { exit: 1 });
|
|
470
|
+
}
|
|
471
|
+
if (filePaths.length === 0) {
|
|
472
|
+
this.log("No XLIFF files found. Nothing to import.");
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
this.log(`Found ${filePaths.length} XLIFF file(s).`);
|
|
476
|
+
if (flags.project) {
|
|
477
|
+
filePaths = filterXliffPathsByProject(filePaths, flags.project);
|
|
478
|
+
if (filePaths.length === 0) {
|
|
479
|
+
this.log(`No XLIFF files found for project '${flags.project}'. Nothing to import.`);
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
this.log(`Filtered to project '${flags.project}'.`);
|
|
483
|
+
}
|
|
484
|
+
if (flags.language) {
|
|
485
|
+
filePaths = filterXliffPathsByLocale(filePaths, flags.language);
|
|
486
|
+
if (filePaths.length === 0) {
|
|
487
|
+
this.log(`No XLIFF files found for language '${flags.language}'. Nothing to import.`);
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
this.log(`Filtered to language '${flags.language}'.`);
|
|
491
|
+
}
|
|
492
|
+
let wroteCount = 0;
|
|
493
|
+
let errorCount = 0;
|
|
494
|
+
for (const xliffPath of filePaths) {
|
|
495
|
+
const { project: projectName, locale } = parseXliffFilename(xliffPath);
|
|
496
|
+
if (!locale) {
|
|
497
|
+
this.warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
try {
|
|
501
|
+
const result = await processXliffFile(
|
|
502
|
+
xliffPath,
|
|
503
|
+
projectsDir,
|
|
504
|
+
projectName,
|
|
505
|
+
locale
|
|
506
|
+
);
|
|
507
|
+
if (result) {
|
|
508
|
+
await writeTranslationFiles(translationsDir, result);
|
|
509
|
+
for (const { title } of result.resources) {
|
|
510
|
+
this.log(` Wrote ${result.project}/${result.locale}/${title}.json`);
|
|
511
|
+
wroteCount += 1;
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
} catch (err) {
|
|
515
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
516
|
+
this.error(`Failed to process ${xliffPath}: ${msg}`, { exit: 1 });
|
|
517
|
+
errorCount += 1;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
if (wroteCount > 0) {
|
|
521
|
+
this.log(`Imported ${wroteCount} translation file(s).`);
|
|
522
|
+
} else if (errorCount === 0) {
|
|
523
|
+
this.log("No translatable XLIFF files found (or all were monolingual / unsupported locale).");
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as _oclif_core_lib_interfaces_parser_js from '@oclif/core/lib/interfaces/parser.js';
|
|
2
|
+
import { Command } from '@oclif/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Imports translations from XLIFF 2.0 files into l10n/translations as JSON.
|
|
6
|
+
*/
|
|
7
|
+
declare class Import extends Command {
|
|
8
|
+
static description: string;
|
|
9
|
+
static flags: {
|
|
10
|
+
help: _oclif_core_lib_interfaces_parser_js.BooleanFlag<void>;
|
|
11
|
+
project: _oclif_core_lib_interfaces_parser_js.OptionFlag<string | undefined, _oclif_core_lib_interfaces_parser_js.CustomOptions>;
|
|
12
|
+
language: _oclif_core_lib_interfaces_parser_js.OptionFlag<string | undefined, _oclif_core_lib_interfaces_parser_js.CustomOptions>;
|
|
13
|
+
};
|
|
14
|
+
run(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { Import as default };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as _oclif_core_lib_interfaces_parser_js from '@oclif/core/lib/interfaces/parser.js';
|
|
2
|
+
import { Command } from '@oclif/core';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Imports translations from XLIFF 2.0 files into l10n/translations as JSON.
|
|
6
|
+
*/
|
|
7
|
+
declare class Import extends Command {
|
|
8
|
+
static description: string;
|
|
9
|
+
static flags: {
|
|
10
|
+
help: _oclif_core_lib_interfaces_parser_js.BooleanFlag<void>;
|
|
11
|
+
project: _oclif_core_lib_interfaces_parser_js.OptionFlag<string | undefined, _oclif_core_lib_interfaces_parser_js.CustomOptions>;
|
|
12
|
+
language: _oclif_core_lib_interfaces_parser_js.OptionFlag<string | undefined, _oclif_core_lib_interfaces_parser_js.CustomOptions>;
|
|
13
|
+
};
|
|
14
|
+
run(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { Import as default };
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import {
|
|
2
|
+
filterXliffPathsByLocale,
|
|
3
|
+
filterXliffPathsByProject,
|
|
4
|
+
findXliffFilePaths,
|
|
5
|
+
parseXliffFilename,
|
|
6
|
+
processXliffFile,
|
|
7
|
+
writeTranslationFiles
|
|
8
|
+
} from "../chunk-KKATA5YE.mjs";
|
|
9
|
+
import "../chunk-LNCUPVEF.mjs";
|
|
10
|
+
import {
|
|
11
|
+
loadPackageJsonForMsg
|
|
12
|
+
} from "../chunk-F7XZRKP7.mjs";
|
|
13
|
+
|
|
14
|
+
// src/commands/import.ts
|
|
15
|
+
import { Command, Flags } from "@oclif/core";
|
|
16
|
+
import { existsSync } from "fs";
|
|
17
|
+
import { join } from "path";
|
|
18
|
+
var Import = class _Import extends Command {
|
|
19
|
+
static description = "Import translations from XLIFF 2.0 files in l10n/xliff to JSON in l10n/translations";
|
|
20
|
+
static flags = {
|
|
21
|
+
help: Flags.help({ char: "h" }),
|
|
22
|
+
project: Flags.string({
|
|
23
|
+
char: "p",
|
|
24
|
+
description: "Filter import to the single named project"
|
|
25
|
+
}),
|
|
26
|
+
language: Flags.string({
|
|
27
|
+
char: "l",
|
|
28
|
+
description: "Filter import to the single locale"
|
|
29
|
+
})
|
|
30
|
+
};
|
|
31
|
+
async run() {
|
|
32
|
+
const { flags } = await this.parse(_Import);
|
|
33
|
+
const cwd = process.cwd();
|
|
34
|
+
let ctx;
|
|
35
|
+
try {
|
|
36
|
+
ctx = loadPackageJsonForMsg(cwd, { requireL10n: true });
|
|
37
|
+
} catch (err) {
|
|
38
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
39
|
+
this.error(msg, { exit: 1 });
|
|
40
|
+
}
|
|
41
|
+
const xliffDir = join(ctx.rootDir, ctx.l10nDir ?? "l10n", "xliff");
|
|
42
|
+
const translationsDir = join(ctx.rootDir, ctx.l10nDir ?? "l10n", "translations");
|
|
43
|
+
const projectsDir = join(ctx.rootDir, ctx.i18nDir, "projects");
|
|
44
|
+
if (!existsSync(xliffDir)) {
|
|
45
|
+
this.warn(
|
|
46
|
+
`l10n/xliff directory does not exist at ${xliffDir}. Run 'msg init' first.`
|
|
47
|
+
);
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
this.log("Finding XLIFF files...");
|
|
51
|
+
let filePaths;
|
|
52
|
+
try {
|
|
53
|
+
filePaths = await findXliffFilePaths(xliffDir);
|
|
54
|
+
} catch (err) {
|
|
55
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
56
|
+
this.error(`Could not read xliff directory: ${msg}`, { exit: 1 });
|
|
57
|
+
}
|
|
58
|
+
if (filePaths.length === 0) {
|
|
59
|
+
this.log("No XLIFF files found. Nothing to import.");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.log(`Found ${filePaths.length} XLIFF file(s).`);
|
|
63
|
+
if (flags.project) {
|
|
64
|
+
filePaths = filterXliffPathsByProject(filePaths, flags.project);
|
|
65
|
+
if (filePaths.length === 0) {
|
|
66
|
+
this.log(`No XLIFF files found for project '${flags.project}'. Nothing to import.`);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
this.log(`Filtered to project '${flags.project}'.`);
|
|
70
|
+
}
|
|
71
|
+
if (flags.language) {
|
|
72
|
+
filePaths = filterXliffPathsByLocale(filePaths, flags.language);
|
|
73
|
+
if (filePaths.length === 0) {
|
|
74
|
+
this.log(`No XLIFF files found for language '${flags.language}'. Nothing to import.`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.log(`Filtered to language '${flags.language}'.`);
|
|
78
|
+
}
|
|
79
|
+
let wroteCount = 0;
|
|
80
|
+
let errorCount = 0;
|
|
81
|
+
for (const xliffPath of filePaths) {
|
|
82
|
+
const { project: projectName, locale } = parseXliffFilename(xliffPath);
|
|
83
|
+
if (!locale) {
|
|
84
|
+
this.warn(`Skipping monolingual file (no target locale): ${xliffPath}`);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const result = await processXliffFile(
|
|
89
|
+
xliffPath,
|
|
90
|
+
projectsDir,
|
|
91
|
+
projectName,
|
|
92
|
+
locale
|
|
93
|
+
);
|
|
94
|
+
if (result) {
|
|
95
|
+
await writeTranslationFiles(translationsDir, result);
|
|
96
|
+
for (const { title } of result.resources) {
|
|
97
|
+
this.log(` Wrote ${result.project}/${result.locale}/${title}.json`);
|
|
98
|
+
wroteCount += 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
} catch (err) {
|
|
102
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
103
|
+
this.error(`Failed to process ${xliffPath}: ${msg}`, { exit: 1 });
|
|
104
|
+
errorCount += 1;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
if (wroteCount > 0) {
|
|
108
|
+
this.log(`Imported ${wroteCount} translation file(s).`);
|
|
109
|
+
} else if (errorCount === 0) {
|
|
110
|
+
this.log("No translatable XLIFF files found (or all were monolingual / unsupported locale).");
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
};
|
|
114
|
+
export {
|
|
115
|
+
Import as default
|
|
116
|
+
};
|