Handle extracted comments
This commit is contained in:
		
							
								
								
									
										20
									
								
								lib/po.js
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								lib/po.js
									
									
									
									
									
								
							| @@ -120,9 +120,13 @@ PO.parse = function (data) { | |||||||
|                 item.flags[flags[i]] = true; |                 item.flags[flags[i]] = true; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         else if (line.match(/^#/)) { // Comment |         else if (line.match(/^#\s+/)) { // Translator comment | ||||||
|             finish(); |             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 |         else if (line.match(/^msgid_plural/)) { // Plural form | ||||||
|             item.msgid_plural = extract(line); |             item.msgid_plural = extract(line); | ||||||
| @@ -168,7 +172,8 @@ PO.Item = function () { | |||||||
|     this.references = []; |     this.references = []; | ||||||
|     this.msgid_plural = null; |     this.msgid_plural = null; | ||||||
|     this.msgstr = []; |     this.msgstr = []; | ||||||
|     this.comments = []; |     this.comments = []; // translator comments | ||||||
|  |     this.extractedComments = []; | ||||||
|     this.flags = {}; |     this.flags = {}; | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -192,12 +197,21 @@ PO.Item.prototype.toString = function () { | |||||||
|         return lines; |         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) { |     if (this.comments.length > 0) { | ||||||
|         this.comments.forEach(function (c) { |         this.comments.forEach(function (c) { | ||||||
|             lines.push('# ' + c); |             lines.push('# ' + c); | ||||||
|         }); |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     if (this.extractedComments.length > 0) { | ||||||
|  |         this.extractedComments.forEach(function (c) { | ||||||
|  |             lines.push('#. ' + c); | ||||||
|  |         }); | ||||||
|  |     } | ||||||
|  |  | ||||||
|     if (this.references.length > 0) { |     if (this.references.length > 0) { | ||||||
|         this.references.forEach(function (ref) { |         this.references.forEach(function (ref) { | ||||||
|             lines.push('#: ' + ref); |             lines.push('#: ' + ref); | ||||||
|   | |||||||
							
								
								
									
										3
									
								
								test/fixtures/comment.po
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								test/fixtures/comment.po
									
									
									
									
										vendored
									
									
								
							| @@ -15,6 +15,7 @@ msgstr "" | |||||||
| "Language: fr\n" | "Language: fr\n" | ||||||
| "X-Generator: Poedit 1.6.2\n" | "X-Generator: Poedit 1.6.2\n" | ||||||
|  |  | ||||||
| # Comment | # Translator comment | ||||||
|  | #. Extracted 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" | ||||||
|   | |||||||
| @@ -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."); |         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')); |         var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/comment.po', 'utf8')); | ||||||
|         assert.notEqual(po, null); |         assert.notEqual(po, null); | ||||||
|         assert.equal(po.items.length, 1); |         assert.equal(po.items.length, 1); | ||||||
| @@ -31,7 +31,18 @@ describe('Parse', 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, ["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 () { |     it('Handles string references', function () { | ||||||
|   | |||||||
| @@ -42,7 +42,14 @@ 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, "# 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 () { |     describe('msgctxt', function () { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user