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:
Julian Bäume
2014-03-05 10:13:30 +01:00
parent 6903bbf967
commit 94f5f4a83e
2 changed files with 24 additions and 2 deletions

View File

@@ -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;
};