diff --git a/README.md b/README.md index 335e0b3..c2bf1d6 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,15 @@ Used to load and save PO files. - var po = require('po') - , fs = require('fs') - - po.load('text.po', function(_po){ - console.log(_po.headers); - console.log(_po.items); - - _po.save('copy.po', function(){ - console.log('We copied a PO file for no reason!'); - }); +```javascript +var po = require('po') + , fs = require('fs') + +po.load('text.po', function(_po){ + console.log(_po.headers); + console.log(_po.items); + + _po.save('copy.po', function(){ + console.log('We copied a PO file for no reason!'); }); - \ No newline at end of file +}); +``` diff --git a/lib/po.js b/lib/po.js index 2accd8f..e7bb56d 100644 --- a/lib/po.js +++ b/lib/po.js @@ -9,6 +9,7 @@ var PO = function() { this.comments = []; this.headers = {}; this.items = []; + this.errors = []; }; PO.prototype.save = function(filename, callback) { @@ -73,16 +74,27 @@ PO.parse = function(data) { 'Plural-Forms': '', }; - headers.split(/\n/).forEach(function(header) { - if (header.match(/^#/)) { + headers.split(/\n/).forEach(function(header, i) { + if (/^#/.test(header)) { po.comments.push(header.replace(/^#\s*/, '')); } - if (header.match(/^"/)) { - header = header.trim().replace(/^"/, '').replace(/\\n"$/, ''); + else if (/^"/.test(header)) { + header = header.trim().replace(/^"|"$/g, '');//.replace(/"$/, ''); var p = header.split(/:/) , name = p.shift().trim() - , value = p.join(':').trim().replace(/n$/); - po.headers[name] = value; + , value = p.join(':').trim().replace(/\\n$/, ''); + if (po.headers[name] && po.headers[name].length) { + po.errors.push("skipping duplicate header line "+(i+1)+": "+header); + } + else if (/#-#-#-#-#/.test(name)) { + po.errors.push("skipping error marker in header line "+(i+1)+": "+header); + } + else { + po.headers[name] = value; + } + } + else { + po.errors.push("problem line in header line "+(i+1)+": "+header); } }); @@ -108,24 +120,24 @@ PO.parse = function(data) { while (lines.length > 0) { var line = trim(lines.shift()) , add = false; - if (line.match(/^#:/)) { // Reference + if (/^#:/.test(line)) { // Reference finish(); item.references.push(trim(line.replace(/^#:/, ''))); } - else if (line.match(/^#/)) { // Comment + else if (/^#/.test(line)) { // Comment finish(); item.comments.push(trim(line.replace(/^#/, ''))); } - else if (line.match(/^msgid_plural/)) { // Plural form + else if (/^msgid_plural/.test(line)) { // Plural form item.msgid_plural = extract(line); context = 'msgid_plural'; } - else if (line.match(/^msgid/)) { // Original + else if (/^msgid/.test(line)) { // Original finish(); item.msgid = extract(line); context = 'msgid'; } - else if (line.match(/^msgstr/)) { // Translation + else if (/^msgstr/.test(line)) { // Translation var m = line.match(/^msgstr\[(\d+)\]/); plural = m && m[1] ? parseInt(m[1]) : 0; item.msgstr[plural] = extract(line); diff --git a/package.json b/package.json index 27282b6..eaecc90 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "author" : "Mike Holly", "homepage" : "http://github.com/mikejholly/node-po", "repository" : {"type" : "git", "url" : "http://github.com/mikejholly/node-po.git"}, + "contributors" : ["dodo (https://github.com/dodo)"], "dependencies" : [], "main" : "./lib/po", "keywords" : ["i18n", "l10n", "gettext", "mo", "po"] diff --git a/tests/run.js b/tests/run.js index cb52d88..7ba485d 100644 --- a/tests/run.js +++ b/tests/run.js @@ -4,6 +4,7 @@ var PO = require('../lib/po.js') PO.load('text.po', function(po) { assert.equal(po.headers['Plural-Forms'], 'nplurals=2; plural=(n!=1);', 'Parsed "Plural-Forms" header.'); + assert.equal(po.errors.length, 3, 'Two lines before headers and one duplicate header found.'); assert.equal(po.items.length, 67, 'Successfully parsed 67 items.'); var item = po.items.pop(); assert.equal(item.comments.length, 1, 'Parsed item comment.'); diff --git a/tests/text.po b/tests/text.po index 4dd22b5..05e8b57 100644 --- a/tests/text.po +++ b/tests/text.po @@ -11,6 +11,7 @@ msgstr "" "Content-Type: text/plain; charset=utf-8\n" "Content-Transfer-Encoding: 8bit\n" "Plural-Forms: nplurals=2; plural=(n!=1);\n" +"Language-Team: FrEnch\n" msgid "Title" msgstr "Titre"