Skip to content

Instantly share code, notes, and snippets.

@worldspawn
Last active August 29, 2015 14:18
Show Gist options
  • Save worldspawn/720b83a034d613571111 to your computer and use it in GitHub Desktop.
Save worldspawn/720b83a034d613571111 to your computer and use it in GitHub Desktop.

Does a rudimentary merge. It uses the pofile package to read po files. It adds missing entries from pot files to po files and replaces existing entries references and extracted comments. Usage example is at the bottom.

  • Added a lookup to check for entries in the po file that cannot be found in the catalog. If an entry is not found a comment is added indicating that, if the entry is found then if that comment is there it is removed.
var po = require('pofile');
var through = require('through2');
var Q = require('q');
//
function gulppomerge(potfile) {
var pofiles = [];
function buildList(file, enc, cb){
pofiles.push(file.path);
cb(null, file);
}
function mergePot(cb) {
merge(potfile, pofiles)
.done(function () {
cb();
}, function(err) {
cb(err);
});
}
return through.obj(buildList, mergePot);
}
function merge(potfile, pofiles) {
var defer = Q.defer();
Po.load(potfile, function (err, pot) {
if (err) {
console.log(err);
defer.reject(err);
return;
}
var waits = [];
pofiles.forEach(function (pofile) {
var defer = Q.defer();
waits.push(defer.promise);
Po.load(pofile, function (err, po) {
if (err) {
console.log(err);
defer.reject(err);
return;
}
var nfwarning = 'msgid not found in catalog';
po.items.forEach(function (poItem){
var found = false;
var index;
pot.items.forEach(function (potItem) {
if (poItem.msgid === potItem.msgid) {
found = true;
}
});
if (found) {
index = poItem.comments.indexOf(nfwarning);
if (index > -1) {
poItem.comments.splice(index, 1);
}
}
else {
index = poItem.comments.indexOf(nfwarning);
if (index === -1){
poItem.comments.push(nfwarning);
}
}
});
pot.items.forEach(function (potItem) {
var match = null;
po.items.forEach(function (poItem) {
if (poItem.msgid === potItem.msgid) {
match = poItem;
}
});
if (match === null) {
po.items.push(potItem);
}
else {
match.references = potItem.references;
match.extractedComments = potItem.extractedComments;
}
});
po.save(pofile, function (err) {
if (err) {
defer.reject(err);
}
else{
defer.resolve();
}
});
});
});
Q.all(waits)
.then(function () {
defer.resolve();
}, function (err){
defer.reject(err);
});
});
return defer.promise;
}
gulp.task('mergepot', function(done) {
return gulp.src('po/**/*.po').pipe(gulppomerge(__dirname + '/../po/template.pot'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment