auto-translate-json-library 1.5.5 → 2.0.2
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 +1141 -148
- package/build/package.json +96 -22
- package/build/src/aws.d.ts +1 -1
- package/build/src/aws.js +27 -38
- package/build/src/azure.d.ts +1 -1
- package/build/src/azure.js +50 -64
- package/build/src/bin/cli.d.ts +1 -1
- package/build/src/bin/cli.js +196 -37
- package/build/src/config.d.ts +1 -0
- package/build/src/config.js +1 -2
- package/build/src/deepl.d.ts +1 -1
- package/build/src/deepl.js +18 -64
- package/build/src/files.d.ts +11 -2
- package/build/src/files.js +356 -69
- package/build/src/folderFiles.d.ts +6 -3
- package/build/src/folderFiles.js +33 -44
- package/build/src/format/arb-handler.d.ts +33 -0
- package/build/src/format/arb-handler.js +679 -0
- package/build/src/format/base-format-handler.d.ts +75 -0
- package/build/src/format/base-format-handler.js +166 -0
- package/build/src/format/csv-handler.d.ts +38 -0
- package/build/src/format/csv-handler.js +604 -0
- package/build/src/format/index.d.ts +17 -0
- package/build/src/format/index.js +48 -0
- package/build/src/format/json-handler.d.ts +17 -0
- package/build/src/format/json-handler.js +294 -0
- package/build/src/format/po-handler.d.ts +17 -0
- package/build/src/format/po-handler.js +338 -0
- package/build/src/format/po-utils.d.ts +86 -0
- package/build/src/format/po-utils.js +385 -0
- package/build/src/format/pot-handler.d.ts +22 -0
- package/build/src/format/pot-handler.js +331 -0
- package/build/src/format/properties-handler.d.ts +26 -0
- package/build/src/format/properties-handler.js +500 -0
- package/build/src/format/tsv-handler.d.ts +13 -0
- package/build/src/format/tsv-handler.js +67 -0
- package/build/src/format/xliff-handler.d.ts +24 -0
- package/build/src/format/xliff-handler.js +433 -0
- package/build/src/format/xmb-handler.d.ts +33 -0
- package/build/src/format/xmb-handler.js +484 -0
- package/build/src/format/xmb-xtb-utils.d.ts +43 -0
- package/build/src/format/xmb-xtb-utils.js +180 -0
- package/build/src/format/xml-handler.d.ts +28 -0
- package/build/src/format/xml-handler.js +561 -0
- package/build/src/format/xtb-handler.d.ts +33 -0
- package/build/src/format/xtb-handler.js +518 -0
- package/build/src/format/yaml-handler.d.ts +26 -0
- package/build/src/format/yaml-handler.js +522 -0
- package/build/src/format-detector.d.ts +7 -0
- package/build/src/format-detector.js +58 -0
- package/build/src/format-handler-factory.d.ts +9 -0
- package/build/src/format-handler-factory.js +18 -0
- package/build/src/format.interface.d.ts +56 -0
- package/build/src/format.interface.js +1 -0
- package/build/src/google.d.ts +1 -1
- package/build/src/google.js +32 -49
- package/build/src/index.d.ts +4 -3
- package/build/src/index.js +15 -17
- package/build/src/lib.d.ts +1 -1
- package/build/src/lib.js +188 -133
- package/build/src/openai.d.ts +2 -2
- package/build/src/openai.js +59 -50
- package/build/src/translate.interface.d.ts +1 -1
- package/build/src/translate.interface.js +1 -2
- package/build/src/util.js +4 -9
- package/build/src/validation/enhanced-validation-result.d.ts +96 -0
- package/build/src/validation/enhanced-validation-result.js +198 -0
- package/build/src/validation/error-messages.d.ts +88 -0
- package/build/src/validation/error-messages.js +436 -0
- package/build/src/validation/error-recovery.d.ts +78 -0
- package/build/src/validation/error-recovery.js +593 -0
- package/build/src/validation/example-usage.d.ts +31 -0
- package/build/src/validation/example-usage.js +148 -0
- package/build/src/validation/format-rules.d.ts +37 -0
- package/build/src/validation/format-rules.js +550 -0
- package/build/src/validation/format-validator.d.ts +70 -0
- package/build/src/validation/format-validator.js +279 -0
- package/build/src/validation/index.d.ts +13 -0
- package/build/src/validation/index.js +30 -0
- package/build/src/validation/validation-service.d.ts +56 -0
- package/build/src/validation/validation-service.js +262 -0
- package/build/tests/arb-handler.test.d.ts +1 -0
- package/build/tests/arb-handler.test.js +694 -0
- package/build/tests/cli-integration.test.d.ts +1 -0
- package/build/tests/cli-integration.test.js +352 -0
- package/build/tests/csv-handler.test.d.ts +1 -0
- package/build/tests/csv-handler.test.js +507 -0
- package/build/tests/error-handling.test.d.ts +1 -0
- package/build/tests/error-handling.test.js +456 -0
- package/build/tests/files-integration.test.d.ts +1 -0
- package/build/tests/files-integration.test.js +421 -0
- package/build/tests/json-handler.test.d.ts +1 -0
- package/build/tests/json-handler.test.js +526 -0
- package/build/tests/po-handler.test.d.ts +1 -0
- package/build/tests/po-handler.test.js +768 -0
- package/build/tests/properties-handler.test.d.ts +1 -0
- package/build/tests/properties-handler.test.js +316 -0
- package/build/tests/setup.d.ts +1 -0
- package/build/tests/setup.js +5 -0
- package/build/tests/xliff-handler.test.d.ts +1 -0
- package/build/tests/xliff-handler.test.js +459 -0
- package/build/tests/xmb-xtb-handler.test.d.ts +1 -0
- package/build/tests/xmb-xtb-handler.test.js +758 -0
- package/build/tests/xml-handler.test.d.ts +1 -0
- package/build/tests/xml-handler.test.js +570 -0
- package/build/tests/yaml-handler.test.d.ts +1 -0
- package/build/tests/yaml-handler.test.js +156 -0
- package/package.json +96 -22
package/build/src/bin/cli.js
CHANGED
|
@@ -1,52 +1,167 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
2
|
+
import { existsSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
import minimist from "minimist";
|
|
5
|
+
import c from "picocolors";
|
|
6
|
+
import packageJson from "../../package.json" with { type: "json" };
|
|
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
|
+
import { config as dotenvConfig } from "dotenv";
|
|
13
|
+
dotenvConfig();
|
|
14
14
|
// Define a function to display the help message
|
|
15
15
|
function displayHelp() {
|
|
16
|
-
console.log(`🔨 Auto translate json cli v${
|
|
16
|
+
console.log(`🔨 Auto translate json/xml cli v${version}`);
|
|
17
17
|
console.log("Usage: atj [options] <inputPath>");
|
|
18
18
|
console.log("");
|
|
19
19
|
console.log("Options:");
|
|
20
20
|
console.log(" --help, -h Display this help message");
|
|
21
|
+
console.log(" --list-formats List all supported file formats");
|
|
21
22
|
console.log(" --mode, -m <mode> Specify the translation mode:file or folder");
|
|
22
23
|
console.log(" --engine, -e <engine> Specify the translation engine:aws,azure,google,deepLPro,deepLFree or openai");
|
|
23
24
|
console.log(" --sourceLocale, -s <locale> Specify the source locale");
|
|
25
|
+
console.log(" --format, -f <format> Manually specify the file format (overrides auto-detection)");
|
|
26
|
+
console.log(" Note: inputPath can be a translation file in any supported format, or a folder structure");
|
|
24
27
|
console.log(" --keepTranslations, --no-keepTranslations Keep or retranslate existing translations");
|
|
25
28
|
console.log(" --keepExtraTranslations, --no-keepExtraTranslations Keep or remove extra translations");
|
|
26
29
|
console.log("");
|
|
30
|
+
console.log("Supported formats:");
|
|
31
|
+
const formats = FormatHandlerFactory.getSupportedFormats();
|
|
32
|
+
const formatGroups = {
|
|
33
|
+
"JSON-based": ["json", "arb"],
|
|
34
|
+
"XML-based": [
|
|
35
|
+
"xml",
|
|
36
|
+
"android-xml",
|
|
37
|
+
"ios-xml",
|
|
38
|
+
"generic-xml",
|
|
39
|
+
"xliff",
|
|
40
|
+
"xmb",
|
|
41
|
+
"xtb",
|
|
42
|
+
],
|
|
43
|
+
"Text-based": ["po", "pot", "yaml", "properties"],
|
|
44
|
+
Tabular: ["csv", "tsv"],
|
|
45
|
+
};
|
|
46
|
+
for (const [group, groupFormats] of Object.entries(formatGroups)) {
|
|
47
|
+
const availableFormats = groupFormats.filter((f) => formats.includes(f));
|
|
48
|
+
if (availableFormats.length > 0) {
|
|
49
|
+
console.log(` ${group}: ${availableFormats.join(", ")}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
console.log("");
|
|
53
|
+
console.log("Format examples:");
|
|
54
|
+
console.log(" JSON: translations.json, app.arb (Flutter)");
|
|
55
|
+
console.log(" XML: strings.xml (Android), messages.xlf (XLIFF), template.xmb (Google)");
|
|
56
|
+
console.log(" Text: messages.po (gettext), config.yaml, app.properties (Java)");
|
|
57
|
+
console.log(" Tabular: translations.csv, data.tsv");
|
|
58
|
+
console.log("");
|
|
59
|
+
console.log("Usage examples:");
|
|
60
|
+
console.log(" atj translations.json # Auto-detect JSON format");
|
|
61
|
+
console.log(" atj --format xliff messages.xlf # Force XLIFF format");
|
|
62
|
+
console.log(" atj --mode folder locales/ # Process entire folder");
|
|
63
|
+
console.log(" atj --engine google --format po msgs.po # Use Google Translate with PO files");
|
|
64
|
+
console.log("");
|
|
27
65
|
console.log("Default values");
|
|
28
66
|
console.log(" --mode, -m <mode> file");
|
|
29
67
|
console.log(" --engine, -e <engine> aws");
|
|
30
68
|
console.log(" --sourceLocale, -s <locale> en");
|
|
69
|
+
console.log(" --format, -f <format> auto-detect");
|
|
31
70
|
console.log(" --keepTranslations, --no-keepTranslations --keepTranslations");
|
|
32
71
|
console.log(" --keepExtraTranslations, --no-keepExtraTranslations --no-keepExtraTranslations");
|
|
33
72
|
}
|
|
73
|
+
// Define a function to list supported formats
|
|
74
|
+
function listFormats() {
|
|
75
|
+
console.log(`🔨 Auto translate json/xml cli v${version}`);
|
|
76
|
+
console.log("");
|
|
77
|
+
console.log("Supported file formats:");
|
|
78
|
+
console.log("");
|
|
79
|
+
const formats = FormatHandlerFactory.getSupportedFormats();
|
|
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()) {
|
|
122
|
+
const details = formatDetails[format];
|
|
123
|
+
if (details) {
|
|
124
|
+
console.log(` ${format.padEnd(15)} ${details.extensions.join(", ").padEnd(20)} ${details.description}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
console.log("");
|
|
128
|
+
console.log("Usage examples:");
|
|
129
|
+
console.log(" atj --format json translations.json");
|
|
130
|
+
console.log(" atj --format xliff messages.xlf");
|
|
131
|
+
console.log(" atj --format po locales/messages.po");
|
|
132
|
+
console.log(" atj --format yaml config/translations.yaml");
|
|
133
|
+
console.log(" atj --format arb --engine google lib/l10n/app_en.arb");
|
|
134
|
+
console.log(" atj --format android-xml --sourceLocale en res/values/strings.xml");
|
|
135
|
+
console.log("");
|
|
136
|
+
console.log("Best practices:");
|
|
137
|
+
console.log(" • Format is usually auto-detected from file extension");
|
|
138
|
+
console.log(" • Use --format to override auto-detection when needed");
|
|
139
|
+
console.log(" • For XML files, specify the exact variant (android-xml, ios-xml, xliff)");
|
|
140
|
+
console.log(" • Use --keepTranslations to preserve existing translations");
|
|
141
|
+
console.log(" • Test with a single file before processing entire folders");
|
|
142
|
+
}
|
|
34
143
|
const arguments_ = process.argv.slice(2);
|
|
35
144
|
if (arguments_.length === 0) {
|
|
36
145
|
displayHelp();
|
|
37
146
|
process.exit(0);
|
|
38
147
|
}
|
|
39
|
-
const flags = (
|
|
148
|
+
const flags = minimist(arguments_, {
|
|
40
149
|
alias: {
|
|
41
150
|
mode: ["m"],
|
|
42
151
|
engine: ["e"],
|
|
43
152
|
keepTranslations: ["kt"],
|
|
44
153
|
keepExtraTranslations: ["ket"],
|
|
45
154
|
sourceLocale: ["s"],
|
|
155
|
+
format: ["f"],
|
|
46
156
|
help: ["h"],
|
|
47
157
|
},
|
|
48
|
-
string: ["mode", "engine", "sourceLocale"],
|
|
49
|
-
boolean: [
|
|
158
|
+
string: ["mode", "engine", "sourceLocale", "format"],
|
|
159
|
+
boolean: [
|
|
160
|
+
"keepTranslations",
|
|
161
|
+
"keepExtraTranslations",
|
|
162
|
+
"help",
|
|
163
|
+
"list-formats",
|
|
164
|
+
],
|
|
50
165
|
default: {
|
|
51
166
|
engine: "aws",
|
|
52
167
|
sourceLocale: "en",
|
|
@@ -55,20 +170,61 @@ const flags = (0, minimist_1.default)(arguments_, {
|
|
|
55
170
|
mode: "file",
|
|
56
171
|
},
|
|
57
172
|
});
|
|
58
|
-
console.log(
|
|
173
|
+
console.log(c.green(`🔨 Auto translate json cli v${version}`));
|
|
59
174
|
if (flags.help) {
|
|
60
175
|
displayHelp();
|
|
61
176
|
process.exit(0);
|
|
62
177
|
}
|
|
178
|
+
if (flags["list-formats"]) {
|
|
179
|
+
listFormats();
|
|
180
|
+
process.exit(0);
|
|
181
|
+
}
|
|
63
182
|
const inputPath = flags._[0];
|
|
64
|
-
const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations } = flags;
|
|
183
|
+
const { mode, engine, sourceLocale, keepTranslations, keepExtraTranslations, format, } = flags;
|
|
184
|
+
// Validate input path
|
|
185
|
+
if (!inputPath) {
|
|
186
|
+
console.error(c.red("❌ Input path is required"));
|
|
187
|
+
console.error(c.yellow("💡 Usage: atj [options] <inputPath>"));
|
|
188
|
+
process.exit(1);
|
|
189
|
+
}
|
|
190
|
+
// Validate format if specified
|
|
191
|
+
if (format && !FormatHandlerFactory.hasHandler(format)) {
|
|
192
|
+
console.error(c.red(`❌ Unsupported format: ${format}`));
|
|
193
|
+
console.error(c.yellow("💡 Use --list-formats to see all supported formats"));
|
|
194
|
+
process.exit(1);
|
|
195
|
+
}
|
|
196
|
+
// Validate mode
|
|
197
|
+
if (mode && !["file", "folder"].includes(mode)) {
|
|
198
|
+
console.error(c.red(`❌ Invalid mode: ${mode}`));
|
|
199
|
+
console.error(c.yellow("💡 Mode must be either 'file' or 'folder'"));
|
|
200
|
+
process.exit(1);
|
|
201
|
+
}
|
|
202
|
+
// Validate engine
|
|
203
|
+
const validEngines = [
|
|
204
|
+
"aws",
|
|
205
|
+
"azure",
|
|
206
|
+
"google",
|
|
207
|
+
"deepLPro",
|
|
208
|
+
"deepLFree",
|
|
209
|
+
"openai",
|
|
210
|
+
];
|
|
211
|
+
if (engine && !validEngines.includes(engine)) {
|
|
212
|
+
console.error(c.red(`❌ Invalid engine: ${engine}`));
|
|
213
|
+
console.error(c.yellow(`💡 Supported engines: ${validEngines.join(", ")}`));
|
|
214
|
+
process.exit(1);
|
|
215
|
+
}
|
|
216
|
+
// Check if input path exists
|
|
217
|
+
if (!existsSync(inputPath)) {
|
|
218
|
+
console.error(c.red(`❌ Input path does not exist: ${inputPath}`));
|
|
219
|
+
process.exit(1);
|
|
220
|
+
}
|
|
65
221
|
const config = {};
|
|
66
222
|
// set translate engine config
|
|
67
223
|
switch (engine) {
|
|
68
224
|
case "google": {
|
|
69
225
|
const googleApiKey = process.env.ATJ_GOOGLE_API_KEY;
|
|
70
226
|
if (!googleApiKey) {
|
|
71
|
-
console.error(
|
|
227
|
+
console.error(c.red("❌ Google api key not found in environment variable ATJ_GOOGLE_API_KEY"));
|
|
72
228
|
process.exit(1);
|
|
73
229
|
}
|
|
74
230
|
config.translationKeyInfo = {
|
|
@@ -82,7 +238,7 @@ switch (engine) {
|
|
|
82
238
|
const awsSecretAccessKey = process.env.ATJ_AWS_SECRET_ACCESS_KEY;
|
|
83
239
|
const awsRegion = process.env.ATJ_AWS_REGION;
|
|
84
240
|
if (!awsAccessKeyId || !awsSecretAccessKey || !awsRegion) {
|
|
85
|
-
console.error(
|
|
241
|
+
console.error(c.red("❌ AWS credentials not found in environment variables ATJ_AWS_ACCESS_KEY_ID, ATJ_AWS_SECRET_ACCESS_KEY, ATJ_AWS_REGION"));
|
|
86
242
|
process.exit(1);
|
|
87
243
|
}
|
|
88
244
|
config.translationKeyInfo = {
|
|
@@ -97,7 +253,7 @@ switch (engine) {
|
|
|
97
253
|
const azureSecretKey = process.env.ATJ_AZURE_SECRET_KEY;
|
|
98
254
|
const azureRegion = process.env.ATJ_AZURE_REGION;
|
|
99
255
|
if (!azureSecretKey || !azureRegion) {
|
|
100
|
-
console.error(
|
|
256
|
+
console.error(c.red("❌ Azure credentials not found in environment variables ATJ_AZURE_SECRET_KEY, ATJ_AZURE_REGION"));
|
|
101
257
|
process.exit(1);
|
|
102
258
|
}
|
|
103
259
|
config.translationKeyInfo = {
|
|
@@ -110,27 +266,27 @@ switch (engine) {
|
|
|
110
266
|
case "openai": {
|
|
111
267
|
const openaiApiKey = process.env.ATJ_OPEN_AI_SECRET_KEY;
|
|
112
268
|
if (!openaiApiKey) {
|
|
113
|
-
console.error(
|
|
269
|
+
console.error(c.red("❌ Openai api key not found in environment variable ATJ_OPEN_AI_SECRET_KEY"));
|
|
114
270
|
process.exit(1);
|
|
115
271
|
}
|
|
116
272
|
config.translationKeyInfo = {
|
|
117
273
|
kind: "openai",
|
|
118
274
|
apiKey: openaiApiKey,
|
|
119
|
-
baseUrl:
|
|
120
|
-
model:
|
|
121
|
-
maxTokens: Number(
|
|
122
|
-
temperature: Number(
|
|
123
|
-
topP: Number(
|
|
124
|
-
n: Number(
|
|
125
|
-
frequencyPenalty: Number(
|
|
126
|
-
presencePenalty: Number(
|
|
275
|
+
baseUrl: process.env.ATJ_OPEN_AI_BASE_URL ?? "https://api.openai.com/v1",
|
|
276
|
+
model: process.env.ATJ_OPEN_AI_MODEL ?? "gpt-3.5-turbo",
|
|
277
|
+
maxTokens: Number(process.env.ATJ_OPEN_AI_MAX_TOKENS ?? "256"),
|
|
278
|
+
temperature: Number(process.env.ATJ_OPEN_AI_TEMPERATURE ?? "0.7"),
|
|
279
|
+
topP: Number(process.env.ATJ_OPEN_AI_TOP_P ?? "0.9"),
|
|
280
|
+
n: Number(process.env.ATJ_OPEN_AI_N ?? "1"),
|
|
281
|
+
frequencyPenalty: Number(process.env.ATJ_OPEN_AI_FREQUENCY_PENALTY ?? "0"),
|
|
282
|
+
presencePenalty: Number(process.env.ATJ_OPEN_AI_PRESENCE_PENALTY ?? "0"),
|
|
127
283
|
};
|
|
128
284
|
break;
|
|
129
285
|
}
|
|
130
286
|
case "deepLPro": {
|
|
131
287
|
const deepLProSecretKey = process.env.ATJ_DEEPL_PRO_SECRET_KEY;
|
|
132
288
|
if (!deepLProSecretKey) {
|
|
133
|
-
console.log(
|
|
289
|
+
console.log(c.red("❌ DeepL pro api key not found in environment variable ATJ_DEEPL_PRO_SECRET_KEY"));
|
|
134
290
|
process.exit(1);
|
|
135
291
|
}
|
|
136
292
|
config.translationKeyInfo = {
|
|
@@ -142,7 +298,7 @@ switch (engine) {
|
|
|
142
298
|
case "deepLFree": {
|
|
143
299
|
const deepLFreeSecretKey = process.env.ATJ_DEEPL_FREE_SECRET_KEY;
|
|
144
300
|
if (!deepLFreeSecretKey) {
|
|
145
|
-
console.error(
|
|
301
|
+
console.error(c.red("❌ DeepL free api key not found in environment variable ATJ_DEEPL_FREE_SECRET_KEY"));
|
|
146
302
|
process.exit(1);
|
|
147
303
|
}
|
|
148
304
|
config.translationKeyInfo = {
|
|
@@ -152,7 +308,7 @@ switch (engine) {
|
|
|
152
308
|
break;
|
|
153
309
|
}
|
|
154
310
|
default:
|
|
155
|
-
console.error(
|
|
311
|
+
console.error(c.red(`❌ Engine ${engine} not supported`));
|
|
156
312
|
process.exit(1);
|
|
157
313
|
}
|
|
158
314
|
if (process.env.ATJ_START_DELIMITER) {
|
|
@@ -165,9 +321,12 @@ config.sourceLocale = sourceLocale;
|
|
|
165
321
|
config.keepTranslations = keepTranslations ? "keep" : "retranslate";
|
|
166
322
|
config.keepExtraTranslations = keepExtraTranslations ? "keep" : "remove";
|
|
167
323
|
config.mode = mode;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
324
|
+
if (format) {
|
|
325
|
+
config.format = format;
|
|
326
|
+
}
|
|
327
|
+
const sourcePath = path.join(process.cwd(), inputPath);
|
|
328
|
+
console.log(c.green(`🌐 Translating ${sourcePath}`));
|
|
329
|
+
translate(sourcePath, config).catch((error) => {
|
|
330
|
+
console.error(c.red("❌ Translate error:\n"), error);
|
|
172
331
|
process.exit(1);
|
|
173
332
|
});
|
package/build/src/config.d.ts
CHANGED
package/build/src/config.js
CHANGED
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
1
|
+
export {};
|
package/build/src/deepl.d.ts
CHANGED
package/build/src/deepl.js
CHANGED
|
@@ -1,50 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
|
-
exports.DeepLTranslate = void 0;
|
|
46
|
-
const deepl = __importStar(require("deepl-node"));
|
|
47
|
-
const util_1 = require("./util");
|
|
1
|
+
import * as deepl from "deepl-node";
|
|
2
|
+
import { Util } from "./util.js";
|
|
48
3
|
const supportedLanguages = [
|
|
49
4
|
"AR",
|
|
50
5
|
"BG",
|
|
@@ -82,11 +37,13 @@ const supportedLanguages = [
|
|
|
82
37
|
"UK",
|
|
83
38
|
"ZH",
|
|
84
39
|
];
|
|
85
|
-
class DeepLTranslate {
|
|
40
|
+
export class DeepLTranslate {
|
|
41
|
+
subscriptionKey;
|
|
42
|
+
type;
|
|
43
|
+
endpoint = "https://api.deepl.com";
|
|
86
44
|
constructor(subscriptionKey, type) {
|
|
87
45
|
this.subscriptionKey = subscriptionKey;
|
|
88
46
|
this.type = type;
|
|
89
|
-
this.endpoint = "https://api.deepl.com";
|
|
90
47
|
if (this.type === "free") {
|
|
91
48
|
this.endpoint = "https://api-free.deepl.com";
|
|
92
49
|
}
|
|
@@ -94,22 +51,19 @@ class DeepLTranslate {
|
|
|
94
51
|
isValidLocale(targetLocale) {
|
|
95
52
|
return supportedLanguages.includes(targetLocale.toUpperCase());
|
|
96
53
|
}
|
|
97
|
-
translateText(text, sourceLocale, targetLocale) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
maxRetries: 5,
|
|
106
|
-
});
|
|
107
|
-
const translation = yield translator.translateText(text, sourceLocale, targetLocale);
|
|
108
|
-
result = translation.text;
|
|
109
|
-
result = util_1.Util.replaceArgumentsWithNumbers(args, result);
|
|
110
|
-
return result;
|
|
54
|
+
async translateText(text, sourceLocale, targetLocale) {
|
|
55
|
+
let args;
|
|
56
|
+
({ args, text } = Util.replaceContextVariables(text));
|
|
57
|
+
let result = "";
|
|
58
|
+
const translator = new deepl.Translator(this.subscriptionKey, {
|
|
59
|
+
serverUrl: this.endpoint,
|
|
60
|
+
minTimeout: 10000,
|
|
61
|
+
maxRetries: 5,
|
|
111
62
|
});
|
|
63
|
+
const translation = await translator.translateText(text, sourceLocale, targetLocale);
|
|
64
|
+
result = translation.text;
|
|
65
|
+
result = Util.replaceArgumentsWithNumbers(args, result);
|
|
66
|
+
return result;
|
|
112
67
|
}
|
|
113
68
|
}
|
|
114
|
-
exports.DeepLTranslate = DeepLTranslate;
|
|
115
69
|
// https://www.deepl.com/docs-api/translating-text/example/
|
package/build/src/files.d.ts
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import type { TranslationFile } from "./translate.interface";
|
|
1
|
+
import type { TranslationFile } from "./translate.interface.js";
|
|
2
|
+
import "./format/index.js";
|
|
2
3
|
export interface IFiles {
|
|
3
4
|
sourceLocale: string;
|
|
4
5
|
targetLocales: Array<string>;
|
|
5
6
|
loadJsonFromLocale(locale: string): Promise<TranslationFile>;
|
|
6
7
|
saveJsonToLocale(locale: string, file: TranslationFile): void;
|
|
8
|
+
getDetectedFormat?(): string | undefined;
|
|
9
|
+
getFormatOverride?(): string | undefined;
|
|
7
10
|
}
|
|
8
11
|
export declare const readFileAsync: (filename: string) => Promise<string>;
|
|
9
12
|
export declare const loadJsonFromLocale: (fileName: string) => Promise<TranslationFile>;
|
|
@@ -12,9 +15,15 @@ export declare class Files implements IFiles {
|
|
|
12
15
|
folderPath: string;
|
|
13
16
|
sourceLocale: string;
|
|
14
17
|
targetLocales: Array<string>;
|
|
15
|
-
|
|
18
|
+
private fileExt;
|
|
19
|
+
private formatOverride?;
|
|
20
|
+
private detectedFormat?;
|
|
21
|
+
constructor(filePath: string, formatOverride?: string);
|
|
16
22
|
private getLocaleFromFilename;
|
|
23
|
+
private detectFileFormat;
|
|
17
24
|
private getTargetLocales;
|
|
25
|
+
getDetectedFormat(): string | undefined;
|
|
26
|
+
getFormatOverride(): string | undefined;
|
|
18
27
|
loadJsonFromLocale(locale: string): Promise<TranslationFile>;
|
|
19
28
|
saveJsonToLocale(locale: string, file: TranslationFile): void;
|
|
20
29
|
}
|