mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-08-19 05:00:21 +02:00
Add DeepL translations as placeholders
This commit is contained in:
@@ -1,30 +1,119 @@
|
||||
// Take one (hardcoded) translation file and ensure that all other translation files have the same keys in the same order
|
||||
// Then report which other translation files have identical items
|
||||
// Then report which other translation files have identical items (or auto-translate them if a DeepL API key is provided)
|
||||
|
||||
const fs = require('fs')
|
||||
const https = require('https')
|
||||
|
||||
const translationsDir = __dirname
|
||||
const templateFile = `${translationsDir}/en.json`
|
||||
const otherFiles = fs.readdirSync(translationsDir).map(f => {
|
||||
return `${translationsDir}/${f}`
|
||||
}).filter(f => f.endsWith('.json') && f != templateFile)
|
||||
const deeplAPIKey = process.argv[2]
|
||||
|
||||
const templateTranslation = require(templateFile)
|
||||
const neverAutoTranslate = {
|
||||
steamMobile: ['*'],
|
||||
steamChat: ['*'],
|
||||
root: ['*'],
|
||||
obtainiumExportHyphenatedLowercase: ['*'],
|
||||
theme: ['de'],
|
||||
appId: ['de']
|
||||
}
|
||||
|
||||
otherFiles.forEach(file => {
|
||||
const thisTranslationOriginal = require(file)
|
||||
const thisTranslationNew = {}
|
||||
Object.keys(templateTranslation).forEach(k => {
|
||||
thisTranslationNew[k] = thisTranslationOriginal[k] || templateTranslation[k]
|
||||
})
|
||||
fs.writeFileSync(file, `${JSON.stringify(thisTranslationNew, null, ' ')}\n`)
|
||||
});
|
||||
|
||||
otherFiles.forEach(file => {
|
||||
const thisTranslation = require(file)
|
||||
Object.keys(templateTranslation).forEach(k => {
|
||||
if (JSON.stringify(thisTranslation[k]) == JSON.stringify(templateTranslation[k])) {
|
||||
console.log(`${file} :::: ${k} :::: ${JSON.stringify(thisTranslation[k])}`)
|
||||
const translateText = async (text, targetLang, authKey) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const postData = `text=${encodeURIComponent(text)}&target_lang=${encodeURIComponent(targetLang)}&source_lang=EN`
|
||||
const options = {
|
||||
hostname: 'api-free.deepl.com',
|
||||
port: 443,
|
||||
path: '/v2/translate',
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `DeepL-Auth-Key ${authKey}`,
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Content-Length': Buffer.byteLength(postData)
|
||||
}
|
||||
}
|
||||
const req = https.request(options, (res) => {
|
||||
let responseData = ''
|
||||
res.on('data', (chunk) => {
|
||||
responseData += chunk
|
||||
})
|
||||
res.on('end', () => {
|
||||
try {
|
||||
const jsonResponse = JSON.parse(responseData)
|
||||
resolve(jsonResponse)
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
}
|
||||
})
|
||||
})
|
||||
req.on('error', (error) => {
|
||||
reject(error)
|
||||
})
|
||||
req.write(postData)
|
||||
req.end()
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
const main = async () => {
|
||||
const translationsDir = __dirname
|
||||
const templateFile = `${translationsDir}/en.json`
|
||||
const otherFiles = fs.readdirSync(translationsDir).map(f => {
|
||||
return `${translationsDir}/${f}`
|
||||
}).filter(f => f.endsWith('.json') && f != templateFile)
|
||||
|
||||
const templateTranslation = require(templateFile)
|
||||
|
||||
|
||||
otherFiles.forEach(file => {
|
||||
const thisTranslationOriginal = require(file)
|
||||
const thisTranslationNew = {}
|
||||
Object.keys(templateTranslation).forEach(k => {
|
||||
thisTranslationNew[k] = thisTranslationOriginal[k] || templateTranslation[k]
|
||||
})
|
||||
fs.writeFileSync(file, `${JSON.stringify(thisTranslationNew, null, ' ')}\n`)
|
||||
})
|
||||
|
||||
for (let i in otherFiles) {
|
||||
const file = otherFiles[i]
|
||||
const thisTranslation = require(file)
|
||||
const translationKeys = Object.keys(templateTranslation)
|
||||
for (let j in translationKeys) {
|
||||
const k = translationKeys[j]
|
||||
if (JSON.stringify(thisTranslation[k]) == JSON.stringify(templateTranslation[k])) {
|
||||
const lang = file.split('/').pop().split('.')[0]
|
||||
if (!neverAutoTranslate[k] || (neverAutoTranslate[k].indexOf('*') < 0 && neverAutoTranslate[k].indexOf(lang) < 0)) {
|
||||
const reportLine = `${file} :::: ${k} :::: ${JSON.stringify(thisTranslation[k])}`
|
||||
if (deeplAPIKey) {
|
||||
const translateFunc = async (str) => {
|
||||
const response = await translateText(str, lang, deeplAPIKey)
|
||||
if (response.translations) {
|
||||
return response.translations[0].text
|
||||
} else {
|
||||
throw JSON.stringify(response)
|
||||
}
|
||||
}
|
||||
try {
|
||||
if (typeof templateTranslation[k] == 'string') {
|
||||
thisTranslation[k] = await translateFunc(thisTranslation[k])
|
||||
} else {
|
||||
const subKeys = Object.keys(templateTranslation[k])
|
||||
for (let n in subKeys) {
|
||||
const kk = subKeys[n]
|
||||
thisTranslation[k][kk] = await translateFunc(thisTranslation[k][kk])
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (typeof e == 'string') {
|
||||
console.log(`${reportLine} :::: ${e}`)
|
||||
} else {
|
||||
throw e
|
||||
}
|
||||
}
|
||||
} else {
|
||||
console.log(reportLine)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
fs.writeFileSync(file, `${JSON.stringify(thisTranslation, null, ' ')}\n`)
|
||||
}
|
||||
}
|
||||
|
||||
main().catch(e => console.error)
|
||||
|
Reference in New Issue
Block a user