pofile/lib/po.js

146 lines
3.4 KiB
JavaScript
Raw Normal View History

var fs = require('fs')
, util = require('util');
2011-12-22 10:50:01 +04:00
var PO = function() {
this.headers = {};
this.items = [];
};
PO.prototype.save = function(filename, callback) {
2012-01-02 05:02:19 +04:00
fs.writeFile(filename, this.toString(), function(err){
if (err) throw err;
callback && callback();
2012-01-02 05:52:51 +04:00
});
2012-01-02 05:02:19 +04:00
};
PO.prototype.toString = function() {
var lines = ['msgid ""', 'msgstr ""']
, that = this;
var keys = Object.keys(this.headers);
keys.forEach(function(key){
lines.push(util.format('"%s: %s\\n"', key, that.headers[key]));
});
2012-01-02 05:02:19 +04:00
lines.push('');
2012-01-02 05:02:19 +04:00
this.items.forEach(function(item){
lines.push(item.toString());
lines.push('');
});
2012-01-02 05:02:19 +04:00
return lines.join("\n");
2011-12-22 10:50:01 +04:00
};
PO.load = function(filename, callback) {
fs.readFile(filename, 'utf-8', function(err, data){
if (err) throw err;
var po = new PO
, parts = data.split(/\n\n/)
, headers = parts.shift().split(/\n/);
2012-01-02 05:02:19 +04:00
2011-12-22 10:50:01 +04:00
headers.forEach(function(header){
if (header.match(/^"/)) {
header = header.trim().replace(/^"/, '').replace(/\\n"$/, '');
var p = header.split(/:/)
, name = p.shift().trim()
, value = p.join(':').trim().replace(/n$/);
po.headers[name] = value;
2011-12-22 10:50:01 +04:00
}
});
2012-01-02 05:02:19 +04:00
2011-12-22 10:50:01 +04:00
parts.forEach(function(part){
if (part.length < 1) return;
2011-12-22 10:50:01 +04:00
var item = PO.Item.parse(part);
2011-12-27 06:22:05 +04:00
po.items.push(item);
2011-12-22 10:50:01 +04:00
});
2012-01-02 05:02:19 +04:00
callback && callback(po);
2011-12-22 10:50:01 +04:00
});
};
PO.Item = function() {
this.msgid = null;
this.references = [];
this.msgid_plural = null;
this.msgstr = [];
2012-01-02 05:52:51 +04:00
this.comments = [];
2011-12-22 10:50:01 +04:00
};
PO.Item.prototype.toString = function() {
var lines = []
, that = this;
2012-01-02 05:02:19 +04:00
var _processEach = function(keyword, text, i) {
var lines = []
, parts = text.split(/\n/)
, index = typeof i != 'undefined' ? util.format('[%d]', i) : '';
if (parts.length > 1) {
lines.push(util.format('%s%s ""', keyword, index));
parts.forEach(function(part){
lines.push(util.format('"%s"', part))
});
}
else {
lines.push(util.format('%s%s "%s"', keyword, index, text));
}
return lines;
}
2012-01-02 05:02:19 +04:00
if (this.references.length > 0) {
this.references.forEach(function(ref){
lines.push(util.format('#: %s', ref));
});
['msgid', 'msgid_plural', 'msgstr'].forEach(function(keyword){
var text = that[keyword];
if (text != null) {
if (util.isArray(text) && text.length > 1) {
text.forEach(function(t, i){
lines = lines.concat(_processEach(keyword, t, i));
});
}
else {
text = util.isArray(text) ? text.join() : text;
lines = lines.concat(_processEach(keyword, text));
}
}
});
};
2012-01-02 05:02:19 +04:00
return lines.join("\n");
};
2011-12-22 10:50:01 +04:00
PO.Item.parse = function(chunk) {
2012-01-02 05:02:19 +04:00
2011-12-22 10:50:01 +04:00
var item = new PO.Item();
2012-01-02 05:52:51 +04:00
var parts = chunk.split(/msg/);
2012-01-02 05:02:19 +04:00
var _extract = function(string) {
2011-12-22 10:50:01 +04:00
var lines = string.split(/\n/);
var value = '';
lines.forEach(function(line){
var p = line.split(/"/); p.shift(); p.pop();
value += "\n" + p.join('"');
});
return value.trim();
};
2012-01-02 05:02:19 +04:00
2011-12-22 10:50:01 +04:00
parts.forEach(function(part){
if (part.match(/^#:/)) {
item.references.push(part.replace(/^#:\s/, ''));
}
else if (part.match(/^id\s/)) {
item.msgid = _extract(part);
2011-12-22 10:50:01 +04:00
}
else if (part.match(/id_plural/)) {
item.msgid_plural = _extract(part);
2011-12-22 10:50:01 +04:00
}
else if (part.match(/str/)) {
item.msgstr.push(_extract(part));
2011-12-22 10:50:01 +04:00
}
});
return item;
};
module.exports = PO;