Skip to content

Commit

Permalink
Merge pull request #56 from pettyalex/master
Browse files Browse the repository at this point in the history
Fix parsing of .po files with no headers
  • Loading branch information
mikeedwards authored Aug 29, 2016
2 parents 77f5dcd + 13be2c9 commit 4520fb0
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
6 changes: 4 additions & 2 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ module.exports = function(buffer, options) {

Object.keys(contexts).forEach(function (context) {
var translations = parsed.translations[context];
var pluralForms = parsed.headers['plural-forms'];
var pluralForms = parsed.headers ? parsed.headers['plural-forms'] : '';

Object.keys(translations).forEach(function (key, i) {
var t = translations[key],
Expand Down Expand Up @@ -66,7 +66,9 @@ module.exports = function(buffer, options) {
});

// Attach headers (overwrites any empty translation keys that may have somehow gotten in)
result[''] = parsed.headers;
if (parsed.headers) {
result[''] = parsed.headers;
}

if (options.format === 'mf') {
delete result[''];
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "po2json",
"description": "Convert PO files to JSON",
"version": "0.4.3",
"version": "0.4.4",
"homepage": "https://github.com/mikeedwards/po2json",
"author": {
"name": "Joshua I. Miller",
Expand Down Expand Up @@ -33,6 +33,10 @@
},
{
"name": "rafalt-iRonin"
},
{
"name": "Alex Petty",
"email": "[email protected]"
}
],
"repository": {
Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/en-no-header.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Hello World": [
null,
"Hello World"
]
}
4 changes: 4 additions & 0 deletions test/fixtures/en-no-header.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Very minimal .po

msgid "Hello World"
msgstr "Hello World"
14 changes: 14 additions & 0 deletions test/po2json_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,17 @@ module.exports["parse with Plural-Forms == nplurals=1; plural=0;"] = {
test.done();
}
}

module.exports["parse with no headers"] ={
setUp: function(callback){
this.po = fs.readFileSync(__dirname + "/fixtures/en-no-header.po");
this.json = JSON.parse(fs.readFileSync(__dirname + "/fixtures/en-no-header.json", "utf-8"));
callback();
},

parse: function(test){
var parsed = po2json.parse(this.po);
test.deepEqual(parsed, this.json);
test.done();
}
}

0 comments on commit 4520fb0

Please sign in to comment.