Last active
August 21, 2019 19:52
-
-
Save ToxicTree/1429957870330b82263c1d6213a4a72e to your computer and use it in GitHub Desktop.
Node.js script to concat .csv files with translations into a single .json file.
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 fs = require('fs'); | |
const localizationDir = ""; | |
const buildDir = "build"; | |
function showHelp() { | |
console.log( | |
"\nUsage: " | |
+ "node " | |
+ process.argv[1].substring(process.argv[1].lastIndexOf("\\") + process.argv[1].lastIndexOf("/") + 2) | |
+ " [options]" + "\n\n" | |
+ " help \t\t\t show this help\n" | |
+ "\n" | |
+ " list \t\t\t show all languages and their codes\n" | |
+ " build \t\t\t concat all .csv files into a .json\n" | |
+ " build [language_code]\t only build translations for language with language code\n" | |
+ "\n" | |
); | |
} | |
// Supported languages | |
const languages = [ | |
{code: "ar", name: "Arabic"}, | |
{code: "bs", name: "Bosnian"}, | |
{code: "cs", name: "Czech"}, | |
{code: "de", name: "German"}, | |
{code: "el", name: "Greek"}, | |
{code: "en", name: "English"}, | |
{code: "es-ES", name: "Spanish", folder: "es"}, | |
{code: "fa", name: "Persian"}, | |
{code: "fil", name: "Filipino"}, | |
{code: "fr", name: "French"}, | |
{code: "hr", name: "Croatian"}, | |
{code: "id", name: "Indonesian"}, | |
{code: "ja", name: "Japanese"}, | |
{code: "lv", name: "Latvian"}, | |
{code: "pl", name: "Polish"}, | |
{code: "pt-BR", name: "PortugueseBrazilian", folder: "pt_BR"}, | |
{code: "ro", name: "Romanian"}, | |
{code: "ru", name: "Russian"}, | |
{code: "sr", name: "Serbian", folder: "sr_Latn"}, | |
{code: "sv-SE", name: "Swedish", folder: "sv"}, | |
{code: "tr", name: "Turkish"}, | |
]; | |
function buildTranslations(selectedLang) { | |
languages.forEach((lang) => { | |
// If running for a specific language, skip others. | |
if (selectedLang && selectedLang != lang.code) | |
return; | |
let path = localizationDir + (lang.folder ? lang.folder : lang.code) + "/"; | |
if (!fs.existsSync(path)) { | |
console.log("The folder is missing. " + path); | |
return; | |
} | |
let data = {}; | |
try { | |
let files = fs.readdirSync(path); | |
for (let i = 0; i < files.length; i++) { | |
let lines = fs.readFileSync(path + files[i], {encoding: 'utf-8'}).split("\n"); | |
for (let l = 0; l < lines.length; l++) { | |
let key = lines[l].substr(1, lines[l].indexOf(':') - 2); | |
let value = lines[l].substr(lines[l].indexOf(':') + 2); | |
value = value.substr(0, value.length - 2); | |
if (key && value && key != "source") { | |
data[key] = value.replace(/\"\"/g,"\""); | |
} | |
} | |
} | |
// Test | |
// console.log(JSON.stringify(data)); | |
} catch (err) { | |
console.error(err); | |
} | |
// Save as JSON | |
try { | |
if (!fs.existsSync(buildDir)) { | |
fs.mkdirSync(buildDir); | |
} | |
fs.writeFileSync(buildDir + "/" + lang.name + ".json", "[" + JSON.stringify(data, null, 4) + "]"); | |
console.log("The file was saved! "); | |
} catch (err) { | |
console.error(err); | |
} | |
}); | |
} | |
if (process.argv.length > 2) { | |
if (process.argv[2].indexOf("list") != -1) { | |
console.info("Code\t Folder\t Name"); | |
languages.forEach((lang) => { | |
console.log(lang.code + "\t " + (lang.folder ? lang.folder : lang.code) + "\t " + lang.name); | |
}); | |
} else if (process.argv[2].indexOf("build") != -1) { | |
buildTranslations(process.argv[3]); | |
} else { | |
showHelp(); | |
} | |
} else { | |
showHelp(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment