Even if there is a Grunt plugin that deals with adding textdomain to the i18n functions, for Gulp I found right about… nothing. To be honest, there is a plugin that’s related to localization, but this deals only with the pot
file. If you’re like me and you don’t add textdomain to your strings, then you’re kind of out of luck.
Anyhow, I’m not sure why there is nothing about this matter: it’s either because it’s too obvious? Or because no one uses Gulp? I don’t know and to be honest, I don’t really care either, because I use Gulp and I need this. So here is how you can do it:
var wpi18n = require('node-wp-i18n'); var through = require('through2'); gulp.task('addtextdomain', function(){ var options = { textdomain: 'my-text-domain', }; var files = []; return gulp.src(['dist/**/*.php', '!dist/vendor/**']) .pipe(through.obj(function (file, enc, cb) { files.push(file.path); cb(null, file); }, function(cb){ wpi18n.addtextdomain(files, options); cb(); })); });
And… believe it or not, this is all you need. You only need to run gulp addtextdomain
and you’re all set!
Please note, this will add text domain strings even to the functions that currently have different textdomains, so you’ll have:
__('foo', 'bar'); // will be converted to: __('foo', 'bar', 'my-text-domain');
Hey,
thanks for the article, as for the __(‘foo’, ‘bar’, ‘my-text-domain’); issue — just add option
updateDomains: true
and domain will be replaced correctly.