这就是我结束做的事情并且解决了我的问题。对于任务配置,我执行了以下操作:
grunt.initConfig({
convert_po: {
build: {
src: 'C:/temp/Locale/*.po',
dest: 'C:/temp/Locale/output/'
}
}
});
这是任务的实现:
grunt.registerMultiTask('convert_po', 'Convert PO files to JSON format', function() {
var po = require('node-po');
var path = require('path');
grunt.log.write('Loaded dependencies...').ok();
//make grunt know this task is async.
var done = this.async();
var i =0;
this.files.forEach(function(file) {
grunt.log.writeln('Processing ' + file.src.length + ' files.');
//file.src is the list of all matching file names.
file.src.forEach(function(f){
//this is an async function that loads a PO file
po.load(f, function(_po){
strings = {};
for (var idx in _po.items){
var item = _po.items[idx];
strings[item.msgid] = item.msgstr.length == 1 ? item.msgstr[0] : item.msgstr;
}
var destFile = file.dest + path.basename(f, '.po') + '.json';
grunt.log.writeln('Now saving file:' + destFile);
fs.writeFileSync(destFile, JSON.stringify(strings, null, 4));
//if we processed all files notify grunt that we are done.
if( i >= file.src.length) done(true);
});
});
});
});