Merge pull request #9 from Open-Xchange-Frontend/fix_commented_obsolete_strings

Fix commented obsolete strings
This commit is contained in:
Ruben Vermeersch 2014-06-19 14:05:32 +02:00
commit 03572f9711
4 changed files with 66 additions and 18 deletions

View File

@ -101,14 +101,18 @@ PO.parse = function (data) {
var item = new PO.Item(), var item = new PO.Item(),
context = null, context = null,
plural = 0, plural = 0,
obsolete = false; obsoleteCount = 0,
noCommentLineCount = 0;
function finish() { function finish() {
if (item.msgid.length > 0) { if (item.msgid.length > 0) {
if (obsoleteCount >= noCommentLineCount) {
item.obsolete = true;
}
obsoleteCount = 0;
noCommentLineCount = 0;
po.items.push(item); po.items.push(item);
item = new PO.Item(); item = new PO.Item();
item.obsolete = obsolete;
obsolete = false;
} }
} }
@ -122,13 +126,15 @@ PO.parse = function (data) {
while (lines.length > 0) { while (lines.length > 0) {
var line = trim(lines.shift()), var line = trim(lines.shift()),
lineObsolete = false,
add = false; add = false;
if (line.match(/^#\~/)) { // Obsolete item 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)); line = trim(line.substring(2));
} else { lineObsolete = true;
obsolete = false;
} }
if (line.match(/^#:/)) { // Reference if (line.match(/^#:/)) { // Reference
@ -153,24 +159,29 @@ PO.parse = function (data) {
else if (line.match(/^msgid_plural/)) { // Plural form else if (line.match(/^msgid_plural/)) { // Plural form
item.msgid_plural = extract(line); item.msgid_plural = extract(line);
context = 'msgid_plural'; context = 'msgid_plural';
noCommentLineCount++;
} }
else if (line.match(/^msgid/)) { // Original else if (line.match(/^msgid/)) { // Original
finish(); finish();
item.msgid = extract(line); item.msgid = extract(line);
context = 'msgid'; context = 'msgid';
noCommentLineCount++;
} }
else if (line.match(/^msgstr/)) { // Translation else if (line.match(/^msgstr/)) { // Translation
var m = line.match(/^msgstr\[(\d+)\]/); var m = line.match(/^msgstr\[(\d+)\]/);
plural = m && m[1] ? parseInt(m[1]) : 0; plural = m && m[1] ? parseInt(m[1]) : 0;
item.msgstr[plural] = extract(line); item.msgstr[plural] = extract(line);
context = 'msgstr'; context = 'msgstr';
noCommentLineCount++;
} }
else if (line.match(/^msgctxt/)) { // Context else if (line.match(/^msgctxt/)) { // Context
finish(); finish();
item.msgctxt = extract(line); item.msgctxt = extract(line);
noCommentLineCount++;
} }
else { // Probably multiline string or blank else { // Probably multiline string or blank
if (line.length > 0) { if (line.length > 0) {
noCommentLineCount++;
if (context === 'msgstr') { if (context === 'msgstr') {
item.msgstr[plural] += extract(line); item.msgstr[plural] += extract(line);
} }
@ -182,6 +193,10 @@ PO.parse = function (data) {
} }
} }
} }
if (lineObsolete) {
//count obsolete lines for this item
obsoleteCount++;
}
} }
finish(); finish();
@ -245,27 +260,24 @@ PO.Item.prototype.toString = function () {
if (flags.length > 0) { if (flags.length > 0) {
lines.push('#, ' + flags.join(",")); lines.push('#, ' + flags.join(","));
} }
var mkObsolete = this.obsolete ? '#~ ' : '';
['msgctxt', 'msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) { ['msgctxt', 'msgid', 'msgid_plural', 'msgstr'].forEach(function (keyword) {
var text = that[keyword]; var text = that[keyword];
if (text != null) { if (text != null) {
if (isArray(text) && text.length > 1) { if (isArray(text) && text.length > 1) {
text.forEach(function (t, i) { text.forEach(function (t, i) {
lines = lines.concat(_process(keyword, t, i)); lines = lines.concat(mkObsolete + _process(keyword, t, i));
}); });
} }
else { else {
text = isArray(text) ? text.join() : text; 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");
return "#~ " + lines.join("\n#~ ");
} else {
return lines.join("\n");
}
}; };
module.exports = PO; module.exports = PO;

View File

@ -21,3 +21,13 @@ msgstr "{{dataLoader.data.length}} resultaten"
#~ msgid "Add order" #~ msgid "Add order"
#~ msgstr "Order toevoegen" #~ msgstr "Order toevoegen"
#~ # commented obsolete item
#~ #, fuzzy
#~ msgid "Commented item"
#~ msgstr "not sure"
# commented obsolete item
#, fuzzy
#~ msgid "Second commented item"
#~ msgstr "also not sure"

View File

@ -96,16 +96,26 @@ describe('Parse', function () {
it('Handles obsolete items', function () { it('Handles obsolete items', function () {
var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/commented.po', 'utf8')); var po = PO.parse(fs.readFileSync(__dirname + '/fixtures/commented.po', 'utf8'));
assert.equal(po.items.length, 2); assert.equal(po.items.length, 4);
var item = po.items[0]; var item = po.items[0];
assert.equal(item.obsolete, false); assert.equal(item.obsolete, false);
assert.equal(item.msgid, "{{dataLoader.data.length}} results"); assert.equal(item.msgid, '{{dataLoader.data.length}} results');
assert.equal(item.msgstr, "{{dataLoader.data.length}} resultaten"); assert.equal(item.msgstr, '{{dataLoader.data.length}} resultaten');
item = po.items[1]; item = po.items[1];
assert.equal(item.obsolete, true); assert.equal(item.obsolete, true);
assert.equal(item.msgid, "Add order"); assert.equal(item.msgid, 'Add order');
assert.equal(item.msgstr, "Order toevoegen"); assert.equal(item.msgstr, 'Order toevoegen');
item = po.items[2];
assert.equal(item.obsolete, true);
assert.equal(item.msgid, 'Commented item');
assert.equal(item.msgstr, 'not sure');
item = po.items[3];
assert.equal(item.obsolete, true);
assert.equal(item.msgid, 'Second commented item');
assert.equal(item.msgstr, 'also not sure');
}); });
describe('C-Strings', function () { describe('C-Strings', function () {

View File

@ -61,6 +61,22 @@ describe('Write', function () {
assertHasLine(str, '#~ msgstr "Order toevoegen"'); assertHasLine(str, '#~ msgstr "Order toevoegen"');
}); });
it('write obsolete items with comment', function () {
var input = fs.readFileSync(__dirname + '/fixtures/commented.po', 'utf8');
var po = PO.parse(input);
var str = po.toString();
//this is what msgcat tool of gettext does: don't print #~ for comments
assertHasLine(str, '# commented obsolete item');
assertHasLine(str, '#, fuzzy');
//items made obsolete by commenting every keyword with #~
assertHasLine(str, '#~ msgid "Commented item"');
assertHasLine(str, '#~ msgstr "not sure"');
assertHasLine(str, '#~ msgid "Second commented item"');
assertHasLine(str, '#~ msgstr "also not sure"');
});
describe('C-Strings', function () { describe('C-Strings', function () {
it('should escape "', function () { it('should escape "', function () {
var item = new PO.Item(); var item = new PO.Item();