Created
September 19, 2021 16:16
-
-
Save majirosstefan/4d03081ce2daa1d4ca1b9a244ab9915d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 {createWriteStream} = require('fs'); | |
// installed from my own repository , globally | |
var TJO = require('translate-json-object')(); | |
// π | |
var json_SOURCE_OF_TRUTH = require('../src/localization/translations/en.json'); | |
var supportedLanguages = require('./supportedLanguages'); | |
var fs = require('fs'); | |
var path = require('path'); | |
// π | |
const outputDirectory = 'translations'; | |
TJO.init({ | |
// π | |
googleApiKey: '', | |
}); | |
(async function main() { | |
try { | |
const outputDirectoryPath = path.join(__dirname, outputDirectory); | |
const translationFolderExists = fs.existsSync(outputDirectoryPath); | |
if (!translationFolderExists) { | |
fs.mkdirSync(outputDirectoryPath); | |
} | |
// π | |
for (const lang of supportedLanguages) { | |
if (fileExists(lang) === false) { | |
let data = await TJO.translate(json_SOURCE_OF_TRUTH, lang); | |
writeToFile(data, lang); | |
sleep(1000); | |
} else { | |
console.log('File already exits for lang ', lang); | |
} | |
} | |
} catch (error) { | |
console.log('error ', error); | |
} | |
// π | |
console.log( | |
'Do not forget to run prettier on translations folder to make output pretty', | |
); | |
console.log('Now you can copy translations folder into app.'); | |
})(); | |
function fileExists(lang) { | |
return fs.existsSync(pathRelative(lang)); | |
} | |
function pathRelative(lang) { | |
return path.join(__dirname, outputDirectory, lang + '.json'); | |
} | |
const writeToFile = (data, lang = 'out.txt') => { | |
//TODO mkdir translation folder - if it does not exist, code fails | |
const stream = createWriteStream(pathRelative(lang)); | |
stream.once('open', function (fd) { | |
stream.write(JSON.stringify(data)); | |
stream.end(); | |
console.log('lang', lang, ' written'); | |
}); | |
}; | |
function sleep(ms = 1000) { | |
return new Promise(resolve => { | |
setTimeout(resolve, ms); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment