From ada76116b0eb84104f15de2095373be1b31daa3d Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Sat, 1 Mar 2014 17:21:18 -0500 Subject: [PATCH 1/3] Include comments in `PO.Item` output Fixes #5 --- lib/po.js | 6 ++++++ test/write.js | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/lib/po.js b/lib/po.js index e4a2d2c..c04c842 100644 --- a/lib/po.js +++ b/lib/po.js @@ -192,6 +192,12 @@ PO.Item.prototype.toString = function () { return lines; }; + if (this.comments.length > 0) { + this.comments.forEach(function (c) { + lines.push('# ' + c); + }); + } + if (this.references.length > 0) { this.references.forEach(function (ref) { lines.push('#: ' + ref); diff --git a/test/write.js b/test/write.js index 809b145..78708f3 100644 --- a/test/write.js +++ b/test/write.js @@ -38,6 +38,13 @@ describe('Write', function () { assertHasLine(str, "msgstr \"Source\""); }); + it('write translator comment', function () { + var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8'); + var po = PO.parse(input); + var str = po.toString(); + assertHasLine(str, "# Comment"); + }); + describe('msgctxt', function () { it('should write context field to file', function () { var input = fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8'); From ac85ba0b9c0ed69bcfdc8c809c7705d80bc486f8 Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Sun, 2 Mar 2014 13:11:37 -0500 Subject: [PATCH 2/3] Handle extracted comments --- lib/po.js | 20 +++++++++++++++++--- test/fixtures/comment.po | 3 ++- test/parse.js | 15 +++++++++++++-- test/write.js | 9 ++++++++- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/po.js b/lib/po.js index c04c842..82d13f1 100644 --- a/lib/po.js +++ b/lib/po.js @@ -120,9 +120,13 @@ PO.parse = function (data) { item.flags[flags[i]] = true; } } - else if (line.match(/^#/)) { // Comment + else if (line.match(/^#\s+/)) { // Translator comment finish(); - item.comments.push(trim(line.replace(/^#/, ''))); + item.comments.push(trim(line.replace(/^#\s+/, ''))); + } + else if (line.match(/^#\./)) { // Extracted comment + finish(); + item.extractedComments.push(trim(line.replace(/^#\./, ''))); } else if (line.match(/^msgid_plural/)) { // Plural form item.msgid_plural = extract(line); @@ -168,7 +172,8 @@ PO.Item = function () { this.references = []; this.msgid_plural = null; this.msgstr = []; - this.comments = []; + this.comments = []; // translator comments + this.extractedComments = []; this.flags = {}; }; @@ -192,12 +197,21 @@ PO.Item.prototype.toString = function () { return lines; }; + // https://www.gnu.org/software/gettext/manual/html_node/PO-Files.html + // says order is translator-comments, extracted-comments, references, flags + if (this.comments.length > 0) { this.comments.forEach(function (c) { lines.push('# ' + c); }); } + if (this.extractedComments.length > 0) { + this.extractedComments.forEach(function (c) { + lines.push('#. ' + c); + }); + } + if (this.references.length > 0) { this.references.forEach(function (ref) { lines.push('#: ' + ref); diff --git a/test/fixtures/comment.po b/test/fixtures/comment.po index 9deff3f..e50cd0c 100644 --- a/test/fixtures/comment.po +++ b/test/fixtures/comment.po @@ -15,6 +15,7 @@ msgstr "" "Language: fr\n" "X-Generator: Poedit 1.6.2\n" -# Comment +# Translator comment +#. Extracted comment msgid "Title, as plain text" msgstr "Attribut title, en tant que texte brut" diff --git a/test/parse.js b/test/parse.js index 483f303..e252b08 100644 --- a/test/parse.js +++ b/test/parse.js @@ -23,7 +23,7 @@ describe('Parse', function () { assert.equal(item.msgstr, "Les ébauches de jetons suivantes peuvent être utilisées à la fois dans les chemins et dans les titres. Lorsqu'elles sont utilisées dans un chemin ou un titre, elles seront remplacées par les valeurs appropriées."); }); - it('Handles string comments', function () { + it('Handles translator comments', function () { var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8')); assert.notEqual(po, null); assert.equal(po.items.length, 1); @@ -31,7 +31,18 @@ describe('Parse', function () { var item = po.items[0]; assert.equal(item.msgid, "Title, as plain text"); assert.equal(item.msgstr, "Attribut title, en tant que texte brut"); - assert.deepEqual(item.comments, ["Comment"]); + assert.deepEqual(item.comments, ["Translator comment"]); + }); + + it('Handles extracted comments', function () { + var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8')); + assert.notEqual(po, null); + assert.equal(po.items.length, 1); + + var item = po.items[0]; + assert.equal(item.msgid, "Title, as plain text"); + assert.equal(item.msgstr, "Attribut title, en tant que texte brut"); + assert.deepEqual(item.extractedComments, ["Extracted comment"]); }); it('Handles string references', function () { diff --git a/test/write.js b/test/write.js index 78708f3..a1ccd11 100644 --- a/test/write.js +++ b/test/write.js @@ -42,7 +42,14 @@ describe('Write', function () { var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8'); var po = PO.parse(input); var str = po.toString(); - assertHasLine(str, "# Comment"); + assertHasLine(str, "# Translator comment"); + }); + + it('write extracted comment', function () { + var input = fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8'); + var po = PO.parse(input); + var str = po.toString(); + assertHasLine(str, '#. Extracted comment'); }); describe('msgctxt', function () { From 8dba54b095926b742c28b9e40902d234a5cccf2f Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Sun, 2 Mar 2014 13:20:18 -0500 Subject: [PATCH 3/3] Document extractedComments --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index da2b46d..a25e5a9 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,8 @@ The `PO` class exposes the following members: * `msgstr`: An array of translated strings. Items that have no plural msgid only have one element in this array. * `references`: An array of reference strings. -* `comments`: An array of string comments. +* `comments`: An array of string translator comments. +* `extractedComments`: An array of string extracted comments. * `flags`: A dictionary of the string flags. Each flag is mapped to a key with value true. For instance, a string with the fuzzy flag set will have `item.flags.fuzzy == true`.