diff --git a/csv_to_vanilla-i18n/csv_to_json.js b/csv_to_vanilla-i18n/csv_to_json.js new file mode 100644 index 0000000..114bbb5 --- /dev/null +++ b/csv_to_vanilla-i18n/csv_to_json.js @@ -0,0 +1,80 @@ +const fs = require('fs'); +const path = require('path'); +const readline = require('readline'); +const r1 = readline.createInterface({ + input: process.stdin, + output: process.stdout, +}); +const dirPath = path.join(__dirname, '/vanilla-i18n'); + +r1.question('Enter path of file: ', (csv) => { + if (fs.existsSync(csv)) { + const csvFile = fs.readFileSync(csv, { encoding: 'utf8' }); + const languages = csvFile + .split('\r\n') + .splice(0, 1) + .join('') + .split(',') + .splice(1); + const csvArr = csvFile.split('\r\n').splice(1); + let csvTemp = []; + + for (let k = 0; k < csvArr.length; k++) { + csvTemp[k] = csvArr[k].split(','); + } + + const transpose = csvTemp[0].map((_, colIndex) => + csvTemp.map((row) => row[colIndex]) + ); + + const csvTransposed = transpose.join('\n').split('\n').splice(1); + const header = transpose.join('\n').split('\n').splice(0, 1); + + const constructObj = (str, parentObj, data) => { + if (str.split('.').length === 1) { + parentObj[str] = data; + return parentObj; + } + + let curKey = str.split('.')[0]; + if (!parentObj[curKey]) parentObj[curKey] = {}; + parentObj[curKey] = constructObj( + str.split('.').slice(1).join('.'), + parentObj[curKey], + data + ); + return parentObj; + }; + + const csvFinal = csvTransposed.map((row) => { + let obj = {}; + let rowData = row.split(','); + header[0].split(',').forEach(function (val, idx) { + obj = constructObj(val, obj, rowData[idx]); + }); + return obj; + }); + + const create = (csvFinal, dirPath) => { + if (!fs.existsSync(dirPath)) { + fs.mkdirSync(dirPath); + create(csvFinal, dirPath); + } else { + for (let i = 0; i < languages.length; i++) { + fs.writeFileSync( + `${dirPath}/${languages[i]}.json`, + JSON.stringify(csvFinal.splice(0, 1)).replace(/^\[|]$/g, ''), + 'UTF-8' + ); + } + } + }; + + create(csvFinal, dirPath); + + r1.close(); + } else { + console.log('Invalid File CSV file'); + r1.close(); + } +}); \ No newline at end of file diff --git "a/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" "b/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" index 353ed78..2df6dc8 100644 --- "a/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" +++ "b/csv_to_vanilla-i18n/vanilla-i18n/espa\303\261ola.json" @@ -1 +1 @@ -{"language":"idioma","form":{"name":"nombre","age":"a\u00f1os","resp":{"yes":"si"}}} \ No newline at end of file +{"language":"idioma","form":{"name":"nombre","age":"años","resp":{"yes":"si"}}} \ No newline at end of file diff --git "a/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" "b/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" index b69905b..0f04079 100644 --- "a/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" +++ "b/csv_to_vanilla-i18n/vanilla-i18n/fran\303\247ais.json" @@ -1 +1 @@ -{"language":"Langue","form":{"name":"Nom","age":"\u00e2ge","resp":{"yes":"Oui"}}} \ No newline at end of file +{"language":"Langue","form":{"name":"Nom","age":"âge","resp":{"yes":"Oui"}}} \ No newline at end of file diff --git "a/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" "b/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" index 0e8a84f..a426c76 100644 --- "a/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" +++ "b/csv_to_vanilla-i18n/vanilla-i18n/\340\244\271\340\244\277\340\244\250\340\245\215\340\244\246\340\245\200.json" @@ -1 +1 @@ -{"language":"\u092d\u093e\u0937\u093e","form":{"name":"\u0928\u093e\u092e","age":"\u0909\u092e\u094d\u0930","resp":{"yes":"\u0939\u093e\u0901"}}} \ No newline at end of file +{"language":"भाषा","form":{"name":"नाम","age":"उम्र","resp":{"yes":"हाँ"}}} \ No newline at end of file