-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathscript-translate.js
188 lines (162 loc) · 6.54 KB
/
script-translate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const fs = require('fs');
let promises = [];
/**
* Reads and parses a JSON file.
* @param {string} filename - The name of the file to read.
* @returns {Object|null} The parsed JSON object or null if an error occurs.
*/
function readJSONFile(filename) {
try {
const data = fs.readFileSync(filename, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error(`Error reading ${filename}:`, err);
return null;
}
}
/**
* Writes data to a JSON file after sorting it.
* @param {string} filename - The name of the file to write to.
* @param {Object} data - The data to write to the file.
* @returns {Promise<void>}
*/
async function writeJSONFile(filename, data) {
try {
// Wait for all pending promises to settle
await Promise.allSettled(promises);
promises = [];
// Write sorted data to file
fs.writeFileSync(filename, JSON.stringify(sortJsonObject(data), null, 2));
console.log(`Data written to ${filename}`);
} catch (err) {
console.error(`Error writing to ${filename}:`, err);
}
}
/**
* Sorts a JSON object recursively.
* @param {Object} obj - The object to sort.
* @returns {Object} A new sorted object.
*/
function sortJsonObject(obj) {
const keysWithSubobjects = [];
const keysWithoutSubobjects = [];
// Separate keys into two groups
for (const key in obj) {
if (typeof obj[key] === 'object' && !Array.isArray(obj[key])) {
keysWithSubobjects.push({ key, value: sortJsonObject(obj[key]) });
} else {
keysWithoutSubobjects.push({ key, value: obj[key] });
}
}
// Sort both groups alphabetically by key
keysWithoutSubobjects.sort((a, b) => a.key.localeCompare(b.key));
keysWithSubobjects.sort((a, b) => a.key.localeCompare(b.key));
// Concatenate the sorted groups
const sortedKeys = [...keysWithoutSubobjects, ...keysWithSubobjects];
// Reconstruct the object using sorted keys
const sortedObj = {};
sortedKeys.forEach(item => {
sortedObj[item.key] = item.value;
});
return sortedObj;
}
/**
* Translates text using Google Translate API.
* @param {string} text - The text to translate.
* @param {string} targetLanguage - The target language code.
* @returns {Promise<string>} The translated text.
*/
async function translateText(text, targetLanguage) {
const apiKey = 'REPLACEME'; // Replace with your API key
const url = `https://translation.googleapis.com/language/translate/v2?key=${apiKey}`;
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
charset: 'UTF-8',
},
body: JSON.stringify({
q: text,
target: targetLanguage
})
});
const data = await response.json();
return data.data.translations[0].translatedText;
}
/**
* Recursively copies missing keys from source to destination, translating as necessary.
* @param {Object} sourceData - The source data object.
* @param {Object} destinationData - The destination data object.
* @param {string} targetLanguage - The target language code.
*/
function copyMissingKeys(sourceData, destinationData, targetLanguage) {
for (const key in sourceData) {
if (!(key in destinationData)) {
const englishText = sourceData[key];
// Special handling for certain keys
if (key === "am" || key === "pm" || key === "fileSizeTypes") {
destinationData[key] = englishText;
console.log(`Added key '${key}' with value '${englishText}'`);
} else {
// Translate and add the key
const promise = translateText(englishText, targetLanguage)
.then(translation => {
console.log(`Translated "${englishText}" to ${targetLanguage}: "${translation}"`);
destinationData[key] = translation;
})
.catch(error => {
console.error(`Error translating key '${key}' text '${englishText}':`, error);
destinationData[key] = englishText;
console.log(`Added key '${key}' with value '${englishText}'`);
});
promises.push(promise);
}
} else if (typeof sourceData[key] === 'object' && typeof destinationData[key] === 'object') {
// Recursively handle nested objects
copyMissingKeys(sourceData[key], destinationData[key], targetLanguage);
}
}
}
/**
* Copies missing keys from source file to all destination files in a directory.
* @param {string} sourceFile - The path to the source file.
* @param {string} destinationDir - The path to the destination directory.
*/
function copyMissingKeysRecursive(sourceFile, destinationDir) {
const sourceData = readJSONFile(sourceFile);
if (!sourceData) {
console.error("Error reading source file. Aborting operation.");
return;
}
fs.readdir(destinationDir, (err, files) => {
if (err) {
console.error(`Error reading directory ${destinationDir}:`, err);
return;
}
files.forEach(file => {
const destinationFile = `${destinationDir}/${file}`;
// Process only JSON files that are not the source file or package.json
if (file.endsWith('.json') && !destinationFile.endsWith(sourceFile) && !file.endsWith('package.json')) {
let destinationData = readJSONFile(destinationFile) || {};
if (!destinationData) {
console.error(`Error reading ${destinationFile}. Skipping.`);
return;
}
const sourceLanguage = Object.keys(sourceData)[0];
const destinationLanguage = Object.keys(destinationData)[0];
if (!sourceLanguage || !destinationLanguage) {
console.error("Root keys not found in source or destination file.");
return;
}
console.log(`Source Language '${sourceLanguage}' to Destination Language '${destinationLanguage}'`);
copyMissingKeys(sourceData[sourceLanguage], destinationData[destinationLanguage], destinationLanguage);
writeJSONFile(destinationFile, destinationData);
}
});
});
}
// Example usage
const sourceFile = 'en.json';
const destinationDir = '.';
copyMissingKeysRecursive(sourceFile, destinationDir);