add parsing support for msgctxt field of poitems

The gettext format specifies a msgctxt field for translated items. An
example of how to use this field has been added to the tests and the
parsing of this attribute has been implemented.
This commit is contained in:
Julian Bäume 2014-01-21 13:44:52 +01:00
parent 94ad44f953
commit 6790bfb466
3 changed files with 27 additions and 1 deletions

View File

@ -139,6 +139,10 @@ PO.parse = function (data) {
item.msgstr[plural] = extract(line); item.msgstr[plural] = extract(line);
context = 'msgstr'; context = 'msgstr';
} }
else if (line.match(/^msgctxt/)) { // Context
finish();
item.msgctxt = extract(line);
}
else { // Probably multiline string or blank else { // Probably multiline string or blank
if (line.length > 0) { if (line.length > 0) {
if (context === 'msgstr') { if (context === 'msgstr') {
@ -160,6 +164,7 @@ PO.parse = function (data) {
PO.Item = function () { PO.Item = function () {
this.msgid = ''; this.msgid = '';
this.msgctxt = '';
this.references = []; this.references = [];
this.msgid_plural = null; this.msgid_plural = null;
this.msgstr = []; this.msgstr = [];

10
test/fixtures/big.po vendored
View File

@ -285,3 +285,13 @@ msgstr "Attribut 'title' du lien"
# Comment # Comment
msgid "Title, as plain text" msgid "Title, as plain text"
msgstr "Attribut title, en tant que texte brut" msgstr "Attribut title, en tant que texte brut"
# Empty should be adjective
msgctxt "folder display"
msgid "Empty folder"
msgstr "This folder is empty."
# Empty should be verb
msgctxt "folder action"
msgid "Empty folder"
msgstr "Make this folder empty."

View File

@ -6,7 +6,7 @@ describe('Parse', function () {
it('Parses the big po file', function () { it('Parses the big po file', function () {
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8')); var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8'));
assert.notEqual(po, null); assert.notEqual(po, null);
assert.equal(po.items.length, 67); assert.equal(po.items.length, 69);
var item = po.items[0]; var item = po.items[0];
assert.equal(item.msgid, "Title"); assert.equal(item.msgid, "Title");
@ -62,4 +62,15 @@ describe('Parse', function () {
assert.notEqual(item.flags, null); assert.notEqual(item.flags, null);
assert.equal(item.flags.fuzzy, true); assert.equal(item.flags.fuzzy, true);
}); });
it('Parses item context', function () {
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8'));
var ambiguousItems = po.items.filter(function (item) {
return item.msgid === 'Empty folder';
});
assert.equal(ambiguousItems[0].msgctxt, 'folder display');
assert.equal(ambiguousItems[1].msgctxt, 'folder action');
});
}); });