-
Notifications
You must be signed in to change notification settings - Fork 0
/
readAndTranslate.js
133 lines (109 loc) · 3.35 KB
/
readAndTranslate.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
const { Translate } = require('@google-cloud/translate').v2;
const fastCsv = require('fast-csv'); // https://c2fo.io/fast-csv/
const fs = require('fs');
const mkdirp = require('mkdirp');
const mysql = require('mysql2/promise');
const neatCsv = require('neat-csv'); // https://github.com/sindresorhus/neat-csv
const translate = new Translate();
const pool = mysql.createPool({
host: 'localhost',
user: 'root',
password: 'sasa',
database: 'translate',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
const targets = require('./targets.js');
// const targets = [
// { code:'es',name:'Spanish',out:['es','es-ES'] },
// { code:'de',name:'German',out:['de'] },
// ]
const inputFolder = 'C:\\projects\\blue\\agent\\source\\i18n\\Data\\en\\';
const inputFiles = ['2024.6.1.CustomFields.en.csv'];
let inputFile = '';
let file = '';
const ignorePreviousDatabaseRecords = false;
async function translateText(text, target) {
const [translation] = await translate.translate(text, target);
return translation;
}
async function rowExists(keyword, locale) {
if (ignorePreviousDatabaseRecords) return '';
try {
// eslint-disable-next-line max-len
const [result] = await pool.query(
'select translation from strings where keyword = ? and locale = ?',
[keyword, locale],
);
if (result.length < 1) {
return '';
} else {
return result[0].translation;
}
} catch (err) {
console.error('Table query failed:', err);
}
}
async function insertRow(keyword, locale, translation) {
await pool.query('insert IGNORE into strings set keyword = ?, locale = ?, translation = ?', [
keyword,
locale,
translation,
]);
}
async function writeFile(strArray, output) {
const dir = `./results/${output}`;
mkdirp.sync(dir);
const outputFile = `${dir}/${inputFile.replace('.en.', `.${output}.`)}`;
return new Promise((resolve) => {
const ws = fs.createWriteStream(outputFile);
fastCsv.write(strArray, { headers: true, quoteColumns: true }).pipe(ws);
ws.on('finish', () => ws.close(resolve));
});
}
async function readFile() {
return new Promise((resolve, reject) => {
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
reject(err);
}
resolve(data);
});
});
}
async function process(data) {
const keys = await neatCsv(data);
for (const target of targets) {
console.log(target);
for (const output of target.out) {
let strArray = [];
for (const key of keys) {
const obj = {
Key: key.Key,
en: key.en,
};
const existingTranslation = await rowExists(key.Key, output);
if (existingTranslation > '') {
obj[output] = existingTranslation;
strArray.push(obj);
} else {
const translatedText = await translateText(key.en, target.code);
await insertRow(key.Key, output, translatedText);
obj[output] = translatedText;
strArray.push(obj);
}
}
await writeFile(strArray, output);
}
}
}
async function main() {
for (inputFile of inputFiles) {
file = `${inputFolder}${inputFile}`;
const data = await readFile();
await process(data);
}
pool.end();
}
main();