forked from primefaces/primelocale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.js
157 lines (128 loc) · 5.29 KB
/
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
const fs = require('fs');
let promises = [];
// Function to read JSON file
function readJSONFile(filename) {
try {
const data = fs.readFileSync(filename, 'utf8');
return JSON.parse(data);
} catch (err) {
console.error(`Error reading ${filename}:`, err);
return null;
}
}
// Function to write JSON file
async function writeJSONFile(filename, data) {
try {
await Promise.allSettled(promises);
promises = [];
fs.writeFileSync(filename, JSON.stringify(sortJsonObject(data), null, 2));
console.log(`Data written to ${filename}`);
} catch (err) {
console.error(`Error writing to ${filename}:`, err);
}
}
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;
}
// Function to translate text using Google Translate API
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;
}
// Function to recursively copy missing keys from source to destination
function copyMissingKeys(sourceData, destinationData, targetLanguage) {
for (const key in sourceData) {
if (!(key in destinationData)) {
const englishText = sourceData[key];
if (key === "am" || key === "pm" || key === "fileSizeTypes") {
destinationData[key] = englishText;
console.log(`Added key '${key}' with value '${englishText}'`);
} else {
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') {
copyMissingKeys(sourceData[key], destinationData[key], targetLanguage);
}
}
}
// Function to copy missing keys from source to destination
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}`;
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);