-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtranslate.js
33 lines (29 loc) · 1.07 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
const fs = require('fs')
const { Translate } = require('@google-cloud/translate').v2
const translate = new Translate()
function translateText (text, targetLang) {
return translate.translate(text, targetLang)
}
async function translateLocale (localeData, targetLang) {
await Promise.all(Object.keys(localeData).map(async k => {
const value = localeData[k]
if (typeof value === 'string') {
return translateText(value, targetLang).then(translation => {
// console.log('Original:', value, 'Translation:', translation[0])
localeData[k] = translation[0]
})
} else {
return translateLocale(value, targetLang)
}
}))
return localeData
}
const targetLang = process.argv[2]
const localeFile = process.argv[3]
const outputFile = localeFile.replace('/en/', `/${targetLang}/`)
console.log(`Translating ${localeFile} to ${targetLang} and writing to ${outputFile})`)
const localeData = require(localeFile)
translateLocale(localeData, targetLang).then(data => {
console.log(data)
fs.writeFileSync(outputFile, JSON.stringify(data, ' ', 2))
})