Created
March 8, 2012 10:20
-
-
Save xenophy/2000164 to your computer and use it in GitHub Desktop.
.po file to JSON
This file contains hidden or 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
/*! | |
* po2json | |
* Copyright (c)2012 Xenophy.CO.,LTD All rights Reserved. | |
* http://www.xenophy.com | |
*/ | |
// {{{ po2json | |
var fs = require('fs'), | |
path = require('path'); | |
(function() { | |
var pofile = process.argv[2]; | |
if(!pofile) { | |
console.log('.po file is not specifed.'); | |
return; | |
} | |
if(!path.existsSync(pofile)) { | |
console.log('.po file is not exists.'); | |
return; | |
} | |
var src = fs.readFileSync(pofile); | |
var dest = {}; | |
src = src.toString(); | |
src = src.split("\n"); | |
src.forEach(function(line, i) { | |
if(line.substr(0, 'msgid'.length) === 'msgid') { | |
var s = line.indexOf('"'); | |
var e = line.lastIndexOf('"'); | |
var id = line.substr(s + 1, e - s - 1); | |
if(id) { | |
var msgstr = src[i + 1]; | |
var s = msgstr.indexOf('"'); | |
var e = msgstr.lastIndexOf('"'); | |
dest[id] = msgstr.substr(s + 1, e - s - 1); | |
} | |
} | |
}); | |
console.log(JSON.stringify(dest)); | |
})(); | |
// }}} | |
/* | |
* Local variables: | |
* tab-width: 4 | |
* c-basic-offset: 4 | |
* c-hanging-comment-ender-p: nil | |
* End: | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment