diff --git a/lib/po.js b/lib/po.js index 82d13f1..0d7a82f 100644 --- a/lib/po.js +++ b/lib/po.js @@ -181,6 +181,12 @@ PO.Item.prototype.toString = function () { var lines = [], that = this; + // reverse what extract(string) method during PO.parse does + var _escape = function (string) { + string = string.replace(/\\/g, '\\\\'); + return string.replace(/"/g, '\\"'); + }; + var _process = function (keyword, text, i) { var lines = [], parts = text.split(/\n/), @@ -188,11 +194,11 @@ PO.Item.prototype.toString = function () { if (parts.length > 1) { lines.push(keyword + index + ' ""'); parts.forEach(function (part) { - lines.push('"' + part + '"'); + lines.push('"' + _escape(part) + '"'); }); } else { - lines.push(keyword + index + ' "' + text + '"'); + lines.push(keyword + index + ' "' + _escape(text) + '"'); } return lines; }; diff --git a/test/write.js b/test/write.js index a1ccd11..b0e3fee 100644 --- a/test/write.js +++ b/test/write.js @@ -52,6 +52,22 @@ describe('Write', function () { assertHasLine(str, '#. Extracted comment'); }); + describe('C-Strings', function () { + it('should escape "', function () { + var item = new PO.Item(); + + item.msgid = '" should be written escaped'; + assertHasLine(item.toString(), 'msgid "\\" should be written escaped"'); + }); + + it('shoudl escape \\', function () { + var item = new PO.Item(); + + item.msgid = '\\ should be written escaped'; + assertHasLine(item.toString(), 'msgid "\\\\ should be written escaped"'); + }); + }); + describe('msgctxt', function () { it('should write context field to file', function () { var input = fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8');