-
Notifications
You must be signed in to change notification settings - Fork 7
/
translate.js
91 lines (81 loc) · 2.54 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
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
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 = ["de"]
const defaultLanguage = "en"
const readFile = util.promisify(fs.readFile)
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 || "eu-west-1"
const translate = new AWS.Translate()
async function main() {
console.log("start translating")
const data = await readFile(
`locales/${defaultLanguage}/translation.json`,
"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}/translation.json`,
"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
if (typeof translations[key] === "object") {
await BP.map(
Object.keys(translations[key]),
async (nestedKey) => {
const { TranslatedText } = await translate
.translateText({
Text: translations[key][nestedKey],
SourceLanguageCode: defaultLanguage,
TargetLanguageCode: lng,
})
.promise()
resultMap[lng][key]
? (resultMap[lng][key][nestedKey] = TranslatedText)
: (resultMap[lng][key] = { [nestedKey]: TranslatedText })
},
{ concurrency: 10 }
)
} else {
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}/translation.json`, resultMap[lng])
})
console.log("All done!")
}
main()