2011-12-23 04:12:01 +04:00
|
|
|
var fs = require('fs')
|
|
|
|
, util = require('util');
|
2011-12-22 10:50:01 +04:00
|
|
|
|
2012-01-03 03:52:05 +04:00
|
|
|
function trim(string) {
|
|
|
|
return string.replace(/^\s+|\s+$/g, '');
|
|
|
|
};
|
|
|
|
|
2011-12-22 10:50:01 +04:00
|
|
|
var PO = function() {
|
2012-01-03 03:52:05 +04:00
|
|
|
this.comments = [];
|
2011-12-22 10:50:01 +04:00
|
|
|
this.headers = {};
|
|
|
|
this.items = [];
|
|
|
|
};
|
|
|
|
|
2011-12-23 04:12:01 +04:00
|
|
|
PO.prototype.save = function(filename, callback) {
|
2012-01-03 03:52:05 +04:00
|
|
|
fs.writeFile(filename, this.toString(), function(err) {
|
2012-01-02 05:02:19 +04:00
|
|
|
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() {
|
2012-01-03 03:52:05 +04:00
|
|
|
var lines = []
|
2011-12-23 04:12:01 +04:00
|
|
|
, that = this;
|
|
|
|
|
2012-01-03 03:52:05 +04:00
|
|
|
if (this.comments) {
|
|
|
|
this.comments.forEach(function(comment) {
|
|
|
|
lines.push('# ' + comment);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
lines.push('msgid ""');
|
|
|
|
lines.push('msgstr ""');
|
|
|
|
|
2011-12-23 04:12:01 +04:00
|
|
|
var keys = Object.keys(this.headers);
|
2012-01-03 03:52:05 +04:00
|
|
|
keys.forEach(function(key) {
|
2011-12-23 04:12:01 +04:00
|
|
|
lines.push(util.format('"%s: %s\\n"', key, that.headers[key]));
|
|
|
|
});
|
2012-01-02 05:02:19 +04:00
|
|
|
|
2011-12-23 04:12:01 +04:00
|
|
|
lines.push('');
|
2012-01-02 05:02:19 +04:00
|
|
|
|
2012-01-03 03:52:05 +04:00
|
|
|
this.items.forEach(function(item) {
|
2011-12-23 04:17:04 +04:00
|
|
|
lines.push(item.toString());
|
2011-12-23 04:12:01 +04:00
|
|
|
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) {
|
2012-01-03 03:52:05 +04:00
|
|
|
fs.readFile(filename, 'utf-8', function(err, data) {
|
2011-12-22 10:50:01 +04:00
|
|
|
if (err) throw err;
|
2012-01-03 03:52:05 +04:00
|
|
|
var po = PO.parse(data);
|
|
|
|
callback && callback(po);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
PO.parse = function(data) {
|
2013-09-12 15:01:37 +04:00
|
|
|
//support both unix and windows newline formats.
|
|
|
|
data = data.replace(/\r\n/g, '\n');
|
2011-12-22 10:50:01 +04:00
|
|
|
var po = new PO
|
2012-01-03 03:52:05 +04:00
|
|
|
, sections = data.split(/\n\n/)
|
|
|
|
, headers = sections.shift()
|
|
|
|
, lines = sections.join("\n").split(/\n/);
|
|
|
|
|
|
|
|
po.headers = {
|
|
|
|
'Project-Id-Version': '',
|
|
|
|
'Report-Msgid-Bugs-To': '',
|
|
|
|
'POT-Creation-Date': '',
|
|
|
|
'PO-Revision-Date': '',
|
|
|
|
'Last-Translator': '',
|
2012-01-15 23:31:07 +04:00
|
|
|
'Language': '',
|
2012-01-03 03:52:05 +04:00
|
|
|
'Language-Team': '',
|
|
|
|
'Content-Type': '',
|
|
|
|
'Content-Transfer-Encoding': '',
|
|
|
|
'Plural-Forms': '',
|
|
|
|
};
|
|
|
|
|
|
|
|
headers.split(/\n/).forEach(function(header) {
|
|
|
|
if (header.match(/^#/)) {
|
|
|
|
po.comments.push(header.replace(/^#\s*/, ''));
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
var item = new PO.Item()
|
|
|
|
, context = null
|
|
|
|
, plural = 0;
|
2012-01-02 05:02:19 +04:00
|
|
|
|
2012-01-03 03:52:05 +04:00
|
|
|
function finish() {
|
|
|
|
if (item.msgid.length > 0) {
|
2011-12-27 06:22:05 +04:00
|
|
|
po.items.push(item);
|
2012-01-03 03:52:05 +04:00
|
|
|
item = new PO.Item();
|
|
|
|
}
|
|
|
|
};
|
2012-01-02 05:02:19 +04:00
|
|
|
|
2012-01-03 03:52:05 +04:00
|
|
|
function extract(string) {
|
|
|
|
string = trim(string);
|
|
|
|
string = string.replace(/^[^"]*"|"$/g, '');
|
|
|
|
string = string.replace(/\\"/g, '"');
|
|
|
|
string = string.replace(/\\\\/g, '\\');
|
|
|
|
return string;
|
|
|
|
};
|
|
|
|
|
|
|
|
while (lines.length > 0) {
|
|
|
|
var line = trim(lines.shift())
|
|
|
|
, add = false;
|
|
|
|
if (line.match(/^#:/)) { // Reference
|
|
|
|
finish();
|
|
|
|
item.references.push(trim(line.replace(/^#:/, '')));
|
|
|
|
}
|
|
|
|
else if (line.match(/^#/)) { // Comment
|
|
|
|
finish();
|
|
|
|
item.comments.push(trim(line.replace(/^#/, '')));
|
|
|
|
}
|
|
|
|
else if (line.match(/^msgid_plural/)) { // Plural form
|
|
|
|
item.msgid_plural = extract(line);
|
|
|
|
context = 'msgid_plural';
|
|
|
|
}
|
|
|
|
else if (line.match(/^msgid/)) { // Original
|
|
|
|
finish();
|
|
|
|
item.msgid = extract(line);
|
|
|
|
context = 'msgid';
|
|
|
|
}
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
else { // Probably multiline string or blank
|
|
|
|
if (line.length > 0) {
|
|
|
|
if (context == 'msgstr') {
|
|
|
|
item.msgstr[plural] += extract(line);
|
|
|
|
}
|
|
|
|
else if (context == 'msgid') {
|
|
|
|
item.msgid += extract(line);
|
|
|
|
}
|
|
|
|
else if (context == 'msgid_plural') {
|
|
|
|
item.msgid_plural += extract(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
finish();
|
|
|
|
|
|
|
|
return po;
|
2011-12-22 10:50:01 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
PO.Item = function() {
|
2012-01-03 03:52:05 +04:00
|
|
|
this.msgid = '';
|
2011-12-22 10:50:01 +04:00
|
|
|
this.references = [];
|
2012-01-15 23:31:07 +04:00
|
|
|
this.msgid_plural = null;
|
2011-12-22 10:50:01 +04:00
|
|
|
this.msgstr = [];
|
2012-01-02 05:52:51 +04:00
|
|
|
this.comments = [];
|
2011-12-22 10:50:01 +04:00
|
|
|
};
|
|
|
|
|
2011-12-23 04:17:04 +04:00
|
|
|
PO.Item.prototype.toString = function() {
|
|
|
|
var lines = []
|
|
|
|
, that = this;
|
2012-01-02 05:02:19 +04:00
|
|
|
|
2012-01-03 03:52:05 +04:00
|
|
|
var _process = function(keyword, text, i) {
|
2011-12-23 04:17:04 +04:00
|
|
|
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));
|
2012-01-03 03:52:05 +04:00
|
|
|
parts.forEach(function(part) {
|
2011-12-23 04:17:04 +04:00
|
|
|
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
|
|
|
|
2011-12-23 04:17:04 +04:00
|
|
|
if (this.references.length > 0) {
|
2012-01-03 03:52:05 +04:00
|
|
|
this.references.forEach(function(ref) {
|
2011-12-23 04:17:04 +04:00
|
|
|
lines.push(util.format('#: %s', ref));
|
|
|
|
});
|
|
|
|
};
|
2012-01-02 05:02:19 +04:00
|
|
|
|
2012-01-03 04:06:32 +04:00
|
|
|
['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(_process(keyword, t, i));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
text = util.isArray(text) ? text.join() : text;
|
|
|
|
lines = lines.concat(_process(keyword, text));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2011-12-23 04:17:04 +04:00
|
|
|
return lines.join("\n");
|
|
|
|
};
|
|
|
|
|
2011-12-23 04:12:01 +04:00
|
|
|
module.exports = PO;
|