Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fbe773c636 | ||
|
|
7ceda82794 | ||
|
|
d1be0f51b0 | ||
|
|
869f763d80 | ||
|
|
b9394176b1 | ||
|
|
9060221403 | ||
|
|
63c4209cd5 | ||
|
|
104d114d5d | ||
|
|
db5e540824 | ||
|
|
cfc9b2ae82 |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pofile",
|
"name": "pofile",
|
||||||
"version": "0.2.12",
|
"version": "1.0.1",
|
||||||
"authors": [
|
"authors": [
|
||||||
"Ruben Vermeersch <ruben@rocketeer.be>"
|
"Ruben Vermeersch <ruben@rocketeer.be>"
|
||||||
],
|
],
|
||||||
|
|||||||
24
dist/pofile.js
vendored
24
dist/pofile.js
vendored
@@ -8,6 +8,7 @@ function trim(string) {
|
|||||||
|
|
||||||
var PO = function () {
|
var PO = function () {
|
||||||
this.comments = [];
|
this.comments = [];
|
||||||
|
this.extractedComments = [];
|
||||||
this.headers = {};
|
this.headers = {};
|
||||||
this.items = [];
|
this.items = [];
|
||||||
};
|
};
|
||||||
@@ -24,6 +25,11 @@ PO.prototype.toString = function () {
|
|||||||
lines.push('# ' + comment);
|
lines.push('# ' + comment);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (this.extractedComments) {
|
||||||
|
this.extractedComments.forEach(function (comment) {
|
||||||
|
lines.push('#. ' + comment);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
lines.push('msgid ""');
|
lines.push('msgid ""');
|
||||||
lines.push('msgstr ""');
|
lines.push('msgstr ""');
|
||||||
@@ -59,7 +65,12 @@ 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 = [sections.shift()];
|
||||||
|
//everything until the first 'msgid ""' is considered header
|
||||||
|
while (headers[headers.length - 1].indexOf('msgid ""') < 0) {
|
||||||
|
headers.push(sections.shift());
|
||||||
|
}
|
||||||
|
headers = headers.join('\n');
|
||||||
var lines = sections.join('\n').split(/\n/);
|
var lines = sections.join('\n').split(/\n/);
|
||||||
|
|
||||||
po.headers = {
|
po.headers = {
|
||||||
@@ -87,10 +98,11 @@ PO.parse = function (data) {
|
|||||||
acc.push(line);
|
acc.push(line);
|
||||||
return acc;
|
return acc;
|
||||||
}, []).forEach(function (header) {
|
}, []).forEach(function (header) {
|
||||||
if (header.match(/^#/)) {
|
if (header.match(/^#\./)) {
|
||||||
|
po.extractedComments.push(header.replace(/^#\.\s*/, ''));
|
||||||
|
} else if (header.match(/^#/)) {
|
||||||
po.comments.push(header.replace(/^#\s*/, ''));
|
po.comments.push(header.replace(/^#\s*/, ''));
|
||||||
}
|
} else if (header.match(/^"/)) {
|
||||||
if (header.match(/^"/)) {
|
|
||||||
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
||||||
var p = header.split(/:/);
|
var p = header.split(/:/);
|
||||||
var name = p.shift().trim();
|
var name = p.shift().trim();
|
||||||
@@ -290,7 +302,9 @@ PO.Item.prototype.toString = function () {
|
|||||||
lines.push('#: ' + ref);
|
lines.push('#: ' + ref);
|
||||||
});
|
});
|
||||||
|
|
||||||
var flags = Object.keys(this.flags);
|
var flags = Object.keys(this.flags).filter(function (flag) {
|
||||||
|
return !!this.flags[flag];
|
||||||
|
}, this);
|
||||||
if (flags.length > 0) {
|
if (flags.length > 0) {
|
||||||
lines.push('#, ' + flags.join(','));
|
lines.push('#, ' + flags.join(','));
|
||||||
}
|
}
|
||||||
|
|||||||
2
dist/pofile.min.js
vendored
2
dist/pofile.min.js
vendored
File diff suppressed because one or more lines are too long
24
lib/po.js
24
lib/po.js
@@ -7,6 +7,7 @@ function trim(string) {
|
|||||||
|
|
||||||
var PO = function () {
|
var PO = function () {
|
||||||
this.comments = [];
|
this.comments = [];
|
||||||
|
this.extractedComments = [];
|
||||||
this.headers = {};
|
this.headers = {};
|
||||||
this.items = [];
|
this.items = [];
|
||||||
};
|
};
|
||||||
@@ -23,6 +24,11 @@ PO.prototype.toString = function () {
|
|||||||
lines.push('# ' + comment);
|
lines.push('# ' + comment);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (this.extractedComments) {
|
||||||
|
this.extractedComments.forEach(function (comment) {
|
||||||
|
lines.push('#. ' + comment);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
lines.push('msgid ""');
|
lines.push('msgid ""');
|
||||||
lines.push('msgstr ""');
|
lines.push('msgstr ""');
|
||||||
@@ -58,7 +64,12 @@ 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 = [sections.shift()];
|
||||||
|
//everything until the first 'msgid ""' is considered header
|
||||||
|
while (headers[headers.length - 1].indexOf('msgid ""') < 0) {
|
||||||
|
headers.push(sections.shift());
|
||||||
|
}
|
||||||
|
headers = headers.join('\n');
|
||||||
var lines = sections.join('\n').split(/\n/);
|
var lines = sections.join('\n').split(/\n/);
|
||||||
|
|
||||||
po.headers = {
|
po.headers = {
|
||||||
@@ -86,10 +97,11 @@ PO.parse = function (data) {
|
|||||||
acc.push(line);
|
acc.push(line);
|
||||||
return acc;
|
return acc;
|
||||||
}, []).forEach(function (header) {
|
}, []).forEach(function (header) {
|
||||||
if (header.match(/^#/)) {
|
if (header.match(/^#\./)) {
|
||||||
|
po.extractedComments.push(header.replace(/^#\.\s*/, ''));
|
||||||
|
} else if (header.match(/^#/)) {
|
||||||
po.comments.push(header.replace(/^#\s*/, ''));
|
po.comments.push(header.replace(/^#\s*/, ''));
|
||||||
}
|
} else if (header.match(/^"/)) {
|
||||||
if (header.match(/^"/)) {
|
|
||||||
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
|
||||||
var p = header.split(/:/);
|
var p = header.split(/:/);
|
||||||
var name = p.shift().trim();
|
var name = p.shift().trim();
|
||||||
@@ -289,7 +301,9 @@ PO.Item.prototype.toString = function () {
|
|||||||
lines.push('#: ' + ref);
|
lines.push('#: ' + ref);
|
||||||
});
|
});
|
||||||
|
|
||||||
var flags = Object.keys(this.flags);
|
var flags = Object.keys(this.flags).filter(function (flag) {
|
||||||
|
return !!this.flags[flag];
|
||||||
|
}, this);
|
||||||
if (flags.length > 0) {
|
if (flags.length > 0) {
|
||||||
lines.push('#, ' + flags.join(','));
|
lines.push('#, ' + flags.join(','));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "pofile",
|
"name": "pofile",
|
||||||
"description": "Parse and serialize Gettext PO files.",
|
"description": "Parse and serialize Gettext PO files.",
|
||||||
"version": "0.2.12",
|
"version": "1.0.1",
|
||||||
"author": {
|
"author": {
|
||||||
"name": "Ruben Vermeersch",
|
"name": "Ruben Vermeersch",
|
||||||
"email": "ruben@savanne.be",
|
"email": "ruben@savanne.be",
|
||||||
|
|||||||
1
test/fixtures/big.po
vendored
1
test/fixtures/big.po
vendored
@@ -1,4 +1,5 @@
|
|||||||
# French translation of Link (6.x-2.9)
|
# French translation of Link (6.x-2.9)
|
||||||
|
|
||||||
# Copyright (c) 2011 by the French translation team
|
# Copyright (c) 2011 by the French translation team
|
||||||
#
|
#
|
||||||
msgid ""
|
msgid ""
|
||||||
|
|||||||
1
test/fixtures/comment.po
vendored
1
test/fixtures/comment.po
vendored
@@ -1,6 +1,7 @@
|
|||||||
# French translation of Link (6.x-2.9)
|
# French translation of Link (6.x-2.9)
|
||||||
# Copyright (c) 2011 by the French translation team
|
# Copyright (c) 2011 by the French translation team
|
||||||
#
|
#
|
||||||
|
#. extracted from test
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: Link (6.x-2.9)\n"
|
"Project-Id-Version: Link (6.x-2.9)\n"
|
||||||
|
|||||||
4
test/fixtures/reference.po
vendored
4
test/fixtures/reference.po
vendored
@@ -24,3 +24,7 @@ msgstr "Attribut title, en tant que texte brut"
|
|||||||
#: b
|
#: b
|
||||||
msgid "X"
|
msgid "X"
|
||||||
msgstr "Y"
|
msgstr "Y"
|
||||||
|
|
||||||
|
#: standard input:12 standard input:17
|
||||||
|
msgid "Z"
|
||||||
|
msgstr "ZZ"
|
||||||
|
|||||||
@@ -61,29 +61,43 @@ describe('Parse', function () {
|
|||||||
assert.notEqual(po, null);
|
assert.notEqual(po, null);
|
||||||
assert.equal(po.items.length, 2);
|
assert.equal(po.items.length, 2);
|
||||||
|
|
||||||
|
assert.equal(po.extractedComments.length, 1);
|
||||||
|
assert.equal(po.extractedComments[0], 'extracted from test');
|
||||||
|
|
||||||
var item = po.items[0];
|
var item = po.items[0];
|
||||||
assert.equal(item.msgid, 'Title, as plain text');
|
assert.equal(item.msgid, 'Title, as plain text');
|
||||||
assert.equal(item.msgstr, 'Attribut title, en tant que texte brut');
|
assert.equal(item.msgstr, 'Attribut title, en tant que texte brut');
|
||||||
assert.deepEqual(item.extractedComments, ['Extracted comment']);
|
assert.deepEqual(item.extractedComments, ['Extracted comment']);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Handles string references', function () {
|
describe('Handles string references', function () {
|
||||||
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/reference.po', 'utf8'));
|
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/reference.po', 'utf8'));
|
||||||
assert.notEqual(po, null);
|
assert.notEqual(po, null);
|
||||||
assert.equal(po.items.length, 2);
|
assert.equal(po.items.length, 3);
|
||||||
|
|
||||||
|
it('in simple cases', function () {
|
||||||
var item = po.items[0];
|
var item = po.items[0];
|
||||||
assert.equal(item.msgid, 'Title, as plain text');
|
assert.equal(item.msgid, 'Title, as plain text');
|
||||||
assert.equal(item.msgstr, 'Attribut title, en tant que texte brut');
|
assert.equal(item.msgstr, 'Attribut title, en tant que texte brut');
|
||||||
assert.deepEqual(item.comments, ['Comment']);
|
assert.deepEqual(item.comments, ['Comment']);
|
||||||
assert.deepEqual(item.references, ['.tmp/crm/controllers/map.js']);
|
assert.deepEqual(item.references, ['.tmp/crm/controllers/map.js']);
|
||||||
|
});
|
||||||
|
|
||||||
item = po.items[1];
|
it('with two different references', function () {
|
||||||
|
var item = po.items[1];
|
||||||
assert.equal(item.msgid, 'X');
|
assert.equal(item.msgid, 'X');
|
||||||
assert.equal(item.msgstr, 'Y');
|
assert.equal(item.msgstr, 'Y');
|
||||||
assert.deepEqual(item.references, ['a', 'b']);
|
assert.deepEqual(item.references, ['a', 'b']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('and does not process reference items', function () {
|
||||||
|
var item = po.items[2];
|
||||||
|
assert.equal(item.msgid, 'Z');
|
||||||
|
assert.equal(item.msgstr, 'ZZ');
|
||||||
|
assert.deepEqual(item.references, ['standard input:12 standard input:17']);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
it('Parses flags', function () {
|
it('Parses flags', function () {
|
||||||
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8'));
|
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8'));
|
||||||
assert.notEqual(po, null);
|
assert.notEqual(po, null);
|
||||||
@@ -145,12 +159,12 @@ describe('Parse', function () {
|
|||||||
assert.equal(items[0].msgid, 'The name field must not contain characters like " or \\');
|
assert.equal(items[0].msgid, 'The name field must not contain characters like " or \\');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle \n characters', function () {
|
it('should handle \\n characters', function () {
|
||||||
var item = po.items[1];
|
var item = po.items[1];
|
||||||
assert.equal(item.msgid, '%1$s\n%2$s %3$s\n%4$s\n%5$s');
|
assert.equal(item.msgid, '%1$s\n%2$s %3$s\n%4$s\n%5$s');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should handle \t characters', function () {
|
it('should handle \\t characters', function () {
|
||||||
var item = po.items[2];
|
var item = po.items[2];
|
||||||
assert.equal(item.msgid, 'define(\'some/test/module\', function () {\n' +
|
assert.equal(item.msgid, 'define(\'some/test/module\', function () {\n' +
|
||||||
'\t\'use strict\';\n' +
|
'\t\'use strict\';\n' +
|
||||||
|
|||||||
@@ -16,6 +16,20 @@ function assertHasLine(str, line) {
|
|||||||
assert(found, 'Could not find line: ' + line);
|
assert(found, 'Could not find line: ' + line);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function assertDoesntHaveLine(str, line) {
|
||||||
|
var lines = str.split('\n');
|
||||||
|
var found = false;
|
||||||
|
|
||||||
|
for (var i = 0; i < lines.length; i++) {
|
||||||
|
if (lines[i].trim() === line) {
|
||||||
|
found = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert(!found, 'Shouldn\'t have line: ' + line);
|
||||||
|
}
|
||||||
|
|
||||||
describe('Write', function () {
|
describe('Write', function () {
|
||||||
it('write flags', function () {
|
it('write flags', function () {
|
||||||
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
||||||
@@ -24,6 +38,17 @@ describe('Write', function () {
|
|||||||
assertHasLine(str, '#, fuzzy');
|
assertHasLine(str, '#, fuzzy');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('write flags only when true', function () {
|
||||||
|
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
||||||
|
var po = PO.parse(input);
|
||||||
|
|
||||||
|
// Flip flag
|
||||||
|
po.items[0].flags.fuzzy = false;
|
||||||
|
|
||||||
|
var str = po.toString();
|
||||||
|
assertDoesntHaveLine(str, '#, fuzzy');
|
||||||
|
});
|
||||||
|
|
||||||
it('write msgid', function () {
|
it('write msgid', function () {
|
||||||
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
var input = fs.readFileSync(__dirname + '/fixtures/fuzzy.po', 'utf8');
|
||||||
var po = PO.parse(input);
|
var po = PO.parse(input);
|
||||||
@@ -49,6 +74,7 @@ describe('Write', function () {
|
|||||||
var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8');
|
var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8');
|
||||||
var po = PO.parse(input);
|
var po = PO.parse(input);
|
||||||
var str = po.toString();
|
var str = po.toString();
|
||||||
|
assertHasLine(str, '#. extracted from test');
|
||||||
assertHasLine(str, '#. Extracted comment');
|
assertHasLine(str, '#. Extracted comment');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user