Skip to content

Commit 34dde79

Browse files
committed
added deepl translations
1 parent 45fd1f1 commit 34dde79

File tree

4 files changed

+1776
-486
lines changed

4 files changed

+1776
-486
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,5 @@ Icon
6464
Network Trash Folder
6565
Temporary Items
6666
.apdisk
67+
68+
.env

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@material/theme": "^0.43.0",
5050
"@material/toolbar": "^0.43.0",
5151
"@material/typography": "^0.43.0",
52+
"axios": "^1.2.2",
5253
"babel-core": "^6.26.3",
5354
"babel-eslint": "^7.2.3",
5455
"babel-loader": "^7.1.5",
@@ -60,6 +61,7 @@
6061
"connected-react-router": "^6.5.2",
6162
"cross-env": "^5.2.0",
6263
"css-loader": "^3.2.0",
64+
"dotenv": "^16.0.3",
6365
"es6-promise": "^4.2.5",
6466
"eslint": "^4.19.1",
6567
"eslint-loader": "^1.9.0",

scripts/deepl.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// This script uses the Deepl API to provide missing localization translations.
2+
// To provide the API key, add a .env file with DEEPLKEY=key
3+
const fs = require('fs')
4+
const path = require('path')
5+
const axios = require('axios')
6+
require('dotenv').config({
7+
path: `.env`,
8+
})
9+
10+
// Set target languages
11+
const TARGETLANGS = [
12+
'fr',
13+
'es',
14+
'de',
15+
'pt',
16+
'ja',
17+
'zh'
18+
]
19+
20+
// Set API key
21+
axios.defaults.headers.common.Authorization = `DeepL-Auth-Key ${process.env.DEEPLKEY}`
22+
23+
// DeepL translation function
24+
function translateString(text, lang) {
25+
return axios.post('https://api-free.deepl.com/v2/translate', {
26+
text: [text],
27+
target_lang: lang.toUpperCase()
28+
})
29+
.then(function(response) {
30+
return response.data.translations[0].text
31+
})
32+
.catch(function(error) {
33+
console.log(error)
34+
})
35+
}
36+
37+
async function translateAll(i18nData) {
38+
for (const sectionKey of Object.keys(i18nData)) {
39+
const section = i18nData[sectionKey]
40+
for (const entryKey of Object.keys(section)) {
41+
const entry = section[entryKey]
42+
for (const lang of TARGETLANGS) {
43+
if (!entry[lang]) {
44+
const enText = entry.en ? entry.en : entryKey
45+
// Cleanup text from HTML tags
46+
const cleanText = enText.replace(/<[^>]+>/g, '')
47+
entry[`${lang}-notchecked`] = await translateString(cleanText, lang)
48+
}
49+
}
50+
}
51+
}
52+
53+
return i18nData
54+
}
55+
56+
async function processi18n() {
57+
// Load i18n file
58+
const i18nData = JSON.parse(fs.readFileSync(path.join(__dirname, '../src/localization/i18n.json')))
59+
const newi18nData = await translateAll(i18nData)
60+
61+
fs.writeFileSync(
62+
path.join(__dirname, '../src/localization/i18n-deepl.json'),
63+
JSON.stringify(newi18nData, null, 2)
64+
)
65+
}
66+
67+
processi18n()

0 commit comments

Comments
 (0)