diff --git a/lib/po.js b/lib/po.js index 23a2160..876f18f 100644 --- a/lib/po.js +++ b/lib/po.js @@ -7,6 +7,13 @@ var PO = function() { }; PO.prototype.save = function(filename, callback) { + fs.writeFile(filename, this.toString(), function(err){ + if (err) throw err; + callback && callback(); + }) +}; + +PO.prototype.toString = function() { var lines = ['msgid ""', 'msgstr ""'] , that = this; @@ -14,18 +21,15 @@ PO.prototype.save = function(filename, callback) { keys.forEach(function(key){ lines.push(util.format('"%s: %s\\n"', key, that.headers[key])); }); - + lines.push(''); - + this.items.forEach(function(item){ lines.push(item.toString()); lines.push(''); }); - - fs.writeFile(filename, lines.join("\n"), function(err){ - if (err) throw err; - callback && callback(); - }) + + return lines.join("\n"); }; PO.load = function(filename, callback) { @@ -34,7 +38,7 @@ PO.load = function(filename, callback) { var po = new PO , parts = data.split(/\n\n/) , headers = parts.shift().split(/\n/); - + headers.forEach(function(header){ if (header.match(/^"/)) { header = header.trim().replace(/^"/, '').replace(/\\n"$/, ''); @@ -44,13 +48,13 @@ PO.load = function(filename, callback) { po.headers[name] = value; } }); - + parts.forEach(function(part){ if (part.length < 1) return; var item = PO.Item.parse(part); po.items.push(item); }); - + callback && callback(po); }); }; @@ -65,7 +69,7 @@ PO.Item = function() { PO.Item.prototype.toString = function() { var lines = [] , that = this; - + var _processEach = function(keyword, text, i) { var lines = [] , parts = text.split(/\n/) @@ -81,7 +85,7 @@ PO.Item.prototype.toString = function() { } return lines; } - + if (this.references.length > 0) { this.references.forEach(function(ref){ lines.push(util.format('#: %s', ref)); @@ -101,15 +105,15 @@ PO.Item.prototype.toString = function() { } }); }; - + return lines.join("\n"); }; PO.Item.parse = function(chunk) { - + var item = new PO.Item(); var parts = chunk.split(/\nmsg/); - + var _extract = function(string) { var lines = string.split(/\n/); var value = ''; @@ -119,7 +123,7 @@ PO.Item.parse = function(chunk) { }); return value.trim(); }; - + parts.forEach(function(part){ if (part.match(/^#:/)) { item.references.push(part.replace(/^#:\s/, ''));