new implementation of items marked obsolete
the current implementation of items marked obsolete did not allow "plain" comments for these items. However, this is perfectly fine according to the original gettext tools. When writing a po file, comments for obsolete items don't contain the '#~ ' mark (tested using msgcat), so this is now also aligned with the behaviour of the original gettext tools. For all these cases I added examples in the po files, that failed with the current implementation and work fine after these changes.
This commit is contained in:
38
lib/po.js
38
lib/po.js
@@ -101,14 +101,18 @@ PO.parse = function (data) {
|
||||
var item = new PO.Item(),
|
||||
context = null,
|
||||
plural = 0,
|
||||
obsolete = false;
|
||||
obsoleteCount = 0,
|
||||
noCommentLineCount = 0;
|
||||
|
||||
function finish() {
|
||||
if (item.msgid.length > 0) {
|
||||
if (obsoleteCount >= noCommentLineCount) {
|
||||
item.obsolete = true;
|
||||
}
|
||||
obsoleteCount = 0;
|
||||
noCommentLineCount = 0;
|
||||
po.items.push(item);
|
||||
item = new PO.Item();
|
||||
item.obsolete = obsolete;
|
||||
obsolete = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,13 +126,15 @@ PO.parse = function (data) {
|
||||
|
||||
while (lines.length > 0) {
|
||||
var line = trim(lines.shift()),
|
||||
lineObsolete = false,
|
||||
add = false;
|
||||
|
||||
if (line.match(/^#\~/)) { // Obsolete item
|
||||
obsolete = true;
|
||||
//only remove the obsolte comment mark, here
|
||||
//might be, this is a new item, so
|
||||
//only remember, this line is marked obsolete, count after line is parsed
|
||||
line = trim(line.substring(2));
|
||||
} else {
|
||||
obsolete = false;
|
||||
lineObsolete = true;
|
||||
}
|
||||
|
||||
if (line.match(/^#:/)) { // Reference
|
||||
@@ -153,24 +159,29 @@ PO.parse = function (data) {
|
||||
else if (line.match(/^msgid_plural/)) { // Plural form
|
||||
item.msgid_plural = extract(line);
|
||||
context = 'msgid_plural';
|
||||
noCommentLineCount++;
|
||||
}
|
||||
else if (line.match(/^msgid/)) { // Original
|
||||
finish();
|
||||
item.msgid = extract(line);
|
||||
context = 'msgid';
|
||||
noCommentLineCount++;
|
||||
}
|
||||
else if (line.match(/^msgstr/)) { // Translation
|
||||
var m = line.match(/^msgstr\[(\d+)\]/);
|
||||
plural = m && m[1] ? parseInt(m[1]) : 0;
|
||||
item.msgstr[plural] = extract(line);
|
||||
context = 'msgstr';
|
||||
noCommentLineCount++;
|
||||
}
|
||||
else if (line.match(/^msgctxt/)) { // Context
|
||||
finish();
|
||||
item.msgctxt = extract(line);
|
||||
noCommentLineCount++;
|
||||
}
|
||||
else { // Probably multiline string or blank
|
||||
if (line.length > 0) {
|
||||
noCommentLineCount++;
|
||||
if (context === 'msgstr') {
|
||||
item.msgstr[plural] += extract(line);
|
||||
}
|
||||
@@ -182,6 +193,10 @@ PO.parse = function (data) {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (lineObsolete) {
|
||||
//count obsolete lines for this item
|
||||
obsoleteCount++;
|
||||
}
|
||||
}
|
||||
finish();
|
||||
|
||||
@@ -245,27 +260,24 @@ PO.Item.prototype.toString = function () {
|
||||
if (flags.length > 0) {
|
||||
lines.push('#, ' + flags.join(","));
|
||||
}
|
||||
var mkObsolete = this.obsolete ? '#~ ' : '';
|
||||
|
||||
['msgctxt', 'msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) {
|
||||
var text = that[keyword];
|
||||
if (text != null) {
|
||||
if (isArray(text) && text.length > 1) {
|
||||
text.forEach(function (t, i) {
|
||||
lines = lines.concat(_process(keyword, t, i));
|
||||
lines = lines.concat(mkObsolete + _process(keyword, t, i));
|
||||
});
|
||||
}
|
||||
else {
|
||||
text = isArray(text) ? text.join() : text;
|
||||
lines = lines.concat(_process(keyword, text));
|
||||
lines = lines.concat(mkObsolete + _process(keyword, text));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (this.obsolete) {
|
||||
return "#~ " + lines.join("\n#~ ");
|
||||
} else {
|
||||
return lines.join("\n");
|
||||
}
|
||||
return lines.join("\n");
|
||||
};
|
||||
|
||||
module.exports = PO;
|
||||
|
||||
Reference in New Issue
Block a user