forked from bankless-academy/bankless-academy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-translations.js
85 lines (83 loc) · 3.26 KB
/
import-translations.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
/* eslint-disable no-console */
const axios = require('axios')
const fs = require('fs')
async function main() {
const languages = [
'cn',
'de',
'es',
'fr',
'it',
'jp'
]
const nameSpaces = [
'common',
'homepage',
'quests'
]
for (const language of languages) {
for (const nameSpace of nameSpaces) {
try {
console.log(`${language}/${nameSpace}`)
const jsonPath = `translation/website/${language}/${nameSpace}.json`
const random = Math.floor(Math.random() * 100000)
const github = `https://raw.githubusercontent.com/bankless-academy/bankless-academy/l10n_main/${jsonPath}?${random}`
console.log(github)
const crowdin = await axios.get(github)
if (crowdin.status === 200) {
const newTranslation = JSON.stringify(crowdin.data, null, 2)
// console.log('newTranslation', newTranslation)
const existingTranslation = fs.existsSync(jsonPath) ? await fs.promises.readFile(jsonPath, 'utf8') : ''
// console.log('existingTranslation', existingTranslation)
if (newTranslation.trim() !== existingTranslation.trim()) {
console.log('- new translation available')
fs.writeFile(jsonPath, `${newTranslation}\n`, (error) => {
if (error) throw error
})
} else {
console.log('- same same')
}
} else console.log(`- ${language}/${nameSpace}.json error ${crowdin.status}`)
} catch (error) {
if (error.response.status === 404)
console.log(`- ${language}/${nameSpace}.json not available yet`)
else
console.log(error)
}
}
try {
const jsonPath = `translation/keywords/${language}/keywords.json`
const random = Math.floor(Math.random() * 100000)
const github = `https://raw.githubusercontent.com/bankless-academy/bankless-academy/l10n_main/${jsonPath}?${random}`
console.log(github)
const crowdin = await axios.get(github)
if (crowdin.status === 200) {
const data = {}
for (const [, v] of Object.entries(crowdin.data)) {
data[v.keyword?.toLowerCase()] = { keyword: v.keyword, definition: v.definition }
if (v.keyword?.toLowerCase() !== v.keyword_plural?.toLowerCase() && v.keyword_plural !== '') {
data[v.keyword_plural?.toLowerCase()] = { keyword: v.keyword_plural, definition: v.definition }
}
}
// console.log('newTranslation', data)
const newTranslation = JSON.stringify(data, null, 2)
const existingTranslation = fs.existsSync(jsonPath) ? await fs.promises.readFile(jsonPath, 'utf8') : ''
// console.log('existingTranslation', existingTranslation)
if (newTranslation.trim() !== existingTranslation.trim()) {
console.log('- new translation available')
fs.writeFile(jsonPath, `${newTranslation}\n`, (error) => {
if (error) throw error
})
} else {
console.log('- same same')
}
} else console.log(`- ${language}/keywords.json error ${crowdin.status}`)
} catch (error) {
if (error.response.status === 404)
console.log(`- ${language}/keywords.json not available yet`)
else
console.log(error)
}
}
}
main()