auto-translate-json-library 2.0.2 → 2.1.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 +305 -3
- package/build/package.json +31 -16
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +253 -58
- package/build/src/config.d.ts +12 -1
- package/build/src/files.js +2 -2
- package/build/src/format-detector.js +1 -1
- package/build/src/huggingface-local.d.ts +11 -0
- package/build/src/huggingface-local.js +71 -0
- package/build/src/huggingface.d.ts +12 -0
- package/build/src/huggingface.js +76 -0
- package/build/src/index.js +16 -0
- package/build/src/lib.js +10 -26
- package/build/src/openai.js +1 -0
- package/build/src/provider-factory.d.ts +3 -0
- package/build/src/provider-factory.js +38 -0
- package/package.json +31 -16
- package/build/tests/arb-handler.test.d.ts +0 -1
- package/build/tests/arb-handler.test.js +0 -694
- package/build/tests/cli-integration.test.d.ts +0 -1
- package/build/tests/cli-integration.test.js +0 -352
- package/build/tests/csv-handler.test.d.ts +0 -1
- package/build/tests/csv-handler.test.js +0 -507
- package/build/tests/error-handling.test.d.ts +0 -1
- package/build/tests/error-handling.test.js +0 -456
- package/build/tests/files-integration.test.d.ts +0 -1
- package/build/tests/files-integration.test.js +0 -421
- package/build/tests/json-handler.test.d.ts +0 -1
- package/build/tests/json-handler.test.js +0 -526
- package/build/tests/po-handler.test.d.ts +0 -1
- package/build/tests/po-handler.test.js +0 -768
- package/build/tests/properties-handler.test.d.ts +0 -1
- package/build/tests/properties-handler.test.js +0 -316
- package/build/tests/setup.d.ts +0 -1
- package/build/tests/setup.js +0 -5
- package/build/tests/xliff-handler.test.d.ts +0 -1
- package/build/tests/xliff-handler.test.js +0 -459
- package/build/tests/xmb-xtb-handler.test.d.ts +0 -1
- package/build/tests/xmb-xtb-handler.test.js +0 -758
- package/build/tests/xml-handler.test.d.ts +0 -1
- package/build/tests/xml-handler.test.js +0 -570
- package/build/tests/yaml-handler.test.d.ts +0 -1
- package/build/tests/yaml-handler.test.js +0 -156
package/build/src/bin/cli.js
CHANGED
|
@@ -5,12 +5,50 @@ import minimist from "minimist";
|
|
|
5
5
|
import c from "picocolors";
|
|
6
6
|
import packageJson from "../../package.json" with { type: "json" };
|
|
7
7
|
const { version } = packageJson;
|
|
8
|
-
import { FormatHandlerFactory } from "../format-handler-factory.js";
|
|
9
|
-
import { translate } from "../lib.js";
|
|
10
|
-
// Import format handlers to ensure they are registered
|
|
11
|
-
import "../format/index.js";
|
|
12
8
|
import { config as dotenvConfig } from "dotenv";
|
|
13
9
|
dotenvConfig();
|
|
10
|
+
const formatDetails = {
|
|
11
|
+
json: { extensions: [".json"], description: "JSON translation files" },
|
|
12
|
+
arb: {
|
|
13
|
+
extensions: [".arb"],
|
|
14
|
+
description: "Flutter Application Resource Bundle",
|
|
15
|
+
},
|
|
16
|
+
xml: { extensions: [".xml"], description: "Generic XML translation files" },
|
|
17
|
+
"android-xml": {
|
|
18
|
+
extensions: [".xml"],
|
|
19
|
+
description: "Android strings.xml format",
|
|
20
|
+
},
|
|
21
|
+
"ios-xml": {
|
|
22
|
+
extensions: [".xml"],
|
|
23
|
+
description: "iOS localization XML format",
|
|
24
|
+
},
|
|
25
|
+
"generic-xml": { extensions: [".xml"], description: "Generic XML format" },
|
|
26
|
+
xliff: {
|
|
27
|
+
extensions: [".xlf", ".xliff"],
|
|
28
|
+
description: "XLIFF 1.2 and 2.x translation files",
|
|
29
|
+
},
|
|
30
|
+
xmb: {
|
|
31
|
+
extensions: [".xmb"],
|
|
32
|
+
description: "XML Message Bundle source files",
|
|
33
|
+
},
|
|
34
|
+
xtb: { extensions: [".xtb"], description: "XML Translation Bundle files" },
|
|
35
|
+
po: { extensions: [".po"], description: "GNU gettext PO files" },
|
|
36
|
+
pot: {
|
|
37
|
+
extensions: [".pot"],
|
|
38
|
+
description: "GNU gettext POT template files",
|
|
39
|
+
},
|
|
40
|
+
yaml: {
|
|
41
|
+
extensions: [".yaml", ".yml"],
|
|
42
|
+
description: "YAML translation files",
|
|
43
|
+
},
|
|
44
|
+
properties: {
|
|
45
|
+
extensions: [".properties"],
|
|
46
|
+
description: "Java Properties files",
|
|
47
|
+
},
|
|
48
|
+
csv: { extensions: [".csv"], description: "Comma-separated values files" },
|
|
49
|
+
tsv: { extensions: [".tsv"], description: "Tab-separated values files" },
|
|
50
|
+
};
|
|
51
|
+
const supportedFormats = Object.keys(formatDetails);
|
|
14
52
|
// Define a function to display the help message
|
|
15
53
|
function displayHelp() {
|
|
16
54
|
console.log(`🔨 Auto translate json/xml cli v${version}`);
|
|
@@ -19,8 +57,9 @@ function displayHelp() {
|
|
|
19
57
|
console.log("Options:");
|
|
20
58
|
console.log(" --help, -h Display this help message");
|
|
21
59
|
console.log(" --list-formats List all supported file formats");
|
|
60
|
+
console.log(" --json, -j Output in JSON format (LLM-friendly)");
|
|
22
61
|
console.log(" --mode, -m <mode> Specify the translation mode:file or folder");
|
|
23
|
-
console.log(" --engine, -e <engine> Specify the translation engine:aws,azure,google,deepLPro,deepLFree
|
|
62
|
+
console.log(" --engine, -e <engine> Specify the translation engine:aws,azure,google,deepLPro,deepLFree,openai,huggingface,huggingface-local");
|
|
24
63
|
console.log(" --sourceLocale, -s <locale> Specify the source locale");
|
|
25
64
|
console.log(" --format, -f <format> Manually specify the file format (overrides auto-detection)");
|
|
26
65
|
console.log(" Note: inputPath can be a translation file in any supported format, or a folder structure");
|
|
@@ -28,7 +67,6 @@ function displayHelp() {
|
|
|
28
67
|
console.log(" --keepExtraTranslations, --no-keepExtraTranslations Keep or remove extra translations");
|
|
29
68
|
console.log("");
|
|
30
69
|
console.log("Supported formats:");
|
|
31
|
-
const formats = FormatHandlerFactory.getSupportedFormats();
|
|
32
70
|
const formatGroups = {
|
|
33
71
|
"JSON-based": ["json", "arb"],
|
|
34
72
|
"XML-based": [
|
|
@@ -44,7 +82,7 @@ function displayHelp() {
|
|
|
44
82
|
Tabular: ["csv", "tsv"],
|
|
45
83
|
};
|
|
46
84
|
for (const [group, groupFormats] of Object.entries(formatGroups)) {
|
|
47
|
-
const availableFormats = groupFormats.filter((f) =>
|
|
85
|
+
const availableFormats = groupFormats.filter((f) => supportedFormats.includes(f));
|
|
48
86
|
if (availableFormats.length > 0) {
|
|
49
87
|
console.log(` ${group}: ${availableFormats.join(", ")}`);
|
|
50
88
|
}
|
|
@@ -60,7 +98,41 @@ function displayHelp() {
|
|
|
60
98
|
console.log(" atj translations.json # Auto-detect JSON format");
|
|
61
99
|
console.log(" atj --format xliff messages.xlf # Force XLIFF format");
|
|
62
100
|
console.log(" atj --mode folder locales/ # Process entire folder");
|
|
63
|
-
console.log(" atj --engine google --format po msgs.po
|
|
101
|
+
console.log(" atj --engine google --format po msgs.po # Use Google Translate with PO files");
|
|
102
|
+
console.log(" atj --engine huggingface-local demo.json # Use local Hugging Face model");
|
|
103
|
+
console.log(" atj --engine openai --sourceLocale en --format yaml config.yaml");
|
|
104
|
+
console.log(" atj --engine huggingface --mode folder translations/");
|
|
105
|
+
console.log("");
|
|
106
|
+
console.log("Comprehensive examples:");
|
|
107
|
+
console.log(" # Basic translation with auto-detection");
|
|
108
|
+
console.log(" atj translations/en.json");
|
|
109
|
+
console.log("");
|
|
110
|
+
console.log(" # Specify engine and format");
|
|
111
|
+
console.log(" atj --engine google --format json translations.json");
|
|
112
|
+
console.log(" atj --engine azure --format xml strings.xml");
|
|
113
|
+
console.log(" atj --engine openai --format yaml config.yaml");
|
|
114
|
+
console.log("");
|
|
115
|
+
console.log(" # Local inference (no API keys needed)");
|
|
116
|
+
console.log(" atj --engine huggingface-local demo.json");
|
|
117
|
+
console.log(" atj --engine openai --sourceLocale en messages.po");
|
|
118
|
+
console.log("");
|
|
119
|
+
console.log(" # Folder mode for project structure");
|
|
120
|
+
console.log(" atj --mode folder --engine google locales/");
|
|
121
|
+
console.log(" atj --mode folder --engine huggingface-local translations/");
|
|
122
|
+
console.log("");
|
|
123
|
+
console.log(" # Advanced options");
|
|
124
|
+
console.log(" atj --engine google --sourceLocale en --keepTranslations app.json");
|
|
125
|
+
console.log(" atj --engine azure --no-keepExtraTranslations strings.xml");
|
|
126
|
+
console.log("");
|
|
127
|
+
console.log(" # JSON output for LLM/automation");
|
|
128
|
+
console.log(" atj --engine huggingface-local --json demo.json");
|
|
129
|
+
console.log(" atj --engine google --json --format po messages.po");
|
|
130
|
+
console.log(" atj --format xliff messages.xlf # Force XLIFF format");
|
|
131
|
+
console.log(" atj --mode folder locales/ # Process entire folder");
|
|
132
|
+
console.log(" atj --engine google --format po msgs.po # Use Google Translate with PO files");
|
|
133
|
+
console.log(" atj --engine huggingface-local demo.json # Use local Hugging Face model");
|
|
134
|
+
console.log(" atj --engine openai --sourceLocale en --format yaml config.yaml");
|
|
135
|
+
console.log(" atj --engine huggingface --mode folder translations/");
|
|
64
136
|
console.log("");
|
|
65
137
|
console.log("Default values");
|
|
66
138
|
console.log(" --mode, -m <mode> file");
|
|
@@ -69,6 +141,19 @@ function displayHelp() {
|
|
|
69
141
|
console.log(" --format, -f <format> auto-detect");
|
|
70
142
|
console.log(" --keepTranslations, --no-keepTranslations --keepTranslations");
|
|
71
143
|
console.log(" --keepExtraTranslations, --no-keepExtraTranslations --no-keepExtraTranslations");
|
|
144
|
+
console.log(" --json, -j false");
|
|
145
|
+
console.log("");
|
|
146
|
+
console.log("Engine details:");
|
|
147
|
+
console.log(" aws - AWS Translate (requires ATJ_AWS_* env vars)");
|
|
148
|
+
console.log(" azure - Azure Translator (requires ATJ_AZURE_* env vars)");
|
|
149
|
+
console.log(" google - Google Translate (requires ATJ_GOOGLE_API_KEY)");
|
|
150
|
+
console.log(" deepLPro - DeepL Pro API (requires ATJ_DEEPL_PRO_SECRET_KEY)");
|
|
151
|
+
console.log(" deepLFree - DeepL Free API (requires ATJ_DEEPL_FREE_SECRET_KEY)");
|
|
152
|
+
console.log(" openai - OpenAI API or local Ollama (requires ATJ_OPEN_AI_SECRET_KEY)");
|
|
153
|
+
console.log(" huggingface - Hugging Face Cloud API (requires ATJ_HUGGING_FACE_API_KEY)");
|
|
154
|
+
console.log(" huggingface-local - Hugging Face Local ONNX (requires ATJ_HUGGING_FACE_LOCAL_MODEL)");
|
|
155
|
+
console.log("");
|
|
156
|
+
console.log("Performance note: Engines use lazy loading - only the selected engine is loaded.");
|
|
72
157
|
}
|
|
73
158
|
// Define a function to list supported formats
|
|
74
159
|
function listFormats() {
|
|
@@ -76,49 +161,7 @@ function listFormats() {
|
|
|
76
161
|
console.log("");
|
|
77
162
|
console.log("Supported file formats:");
|
|
78
163
|
console.log("");
|
|
79
|
-
const
|
|
80
|
-
const formatDetails = {
|
|
81
|
-
json: { extensions: [".json"], description: "JSON translation files" },
|
|
82
|
-
arb: {
|
|
83
|
-
extensions: [".arb"],
|
|
84
|
-
description: "Flutter Application Resource Bundle",
|
|
85
|
-
},
|
|
86
|
-
xml: { extensions: [".xml"], description: "Generic XML translation files" },
|
|
87
|
-
"android-xml": {
|
|
88
|
-
extensions: [".xml"],
|
|
89
|
-
description: "Android strings.xml format",
|
|
90
|
-
},
|
|
91
|
-
"ios-xml": {
|
|
92
|
-
extensions: [".xml"],
|
|
93
|
-
description: "iOS localization XML format",
|
|
94
|
-
},
|
|
95
|
-
"generic-xml": { extensions: [".xml"], description: "Generic XML format" },
|
|
96
|
-
xliff: {
|
|
97
|
-
extensions: [".xlf", ".xliff"],
|
|
98
|
-
description: "XLIFF 1.2 and 2.x translation files",
|
|
99
|
-
},
|
|
100
|
-
xmb: {
|
|
101
|
-
extensions: [".xmb"],
|
|
102
|
-
description: "XML Message Bundle source files",
|
|
103
|
-
},
|
|
104
|
-
xtb: { extensions: [".xtb"], description: "XML Translation Bundle files" },
|
|
105
|
-
po: { extensions: [".po"], description: "GNU gettext PO files" },
|
|
106
|
-
pot: {
|
|
107
|
-
extensions: [".pot"],
|
|
108
|
-
description: "GNU gettext POT template files",
|
|
109
|
-
},
|
|
110
|
-
yaml: {
|
|
111
|
-
extensions: [".yaml", ".yml"],
|
|
112
|
-
description: "YAML translation files",
|
|
113
|
-
},
|
|
114
|
-
properties: {
|
|
115
|
-
extensions: [".properties"],
|
|
116
|
-
description: "Java Properties files",
|
|
117
|
-
},
|
|
118
|
-
csv: { extensions: [".csv"], description: "Comma-separated values files" },
|
|
119
|
-
tsv: { extensions: [".tsv"], description: "Tab-separated values files" },
|
|
120
|
-
};
|
|
121
|
-
for (const format of formats.sort()) {
|
|
164
|
+
for (const format of [...supportedFormats].sort()) {
|
|
122
165
|
const details = formatDetails[format];
|
|
123
166
|
if (details) {
|
|
124
167
|
console.log(` ${format.padEnd(15)} ${details.extensions.join(", ").padEnd(20)} ${details.description}`);
|
|
@@ -132,6 +175,9 @@ function listFormats() {
|
|
|
132
175
|
console.log(" atj --format yaml config/translations.yaml");
|
|
133
176
|
console.log(" atj --format arb --engine google lib/l10n/app_en.arb");
|
|
134
177
|
console.log(" atj --format android-xml --sourceLocale en res/values/strings.xml");
|
|
178
|
+
console.log(" atj --engine huggingface-local demo.json");
|
|
179
|
+
console.log(" atj --engine openai --format yaml config.yaml");
|
|
180
|
+
console.log(" atj --engine huggingface --mode folder translations/");
|
|
135
181
|
console.log("");
|
|
136
182
|
console.log("Best practices:");
|
|
137
183
|
console.log(" • Format is usually auto-detected from file extension");
|
|
@@ -139,6 +185,9 @@ function listFormats() {
|
|
|
139
185
|
console.log(" • For XML files, specify the exact variant (android-xml, ios-xml, xliff)");
|
|
140
186
|
console.log(" • Use --keepTranslations to preserve existing translations");
|
|
141
187
|
console.log(" • Test with a single file before processing entire folders");
|
|
188
|
+
console.log(" • Engines use lazy loading - only needed SDKs are loaded");
|
|
189
|
+
console.log(" • For local inference: huggingface-local (ONNX) or openai (Ollama)");
|
|
190
|
+
console.log(" • Set environment variables in .env file for API keys");
|
|
142
191
|
}
|
|
143
192
|
const arguments_ = process.argv.slice(2);
|
|
144
193
|
if (arguments_.length === 0) {
|
|
@@ -154,6 +203,7 @@ const flags = minimist(arguments_, {
|
|
|
154
203
|
sourceLocale: ["s"],
|
|
155
204
|
format: ["f"],
|
|
156
205
|
help: ["h"],
|
|
206
|
+
json: ["j"],
|
|
157
207
|
},
|
|
158
208
|
string: ["mode", "engine", "sourceLocale", "format"],
|
|
159
209
|
boolean: [
|
|
@@ -161,6 +211,7 @@ const flags = minimist(arguments_, {
|
|
|
161
211
|
"keepExtraTranslations",
|
|
162
212
|
"help",
|
|
163
213
|
"list-formats",
|
|
214
|
+
"json",
|
|
164
215
|
],
|
|
165
216
|
default: {
|
|
166
217
|
engine: "aws",
|
|
@@ -168,6 +219,7 @@ const flags = minimist(arguments_, {
|
|
|
168
219
|
keepTranslations: true,
|
|
169
220
|
keepExtraTranslations: false,
|
|
170
221
|
mode: "file",
|
|
222
|
+
json: false,
|
|
171
223
|
},
|
|
172
224
|
});
|
|
173
225
|
console.log(c.green(`🔨 Auto translate json cli v${version}`));
|
|
@@ -180,7 +232,7 @@ if (flags["list-formats"]) {
|
|
|
180
232
|
process.exit(0);
|
|
181
233
|
}
|
|
182
234
|
const inputPath = flags._[0];
|
|
183
|
-
const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations, format, } = flags;
|
|
235
|
+
const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations, format, json, } = flags;
|
|
184
236
|
// Validate input path
|
|
185
237
|
if (!inputPath) {
|
|
186
238
|
console.error(c.red("❌ Input path is required"));
|
|
@@ -188,7 +240,7 @@ if (!inputPath) {
|
|
|
188
240
|
process.exit(1);
|
|
189
241
|
}
|
|
190
242
|
// Validate format if specified
|
|
191
|
-
if (format && !
|
|
243
|
+
if (format && !supportedFormats.includes(format)) {
|
|
192
244
|
console.error(c.red(`❌ Unsupported format: ${format}`));
|
|
193
245
|
console.error(c.yellow("💡 Use --list-formats to see all supported formats"));
|
|
194
246
|
process.exit(1);
|
|
@@ -207,6 +259,8 @@ const validEngines = [
|
|
|
207
259
|
"deepLPro",
|
|
208
260
|
"deepLFree",
|
|
209
261
|
"openai",
|
|
262
|
+
"huggingface",
|
|
263
|
+
"huggingface-local",
|
|
210
264
|
];
|
|
211
265
|
if (engine && !validEngines.includes(engine)) {
|
|
212
266
|
console.error(c.red(`❌ Invalid engine: ${engine}`));
|
|
@@ -224,7 +278,9 @@ switch (engine) {
|
|
|
224
278
|
case "google": {
|
|
225
279
|
const googleApiKey = process.env.ATJ_GOOGLE_API_KEY;
|
|
226
280
|
if (!googleApiKey) {
|
|
227
|
-
console.error(c.red("❌ Google
|
|
281
|
+
console.error(c.red("❌ Google API key not found in environment variable ATJ_GOOGLE_API_KEY"));
|
|
282
|
+
console.error(c.yellow("💡 Get API key: https://cloud.google.com/translate/docs/setup"));
|
|
283
|
+
console.error(c.yellow("💡 Set ATJ_GOOGLE_API_KEY in .env file or environment"));
|
|
228
284
|
process.exit(1);
|
|
229
285
|
}
|
|
230
286
|
config.translationKeyInfo = {
|
|
@@ -266,7 +322,10 @@ switch (engine) {
|
|
|
266
322
|
case "openai": {
|
|
267
323
|
const openaiApiKey = process.env.ATJ_OPEN_AI_SECRET_KEY;
|
|
268
324
|
if (!openaiApiKey) {
|
|
269
|
-
console.error(c.red("❌
|
|
325
|
+
console.error(c.red("❌ OpenAI API key not found in environment variable ATJ_OPEN_AI_SECRET_KEY"));
|
|
326
|
+
console.error(c.yellow("💡 For OpenAI: Set ATJ_OPEN_AI_SECRET_KEY to your OpenAI API key"));
|
|
327
|
+
console.error(c.yellow("💡 For local Ollama: Set ATJ_OPEN_AI_SECRET_KEY to 'ollama' and ATJ_OPEN_AI_BASE_URL to 'http://localhost:11434'"));
|
|
328
|
+
console.error(c.yellow("💡 Example for Ollama: ATJ_OPEN_AI_SECRET_KEY=ollama ATJ_OPEN_AI_BASE_URL=http://localhost:11434"));
|
|
270
329
|
process.exit(1);
|
|
271
330
|
}
|
|
272
331
|
config.translationKeyInfo = {
|
|
@@ -307,6 +366,35 @@ switch (engine) {
|
|
|
307
366
|
};
|
|
308
367
|
break;
|
|
309
368
|
}
|
|
369
|
+
case "huggingface": {
|
|
370
|
+
const huggingFaceApiKey = process.env.ATJ_HUGGING_FACE_API_KEY;
|
|
371
|
+
if (!huggingFaceApiKey) {
|
|
372
|
+
console.error(c.red("❌ Hugging Face API key not found in environment variable ATJ_HUGGING_FACE_API_KEY"));
|
|
373
|
+
process.exit(1);
|
|
374
|
+
}
|
|
375
|
+
config.translationKeyInfo = {
|
|
376
|
+
kind: "huggingface",
|
|
377
|
+
apiKey: huggingFaceApiKey,
|
|
378
|
+
model: process.env.ATJ_HUGGING_FACE_MODEL ?? "Helsinki-NLP/opus-mt-en-fr",
|
|
379
|
+
provider: process.env.ATJ_HUGGING_FACE_PROVIDER,
|
|
380
|
+
};
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
case "huggingface-local": {
|
|
384
|
+
const huggingFaceLocalModel = process.env.ATJ_HUGGING_FACE_LOCAL_MODEL;
|
|
385
|
+
if (!huggingFaceLocalModel) {
|
|
386
|
+
console.error(c.red("❌ Hugging Face local model not found in environment variable ATJ_HUGGING_FACE_LOCAL_MODEL"));
|
|
387
|
+
console.error(c.yellow("💡 Set ATJ_HUGGING_FACE_LOCAL_MODEL in .env file or environment"));
|
|
388
|
+
console.error(c.yellow("💡 Example: ATJ_HUGGING_FACE_LOCAL_MODEL=Xenova/opus-mt-en-fr"));
|
|
389
|
+
console.error(c.yellow("💡 Available models: https://huggingface.co/models?pipeline_tag=translation"));
|
|
390
|
+
process.exit(1);
|
|
391
|
+
}
|
|
392
|
+
config.translationKeyInfo = {
|
|
393
|
+
kind: "huggingface-local",
|
|
394
|
+
model: huggingFaceLocalModel,
|
|
395
|
+
};
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
310
398
|
default:
|
|
311
399
|
console.error(c.red(`❌ Engine ${engine} not supported`));
|
|
312
400
|
process.exit(1);
|
|
@@ -324,9 +412,116 @@ config.mode = mode;
|
|
|
324
412
|
if (format) {
|
|
325
413
|
config.format = format;
|
|
326
414
|
}
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
415
|
+
async function main() {
|
|
416
|
+
// Load the translation runtime only when an actual translation command is executed.
|
|
417
|
+
await import("../format/index.js");
|
|
418
|
+
const { translate } = await import("../lib.js");
|
|
419
|
+
const sourcePath = path.join(process.cwd(), inputPath);
|
|
420
|
+
if (json) {
|
|
421
|
+
// JSON output mode - capture console output
|
|
422
|
+
const originalConsoleLog = console.log;
|
|
423
|
+
const originalConsoleError = console.error;
|
|
424
|
+
const originalConsoleWarn = console.warn;
|
|
425
|
+
const logs = [];
|
|
426
|
+
console.log = (...args) => {
|
|
427
|
+
logs.push({
|
|
428
|
+
type: "log",
|
|
429
|
+
message: args
|
|
430
|
+
.map((arg) => typeof arg === "object" ? JSON.stringify(arg) : String(arg))
|
|
431
|
+
.join(" "),
|
|
432
|
+
timestamp: Date.now(),
|
|
433
|
+
});
|
|
434
|
+
};
|
|
435
|
+
console.error = (...args) => {
|
|
436
|
+
logs.push({
|
|
437
|
+
type: "error",
|
|
438
|
+
message: args
|
|
439
|
+
.map((arg) => typeof arg === "object" ? JSON.stringify(arg) : String(arg))
|
|
440
|
+
.join(" "),
|
|
441
|
+
timestamp: Date.now(),
|
|
442
|
+
});
|
|
443
|
+
};
|
|
444
|
+
console.warn = (...args) => {
|
|
445
|
+
logs.push({
|
|
446
|
+
type: "warn",
|
|
447
|
+
message: args
|
|
448
|
+
.map((arg) => typeof arg === "object" ? JSON.stringify(arg) : String(arg))
|
|
449
|
+
.join(" "),
|
|
450
|
+
timestamp: Date.now(),
|
|
451
|
+
});
|
|
452
|
+
};
|
|
453
|
+
const startTime = Date.now();
|
|
454
|
+
try {
|
|
455
|
+
await translate(sourcePath, config);
|
|
456
|
+
const endTime = Date.now();
|
|
457
|
+
const result = {
|
|
458
|
+
command: "translate",
|
|
459
|
+
version,
|
|
460
|
+
timestamp: new Date().toISOString(),
|
|
461
|
+
input: {
|
|
462
|
+
path: sourcePath,
|
|
463
|
+
mode,
|
|
464
|
+
engine,
|
|
465
|
+
sourceLocale,
|
|
466
|
+
format: format || "auto",
|
|
467
|
+
keepTranslations,
|
|
468
|
+
keepExtraTranslations,
|
|
469
|
+
},
|
|
470
|
+
performance: {
|
|
471
|
+
startTime,
|
|
472
|
+
endTime,
|
|
473
|
+
totalMs: endTime - startTime,
|
|
474
|
+
},
|
|
475
|
+
logs,
|
|
476
|
+
status: "success",
|
|
477
|
+
message: "Translation completed successfully",
|
|
478
|
+
};
|
|
479
|
+
// Restore console
|
|
480
|
+
console.log = originalConsoleLog;
|
|
481
|
+
console.error = originalConsoleError;
|
|
482
|
+
console.warn = originalConsoleWarn;
|
|
483
|
+
console.log(JSON.stringify(result, null, 2));
|
|
484
|
+
}
|
|
485
|
+
catch (error) {
|
|
486
|
+
const errorTime = Date.now();
|
|
487
|
+
const result = {
|
|
488
|
+
command: "translate",
|
|
489
|
+
version,
|
|
490
|
+
timestamp: new Date().toISOString(),
|
|
491
|
+
input: {
|
|
492
|
+
path: sourcePath,
|
|
493
|
+
mode,
|
|
494
|
+
engine,
|
|
495
|
+
sourceLocale,
|
|
496
|
+
format: format || "auto",
|
|
497
|
+
keepTranslations,
|
|
498
|
+
keepExtraTranslations,
|
|
499
|
+
},
|
|
500
|
+
performance: {
|
|
501
|
+
startTime,
|
|
502
|
+
errorTime,
|
|
503
|
+
totalMs: errorTime - startTime,
|
|
504
|
+
},
|
|
505
|
+
logs,
|
|
506
|
+
status: "error",
|
|
507
|
+
error: error instanceof Error ? error.message : String(error),
|
|
508
|
+
stack: error instanceof Error ? error.stack : undefined,
|
|
509
|
+
};
|
|
510
|
+
// Restore console
|
|
511
|
+
console.log = originalConsoleLog;
|
|
512
|
+
console.error = originalConsoleError;
|
|
513
|
+
console.warn = originalConsoleWarn;
|
|
514
|
+
console.log(JSON.stringify(result, null, 2));
|
|
515
|
+
process.exit(1);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
else {
|
|
519
|
+
// Normal output mode
|
|
520
|
+
console.log(c.green(`🌐 Translating ${sourcePath}`));
|
|
521
|
+
await translate(sourcePath, config);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
main().catch((error) => {
|
|
330
525
|
console.error(c.red("❌ Translate error:\n"), error);
|
|
331
526
|
process.exit(1);
|
|
332
527
|
});
|
package/build/src/config.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import type { InferenceProviderOrPolicy } from "@huggingface/inference";
|
|
1
2
|
export type Configuration = {
|
|
2
|
-
translationKeyInfo: GoogleTranslationKey | AwsTranslationKey | AzureTranslationKey | DeepLProTranslationKey | DeepLFreeTranslationKey | OpenAITranslationKey;
|
|
3
|
+
translationKeyInfo: GoogleTranslationKey | AwsTranslationKey | AzureTranslationKey | DeepLProTranslationKey | DeepLFreeTranslationKey | OpenAITranslationKey | HuggingFaceTranslationKey | HuggingFaceLocalTranslationKey;
|
|
3
4
|
startDelimiter: string;
|
|
4
5
|
endDelimiter: string;
|
|
5
6
|
mode: "file" | "folder";
|
|
@@ -44,4 +45,14 @@ type OpenAITranslationKey = {
|
|
|
44
45
|
frequencyPenalty: number;
|
|
45
46
|
presencePenalty: number;
|
|
46
47
|
};
|
|
48
|
+
type HuggingFaceTranslationKey = {
|
|
49
|
+
kind: "huggingface";
|
|
50
|
+
apiKey: string;
|
|
51
|
+
model: string;
|
|
52
|
+
provider?: InferenceProviderOrPolicy;
|
|
53
|
+
};
|
|
54
|
+
type HuggingFaceLocalTranslationKey = {
|
|
55
|
+
kind: "huggingface-local";
|
|
56
|
+
model: string;
|
|
57
|
+
};
|
|
47
58
|
export {};
|
package/build/src/files.js
CHANGED
|
@@ -110,8 +110,8 @@ export const loadJsonFromLocale = async (fileName) => {
|
|
|
110
110
|
.join(", ");
|
|
111
111
|
console.warn(`${format.toUpperCase()} validation warnings for ${fileName}: ${warningMessages}`);
|
|
112
112
|
}
|
|
113
|
-
//
|
|
114
|
-
if (format ===
|
|
113
|
+
// Preserve handler metadata for formats that cannot be reconstructed from plain key/value content.
|
|
114
|
+
if (format === "yaml" || format === "yml" || format === "arb") {
|
|
115
115
|
return tempResult;
|
|
116
116
|
}
|
|
117
117
|
// Remove metadata for backward compatibility
|
|
@@ -32,7 +32,7 @@ export class FormatDetector {
|
|
|
32
32
|
// First try extension-based detection
|
|
33
33
|
const formatFromExtension = this.extensionMap.get(extension);
|
|
34
34
|
// For YAML files, prioritize extension-based detection to avoid confusion with properties
|
|
35
|
-
if (formatFromExtension ===
|
|
35
|
+
if (formatFromExtension === "yaml") {
|
|
36
36
|
return formatFromExtension;
|
|
37
37
|
}
|
|
38
38
|
if (formatFromExtension && !content) {
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ITranslate } from "./translate.interface.js";
|
|
2
|
+
export declare class HuggingFaceLocalTranslate implements ITranslate {
|
|
3
|
+
private model;
|
|
4
|
+
private pipelinePromise;
|
|
5
|
+
constructor(model: string);
|
|
6
|
+
isValidLocale(_targetLocale: string): boolean;
|
|
7
|
+
private getPipeline;
|
|
8
|
+
private sanitizeResult;
|
|
9
|
+
private translateWithTranslationPipeline;
|
|
10
|
+
translateText(text: string, sourceLocale: string, targetLocale: string, _context?: string): Promise<string>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { Util } from "./util.js";
|
|
2
|
+
const supportedLanguages = {
|
|
3
|
+
en: "English",
|
|
4
|
+
fr: "French",
|
|
5
|
+
de: "German",
|
|
6
|
+
es: "Spanish",
|
|
7
|
+
it: "Italian",
|
|
8
|
+
pt: "Portuguese",
|
|
9
|
+
nl: "Dutch",
|
|
10
|
+
pl: "Polish",
|
|
11
|
+
cs: "Czech",
|
|
12
|
+
sk: "Slovak",
|
|
13
|
+
sl: "Slovenian",
|
|
14
|
+
hr: "Croatian",
|
|
15
|
+
hu: "Hungarian",
|
|
16
|
+
ro: "Romanian",
|
|
17
|
+
bg: "Bulgarian",
|
|
18
|
+
el: "Greek",
|
|
19
|
+
ru: "Russian",
|
|
20
|
+
uk: "Ukrainian",
|
|
21
|
+
tr: "Turkish",
|
|
22
|
+
ar: "Arabic",
|
|
23
|
+
he: "Hebrew",
|
|
24
|
+
hi: "Hindi",
|
|
25
|
+
th: "Thai",
|
|
26
|
+
vi: "Vietnamese",
|
|
27
|
+
id: "Indonesian",
|
|
28
|
+
ms: "Malay",
|
|
29
|
+
ja: "Japanese",
|
|
30
|
+
ko: "Korean",
|
|
31
|
+
"zh-CN": "Chinese (Simplified)",
|
|
32
|
+
"zh-TW": "Chinese (Traditional)",
|
|
33
|
+
};
|
|
34
|
+
export class HuggingFaceLocalTranslate {
|
|
35
|
+
model;
|
|
36
|
+
pipelinePromise = null;
|
|
37
|
+
constructor(model) {
|
|
38
|
+
this.model = model;
|
|
39
|
+
}
|
|
40
|
+
isValidLocale(_targetLocale) {
|
|
41
|
+
// Local ONNX models support many locales; validation happens at runtime
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
async getPipeline() {
|
|
45
|
+
if (!this.pipelinePromise) {
|
|
46
|
+
// Dynamically import to avoid issues when the package is not installed
|
|
47
|
+
const { pipeline } = await import("@huggingface/transformers");
|
|
48
|
+
this.pipelinePromise = pipeline("translation", this.model);
|
|
49
|
+
}
|
|
50
|
+
return this.pipelinePromise;
|
|
51
|
+
}
|
|
52
|
+
sanitizeResult(result, args) {
|
|
53
|
+
return Util.replaceArgumentsWithNumbers(args, result.replace(/^\n+|\n+$/g, "").trim());
|
|
54
|
+
}
|
|
55
|
+
async translateWithTranslationPipeline(text, sourceLocale, targetLocale, args) {
|
|
56
|
+
const translator = await this.getPipeline();
|
|
57
|
+
const output = await translator(text, {
|
|
58
|
+
src_lang: sourceLocale,
|
|
59
|
+
tgt_lang: targetLocale,
|
|
60
|
+
});
|
|
61
|
+
const result = Array.isArray(output) && output.length > 0
|
|
62
|
+
? output[0].translation_text
|
|
63
|
+
: output.translation_text;
|
|
64
|
+
return this.sanitizeResult(result, args);
|
|
65
|
+
}
|
|
66
|
+
async translateText(text, sourceLocale, targetLocale, _context) {
|
|
67
|
+
let args;
|
|
68
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
69
|
+
return this.translateWithTranslationPipeline(text, sourceLocale, targetLocale, args);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type InferenceProviderOrPolicy } from "@huggingface/inference";
|
|
2
|
+
import type { ITranslate } from "./translate.interface.js";
|
|
3
|
+
export declare class HuggingFaceTranslate implements ITranslate {
|
|
4
|
+
private model;
|
|
5
|
+
private provider?;
|
|
6
|
+
private client;
|
|
7
|
+
constructor(apiKey: string, model: string, provider?: InferenceProviderOrPolicy | undefined);
|
|
8
|
+
isValidLocale(_targetLocale: string): boolean;
|
|
9
|
+
private getLanguageName;
|
|
10
|
+
private sanitizeResult;
|
|
11
|
+
translateText(text: string, sourceLocale: string, targetLocale: string, _context?: string): Promise<string>;
|
|
12
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { InferenceClient, } from "@huggingface/inference";
|
|
2
|
+
import { Util } from "./util.js";
|
|
3
|
+
const supportedLanguages = {
|
|
4
|
+
en: "English",
|
|
5
|
+
fr: "French",
|
|
6
|
+
de: "German",
|
|
7
|
+
es: "Spanish",
|
|
8
|
+
it: "Italian",
|
|
9
|
+
pt: "Portuguese",
|
|
10
|
+
nl: "Dutch",
|
|
11
|
+
pl: "Polish",
|
|
12
|
+
cs: "Czech",
|
|
13
|
+
sk: "Slovak",
|
|
14
|
+
sl: "Slovenian",
|
|
15
|
+
hr: "Croatian",
|
|
16
|
+
hu: "Hungarian",
|
|
17
|
+
ro: "Romanian",
|
|
18
|
+
bg: "Bulgarian",
|
|
19
|
+
el: "Greek",
|
|
20
|
+
ru: "Russian",
|
|
21
|
+
uk: "Ukrainian",
|
|
22
|
+
tr: "Turkish",
|
|
23
|
+
ar: "Arabic",
|
|
24
|
+
he: "Hebrew",
|
|
25
|
+
hi: "Hindi",
|
|
26
|
+
th: "Thai",
|
|
27
|
+
vi: "Vietnamese",
|
|
28
|
+
id: "Indonesian",
|
|
29
|
+
ms: "Malay",
|
|
30
|
+
ja: "Japanese",
|
|
31
|
+
ko: "Korean",
|
|
32
|
+
"zh-CN": "Chinese (Simplified)",
|
|
33
|
+
"zh-TW": "Chinese (Traditional)",
|
|
34
|
+
};
|
|
35
|
+
export class HuggingFaceTranslate {
|
|
36
|
+
model;
|
|
37
|
+
provider;
|
|
38
|
+
client;
|
|
39
|
+
constructor(apiKey, model, provider) {
|
|
40
|
+
this.model = model;
|
|
41
|
+
this.provider = provider;
|
|
42
|
+
this.client = new InferenceClient(apiKey);
|
|
43
|
+
}
|
|
44
|
+
isValidLocale(_targetLocale) {
|
|
45
|
+
// HF translation models support many locales; validation happens at runtime
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
getLanguageName(locale) {
|
|
49
|
+
return supportedLanguages[locale] ?? locale;
|
|
50
|
+
}
|
|
51
|
+
sanitizeResult(result, args) {
|
|
52
|
+
const trimmedResult = result.replace(/^\n+|\n+$/g, "").trim();
|
|
53
|
+
return Util.replaceArgumentsWithNumbers(args, trimmedResult);
|
|
54
|
+
}
|
|
55
|
+
async translateText(text, sourceLocale, targetLocale, _context) {
|
|
56
|
+
let args;
|
|
57
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
58
|
+
const response = await this.client.translation({
|
|
59
|
+
model: this.model,
|
|
60
|
+
provider: this.provider,
|
|
61
|
+
inputs: text,
|
|
62
|
+
parameters: {
|
|
63
|
+
src_lang: sourceLocale,
|
|
64
|
+
tgt_lang: targetLocale,
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
let result;
|
|
68
|
+
if (Array.isArray(response)) {
|
|
69
|
+
result = response[0].translation_text;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
result = response.translation_text;
|
|
73
|
+
}
|
|
74
|
+
return this.sanitizeResult(result, args);
|
|
75
|
+
}
|
|
76
|
+
}
|