add reverse method of extract(string)
during PO.parse, an extract(string) method is called on each string to unescape some characters (like " and \). This process should be reverted in the toString method. The PO spec says, that all strings should be C-Strings. Otherwise tools like msgmerge (from the gettext package) will fail parsing po files written by this library.
This commit is contained in:
parent
6903bbf967
commit
94f5f4a83e
10
lib/po.js
10
lib/po.js
@ -181,6 +181,12 @@ PO.Item.prototype.toString = function () {
|
|||||||
var lines = [],
|
var lines = [],
|
||||||
that = this;
|
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 _process = function (keyword, text, i) {
|
||||||
var lines = [],
|
var lines = [],
|
||||||
parts = text.split(/\n/),
|
parts = text.split(/\n/),
|
||||||
@ -188,11 +194,11 @@ PO.Item.prototype.toString = function () {
|
|||||||
if (parts.length > 1) {
|
if (parts.length > 1) {
|
||||||
lines.push(keyword + index + ' ""');
|
lines.push(keyword + index + ' ""');
|
||||||
parts.forEach(function (part) {
|
parts.forEach(function (part) {
|
||||||
lines.push('"' + part + '"');
|
lines.push('"' + _escape(part) + '"');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
lines.push(keyword + index + ' "' + text + '"');
|
lines.push(keyword + index + ' "' + _escape(text) + '"');
|
||||||
}
|
}
|
||||||
return lines;
|
return lines;
|
||||||
};
|
};
|
||||||
|
@ -52,6 +52,22 @@ describe('Write', function () {
|
|||||||
assertHasLine(str, '#. Extracted comment');
|
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 () {
|
describe('msgctxt', function () {
|
||||||
it('should write context field to file', function () {
|
it('should write context field to file', function () {
|
||||||
var input = fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8');
|
var input = fs.readFileSync(__dirname + '/fixtures/big.po', 'utf8');
|
||||||
|
Loading…
Reference in New Issue
Block a user