mirror of
https://github.com/scratchfoundation/scratch-www.git
synced 2024-11-23 15:47:53 -05:00
120 lines
3.6 KiB
Text
120 lines
3.6 KiB
Text
|
#!/usr/bin/env node
|
||
|
|
||
|
/*
|
||
|
Downloads translation json files from Transifex for all the languages defined
|
||
|
in the menus. Expects a Transifex API key to be set in the TX_TOKEN environment
|
||
|
variable. Takes as input a directory in which to store the translations.
|
||
|
|
||
|
Creates subdirectories for each view and 'general'. Creates <lang>.json files
|
||
|
for each language downloaded. Uses the english source l10n file if there is
|
||
|
an error.
|
||
|
*/
|
||
|
var fs = require('fs');
|
||
|
var path = require('path');
|
||
|
var async = require('async');
|
||
|
var Transifex = require('transifex');
|
||
|
var languages = require('../languages.json');
|
||
|
var routes = require('../src/routes.json');
|
||
|
|
||
|
|
||
|
// -----------------------------------------------------------------------------
|
||
|
// Main script
|
||
|
// -----------------------------------------------------------------------------
|
||
|
|
||
|
|
||
|
var args = process.argv.slice(2);
|
||
|
|
||
|
if (!args.length) {
|
||
|
process.stdout.write('A destination directory for localizations must be specified.');
|
||
|
process.exit(1);
|
||
|
}
|
||
|
|
||
|
var outputDir = path.resolve(__dirname, '../', args.shift());
|
||
|
try {
|
||
|
fs.accessSync(outputDir, fs.F_OK);
|
||
|
} catch (err) {
|
||
|
// Doesn't exist - create it.
|
||
|
fs.mkdirSync(outputDir);
|
||
|
}
|
||
|
|
||
|
if (process.env.TX_TOKEN) {
|
||
|
var transifex = new Transifex({
|
||
|
project_slug: 'scratch-website',
|
||
|
credential: 'api:'+process.env.TX_TOKEN
|
||
|
});
|
||
|
} else {
|
||
|
process.stdout.write('WARNING: Missing Transifex API key, skipping download.\n');
|
||
|
process.stdout.write('Set TX_TOKEN in env if you want translations.\n');
|
||
|
process.exit(0);
|
||
|
}
|
||
|
|
||
|
//make sure general translation directory exists
|
||
|
var transDir = path.resolve(outputDir + '/general');
|
||
|
try {
|
||
|
fs.accessSync(transDir, fs.F_OK);
|
||
|
} catch (err) {
|
||
|
// Doesn't exist - create it.
|
||
|
fs.mkdirSync(transDir);
|
||
|
}
|
||
|
|
||
|
var localizations = [];
|
||
|
for ( var isoCode in languages ) {
|
||
|
var transFile = transDir + '/' + isoCode + '.json';
|
||
|
localizations.push({view: 'general', lang: isoCode, file: transFile, src: 'l10n.json'});
|
||
|
}
|
||
|
|
||
|
// initialize views, ignore redirect routes
|
||
|
for (var v in routes) {
|
||
|
if (typeof routes[v].redirect !== 'undefined') {
|
||
|
continue;
|
||
|
}
|
||
|
var subdir = routes[v].view.split('/');
|
||
|
subdir.pop();
|
||
|
var l10n = 'src/views/' + subdir.join('/') + '/l10n.json';
|
||
|
var name = routes[v].name;
|
||
|
try {
|
||
|
// only Initialize if there is an l10n file
|
||
|
fs.accessSync(l10n);
|
||
|
transDir = path.resolve(outputDir + '/' + name);
|
||
|
try {
|
||
|
fs.accessSync(transDir, fs.F_OK);
|
||
|
} catch (err) {
|
||
|
// Doesn't exist - create it.
|
||
|
fs.mkdirSync(transDir);
|
||
|
}
|
||
|
for ( var isoCode in languages ) {
|
||
|
var transFile = transDir + '/' + isoCode + '.json';
|
||
|
localizations.push({view: name, lang: isoCode, file: transFile, src: l10n});
|
||
|
}
|
||
|
|
||
|
} catch (err) {
|
||
|
// skip views without l10n files
|
||
|
// TODO: ES6
|
||
|
// process.stdout.write(`Skipping ${name}, no l10n\n`);
|
||
|
process.stdout.write('Skipping ' + name+ ', no l10n\n');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async.forEachLimit(localizations, 3, function (item, cb) {
|
||
|
transifex.translationInstanceMethod(
|
||
|
'scratch-website',
|
||
|
item.view+'-l10njson',
|
||
|
item.lang,
|
||
|
{mode: 'reviewed'},
|
||
|
function (err, data) {
|
||
|
if (err) {
|
||
|
process.stdout.write(item.view + ': ' + item.lang + '(' + err + ')' + ' using english\n');
|
||
|
fs.createReadStream(item.src).pipe(fs.createWriteStream(item.file));
|
||
|
} else {
|
||
|
fs.writeFile(item.file, data);
|
||
|
}
|
||
|
return;
|
||
|
});
|
||
|
cb();
|
||
|
}, function (err) {
|
||
|
if (err) {
|
||
|
process.stdout.write('Import completed with errors\n');
|
||
|
}
|
||
|
return;
|
||
|
});
|