@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
package/dist/lib/utilities.cjs
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
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/utilities.ts
|
|
21
|
-
var utilities_exports = {};
|
|
22
|
-
__export(utilities_exports, {
|
|
23
|
-
findMsgResourceFiles: () => findMsgResourceFiles,
|
|
24
|
-
importMsgResources: () => importMsgResources,
|
|
25
|
-
parseXliff: () => parseXliff,
|
|
26
|
-
readXliff: () => readXliff,
|
|
27
|
-
resourcesToXliffString: () => resourcesToXliffString,
|
|
28
|
-
writeTranslationData: () => writeTranslationData,
|
|
29
|
-
writeXliff: () => writeXliff,
|
|
30
|
-
xliffDataToResourceTranslationData: () => xliffDataToResourceTranslationData,
|
|
31
|
-
xliffToTranslationData: () => xliffToTranslationData
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(utilities_exports);
|
|
34
|
-
var import_msg = require("@worldware/msg");
|
|
35
|
-
var import_promises = require("fs/promises");
|
|
36
|
-
var import_path = require("path");
|
|
37
|
-
var import_url = require("url");
|
|
38
|
-
var import_fast_xml_parser = require("fast-xml-parser");
|
|
39
|
-
var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
40
|
-
async function findMsgResourceFiles(directory) {
|
|
41
|
-
const result = [];
|
|
42
|
-
const entries = await (0, import_promises.readdir)(directory, { withFileTypes: true });
|
|
43
|
-
for (const entry of entries) {
|
|
44
|
-
const fullPath = (0, import_path.join)(directory, entry.name);
|
|
45
|
-
if (entry.isFile() && MSG_PATTERN.test(entry.name)) {
|
|
46
|
-
result.push(fullPath);
|
|
47
|
-
} else if (entry.isDirectory()) {
|
|
48
|
-
result.push(...await findMsgResourceFiles(fullPath));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
return result;
|
|
52
|
-
}
|
|
53
|
-
async function importMsgResources(filePaths) {
|
|
54
|
-
const byProjectName = /* @__PURE__ */ new Map();
|
|
55
|
-
for (const filePath of filePaths) {
|
|
56
|
-
if (!MSG_PATTERN.test(filePath)) {
|
|
57
|
-
throw new Error(`File does not have .msg. in filename: ${filePath}`);
|
|
58
|
-
}
|
|
59
|
-
const url = (0, import_url.pathToFileURL)(filePath).href;
|
|
60
|
-
const mod = await import(url);
|
|
61
|
-
const resource = mod.default ?? mod.resource ?? mod.MsgResource;
|
|
62
|
-
if (!resource || !(resource instanceof import_msg.MsgResource)) {
|
|
63
|
-
throw new Error(
|
|
64
|
-
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
65
|
-
);
|
|
66
|
-
}
|
|
67
|
-
const project = resource.getProject();
|
|
68
|
-
const projectName = project.project.name;
|
|
69
|
-
let group = byProjectName.get(projectName);
|
|
70
|
-
if (!group) {
|
|
71
|
-
group = { project, resources: [] };
|
|
72
|
-
byProjectName.set(projectName, group);
|
|
73
|
-
}
|
|
74
|
-
group.resources.push(resource);
|
|
75
|
-
}
|
|
76
|
-
const result = /* @__PURE__ */ new Map();
|
|
77
|
-
for (const { project, resources } of byProjectName.values()) {
|
|
78
|
-
result.set(project, resources);
|
|
79
|
-
}
|
|
80
|
-
return result;
|
|
81
|
-
}
|
|
82
|
-
async function resourcesToXliffString(resources) {
|
|
83
|
-
const byProject = /* @__PURE__ */ new Map();
|
|
84
|
-
for (const resource of resources) {
|
|
85
|
-
const project = resource.getProject();
|
|
86
|
-
let arr = byProject.get(project);
|
|
87
|
-
if (!arr) {
|
|
88
|
-
arr = [];
|
|
89
|
-
byProject.set(project, arr);
|
|
90
|
-
}
|
|
91
|
-
arr.push(resource);
|
|
92
|
-
}
|
|
93
|
-
const result = /* @__PURE__ */ new Map();
|
|
94
|
-
for (const [project, arr] of byProject) {
|
|
95
|
-
const xliff = serializeResourcesToXliff(arr);
|
|
96
|
-
result.set(project, xliff);
|
|
97
|
-
}
|
|
98
|
-
return result;
|
|
99
|
-
}
|
|
100
|
-
function serializeResourcesToXliff(resources) {
|
|
101
|
-
const xmlns = "urn:oasis:names:tc:xliff:document:1.2";
|
|
102
|
-
const parts = [
|
|
103
|
-
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
104
|
-
`<xliff version="1.2" xmlns="${xmlns}">`
|
|
105
|
-
];
|
|
106
|
-
for (const resource of resources) {
|
|
107
|
-
const orig = `${resource.title}.json`;
|
|
108
|
-
const sourceLang = resource.attributes.lang || "en";
|
|
109
|
-
const data = resource.getData(false);
|
|
110
|
-
parts.push(
|
|
111
|
-
` <file original="${escapeXml(orig)}" source-language="${escapeXml(sourceLang)}" datatype="plaintext">`
|
|
112
|
-
);
|
|
113
|
-
parts.push(" <body>");
|
|
114
|
-
for (const msg of data.messages || []) {
|
|
115
|
-
const key = msg.key;
|
|
116
|
-
const value = msg.value;
|
|
117
|
-
const attrs = [`id="${escapeXml(key)}"`, `resname="${escapeXml(key)}"`];
|
|
118
|
-
parts.push(` <trans-unit ${attrs.join(" ")}>`);
|
|
119
|
-
parts.push(` <source>${escapeXml(value)}</source>`);
|
|
120
|
-
parts.push(` </trans-unit>`);
|
|
121
|
-
}
|
|
122
|
-
parts.push(" </body>");
|
|
123
|
-
parts.push(" </file>");
|
|
124
|
-
}
|
|
125
|
-
parts.push("</xliff>");
|
|
126
|
-
return parts.join("\n");
|
|
127
|
-
}
|
|
128
|
-
function escapeXml(s) {
|
|
129
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
130
|
-
}
|
|
131
|
-
async function writeXliff(filePath, xliff) {
|
|
132
|
-
await (0, import_promises.writeFile)(filePath, xliff, "utf-8");
|
|
133
|
-
}
|
|
134
|
-
async function readXliff(filePath) {
|
|
135
|
-
return (0, import_promises.readFile)(filePath, "utf-8");
|
|
136
|
-
}
|
|
137
|
-
var xmlParser = new import_fast_xml_parser.XMLParser({
|
|
138
|
-
ignoreAttributes: false,
|
|
139
|
-
attributeNamePrefix: "@_"
|
|
140
|
-
});
|
|
141
|
-
async function parseXliff(xliff) {
|
|
142
|
-
try {
|
|
143
|
-
const parsed = xmlParser.parse(xliff);
|
|
144
|
-
if (!parsed || typeof parsed !== "object") {
|
|
145
|
-
throw new Error("Invalid XLIFF: failed to parse");
|
|
146
|
-
}
|
|
147
|
-
return parsed;
|
|
148
|
-
} catch (err) {
|
|
149
|
-
const message = err instanceof Error ? err.message : "Failed to parse XLIFF";
|
|
150
|
-
throw new Error(`Invalid XLIFF 1.2: ${message}`);
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
async function xliffDataToResourceTranslationData(parsedXliff) {
|
|
154
|
-
const result = [];
|
|
155
|
-
const xliff = parsedXliff;
|
|
156
|
-
const fileEl = xliff?.xliff?.file;
|
|
157
|
-
if (!fileEl) return result;
|
|
158
|
-
const files = Array.isArray(fileEl) ? fileEl : [fileEl];
|
|
159
|
-
for (const file of files) {
|
|
160
|
-
const f = file;
|
|
161
|
-
const orig = f["@_original"] ?? "unknown";
|
|
162
|
-
const title = (0, import_path.basename)(orig, (0, import_path.extname)(orig)) || orig;
|
|
163
|
-
const targetLang = f["@_target-language"] ?? f["@_source-language"] ?? "";
|
|
164
|
-
const transUnits = f.body?.["trans-unit"];
|
|
165
|
-
const units = Array.isArray(transUnits) ? transUnits : transUnits ? [transUnits] : [];
|
|
166
|
-
const messages = [];
|
|
167
|
-
for (const tu of units) {
|
|
168
|
-
const t = tu;
|
|
169
|
-
const translate = (t["@_translate"] ?? "yes").toLowerCase();
|
|
170
|
-
if (translate === "no" || translate === "false") continue;
|
|
171
|
-
const key = t["@_resname"] ?? t["@_id"] ?? "";
|
|
172
|
-
const targetEl = t.target;
|
|
173
|
-
const value = (typeof targetEl === "object" && targetEl && "#text" in targetEl ? targetEl["#text"] : typeof targetEl === "string" ? targetEl : "") ?? "";
|
|
174
|
-
messages.push({ key, value });
|
|
175
|
-
}
|
|
176
|
-
result.push({
|
|
177
|
-
title,
|
|
178
|
-
attributes: { lang: targetLang, dir: "", dnt: false },
|
|
179
|
-
messages
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return result;
|
|
183
|
-
}
|
|
184
|
-
async function xliffToTranslationData(xliff) {
|
|
185
|
-
const parsed = await parseXliff(xliff);
|
|
186
|
-
return xliffDataToResourceTranslationData(parsed);
|
|
187
|
-
}
|
|
188
|
-
async function writeTranslationData(filePath, data) {
|
|
189
|
-
const json = JSON.stringify(data, null, 2);
|
|
190
|
-
await (0, import_promises.writeFile)(filePath, json, "utf-8");
|
|
191
|
-
}
|
|
192
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
193
|
-
0 && (module.exports = {
|
|
194
|
-
findMsgResourceFiles,
|
|
195
|
-
importMsgResources,
|
|
196
|
-
parseXliff,
|
|
197
|
-
readXliff,
|
|
198
|
-
resourcesToXliffString,
|
|
199
|
-
writeTranslationData,
|
|
200
|
-
writeXliff,
|
|
201
|
-
xliffDataToResourceTranslationData,
|
|
202
|
-
xliffToTranslationData
|
|
203
|
-
});
|
package/dist/lib/utilities.d.mts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { MsgProject, MsgResource, MsgResourceData } from '@worldware/msg';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Recursively finds all javascript and typescript files in a directory that have
|
|
5
|
-
* an `.msg.` substring in their filename just before the file extension.
|
|
6
|
-
* @param directory - The directory path to search
|
|
7
|
-
* @returns Promise resolving to array of file paths
|
|
8
|
-
*/
|
|
9
|
-
declare function findMsgResourceFiles(directory: string): Promise<string[]>;
|
|
10
|
-
/**
|
|
11
|
-
* Dynamically imports MsgResource objects from .msg. files and groups them by project.
|
|
12
|
-
* @param filePaths - Array of paths to .msg. files
|
|
13
|
-
* @returns Promise resolving to Map of MsgProject keys and MsgResource array values
|
|
14
|
-
*/
|
|
15
|
-
declare function importMsgResources(filePaths: string[]): Promise<Map<MsgProject, MsgResource[]>>;
|
|
16
|
-
/**
|
|
17
|
-
* Serializes MsgResource arrays into monolingual XLIFF 1.2 strings per project.
|
|
18
|
-
* @param resources - Array of MsgResource objects
|
|
19
|
-
* @returns Promise resolving to Map of MsgProject keys and xliff string values
|
|
20
|
-
*/
|
|
21
|
-
declare function resourcesToXliffString(resources: MsgResource[]): Promise<Map<MsgProject, string>>;
|
|
22
|
-
/**
|
|
23
|
-
* Writes an XLIFF 1.2 string to a file with appropriate indentation.
|
|
24
|
-
* @param filePath - The destination file path
|
|
25
|
-
* @param xliff - The serialized XLIFF 1.2 string
|
|
26
|
-
*/
|
|
27
|
-
declare function writeXliff(filePath: string, xliff: string): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Reads XLIFF 1.2 content from a file.
|
|
30
|
-
* @param filePath - The path to the XLIFF file
|
|
31
|
-
* @returns Promise resolving to the XLIFF string content
|
|
32
|
-
*/
|
|
33
|
-
declare function readXliff(filePath: string): Promise<string>;
|
|
34
|
-
/**
|
|
35
|
-
* Parses an XLIFF 1.2 string into a javascript object, preserving notes and attributes.
|
|
36
|
-
* @param xliff - Valid XLIFF 1.2 string
|
|
37
|
-
* @returns Promise resolving to parsed object
|
|
38
|
-
*/
|
|
39
|
-
declare function parseXliff(xliff: string): Promise<object>;
|
|
40
|
-
/**
|
|
41
|
-
* Extracts MsgResourceData from parsed XLIFF 1.2 object.
|
|
42
|
-
* @param parsedXliff - Parsed XLIFF 1.2 javascript object
|
|
43
|
-
* @returns Promise resolving to array of MsgResourceData objects
|
|
44
|
-
*/
|
|
45
|
-
declare function xliffDataToResourceTranslationData(parsedXliff: object): Promise<MsgResourceData[]>;
|
|
46
|
-
/**
|
|
47
|
-
* Parses XLIFF 1.2 string and extracts translation data to MsgResourceData objects.
|
|
48
|
-
* @param xliff - Valid XLIFF 1.2 string
|
|
49
|
-
* @returns Promise resolving to array of MsgResourceData objects
|
|
50
|
-
*/
|
|
51
|
-
declare function xliffToTranslationData(xliff: string): Promise<MsgResourceData[]>;
|
|
52
|
-
/**
|
|
53
|
-
* Serializes MsgResourceData to JSON and writes it to a file.
|
|
54
|
-
* @param filePath - The destination file path
|
|
55
|
-
* @param data - MsgResourceData object to serialize
|
|
56
|
-
*/
|
|
57
|
-
declare function writeTranslationData(filePath: string, data: MsgResourceData): Promise<void>;
|
|
58
|
-
|
|
59
|
-
export { findMsgResourceFiles, importMsgResources, parseXliff, readXliff, resourcesToXliffString, writeTranslationData, writeXliff, xliffDataToResourceTranslationData, xliffToTranslationData };
|
package/dist/lib/utilities.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { MsgProject, MsgResource, MsgResourceData } from '@worldware/msg';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Recursively finds all javascript and typescript files in a directory that have
|
|
5
|
-
* an `.msg.` substring in their filename just before the file extension.
|
|
6
|
-
* @param directory - The directory path to search
|
|
7
|
-
* @returns Promise resolving to array of file paths
|
|
8
|
-
*/
|
|
9
|
-
declare function findMsgResourceFiles(directory: string): Promise<string[]>;
|
|
10
|
-
/**
|
|
11
|
-
* Dynamically imports MsgResource objects from .msg. files and groups them by project.
|
|
12
|
-
* @param filePaths - Array of paths to .msg. files
|
|
13
|
-
* @returns Promise resolving to Map of MsgProject keys and MsgResource array values
|
|
14
|
-
*/
|
|
15
|
-
declare function importMsgResources(filePaths: string[]): Promise<Map<MsgProject, MsgResource[]>>;
|
|
16
|
-
/**
|
|
17
|
-
* Serializes MsgResource arrays into monolingual XLIFF 1.2 strings per project.
|
|
18
|
-
* @param resources - Array of MsgResource objects
|
|
19
|
-
* @returns Promise resolving to Map of MsgProject keys and xliff string values
|
|
20
|
-
*/
|
|
21
|
-
declare function resourcesToXliffString(resources: MsgResource[]): Promise<Map<MsgProject, string>>;
|
|
22
|
-
/**
|
|
23
|
-
* Writes an XLIFF 1.2 string to a file with appropriate indentation.
|
|
24
|
-
* @param filePath - The destination file path
|
|
25
|
-
* @param xliff - The serialized XLIFF 1.2 string
|
|
26
|
-
*/
|
|
27
|
-
declare function writeXliff(filePath: string, xliff: string): Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Reads XLIFF 1.2 content from a file.
|
|
30
|
-
* @param filePath - The path to the XLIFF file
|
|
31
|
-
* @returns Promise resolving to the XLIFF string content
|
|
32
|
-
*/
|
|
33
|
-
declare function readXliff(filePath: string): Promise<string>;
|
|
34
|
-
/**
|
|
35
|
-
* Parses an XLIFF 1.2 string into a javascript object, preserving notes and attributes.
|
|
36
|
-
* @param xliff - Valid XLIFF 1.2 string
|
|
37
|
-
* @returns Promise resolving to parsed object
|
|
38
|
-
*/
|
|
39
|
-
declare function parseXliff(xliff: string): Promise<object>;
|
|
40
|
-
/**
|
|
41
|
-
* Extracts MsgResourceData from parsed XLIFF 1.2 object.
|
|
42
|
-
* @param parsedXliff - Parsed XLIFF 1.2 javascript object
|
|
43
|
-
* @returns Promise resolving to array of MsgResourceData objects
|
|
44
|
-
*/
|
|
45
|
-
declare function xliffDataToResourceTranslationData(parsedXliff: object): Promise<MsgResourceData[]>;
|
|
46
|
-
/**
|
|
47
|
-
* Parses XLIFF 1.2 string and extracts translation data to MsgResourceData objects.
|
|
48
|
-
* @param xliff - Valid XLIFF 1.2 string
|
|
49
|
-
* @returns Promise resolving to array of MsgResourceData objects
|
|
50
|
-
*/
|
|
51
|
-
declare function xliffToTranslationData(xliff: string): Promise<MsgResourceData[]>;
|
|
52
|
-
/**
|
|
53
|
-
* Serializes MsgResourceData to JSON and writes it to a file.
|
|
54
|
-
* @param filePath - The destination file path
|
|
55
|
-
* @param data - MsgResourceData object to serialize
|
|
56
|
-
*/
|
|
57
|
-
declare function writeTranslationData(filePath: string, data: MsgResourceData): Promise<void>;
|
|
58
|
-
|
|
59
|
-
export { findMsgResourceFiles, importMsgResources, parseXliff, readXliff, resourcesToXliffString, writeTranslationData, writeXliff, xliffDataToResourceTranslationData, xliffToTranslationData };
|
package/dist/lib/utilities.mjs
DELETED
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
// src/lib/utilities.ts
|
|
2
|
-
import {
|
|
3
|
-
MsgResource
|
|
4
|
-
} from "@worldware/msg";
|
|
5
|
-
import { readdir, readFile, writeFile } from "fs/promises";
|
|
6
|
-
import { basename, extname, join } from "path";
|
|
7
|
-
import { pathToFileURL } from "url";
|
|
8
|
-
import { XMLParser } from "fast-xml-parser";
|
|
9
|
-
var MSG_PATTERN = /\.msg\.(ts|js)$/i;
|
|
10
|
-
async function findMsgResourceFiles(directory) {
|
|
11
|
-
const result = [];
|
|
12
|
-
const entries = await readdir(directory, { withFileTypes: true });
|
|
13
|
-
for (const entry of entries) {
|
|
14
|
-
const fullPath = join(directory, entry.name);
|
|
15
|
-
if (entry.isFile() && MSG_PATTERN.test(entry.name)) {
|
|
16
|
-
result.push(fullPath);
|
|
17
|
-
} else if (entry.isDirectory()) {
|
|
18
|
-
result.push(...await findMsgResourceFiles(fullPath));
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
return result;
|
|
22
|
-
}
|
|
23
|
-
async function importMsgResources(filePaths) {
|
|
24
|
-
const byProjectName = /* @__PURE__ */ new Map();
|
|
25
|
-
for (const filePath of filePaths) {
|
|
26
|
-
if (!MSG_PATTERN.test(filePath)) {
|
|
27
|
-
throw new Error(`File does not have .msg. in filename: ${filePath}`);
|
|
28
|
-
}
|
|
29
|
-
const url = pathToFileURL(filePath).href;
|
|
30
|
-
const mod = await import(url);
|
|
31
|
-
const resource = mod.default ?? mod.resource ?? mod.MsgResource;
|
|
32
|
-
if (!resource || !(resource instanceof MsgResource)) {
|
|
33
|
-
throw new Error(
|
|
34
|
-
`Failed to import MsgResource from ${filePath}: no valid export found`
|
|
35
|
-
);
|
|
36
|
-
}
|
|
37
|
-
const project = resource.getProject();
|
|
38
|
-
const projectName = project.project.name;
|
|
39
|
-
let group = byProjectName.get(projectName);
|
|
40
|
-
if (!group) {
|
|
41
|
-
group = { project, resources: [] };
|
|
42
|
-
byProjectName.set(projectName, group);
|
|
43
|
-
}
|
|
44
|
-
group.resources.push(resource);
|
|
45
|
-
}
|
|
46
|
-
const result = /* @__PURE__ */ new Map();
|
|
47
|
-
for (const { project, resources } of byProjectName.values()) {
|
|
48
|
-
result.set(project, resources);
|
|
49
|
-
}
|
|
50
|
-
return result;
|
|
51
|
-
}
|
|
52
|
-
async function resourcesToXliffString(resources) {
|
|
53
|
-
const byProject = /* @__PURE__ */ new Map();
|
|
54
|
-
for (const resource of resources) {
|
|
55
|
-
const project = resource.getProject();
|
|
56
|
-
let arr = byProject.get(project);
|
|
57
|
-
if (!arr) {
|
|
58
|
-
arr = [];
|
|
59
|
-
byProject.set(project, arr);
|
|
60
|
-
}
|
|
61
|
-
arr.push(resource);
|
|
62
|
-
}
|
|
63
|
-
const result = /* @__PURE__ */ new Map();
|
|
64
|
-
for (const [project, arr] of byProject) {
|
|
65
|
-
const xliff = serializeResourcesToXliff(arr);
|
|
66
|
-
result.set(project, xliff);
|
|
67
|
-
}
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
function serializeResourcesToXliff(resources) {
|
|
71
|
-
const xmlns = "urn:oasis:names:tc:xliff:document:1.2";
|
|
72
|
-
const parts = [
|
|
73
|
-
'<?xml version="1.0" encoding="UTF-8"?>',
|
|
74
|
-
`<xliff version="1.2" xmlns="${xmlns}">`
|
|
75
|
-
];
|
|
76
|
-
for (const resource of resources) {
|
|
77
|
-
const orig = `${resource.title}.json`;
|
|
78
|
-
const sourceLang = resource.attributes.lang || "en";
|
|
79
|
-
const data = resource.getData(false);
|
|
80
|
-
parts.push(
|
|
81
|
-
` <file original="${escapeXml(orig)}" source-language="${escapeXml(sourceLang)}" datatype="plaintext">`
|
|
82
|
-
);
|
|
83
|
-
parts.push(" <body>");
|
|
84
|
-
for (const msg of data.messages || []) {
|
|
85
|
-
const key = msg.key;
|
|
86
|
-
const value = msg.value;
|
|
87
|
-
const attrs = [`id="${escapeXml(key)}"`, `resname="${escapeXml(key)}"`];
|
|
88
|
-
parts.push(` <trans-unit ${attrs.join(" ")}>`);
|
|
89
|
-
parts.push(` <source>${escapeXml(value)}</source>`);
|
|
90
|
-
parts.push(` </trans-unit>`);
|
|
91
|
-
}
|
|
92
|
-
parts.push(" </body>");
|
|
93
|
-
parts.push(" </file>");
|
|
94
|
-
}
|
|
95
|
-
parts.push("</xliff>");
|
|
96
|
-
return parts.join("\n");
|
|
97
|
-
}
|
|
98
|
-
function escapeXml(s) {
|
|
99
|
-
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
100
|
-
}
|
|
101
|
-
async function writeXliff(filePath, xliff) {
|
|
102
|
-
await writeFile(filePath, xliff, "utf-8");
|
|
103
|
-
}
|
|
104
|
-
async function readXliff(filePath) {
|
|
105
|
-
return readFile(filePath, "utf-8");
|
|
106
|
-
}
|
|
107
|
-
var xmlParser = new XMLParser({
|
|
108
|
-
ignoreAttributes: false,
|
|
109
|
-
attributeNamePrefix: "@_"
|
|
110
|
-
});
|
|
111
|
-
async function parseXliff(xliff) {
|
|
112
|
-
try {
|
|
113
|
-
const parsed = xmlParser.parse(xliff);
|
|
114
|
-
if (!parsed || typeof parsed !== "object") {
|
|
115
|
-
throw new Error("Invalid XLIFF: failed to parse");
|
|
116
|
-
}
|
|
117
|
-
return parsed;
|
|
118
|
-
} catch (err) {
|
|
119
|
-
const message = err instanceof Error ? err.message : "Failed to parse XLIFF";
|
|
120
|
-
throw new Error(`Invalid XLIFF 1.2: ${message}`);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
async function xliffDataToResourceTranslationData(parsedXliff) {
|
|
124
|
-
const result = [];
|
|
125
|
-
const xliff = parsedXliff;
|
|
126
|
-
const fileEl = xliff?.xliff?.file;
|
|
127
|
-
if (!fileEl) return result;
|
|
128
|
-
const files = Array.isArray(fileEl) ? fileEl : [fileEl];
|
|
129
|
-
for (const file of files) {
|
|
130
|
-
const f = file;
|
|
131
|
-
const orig = f["@_original"] ?? "unknown";
|
|
132
|
-
const title = basename(orig, extname(orig)) || orig;
|
|
133
|
-
const targetLang = f["@_target-language"] ?? f["@_source-language"] ?? "";
|
|
134
|
-
const transUnits = f.body?.["trans-unit"];
|
|
135
|
-
const units = Array.isArray(transUnits) ? transUnits : transUnits ? [transUnits] : [];
|
|
136
|
-
const messages = [];
|
|
137
|
-
for (const tu of units) {
|
|
138
|
-
const t = tu;
|
|
139
|
-
const translate = (t["@_translate"] ?? "yes").toLowerCase();
|
|
140
|
-
if (translate === "no" || translate === "false") continue;
|
|
141
|
-
const key = t["@_resname"] ?? t["@_id"] ?? "";
|
|
142
|
-
const targetEl = t.target;
|
|
143
|
-
const value = (typeof targetEl === "object" && targetEl && "#text" in targetEl ? targetEl["#text"] : typeof targetEl === "string" ? targetEl : "") ?? "";
|
|
144
|
-
messages.push({ key, value });
|
|
145
|
-
}
|
|
146
|
-
result.push({
|
|
147
|
-
title,
|
|
148
|
-
attributes: { lang: targetLang, dir: "", dnt: false },
|
|
149
|
-
messages
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
return result;
|
|
153
|
-
}
|
|
154
|
-
async function xliffToTranslationData(xliff) {
|
|
155
|
-
const parsed = await parseXliff(xliff);
|
|
156
|
-
return xliffDataToResourceTranslationData(parsed);
|
|
157
|
-
}
|
|
158
|
-
async function writeTranslationData(filePath, data) {
|
|
159
|
-
const json = JSON.stringify(data, null, 2);
|
|
160
|
-
await writeFile(filePath, json, "utf-8");
|
|
161
|
-
}
|
|
162
|
-
export {
|
|
163
|
-
findMsgResourceFiles,
|
|
164
|
-
importMsgResources,
|
|
165
|
-
parseXliff,
|
|
166
|
-
readXliff,
|
|
167
|
-
resourcesToXliffString,
|
|
168
|
-
writeTranslationData,
|
|
169
|
-
writeXliff,
|
|
170
|
-
xliffDataToResourceTranslationData,
|
|
171
|
-
xliffToTranslationData
|
|
172
|
-
};
|