Merge pull request #19 from rubenv/no_headers

Add proper support for no headers in po files
This commit is contained in:
Ruben Vermeersch 2015-09-23 07:40:44 +02:00
commit 1bf498ecf7
3 changed files with 59 additions and 3 deletions

View File

@ -64,11 +64,16 @@ PO.parse = function (data) {
data = data.replace(/\r\n/g, '\n'); data = data.replace(/\r\n/g, '\n');
var po = new PO(); var po = new PO();
var sections = data.split(/\n\n/); var sections = data.split(/\n\n/);
var headers = [sections.shift()]; var headers = [];
//everything until the first 'msgid ""' is considered header //everything until the first 'msgid ""' is considered header
while (headers[headers.length - 1].indexOf('msgid ""') < 0) { while (sections[0] && (headers.length === 0 || headers[headers.length - 1].indexOf('msgid ""') < 0)) {
if (sections[0].match(/msgid "[^"]/)) {
//found first real string, adding a dummy header item
headers.push('msgid ""');
} else {
headers.push(sections.shift()); headers.push(sections.shift());
} }
}
headers = headers.join('\n'); headers = headers.join('\n');
var lines = sections.join('\n').split(/\n/); var lines = sections.join('\n').split(/\n/);

8
test/fixtures/no_header.po vendored Normal file
View File

@ -0,0 +1,8 @@
# some comment
msgid "First id, no header"
msgstr ""
msgid "A second string"
msgstr ""

View File

@ -28,3 +28,46 @@ describe('Headers', function () {
assert.equal(Object.keys(po.headers).length, 12); assert.equal(Object.keys(po.headers).length, 12);
}); });
}); });
describe('PO files with no headers', function () {
it('Parses an empty string', function () {
var po = PO.parse('');
assert.notEqual(po, null);
// all headers should be empty
for (var key in po.headers) {
assert.equal(po.headers[key], '');
}
assert.equal(po.items.length, 0);
});
it('Parses a minimal example', function () {
var po = PO.parse('msgid "minimal PO"\nmsgstr ""');
assert.notEqual(po, null);
// all headers should be empty
for (var key in po.headers) {
assert.equal(po.headers[key], '');
}
assert.equal(po.items.length, 1);
});
describe('advanced example', function () {
var po;
before(function (done) {
PO.load(__dirname + '/fixtures/no_header.po', function (err, result) {
assert.equal(err, null);
po = result;
done();
});
});
it('Parses the po file', function () {
assert.notEqual(po, null);
});
it('Finds all items', function () {
assert.equal(po.items.length, 2);
});
});
});