Created
February 12, 2019 06:03
-
-
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)
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'); | |
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