Merge pull request #1226 from mewtaylor/issue/gh-translations

Use `scratchr2_translations` for language files
This commit is contained in:
Matthew Taylor 2017-02-24 08:53:02 -05:00 committed by GitHub
commit 35490386d6
6 changed files with 17 additions and 126 deletions

2
.gitignore vendored
View file

@ -11,8 +11,6 @@ npm-*
# Locales
/locales
/intl
/localizations
# Elastic Beanstalk Files
.elasticbeanstalk/*

View file

@ -24,8 +24,7 @@ deploy:
@make sync
translations:
./bin/tx-import localizations
./bin/build-locales localizations intl
./bin/build-locales node_modules/scratchr2_translations/www/translations intl
webpack:
$(WEBPACK) --bail

View file

@ -97,7 +97,13 @@ var globalTemplateFile = path.resolve(__dirname, '../src/l10n.json');
var generalLocales = {'en': JSON.parse(fs.readFileSync(globalTemplateFile, 'utf8'))};
for ( var l in languages ) {
try {
var langTranslations = path.resolve(__dirname, '../', localesDir, 'general', l + '.json');
var langTranslations = path.resolve(
__dirname,
'../',
localesDir,
'scratch-website.general-l10njson',
l + '.json'
);
fs.accessSync(langTranslations);
generalLocales[l] = JSON.parse(fs.readFileSync(langTranslations, 'utf8'));
} catch (err) {
@ -187,7 +193,13 @@ async.forEachLimit(views, 5, function (view, cb) {
// merge view specific english strings, first then other languages
process.stdout.write('Merging translations for ' + view + '\n');
async.forEach(allLangs, function (isoCode, cb) {
var translationsFile = path.resolve(__dirname, '../', localesDir, view, isoCode + '.json');
var translationsFile = path.resolve(
__dirname,
'../',
localesDir,
'scratch-website.' + view + '-l10njson',
isoCode + '.json'
);
fs.readFile(translationsFile, 'utf8', function (err, data) {
if (err) {
if (err.code === 'ENOENT') {

View file

@ -1,119 +0,0 @@
#!/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;
});

View file

@ -11,6 +11,7 @@
"cy": "Cymraeg",
"da": "Dansk",
"de": "Deutsch",
"yum": "Edible Scratch",
"et": "Eesti",
"el": "Ελληνικά",
"en": "English",
@ -42,6 +43,7 @@
"hu": "Magyar",
"ml": "മലയാളം",
"mt": "Malti",
"cat": "Meow",
"mr": "मराठी",
"mn": "Монгол хэл",
"my": "မြန်မာဘာသာ",

View file

@ -88,7 +88,6 @@
"source-map-support": "0.3.2",
"style-loader": "0.12.3",
"tap": "7.1.2",
"transifex": "1.4.6",
"url-loader": "0.5.6",
"watch": "0.16.0",
"webpack": "1.12.14",