Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

errors and stuff #1

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -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!');
});

});
```
34 changes: 23 additions & 11 deletions lib/po.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var PO = function() {
this.comments = [];
this.headers = {};
this.items = [];
this.errors = [];
};

PO.prototype.save = function(filename, callback) {
Expand Down Expand Up @@ -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);
}
});

Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions tests/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
Expand Down
1 change: 1 addition & 0 deletions tests/text.po
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down