-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.cjs
138 lines (116 loc) · 4.45 KB
/
index.cjs
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
const axios = require('axios').default;
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const { sourceFilePathName, targetLanguages, folderPath, endpoint, azureKey, location, keyOrValue, testMode, overwrite } = require('./config');
const main = async () => {
const sourceFilePath = path.join(folderPath, sourceFilePathName);
const sourceFile = fs.readFileSync(sourceFilePath, 'utf8');
const sourceJson = JSON.parse(sourceFile);
console.log(`Source file ${sourceFilePath} parsed.`);
if (testMode) {
// test mode is on
const targetFilePath = sourceFilePath;
const language = sourceFilePathName.split(".")[0];
const targetJson = {};
for (const [key, value] of Object.entries(sourceJson)) {
if (keyOrValue === "value"
&& value === "")
continue; // Skip empty keys
//console.log(`Translating ${value}...`);
const response = await axios.post(`${endpoint}/translate`, [{
'text': key
}], {
headers: {
'Ocp-Apim-Subscription-Key': azureKey,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': uuidv4().toString()
},
params: {
'api-version': '3.0',
'to': language
},
responseType: 'json'
});
targetJson[key] = response.data[0].translations[0].text;
//console.log(`Translated ${key} to ${targetJson[key]}`);
}
// Skip empty files
if (Object.keys(targetJson).length === 0) {
console.log(`File ${targetFilePath} is empty. Skipping translation.`);
return;
}
// Write the translated file
fs.writeFileSync(targetFilePath, JSON.stringify(targetJson, null, 2), 'utf8');//format
console.log(`File ${targetFilePath} created and translated.`);
} else {
// test mode is off
for (const language of targetLanguages) {
const targetFilePath = path.join(folderPath, `${language}.json`);
if (sourceFilePathName === `${language}.json`) {
console.log(`Source and target files are the same. Skipping translation.`);
continue;
}
else
if (fs.existsSync(targetFilePath)) {
if (overwrite === false) {
console.log(`File ${targetFilePath} already exists. Skipping translation.`);
continue;
} else {
console.log(`File ${targetFilePath} already exists. Overwriting...`);
}
} else {
console.log(`File ${targetFilePath} does not exist.`);
}
console.log(`Translating to ${language}...`);
const targetJson = {};
if (fs.existsSync(targetFilePath)) {
const targetFile = fs.readFileSync(targetFilePath, 'utf8');
const existingTargetJson = JSON.parse(targetFile);
Object.assign(targetJson, existingTargetJson);
}
for (const [key, value] of Object.entries(sourceJson)) {
// Skip empty values
if (keyOrValue === "value" && value === "") {
targetJson[key] = value;
continue;
}
// Skip if the key already exists in the target file
if (targetJson.hasOwnProperty(key)) {
console.log(`Key ${key} already exists in the target file. Skipping translation.`);
continue;
}
//console.log(`Translating ${value}...`);
const response = await axios.post(`${endpoint}/translate`, [{
'text': keyOrValue === "key" ? key : value
}], {
headers: {
'Ocp-Apim-Subscription-Key': azureKey,
'Ocp-Apim-Subscription-Region': location,
'Content-type': 'application/json',
'X-ClientTraceId': uuidv4().toString()
},
params: {
'api-version': '3.0',
'to': language
},
responseType: 'json'
});
targetJson[key] = response.data[0].translations[0].text;
//console.log(`Translated ${key} to ${targetJson[key]}`);
}
// Skip empty files
if (Object.keys(targetJson).length === 0) {
console.log(`File ${targetFilePath} is empty. Skipping translation.`);
continue;
}
// Write the translated file
fs.writeFileSync(targetFilePath, JSON.stringify(targetJson, null, 2), 'utf8');//format
console.log(`File ${targetFilePath} created and translated.`);
// Wait 100ms to avoid throttling
await new Promise(resolve => setTimeout(resolve, 100));
}
}
};
main();