-
Notifications
You must be signed in to change notification settings - Fork 72
/
translate.js
66 lines (58 loc) · 1.96 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
const AWS = require('aws-sdk');
const BP = require('bluebird');
const fs = require('fs');
const util = require('util');
const mkdirp = require('mkdirp');
const getDirName = require('path').dirname;
const {languages, defaultLanguage} = require('./languages');
const readFile = util.promisify(fs.readFile);
const readdir = util.promisify(fs.readdir);
const writeFile = util.promisify(fs.writeFile);
const write = async (path, content) => {
await mkdirp(getDirName(path));
return writeFile(path, JSON.stringify(content, null, 2), 'utf8');
};
AWS.config.region = process.env.REGION || 'us-east-1';
const translate = new AWS.Translate();
async function main() {
const files = await readdir(`locales/${defaultLanguage}`);
await BP.map(files, async (filename) => {
const data = await readFile(`locales/${defaultLanguage}/${filename}`, 'utf8');
const translations = JSON.parse(data);
const resultMap = {};
await BP.map(
languages,
async (lng) => {
if (lng === defaultLanguage) return;
try {
const localeData = await readFile(`locales/${lng}/${filename}`, 'utf8');
resultMap[lng] = JSON.parse(localeData);
} catch (error) {
resultMap[lng] = {};
}
await BP.map(
Object.keys(translations),
async (key) => {
if (key === '_t' || resultMap[lng][key]) return;
const {TranslatedText} = await translate
.translateText({
Text: translations[key],
SourceLanguageCode: defaultLanguage,
TargetLanguageCode: lng
})
.promise();
resultMap[lng][key] = TranslatedText;
},
{concurrency: 10}
);
console.log(`Translated: ${lng}`);
},
{concurrency: 10}
);
BP.map(Object.keys(resultMap), async (lng) => {
return write(`locales/${lng}/${filename}`, resultMap[lng]);
});
});
console.log('All done!');
}
main();