Skip to content

Instantly share code, notes, and snippets.

@BYK
Created February 27, 2014 11:08
Show Gist options
  • Save BYK/ad954b8970d5c02a2949 to your computer and use it in GitHub Desktop.
Save BYK/ad954b8970d5c02a2949 to your computer and use it in GitHub Desktop.
grunt.registerTask(
'translations.install',
'Installs Transifex client, tx for other translation tasks. Usually requires `sudo`.',
function () {
var done = this.async();
grunt.util.spawn({
cmd: 'easy_install',
args: ['transifex-client']
}, function (error, result, code) {
if (error) {
grunt.log.error(error);
}
else
grunt.log.writeln(result);
// Missing easy_install
if (code === 127)
grunt.log.writeln(
'\nLooks like easy_install is missing. ' +
'\nYou can install it by following the instructions on ' +
'\nhttps://pypi.python.org/pypi/setuptools#installation-instructions'
);
else if (code !== 0)
grunt.log.writeln(
'\nLooks like easy_install failed. Did you try using ' +
'`sudo grunt translations.install`?'
);
return done(code === 0);
});
}
);
grunt.registerTask(
'translations.getlanguages',
'Downloads the language list from Transifex to be used in Cockpit.',
function () {
var done = this.async();
grunt.log.writeln('Getting language list from Transifex…');
var mappingPath = 'locale/mapping.json';
var errorHandler = grunt.fatal.bind(grunt);
https.get('https://www.transifex.com/api/2/languages/', function (res) {
var body = '';
var status = res.statusCode;
if (status !== 200)
grunt.fatal('Server responded with error ' + status);
res.on('data', function (chunk) { body += chunk; });
res.on('end', function () {
grunt.file.write(mappingPath, body);
grunt.log.oklns('Saved language list to ' + mappingPath);
done();
});
res.on('error', errorHandler);
}, errorHandler);
}
);
// Task generator for anything that simply calls Transifex client, tx, with
// some arguments. Automatically detects missing client or invalid credentials
// and tries to fix them automatically and then retries the failed task.
var tx = function () {
var opts = grunt.util.toArray(arguments);
return function () {
var done = this.async();
var txProcess = grunt.util.spawn({
cmd: 'tx',
args: opts.map(grunt.config.process)
}, function (error, result, code) {
// If Transifex client is missing
if (code === 127) {
grunt.task.run(['translations.install', grunt.task.current.name]);
return done(true);
}
if (error)
grunt.log.error(error);
return done(code === 0);
});
txProcess.stdout.on('data', grunt.log.write);
};
};
grunt.registerTask(
'translations.getsource',
'Downloads the current translatable string list from Transifex. ' +
'Used by translations.extract and translations.outsource. ' +
'Not intended for manual use and is a no-op if --overwrite flag is used.',
grunt.option('overwrite') ? // will disable if overwrite is true
function () { grunt.log.writeln('Skipping downloading the source…'); } :
// Pass "0" as the language to avoid downloading any files besides the
// source file (-s option): locale/next.pot
tx('pull', '--skip', '-s', '-f', '-r', '<%= translations.resource %>', '-l', '"0"')
);
grunt.registerTask(
'translations.extract',
'Extracts any translatable strings in templates and JS files. ' +
'Builds the templates to JS files to be able to extract translatable strings. ' +
'Merges any new strings with the ones existing in Transifex by default. ' +
'If --overwrite flag is passed, overwrites all strings with only the ones ' +
'found with this current run. Used by translations.outsource and not ' +
'intended for manual use.',
// We update the source file to avoid any accidental overwrites to existing strings.
['clean', 'templates', 'translations.getsource', 'xgettext']
);
grunt.registerTask(
'translations.fetch',
'Downloads all translations from Transifex. Not intended for manual use.',
tx('pull', '--skip', '--mode', 'reviewed', '--all', '-r', '<%= translations.resource %>')
);
grunt.registerTask(
'translations.build',
'Builds any translations in locale directory and compresses the resultant ' +
'files using UglifyJS. Not intended for manual use.',
['buildPO', 'uglify:translations']
);
grunt.registerTask(
'translations.pull',
'Downloads all available languages from Transifex and builds them. ' +
'Useful for testing translations locally.',
['translations.getlanguages', 'translations.fetch', 'translations.build']
);
grunt.registerTask(
'translations.upload',
'Uploads the translation source file, locale/next.pot to Transifex. ' +
'Not intended for manual use. Do **not** use unless you know exactly ' +
'what you are doing.',
tx('push', '-s', '-r', '<%= translations.resource %>'));
grunt.registerTask(
'translations.outsource',
'Extracts and sends all new translatable strings to Transifex. Not ' +
'necessary to use manually since this is called by the CI system ' +
'automatically for every diff. Not recommended to use manually.',
['translations.extract', 'translations.upload']
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment