auto-translate-json-library 2.2.0 → 2.3.1
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/build/package.json +23 -23
- package/build/src/bin/cli.js +0 -0
- package/build/src/debug.d.ts +1 -0
- package/build/src/debug.js +170 -0
- package/build/src/format/json-handler.d.ts +15 -0
- package/build/src/format/json-handler.js +78 -3
- package/build/src/format/yaml-handler.d.ts +7 -0
- package/build/src/format/yaml-handler.js +67 -3
- package/build/src/openai.js +0 -13
- package/package.json +23 -23
package/build/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Automatically translates JSON language files to other languages using Google Translate, AWS, Azure, DeepL, OpenAI, Hugging Face (cloud & local) or local OpenAI compatible server",
|
|
6
6
|
"main": "./build/src/index.js",
|
|
7
7
|
"bin": {
|
|
8
|
-
"auto-translate-json-library": "build/src/bin/cli.js",
|
|
9
|
-
"atj": "build/src/bin/cli.js"
|
|
8
|
+
"auto-translate-json-library": "./build/src/bin/cli.js",
|
|
9
|
+
"atj": "./build/src/bin/cli.js"
|
|
10
10
|
},
|
|
11
11
|
"types": "./build/src/index.d.ts",
|
|
12
12
|
"files": [
|
|
@@ -128,40 +128,40 @@
|
|
|
128
128
|
],
|
|
129
129
|
"license": "MIT",
|
|
130
130
|
"devDependencies": {
|
|
131
|
-
"@biomejs/biome": "2.
|
|
132
|
-
"@topce/native-preview": "7.0.
|
|
131
|
+
"@biomejs/biome": "2.5.6",
|
|
132
|
+
"@topce/native-preview": "7.0.3",
|
|
133
133
|
"@types/gettext-parser": "9.0.0",
|
|
134
134
|
"@types/jest": "30.0.0",
|
|
135
135
|
"@types/minimist": "1.2.5",
|
|
136
|
-
"@types/node": "
|
|
136
|
+
"@types/node": "26.1.2",
|
|
137
137
|
"@types/properties-parser": "0.3.3",
|
|
138
138
|
"@types/yargs": "17.0.35",
|
|
139
|
-
"jest": "30.
|
|
140
|
-
"ts-jest": "29.4.
|
|
139
|
+
"jest": "30.4.2",
|
|
140
|
+
"ts-jest": "29.4.12",
|
|
141
141
|
"ts-node": "10.9.2",
|
|
142
142
|
"typescript": "6.0.3"
|
|
143
143
|
},
|
|
144
144
|
"dependencies": {
|
|
145
|
-
"@aws-sdk/client-translate": "3.
|
|
146
|
-
"@google-cloud/translate": "9.
|
|
147
|
-
"@huggingface/inference": "^4.13.
|
|
148
|
-
"@huggingface/transformers": "^4.
|
|
145
|
+
"@aws-sdk/client-translate": "3.1100.0",
|
|
146
|
+
"@google-cloud/translate": "9.4.2",
|
|
147
|
+
"@huggingface/inference": "^4.13.24",
|
|
148
|
+
"@huggingface/transformers": "^4.2.0",
|
|
149
149
|
"@types/js-yaml": "4.0.9",
|
|
150
|
-
"axios": "1.
|
|
151
|
-
"csv-parser": "3.2.
|
|
150
|
+
"axios": "1.19.0",
|
|
151
|
+
"csv-parser": "3.2.1",
|
|
152
152
|
"csv-writer": "1.6.0",
|
|
153
|
-
"deepl-node": "1.
|
|
153
|
+
"deepl-node": "1.27.0",
|
|
154
154
|
"dotenv": "17.4.2",
|
|
155
|
-
"fast-xml-parser": "5.
|
|
156
|
-
"form-data": "4.0.
|
|
157
|
-
"gettext-parser": "9.
|
|
158
|
-
"js-yaml": "4.
|
|
155
|
+
"fast-xml-parser": "5.10.1",
|
|
156
|
+
"form-data": "4.0.6",
|
|
157
|
+
"gettext-parser": "9.1.1",
|
|
158
|
+
"js-yaml": "4.3.1",
|
|
159
159
|
"minimist": "1.2.8",
|
|
160
|
-
"openai": "
|
|
160
|
+
"openai": "7.3.0",
|
|
161
161
|
"picocolors": "1.1.1",
|
|
162
162
|
"properties-parser": "0.6.0",
|
|
163
|
-
"sharp": "^0.
|
|
164
|
-
"uuid": "
|
|
165
|
-
"yargs": "18.
|
|
163
|
+
"sharp": "^0.35.3",
|
|
164
|
+
"uuid": "14.0.1",
|
|
165
|
+
"yargs": "18.1.0"
|
|
166
166
|
}
|
|
167
167
|
}
|
package/build/src/bin/cli.js
CHANGED
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import { config as dotenvConfig } from "dotenv";
|
|
2
|
+
import minimist from "minimist";
|
|
3
|
+
import { translate } from "./lib.js";
|
|
4
|
+
const config = {};
|
|
5
|
+
setDefaultConfiguration();
|
|
6
|
+
console.log(config);
|
|
7
|
+
try {
|
|
8
|
+
setConfigurationFromEnvironment();
|
|
9
|
+
}
|
|
10
|
+
catch (error) {
|
|
11
|
+
console.log(config);
|
|
12
|
+
console.log(error);
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
setConfigurationFromCLI();
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
console.log(config);
|
|
19
|
+
console.log(error);
|
|
20
|
+
}
|
|
21
|
+
function setConfigurationFromCLI() {
|
|
22
|
+
const argv = minimist(process.argv.slice(2));
|
|
23
|
+
if (argv === undefined) {
|
|
24
|
+
console.log("No arguments provided in CLI");
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
if (argv.mode === "file") {
|
|
28
|
+
config.mode = "file";
|
|
29
|
+
}
|
|
30
|
+
else if (argv.mode === "folder") {
|
|
31
|
+
config.mode = "folder";
|
|
32
|
+
}
|
|
33
|
+
if (argv.startDelimiter !== undefined) {
|
|
34
|
+
config.startDelimiter = argv.startDelimiter;
|
|
35
|
+
}
|
|
36
|
+
if (argv.endDelimiter !== undefined) {
|
|
37
|
+
config.endDelimiter = argv.endDelimiter;
|
|
38
|
+
}
|
|
39
|
+
if (argv.keepExtraTranslations === "keep") {
|
|
40
|
+
config.keepExtraTranslations = "keep";
|
|
41
|
+
}
|
|
42
|
+
else if (argv.keepExtraTranslations === "remove") {
|
|
43
|
+
config.keepExtraTranslations = "remove";
|
|
44
|
+
}
|
|
45
|
+
if (argv.keepTranslations === "keep") {
|
|
46
|
+
config.keepTranslations = "keep";
|
|
47
|
+
}
|
|
48
|
+
else if (argv.keepTranslations === "retranslate") {
|
|
49
|
+
config.keepTranslations = "retranslate";
|
|
50
|
+
}
|
|
51
|
+
if (argv.sourceLocale !== undefined) {
|
|
52
|
+
config.sourceLocale = argv.sourceLocale;
|
|
53
|
+
}
|
|
54
|
+
if (argv.ignorePrefix !== undefined) {
|
|
55
|
+
config.ignorePrefix = argv.ignorePrefix;
|
|
56
|
+
}
|
|
57
|
+
if (argv.pivotTranslation !== undefined) {
|
|
58
|
+
translate(argv.pivotTranslation, config);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
console.log(argv);
|
|
62
|
+
console.log("Please provide a pivot translation");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
function setDefaultConfiguration() {
|
|
66
|
+
config.mode = "file";
|
|
67
|
+
config.startDelimiter = "{";
|
|
68
|
+
config.endDelimiter = "}";
|
|
69
|
+
config.keepExtraTranslations = "keep";
|
|
70
|
+
config.keepTranslations = "keep";
|
|
71
|
+
config.sourceLocale = "en";
|
|
72
|
+
config.ignorePrefix = "";
|
|
73
|
+
}
|
|
74
|
+
function setConfigurationFromEnvironment() {
|
|
75
|
+
dotenvConfig();
|
|
76
|
+
if (process.env.ATJ_GOOGLE_API_KEY) {
|
|
77
|
+
config.translationKeyInfo = {
|
|
78
|
+
kind: "google",
|
|
79
|
+
apiKey: process.env.ATJ_GOOGLE_API_KEY,
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
if (process.env.ATJ_AWS_ACCESS_KEY_ID &&
|
|
83
|
+
process.env.ATJ_AWS_SECRET_ACCESS_KEY &&
|
|
84
|
+
process.env.ATJ_AWS_REGION) {
|
|
85
|
+
config.translationKeyInfo = {
|
|
86
|
+
kind: "aws",
|
|
87
|
+
accessKeyId: process.env.ATJ_AWS_ACCESS_KEY_ID,
|
|
88
|
+
secretAccessKey: process.env.ATJ_AWS_SECRET_ACCESS_KEY,
|
|
89
|
+
region: process.env.ATJ_AWS_REGION,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
if (process.env.ATJ_AZURE_SECRET_KEY && process.env.ATJ_AZURE_REGION) {
|
|
93
|
+
config.translationKeyInfo = {
|
|
94
|
+
kind: "azure",
|
|
95
|
+
secretKey: process.env.ATJ_AZURE_SECRET_KEY,
|
|
96
|
+
region: process.env.ATJ_AZURE_REGION,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
if (process.env.ATJ_DEEPL_PRO_SECRET_KEY) {
|
|
100
|
+
config.translationKeyInfo = {
|
|
101
|
+
kind: "deepLPro",
|
|
102
|
+
secretKey: process.env.ATJ_DEEPL_PRO_SECRET_KEY,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
if (process.env.ATJ_DEEPL_FREE_SECRET_KEY) {
|
|
106
|
+
config.translationKeyInfo = {
|
|
107
|
+
kind: "deepLFree",
|
|
108
|
+
secretKey: process.env.ATJ_DEEPL_FREE_SECRET_KEY,
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (process.env.ATJ_OPEN_AI_SECRET_KEY) {
|
|
112
|
+
config.translationKeyInfo = {
|
|
113
|
+
kind: "openai",
|
|
114
|
+
apiKey: process.env.ATJ_OPEN_AI_SECRET_KEY,
|
|
115
|
+
baseUrl: process.env.ATJ_OPEN_AI_BASE_URL ?? "https://api.openai.com/v1",
|
|
116
|
+
model: process.env.ATJ_OPEN_AI_MODEL ?? "gpt-3.5-turbo",
|
|
117
|
+
maxTokens: Number(process.env.ATJ_OPEN_AI_MAX_TOKENS ?? "256"),
|
|
118
|
+
temperature: Number(process.env.ATJ_OPEN_AI_TEMPERATURE ?? "0.7"),
|
|
119
|
+
topP: Number(process.env.ATJ_OPEN_AI_TOP_P ?? "0.9"),
|
|
120
|
+
n: Number(process.env.ATJ_OPEN_AI_N ?? "1"),
|
|
121
|
+
frequencyPenalty: Number(process.env.ATJ_OPEN_AI_FREQUENCY_PENALTY ?? "0"),
|
|
122
|
+
presencePenalty: Number(process.env.ATJ_OPEN_AI_PRESENCE_PENALTY ?? "0"),
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
// Hugging Face cloud mode (requires API key)
|
|
126
|
+
if (process.env.ATJ_HUGGING_FACE_API_KEY) {
|
|
127
|
+
config.translationKeyInfo = {
|
|
128
|
+
kind: "huggingface",
|
|
129
|
+
apiKey: process.env.ATJ_HUGGING_FACE_API_KEY,
|
|
130
|
+
model: process.env.ATJ_HUGGING_FACE_MODEL ?? "Helsinki-NLP/opus-mt-en-fr",
|
|
131
|
+
provider: process.env.ATJ_HUGGING_FACE_PROVIDER,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
// Hugging Face local mode (no API key, ONNX on-device — takes priority)
|
|
135
|
+
if (process.env.ATJ_HUGGING_FACE_LOCAL_MODEL) {
|
|
136
|
+
config.translationKeyInfo = {
|
|
137
|
+
kind: "huggingface-local",
|
|
138
|
+
model: process.env.ATJ_HUGGING_FACE_LOCAL_MODEL,
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (process.env.ATJ_START_DELIMITER) {
|
|
142
|
+
config.startDelimiter = process.env.ATJ_START_DELIMITER;
|
|
143
|
+
}
|
|
144
|
+
if (process.env.ATJ_END_DELIMITER) {
|
|
145
|
+
config.endDelimiter = process.env.ATJ_END_DELIMITER;
|
|
146
|
+
}
|
|
147
|
+
if (process.env.ATJ_END_DELIMITER) {
|
|
148
|
+
config.endDelimiter = process.env.ATJ_END_DELIMITER;
|
|
149
|
+
}
|
|
150
|
+
if (process.env.ATJ_MODE &&
|
|
151
|
+
(process.env.ATJ_MODE === "file" || process.env.ATJ_MODE === "folder")) {
|
|
152
|
+
config.mode = process.env.ATJ_MODE;
|
|
153
|
+
}
|
|
154
|
+
if (process.env.ATJ_SOURCE_LOCALE) {
|
|
155
|
+
config.sourceLocale = process.env.ATJ_SOURCE_LOCALE;
|
|
156
|
+
}
|
|
157
|
+
if (process.env.ATJ_KEEP_TRANSLATIONS &&
|
|
158
|
+
(process.env.ATJ_KEEP_TRANSLATIONS === "keep" ||
|
|
159
|
+
process.env.ATJ_KEEP_TRANSLATIONS === "retranslate")) {
|
|
160
|
+
config.keepTranslations = process.env.ATJ_KEEP_TRANSLATIONS;
|
|
161
|
+
}
|
|
162
|
+
if (process.env.ATJ_KEEP_EXTRA_TRANSLATIONS &&
|
|
163
|
+
(process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === "keep" ||
|
|
164
|
+
process.env.ATJ_KEEP_EXTRA_TRANSLATIONS === "remove")) {
|
|
165
|
+
config.keepExtraTranslations = process.env.ATJ_KEEP_EXTRA_TRANSLATIONS;
|
|
166
|
+
}
|
|
167
|
+
if (process.env.ATJ_IGNORE_PREFIX && process.env.ATJ_IGNORE_PREFIX !== "") {
|
|
168
|
+
config.ignorePrefix = process.env.ATJ_IGNORE_PREFIX;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
@@ -10,6 +10,21 @@ export declare class JsonHandler implements IFormatHandler {
|
|
|
10
10
|
private reconstructNestedStructure;
|
|
11
11
|
private updateNestedValues;
|
|
12
12
|
private reconstructFromFlatStructure;
|
|
13
|
+
/**
|
|
14
|
+
* Returns the set of flat keys that cannot be safely expanded into a nested
|
|
15
|
+
* structure because another key is a dot-boundary ancestor of them. Both
|
|
16
|
+
* sides of every such collision are returned so callers can preserve them as
|
|
17
|
+
* literal flat keys instead of splitting on ".".
|
|
18
|
+
*
|
|
19
|
+
* Example: for `["a.b.c", "a.b.c.d"]`, `"a.b.c"` is an ancestor of
|
|
20
|
+
* `"a.b.c.d"`, so both are returned.
|
|
21
|
+
*/
|
|
22
|
+
private findConflictingKeys;
|
|
23
|
+
/**
|
|
24
|
+
* Sets a value at a dot/bracket path, creating intermediate objects/arrays as
|
|
25
|
+
* needed. Returns `false` (instead of throwing) when the path collides with an
|
|
26
|
+
* incompatible existing shape, so the caller can preserve the key literally.
|
|
27
|
+
*/
|
|
13
28
|
private setNestedValue;
|
|
14
29
|
private parsePath;
|
|
15
30
|
private validateTranslatableContent;
|
|
@@ -174,17 +174,72 @@ export class JsonHandler {
|
|
|
174
174
|
}
|
|
175
175
|
reconstructFromFlatStructure(flatData) {
|
|
176
176
|
const result = {};
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
const keys = Object.keys(flatData);
|
|
178
|
+
// Some flat keys contain literal dots and are ALSO dot-boundary prefixes of
|
|
179
|
+
// other flat keys (e.g. "a.b.c" and "a.b.c.d"). Such a set cannot be
|
|
180
|
+
// expressed as a nested tree: the shared path would have to be both a
|
|
181
|
+
// string leaf and an object at the same time. Splitting them blindly either
|
|
182
|
+
// crashes ("Cannot create property 'x' on string") or silently drops data.
|
|
183
|
+
// Keep the colliding keys as literal (flat) keys so the output is lossless.
|
|
184
|
+
const conflicting = this.findConflictingKeys(keys);
|
|
185
|
+
for (const key of keys) {
|
|
186
|
+
const value = flatData[key];
|
|
187
|
+
if (conflicting.has(key)) {
|
|
188
|
+
result[key] = value;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
// If nesting still hits an unexpected collision (e.g. a mixed
|
|
192
|
+
// array/object shape), fall back to a literal key rather than throwing
|
|
193
|
+
// or losing the value.
|
|
194
|
+
if (!this.setNestedValue(result, key, value)) {
|
|
195
|
+
result[key] = value;
|
|
196
|
+
}
|
|
179
197
|
}
|
|
180
198
|
return result;
|
|
181
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Returns the set of flat keys that cannot be safely expanded into a nested
|
|
202
|
+
* structure because another key is a dot-boundary ancestor of them. Both
|
|
203
|
+
* sides of every such collision are returned so callers can preserve them as
|
|
204
|
+
* literal flat keys instead of splitting on ".".
|
|
205
|
+
*
|
|
206
|
+
* Example: for `["a.b.c", "a.b.c.d"]`, `"a.b.c"` is an ancestor of
|
|
207
|
+
* `"a.b.c.d"`, so both are returned.
|
|
208
|
+
*/
|
|
209
|
+
findConflictingKeys(keys) {
|
|
210
|
+
const conflicting = new Set();
|
|
211
|
+
const keySet = new Set(keys);
|
|
212
|
+
for (const key of keys) {
|
|
213
|
+
const segments = key.split(".");
|
|
214
|
+
// Walk every dot-boundary ancestor path of this key. If an ancestor path
|
|
215
|
+
// is itself a full key, then that ancestor is a string leaf while this
|
|
216
|
+
// key needs it to be an object: both collide.
|
|
217
|
+
let prefix = "";
|
|
218
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
219
|
+
prefix = i === 0 ? segments[0] : `${prefix}.${segments[i]}`;
|
|
220
|
+
if (keySet.has(prefix)) {
|
|
221
|
+
conflicting.add(prefix);
|
|
222
|
+
conflicting.add(key);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return conflicting;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Sets a value at a dot/bracket path, creating intermediate objects/arrays as
|
|
230
|
+
* needed. Returns `false` (instead of throwing) when the path collides with an
|
|
231
|
+
* incompatible existing shape, so the caller can preserve the key literally.
|
|
232
|
+
*/
|
|
182
233
|
setNestedValue(obj, path, value) {
|
|
183
234
|
const keys = this.parsePath(path);
|
|
184
235
|
let current = obj;
|
|
185
236
|
for (let i = 0; i < keys.length - 1; i++) {
|
|
186
237
|
const key = keys[i];
|
|
187
238
|
if (key.isArray) {
|
|
239
|
+
if (current[key.name] !== undefined &&
|
|
240
|
+
!Array.isArray(current[key.name])) {
|
|
241
|
+
return false; // expected an array here, found something else
|
|
242
|
+
}
|
|
188
243
|
if (!Array.isArray(current[key.name])) {
|
|
189
244
|
current[key.name] = [];
|
|
190
245
|
}
|
|
@@ -192,9 +247,18 @@ export class JsonHandler {
|
|
|
192
247
|
while (current[key.name].length <= key.index) {
|
|
193
248
|
current[key.name].push({});
|
|
194
249
|
}
|
|
195
|
-
|
|
250
|
+
const next = current[key.name][key.index];
|
|
251
|
+
if (next === null || typeof next !== "object") {
|
|
252
|
+
return false; // slot already holds a primitive, cannot descend
|
|
253
|
+
}
|
|
254
|
+
current = next;
|
|
196
255
|
}
|
|
197
256
|
else {
|
|
257
|
+
const existing = current[key.name];
|
|
258
|
+
if (existing !== undefined &&
|
|
259
|
+
(typeof existing !== "object" || existing === null)) {
|
|
260
|
+
return false; // cannot descend into a primitive leaf
|
|
261
|
+
}
|
|
198
262
|
if (!(key.name in current)) {
|
|
199
263
|
current[key.name] = {};
|
|
200
264
|
}
|
|
@@ -203,14 +267,25 @@ export class JsonHandler {
|
|
|
203
267
|
}
|
|
204
268
|
const lastKey = keys[keys.length - 1];
|
|
205
269
|
if (lastKey.isArray) {
|
|
270
|
+
if (current[lastKey.name] !== undefined &&
|
|
271
|
+
!Array.isArray(current[lastKey.name])) {
|
|
272
|
+
return false;
|
|
273
|
+
}
|
|
206
274
|
if (!Array.isArray(current[lastKey.name])) {
|
|
207
275
|
current[lastKey.name] = [];
|
|
208
276
|
}
|
|
209
277
|
current[lastKey.name][lastKey.index] = value;
|
|
210
278
|
}
|
|
211
279
|
else {
|
|
280
|
+
const existing = current[lastKey.name];
|
|
281
|
+
if (existing !== undefined &&
|
|
282
|
+
typeof existing === "object" &&
|
|
283
|
+
existing !== null) {
|
|
284
|
+
return false; // would clobber an object/array with a scalar leaf
|
|
285
|
+
}
|
|
212
286
|
current[lastKey.name] = value;
|
|
213
287
|
}
|
|
288
|
+
return true;
|
|
214
289
|
}
|
|
215
290
|
parsePath(path) {
|
|
216
291
|
const parts = path.split(".");
|
|
@@ -15,6 +15,13 @@ export declare class YamlHandler implements IFormatHandler {
|
|
|
15
15
|
private updateNestedValues;
|
|
16
16
|
private updateArrayValues;
|
|
17
17
|
private reconstructFromFlatStructure;
|
|
18
|
+
/**
|
|
19
|
+
* Returns the set of flat keys that cannot be safely expanded into a nested
|
|
20
|
+
* structure because another key is a dot-boundary ancestor of them. Both
|
|
21
|
+
* sides of every such collision are returned so callers can preserve them as
|
|
22
|
+
* literal flat keys instead of splitting on ".".
|
|
23
|
+
*/
|
|
24
|
+
private findConflictingKeys;
|
|
18
25
|
private setNestedValue;
|
|
19
26
|
private parsePath;
|
|
20
27
|
private validateTranslatableContent;
|
|
@@ -319,17 +319,61 @@ export class YamlHandler {
|
|
|
319
319
|
}
|
|
320
320
|
reconstructFromFlatStructure(flatData) {
|
|
321
321
|
const result = {};
|
|
322
|
-
|
|
323
|
-
|
|
322
|
+
const keys = Object.keys(flatData);
|
|
323
|
+
// Some flat keys contain literal dots and are ALSO dot-boundary prefixes of
|
|
324
|
+
// other flat keys (e.g. "a.b.c" and "a.b.c.d"). Such a set cannot be
|
|
325
|
+
// expressed as a nested tree: the shared path would have to be both a
|
|
326
|
+
// scalar leaf and a mapping at the same time. Splitting them blindly either
|
|
327
|
+
// crashes ("Cannot create property 'x' on string") or silently drops data.
|
|
328
|
+
// Keep the colliding keys as literal (flat) keys so the output is lossless.
|
|
329
|
+
const conflicting = this.findConflictingKeys(keys);
|
|
330
|
+
for (const key of keys) {
|
|
331
|
+
const value = flatData[key];
|
|
332
|
+
if (conflicting.has(key)) {
|
|
333
|
+
result[key] = value;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
// If nesting still hits an unexpected collision (e.g. a mixed
|
|
337
|
+
// array/object shape), fall back to a literal key rather than throwing
|
|
338
|
+
// or losing the value.
|
|
339
|
+
if (!this.setNestedValue(result, key, value)) {
|
|
340
|
+
result[key] = value;
|
|
341
|
+
}
|
|
324
342
|
}
|
|
325
343
|
return result;
|
|
326
344
|
}
|
|
345
|
+
/**
|
|
346
|
+
* Returns the set of flat keys that cannot be safely expanded into a nested
|
|
347
|
+
* structure because another key is a dot-boundary ancestor of them. Both
|
|
348
|
+
* sides of every such collision are returned so callers can preserve them as
|
|
349
|
+
* literal flat keys instead of splitting on ".".
|
|
350
|
+
*/
|
|
351
|
+
findConflictingKeys(keys) {
|
|
352
|
+
const conflicting = new Set();
|
|
353
|
+
const keySet = new Set(keys);
|
|
354
|
+
for (const key of keys) {
|
|
355
|
+
const segments = key.split(".");
|
|
356
|
+
let prefix = "";
|
|
357
|
+
for (let i = 0; i < segments.length - 1; i++) {
|
|
358
|
+
prefix = i === 0 ? segments[0] : `${prefix}.${segments[i]}`;
|
|
359
|
+
if (keySet.has(prefix)) {
|
|
360
|
+
conflicting.add(prefix);
|
|
361
|
+
conflicting.add(key);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
return conflicting;
|
|
366
|
+
}
|
|
327
367
|
setNestedValue(obj, path, value) {
|
|
328
368
|
const keys = this.parsePath(path);
|
|
329
369
|
let current = obj;
|
|
330
370
|
for (let i = 0; i < keys.length - 1; i++) {
|
|
331
371
|
const key = keys[i];
|
|
332
372
|
if (key.isArray) {
|
|
373
|
+
if (current[key.name] !== undefined &&
|
|
374
|
+
!Array.isArray(current[key.name])) {
|
|
375
|
+
return false; // expected an array here, found something else
|
|
376
|
+
}
|
|
333
377
|
if (!Array.isArray(current[key.name])) {
|
|
334
378
|
current[key.name] = [];
|
|
335
379
|
}
|
|
@@ -341,9 +385,18 @@ export class YamlHandler {
|
|
|
341
385
|
: {};
|
|
342
386
|
current[key.name].push(defaultValue);
|
|
343
387
|
}
|
|
344
|
-
|
|
388
|
+
const next = current[key.name][key.index];
|
|
389
|
+
if (next === null || typeof next !== "object") {
|
|
390
|
+
return false; // slot already holds a scalar, cannot descend
|
|
391
|
+
}
|
|
392
|
+
current = next;
|
|
345
393
|
}
|
|
346
394
|
else {
|
|
395
|
+
const existing = current[key.name];
|
|
396
|
+
if (existing !== undefined &&
|
|
397
|
+
(typeof existing !== "object" || existing === null)) {
|
|
398
|
+
return false; // cannot descend into a scalar leaf
|
|
399
|
+
}
|
|
347
400
|
if (!(key.name in current)) {
|
|
348
401
|
current[key.name] = {};
|
|
349
402
|
}
|
|
@@ -352,6 +405,10 @@ export class YamlHandler {
|
|
|
352
405
|
}
|
|
353
406
|
const lastKey = keys[keys.length - 1];
|
|
354
407
|
if (lastKey.isArray) {
|
|
408
|
+
if (current[lastKey.name] !== undefined &&
|
|
409
|
+
!Array.isArray(current[lastKey.name])) {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
355
412
|
if (!Array.isArray(current[lastKey.name])) {
|
|
356
413
|
current[lastKey.name] = [];
|
|
357
414
|
}
|
|
@@ -362,8 +419,15 @@ export class YamlHandler {
|
|
|
362
419
|
current[lastKey.name][lastKey.index] = value;
|
|
363
420
|
}
|
|
364
421
|
else {
|
|
422
|
+
const existing = current[lastKey.name];
|
|
423
|
+
if (existing !== undefined &&
|
|
424
|
+
typeof existing === "object" &&
|
|
425
|
+
existing !== null) {
|
|
426
|
+
return false; // would clobber a mapping/sequence with a scalar leaf
|
|
427
|
+
}
|
|
365
428
|
current[lastKey.name] = value;
|
|
366
429
|
}
|
|
430
|
+
return true;
|
|
367
431
|
}
|
|
368
432
|
parsePath(path) {
|
|
369
433
|
const parts = path.split(".");
|
package/build/src/openai.js
CHANGED
|
@@ -141,25 +141,14 @@ export class OpenAITranslate {
|
|
|
141
141
|
if (sourceLocale !== "en") {
|
|
142
142
|
throw Error(`${sourceLocale} is not supported.Currently we support just English `);
|
|
143
143
|
}
|
|
144
|
-
// DEBUG: Log what text is being sent for translation
|
|
145
|
-
console.log(`🔍 DEBUG - Translating text: "${text}" (length: ${text.length})`);
|
|
146
|
-
console.log(`🔍 DEBUG - Source locale: ${sourceLocale}, Target locale: ${targetLocale}`);
|
|
147
|
-
if (context) {
|
|
148
|
-
console.log(`🔍 DEBUG - Context: ${context}`);
|
|
149
|
-
}
|
|
150
144
|
let result = "";
|
|
151
145
|
let args;
|
|
152
146
|
({ args, text } = Util.replaceContextVariables(text));
|
|
153
|
-
// DEBUG: Log text after context variable replacement
|
|
154
|
-
console.log(`🔍 DEBUG - Text after context replacement: "${text}" (length: ${text.length})`);
|
|
155
147
|
let systemPrompt = `You will be provided with a sentence or words in English, and your task is to translate it into ${supportedLanguages[targetLocale]}. Return ONLY the translation, without any additional text or explanations.`;
|
|
156
148
|
if (context) {
|
|
157
149
|
systemPrompt += `\nContext for the translation: ${context}`;
|
|
158
150
|
}
|
|
159
151
|
const userPrompt = text;
|
|
160
|
-
// DEBUG: Log the prompts being sent
|
|
161
|
-
console.log(`🔍 DEBUG - System prompt: "${systemPrompt}"`);
|
|
162
|
-
console.log(`🔍 DEBUG - User prompt: "${userPrompt}"`);
|
|
163
152
|
const response = await this.openai.chat.completions.create({
|
|
164
153
|
model: this.model,
|
|
165
154
|
messages: [
|
|
@@ -176,8 +165,6 @@ export class OpenAITranslate {
|
|
|
176
165
|
if (response.choices[0].message.content !== null) {
|
|
177
166
|
result = Util.replaceArgumentsWithNumbers(args, response.choices[0].message.content);
|
|
178
167
|
result = result.replace(/^\n+|\n+$/g, "");
|
|
179
|
-
// DEBUG: Log the translation result
|
|
180
|
-
console.log(`🔍 DEBUG - Translation result: "${result}"`);
|
|
181
168
|
}
|
|
182
169
|
else {
|
|
183
170
|
console.error(`can not translate text with
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "auto-translate-json-library",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Automatically translates JSON language files to other languages using Google Translate, AWS, Azure, DeepL, OpenAI, Hugging Face (cloud & local) or local OpenAI compatible server",
|
|
6
6
|
"main": "./build/src/index.js",
|
|
7
7
|
"bin": {
|
|
8
|
-
"auto-translate-json-library": "build/src/bin/cli.js",
|
|
9
|
-
"atj": "build/src/bin/cli.js"
|
|
8
|
+
"auto-translate-json-library": "./build/src/bin/cli.js",
|
|
9
|
+
"atj": "./build/src/bin/cli.js"
|
|
10
10
|
},
|
|
11
11
|
"types": "./build/src/index.d.ts",
|
|
12
12
|
"files": [
|
|
@@ -128,40 +128,40 @@
|
|
|
128
128
|
],
|
|
129
129
|
"license": "MIT",
|
|
130
130
|
"devDependencies": {
|
|
131
|
-
"@biomejs/biome": "2.
|
|
132
|
-
"@topce/native-preview": "7.0.
|
|
131
|
+
"@biomejs/biome": "2.5.6",
|
|
132
|
+
"@topce/native-preview": "7.0.3",
|
|
133
133
|
"@types/gettext-parser": "9.0.0",
|
|
134
134
|
"@types/jest": "30.0.0",
|
|
135
135
|
"@types/minimist": "1.2.5",
|
|
136
|
-
"@types/node": "
|
|
136
|
+
"@types/node": "26.1.2",
|
|
137
137
|
"@types/properties-parser": "0.3.3",
|
|
138
138
|
"@types/yargs": "17.0.35",
|
|
139
|
-
"jest": "30.
|
|
140
|
-
"ts-jest": "29.4.
|
|
139
|
+
"jest": "30.4.2",
|
|
140
|
+
"ts-jest": "29.4.12",
|
|
141
141
|
"ts-node": "10.9.2",
|
|
142
142
|
"typescript": "6.0.3"
|
|
143
143
|
},
|
|
144
144
|
"dependencies": {
|
|
145
|
-
"@aws-sdk/client-translate": "3.
|
|
146
|
-
"@google-cloud/translate": "9.
|
|
147
|
-
"@huggingface/inference": "^4.13.
|
|
148
|
-
"@huggingface/transformers": "^4.
|
|
145
|
+
"@aws-sdk/client-translate": "3.1100.0",
|
|
146
|
+
"@google-cloud/translate": "9.4.2",
|
|
147
|
+
"@huggingface/inference": "^4.13.24",
|
|
148
|
+
"@huggingface/transformers": "^4.2.0",
|
|
149
149
|
"@types/js-yaml": "4.0.9",
|
|
150
|
-
"axios": "1.
|
|
151
|
-
"csv-parser": "3.2.
|
|
150
|
+
"axios": "1.19.0",
|
|
151
|
+
"csv-parser": "3.2.1",
|
|
152
152
|
"csv-writer": "1.6.0",
|
|
153
|
-
"deepl-node": "1.
|
|
153
|
+
"deepl-node": "1.27.0",
|
|
154
154
|
"dotenv": "17.4.2",
|
|
155
|
-
"fast-xml-parser": "5.
|
|
156
|
-
"form-data": "4.0.
|
|
157
|
-
"gettext-parser": "9.
|
|
158
|
-
"js-yaml": "4.
|
|
155
|
+
"fast-xml-parser": "5.10.1",
|
|
156
|
+
"form-data": "4.0.6",
|
|
157
|
+
"gettext-parser": "9.1.1",
|
|
158
|
+
"js-yaml": "4.3.1",
|
|
159
159
|
"minimist": "1.2.8",
|
|
160
|
-
"openai": "
|
|
160
|
+
"openai": "7.3.0",
|
|
161
161
|
"picocolors": "1.1.1",
|
|
162
162
|
"properties-parser": "0.6.0",
|
|
163
|
-
"sharp": "^0.
|
|
164
|
-
"uuid": "
|
|
165
|
-
"yargs": "18.
|
|
163
|
+
"sharp": "^0.35.3",
|
|
164
|
+
"uuid": "14.0.1",
|
|
165
|
+
"yargs": "18.1.0"
|
|
166
166
|
}
|
|
167
167
|
}
|