Skip to content

Instantly share code, notes, and snippets.

@pmaoui
Created May 1, 2018 16:25
Show Gist options
  • Save pmaoui/b9ac8e00a10a4abde0634dd686705cce to your computer and use it in GitHub Desktop.
Save pmaoui/b9ac8e00a10a4abde0634dd686705cce to your computer and use it in GitHub Desktop.
Extract key/value from JSON to a csv format.
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