2019-02-11 11:27:32 -05:00
|
|
|
#!/usr/bin/env babel-node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Script to pull www translations from transifex for all resources.
|
|
|
|
* Expects that the project and that the person running the script
|
|
|
|
* has the the TX_TOKEN environment variable set to an api
|
|
|
|
* token that has developer access.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const args = process.argv.slice(2);
|
|
|
|
|
|
|
|
const usage = `
|
|
|
|
Pull supported language translations from Transifex for the 'scratch-website' project.
|
|
|
|
It will query transifex for the list of resources.
|
|
|
|
Usage:
|
2019-03-21 06:00:12 -04:00
|
|
|
node tx-pull-www.js path [lang]
|
2019-02-11 11:27:32 -05:00
|
|
|
path: root for the translated resources.
|
|
|
|
Each resource will be a subdirectory containing language json files.
|
2019-03-21 06:00:12 -04:00
|
|
|
lang: optional language code - will only pull resources for that language
|
2019-02-11 11:27:32 -05:00
|
|
|
NOTE: TX_TOKEN environment variable needs to be set with a Transifex API token.
|
|
|
|
See the Localization page on the GUI wiki for information about setting up Transifex.
|
|
|
|
`;
|
|
|
|
// Fail immediately if the TX_TOKEN is not defined
|
|
|
|
if (!process.env.TX_TOKEN || args.length < 1) {
|
|
|
|
process.stdout.write(usage);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
|
|
|
import mkdirp from 'mkdirp';
|
2022-03-10 16:58:12 -05:00
|
|
|
import {txPull, txResources} from '../lib/transifex.js';
|
2019-02-11 11:27:32 -05:00
|
|
|
import locales, {localeMap} from '../src/supported-locales.js';
|
2022-03-22 16:29:53 -04:00
|
|
|
import {batchMap} from '../lib/batch.js';
|
2019-02-11 11:27:32 -05:00
|
|
|
|
|
|
|
// Globals
|
|
|
|
const PROJECT = 'scratch-website';
|
|
|
|
const OUTPUT_DIR = path.resolve(args[0]);
|
|
|
|
// const MODE = {mode: 'reviewed'}; // default is everything for www
|
2022-03-22 16:29:53 -04:00
|
|
|
const CONCURRENCY_LIMIT = 36;
|
2019-02-11 11:27:32 -05:00
|
|
|
|
2019-03-21 06:00:12 -04:00
|
|
|
const lang = args.length === 2 ? args[1] : '';
|
|
|
|
|
2022-03-17 11:52:58 -04:00
|
|
|
const getLocaleData = async function (item) {
|
2019-02-11 11:27:32 -05:00
|
|
|
const locale = item.locale;
|
|
|
|
const resource = item.resource;
|
|
|
|
let txLocale = localeMap[locale] || locale;
|
2022-09-20 14:19:23 -04:00
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
try {
|
|
|
|
const translations = await txPull(PROJECT, resource, txLocale);
|
|
|
|
|
|
|
|
const txOutdir = `${OUTPUT_DIR}/${PROJECT}.${resource}`;
|
|
|
|
mkdirp.sync(txOutdir);
|
|
|
|
const fileName = `${txOutdir}/${locale}.json`;
|
|
|
|
fs.writeFileSync(
|
|
|
|
fileName,
|
|
|
|
JSON.stringify(translations, null, 4)
|
|
|
|
);
|
|
|
|
return {
|
|
|
|
resource: resource,
|
|
|
|
locale: locale,
|
|
|
|
file: fileName
|
|
|
|
};
|
|
|
|
} catch (e) {
|
2022-09-22 13:39:36 -04:00
|
|
|
process.stdout.write(`got ${e.message}, retrying after ${i + 1} attempt(s)\n`);
|
2022-09-20 14:19:23 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw Error('failed to pull translations after 5 retries');
|
2019-02-11 11:27:32 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const expandResourceFiles = (resources) => {
|
|
|
|
let items = [];
|
|
|
|
for (let resource of resources) {
|
2019-03-21 06:00:12 -04:00
|
|
|
if (lang) {
|
2022-03-10 16:58:12 -05:00
|
|
|
items.push({resource: resource, locale: lang});
|
2019-03-21 06:00:12 -04:00
|
|
|
} else {
|
|
|
|
for (let locale of Object.keys(locales)) {
|
2022-03-10 16:58:12 -05:00
|
|
|
items.push({resource: resource, locale: locale});
|
2019-03-21 06:00:12 -04:00
|
|
|
}
|
2019-02-11 11:27:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return items;
|
|
|
|
};
|
|
|
|
|
2022-03-10 16:58:12 -05:00
|
|
|
const pullTranslations = async function () {
|
|
|
|
const resources = await txResources('scratch-website');
|
|
|
|
const allFiles = expandResourceFiles(resources);
|
|
|
|
|
|
|
|
try {
|
2022-03-22 16:29:53 -04:00
|
|
|
await batchMap(allFiles, CONCURRENCY_LIMIT, getLocaleData);
|
2022-03-10 16:58:12 -05:00
|
|
|
} catch (err) {
|
2019-02-11 11:27:32 -05:00
|
|
|
console.error(err); // eslint-disable-line no-console
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-03-10 16:58:12 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pullTranslations();
|