|
| 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