Created
April 29, 2021 06:36
-
-
Save 34fame/a11aaee9f8e29e12d1930cb1a0216a9e to your computer and use it in GitHub Desktop.
Node script to process translations for Quasar using Google Translate API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const { Translate } = require('@google-cloud/translate').v2 | |
const locale = require('locale-codes') | |
const translate = new Translate({ | |
projectId: 'ibofk-dev', | |
keyFilename: './ibofk-service-account.json' | |
}); | |
const basePath = '../client/src/i18n' | |
let buffer = fs.readFileSync(`${basePath}/english.js`) | |
let enString = buffer.toString() | |
let enJSON = enString.substr(15) | |
let enObj = JSON.parse(enJSON) | |
const sourceText = [] | |
Object.keys(enObj).map(key => { | |
sourceText.push(enObj[key]) | |
}) | |
const translateText = async (lang) => { | |
let results = await translate.translate(sourceText, lang.code) | |
return { ...lang, translations: results[0] } | |
} | |
const listLanguages = async () => { | |
let results = await translate.getLanguages() | |
return results[0] | |
} | |
const getTranslations = async () => { | |
let languages = await listLanguages() | |
let locales = locale.all | |
return await Promise.all(locales.map(loc => { | |
let match = languages.filter(lang => lang.code === loc.tag) | |
if (!match.length) { | |
match = languages.filter(lang => lang.code === loc['iso639-1']) | |
if (!match.length) { | |
return false | |
} else { | |
return translateText({ | |
code: loc['iso639-1'], | |
tag: loc.tag, | |
english: loc.name, | |
name: loc.local, | |
location: loc.location, | |
iso6392: loc['iso639-2'], | |
iso6391: loc['iso639-1'], | |
lcid: loc.lcid | |
}) | |
} | |
} else { | |
return translateText({ | |
code: loc.tag, | |
tag: loc.tag, | |
english: loc.name, | |
name: loc.local, | |
location: loc.location, | |
iso6392: loc['iso639-2'], | |
iso6391: loc['iso639-1'], | |
lcid: loc.lcid | |
}) | |
} | |
})) | |
} | |
const normalizeTranslations = async () => { | |
let raw = await getTranslations() | |
let normalized = [] | |
raw.map(r => { | |
if (!r.code) { | |
return false | |
} | |
let obj = { ...r, translations: {} } | |
if (!obj.name) obj.name = obj.english | |
obj.tag = obj.tag.replace(',', '') | |
Object.keys(enObj).map((key, idx) => { | |
obj.translations[key] = r.translations[idx] | |
}) | |
normalized.push(obj) | |
}) | |
return normalized | |
} | |
const saveTranslations = async () => { | |
let translations = await normalizeTranslations() | |
let imports = "" | |
let exports = "export default {\r" | |
let options = [] | |
translations.map(t => { | |
if (!t.code || !t.tag || !t.name) { | |
return false | |
} | |
// These languages have characters that aren't escaping properly | |
if (t.tag.startsWith('he') || t.tag.startsWith('iw')) return false | |
try { | |
imports += `import ${t.tag.replace(/-/g,'_')} from './${t.tag}'\r` | |
exports += ` ${t.tag.replace(/-/g,'_')},\r` | |
let label = t.location ? `${t.name} (${t.location})` : t.name | |
options.push({ label, value: t.tag}) | |
if (!fs.existsSync(`${basePath}/${t.tag}`)) { | |
fs.mkdirSync(`${basePath}/${t.tag}`) | |
} | |
let content = 'export default {\r' | |
Object.keys(t.translations).map(key => { | |
content += ` "${key}": "${t.translations[key]}",\r` | |
}) | |
content += '}' | |
fs.writeFileSync(`${basePath}/${t.tag}/index.js`, content) | |
} catch (err) { | |
console.error(err, t) | |
} | |
let indexContent = imports | |
indexContent += '\r' | |
indexContent += exports | |
indexContent += '}' | |
fs.writeFileSync(`${basePath}/index.js`, indexContent) | |
fs.writeFileSync(`${basePath}/options.json`, JSON.stringify(options, null, 3)) | |
}) | |
} | |
saveTranslations() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment