From 6790bfb466b056fd7f6f9dc2e27561feeaaf99b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julian=20B=C3=A4ume?= Date: Tue, 21 Jan 2014 13:44:52 +0100 Subject: [PATCH] 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. --- lib/po.js | 5 +++++ test/fixtures/big.po | 10 ++++++++++ test/parse.js | 13 ++++++++++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/po.js b/lib/po.js index 2e0c586..0068547 100644 --- a/lib/po.js +++ b/lib/po.js @@ -139,6 +139,10 @@ PO.parse = function (data) { item.msgstr[plural] = extract(line); context = 'msgstr'; } + else if (line.match(/^msgctxt/)) { // Context + finish(); + item.msgctxt = extract(line); + } else { // Probably multiline string or blank if (line.length > 0) { if (context === 'msgstr') { @@ -160,6 +164,7 @@ PO.parse = function (data) { PO.Item = function () { this.msgid = ''; + this.msgctxt = ''; this.references = []; this.msgid_plural = null; this.msgstr = []; diff --git a/test/fixtures/big.po b/test/fixtures/big.po index d2a37a7..4638b40 100644 --- a/test/fixtures/big.po +++ b/test/fixtures/big.po @@ -285,3 +285,13 @@ msgstr "Attribut 'title' du lien" # Comment msgid "Title, as plain text" 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." diff --git a/test/parse.js b/test/parse.js index 3996326..483f303 100644 --- a/test/parse.js +++ b/test/parse.js @@ -6,7 +6,7 @@ describe('Parse', function () { it('Parses the big po file', function () { var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8')); assert.notEqual(po, null); - assert.equal(po.items.length, 67); + assert.equal(po.items.length, 69); var item = po.items[0]; assert.equal(item.msgid, "Title"); @@ -62,4 +62,15 @@ describe('Parse', function () { assert.notEqual(item.flags, null); 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'); + }); });