Created
May 1, 2018 16:25
-
-
Save pmaoui/b9ac8e00a10a4abde0634dd686705cce to your computer and use it in GitHub Desktop.
Extract key/value from JSON to a csv format.
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 _ = require('lodash'); | |
const dot = require('dot-object'); | |
const [node, namefile, ...langs] = process.argv; | |
const separator = '";"'; | |
const AllTrads = {}; | |
const translations = [];; | |
langs.forEach((lng) => { | |
const lnFiles = fs.readdirSync(lng); | |
lnFiles.forEach((file) => { | |
const scope = file.split('.translation.json').join(''); | |
const lang = JSON.parse(fs.readFileSync(lng + '/' + file, 'utf-8')); | |
const obj = dot.dot(lang); | |
AllTrads[scope] = _.isEmpty(AllTrads[scope]) ? {} : AllTrads[scope]; | |
Object.entries(obj).forEach(([key, val]) => { | |
AllTrads[scope][key] = _.isEmpty(AllTrads[scope][key]) ? {} : AllTrads[scope][key]; | |
AllTrads[scope][key][lng] = val; | |
}); | |
}); | |
}); | |
Object.keys(AllTrads).forEach((scope) => { | |
Object.keys(AllTrads[scope]).forEach((key) => { | |
const val = AllTrads[scope][key] | |
const trad = `"${key}${separator}${scope}${separator}${langs.map((ln) => val[ln] ? val[ln].trim():'').join(separator)}"`; | |
translations.push(trad.trim()); | |
}); | |
}); | |
_.compact(translations).forEach(t => console.log(t)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment