Skip to content

Instantly share code, notes, and snippets.

@mapmeld
Created February 12, 2019 06:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mapmeld/8ff95ba1e55fc3520ba95835fd6bc868 to your computer and use it in GitHub Desktop.
Save mapmeld/8ff95ba1e55fc3520ba95835fd6bc868 to your computer and use it in GitHub Desktop.
Convert PO file to JSON (assumes no weird exceptions like escaped-double characters, newlines)
const fs = require('fs');
fs.readFile('./out-blockly-0-all_ig.po', (err, content) => {
if (err) {
throw err;
}
let structure = {
"@metadata": {
"authors": [
"Mapmeld",
"Ukabia"
]
}
};
let lines = (content + '').split('\n'),
currentvar = null,
originalval = null;
lines.forEach((line) => {
if (line.indexOf('msgctxt') === 0) {
currentvar = line.split('"')[1];
} else if (line.indexOf('msgid') === 0) {
originalval = line.split('"')[1];
} else if (line.indexOf('msgstr') === 0) {
let setval = line.split('"')[1];
if (currentvar) {
if (setval.length) {
structure[currentvar] = setval;
} else {
structure[currentvar] = originalval;
}
}
currentvar = null;
originalval = null;
}
});
fs.writeFile('./ig.json', JSON.stringify(structure).replace(/\"\,/g, '",\n'), (err) => {
console.log(err || 'done');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment