-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathcheck_translations.js.example
46 lines (40 loc) · 1.82 KB
/
check_translations.js.example
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
#!/usr/bin/env node
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const projectId = 'focal-welder-174713';
// Imports the Google Cloud client library
const { Translate } = require('@google-cloud/translate').v2;
// Instantiates a client
const translate = new Translate({ projectId });
//Args
const destinationLanguages = ['ru', 'cn'];
const languageGoogleAPICodes = { ru: 'ru', cn: 'zh-CN' };
const languageNamesMap = { ru: 'Russian', cn: 'Chinese' };
async function translateMissingValues(originLanguage, targetLanguage, languageKey) {
if (_.isObject(originLanguage)) {
await Promise.all(
Object.keys(originLanguage).map(async (key) => {
if (_.isObject(originLanguage[key])) {
if (!targetLanguage[key]) {
targetLanguage[key] = {};
}
await translateMissingValues(originLanguage[key], targetLanguage[key], languageKey);
} else if (!targetLanguage[key]) {
console.log(`Translating '${originLanguage[key]}' to ${languageNamesMap[languageKey]}`);
targetLanguage[key] = (
await translate.translate(originLanguage[key], languageGoogleAPICodes[languageKey])
)[0];
}
})
);
}
}
destinationLanguages.forEach((languageKey) => {
const fileName = `src/i18n/${languageKey}.json`;
const originLanguage = JSON.parse(fs.readFileSync(path.resolve(`src/i18n/en.json`), 'utf-8'));
const targetLanguage = JSON.parse(fs.readFileSync(path.resolve(`src/i18n/${languageKey}.json`), 'utf-8'));
translateMissingValues(originLanguage, targetLanguage, languageKey).then(() => {
fs.writeFileSync(fileName, JSON.stringify(targetLanguage, null, 4));
});
});