properly escape all unprintable characters
writing messages should no be in line with gettext tools. I tested using msgcat, it provides the same results. For some common use-cases I wrote explicit tests, for uncommon and even unwanted use-cases I wrote one test to make sure pofile works like msgcat for those messages
This commit is contained in:
24
lib/po.js
24
lib/po.js
@@ -236,8 +236,27 @@ PO.Item.prototype.toString = function () {
|
||||
|
||||
// reverse what extract(string) method during PO.parse does
|
||||
var _escape = function (string) {
|
||||
string = string.replace(/\\/g, '\\\\');
|
||||
return string.replace(/"/g, '\\"');
|
||||
// don't unescape \n, since string can never contain it
|
||||
// since split('\n') is called on it
|
||||
string = string.replace(/[\x07\b\t\v\f\r"\\]/g, function (match) {
|
||||
switch (match) {
|
||||
case '\x07':
|
||||
return '\\a';
|
||||
case '\b':
|
||||
return '\\b';
|
||||
case '\t':
|
||||
return '\\t';
|
||||
case '\v':
|
||||
return '\\v';
|
||||
case '\f':
|
||||
return '\\f';
|
||||
case '\r':
|
||||
return '\\r';
|
||||
default:
|
||||
return '\\' + match;
|
||||
}
|
||||
});
|
||||
return string;
|
||||
};
|
||||
|
||||
var _process = function (keyword, text, i) {
|
||||
@@ -286,6 +305,7 @@ PO.Item.prototype.toString = function () {
|
||||
} else {
|
||||
text = isArray(text) ? text.join() : text;
|
||||
var processed = _process(keyword, text);
|
||||
//handle \n in single-line texts (can not be handled in _escape)
|
||||
for (var i = 1; i < processed.length - 1; i++) {
|
||||
processed[i] = processed[i].slice(0, -1) + '\\n"';
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user