2018-11-25 18:05:44 -05:00
|
|
|
#!/usr/bin/env babel-node
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @fileoverview
|
|
|
|
* Script to pull translations from transifex and generate the editor-msgs file.
|
|
|
|
* Expects that the project and resource have already been defined in Transifex, 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. Usage:
|
2022-03-04 15:48:42 -05:00
|
|
|
node tx-pull-editor.js tx-project tx-resource path
|
2018-11-25 18:05:44 -05:00
|
|
|
tx-project: project on Transifex (e.g., scratch-editor)
|
|
|
|
tx-resource: resource within the project (e.g., interface)
|
|
|
|
path: where to put the downloaded json files
|
|
|
|
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.
|
|
|
|
`;
|
2022-03-04 15:48:42 -05:00
|
|
|
|
2018-11-25 18:05:44 -05:00
|
|
|
// Fail immediately if the TX_TOKEN is not defined
|
|
|
|
if (!process.env.TX_TOKEN || args.length < 3) {
|
|
|
|
process.stdout.write(usage);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2022-03-04 15:48:42 -05:00
|
|
|
import {txPull} from '../lib/transifex.js';
|
2018-11-25 18:05:44 -05:00
|
|
|
import async from 'async';
|
2022-03-04 15:48:42 -05:00
|
|
|
import {validateTranslations} from '../lib/validate.js';
|
2018-11-25 18:05:44 -05:00
|
|
|
import locales, {localeMap} from '../src/supported-locales.js';
|
|
|
|
|
|
|
|
// Globals
|
|
|
|
const PROJECT = args[0];
|
|
|
|
const RESOURCE = args[1];
|
|
|
|
const OUTPUT_DIR = path.resolve(args[2]);
|
2022-03-04 15:48:42 -05:00
|
|
|
const MODE = 'reviewed';
|
|
|
|
const CONCURRENCY_LIMIT = 36;
|
2018-11-25 18:05:44 -05:00
|
|
|
|
2022-03-04 15:48:42 -05:00
|
|
|
const getLocaleData = async function (locale) {
|
2018-11-25 18:05:44 -05:00
|
|
|
let txLocale = localeMap[locale] || locale;
|
2022-03-04 15:48:42 -05:00
|
|
|
const data = await txPull(PROJECT, RESOURCE, txLocale, MODE);
|
|
|
|
|
|
|
|
return {
|
|
|
|
locale: locale,
|
|
|
|
translations: data
|
|
|
|
};
|
2018-11-25 18:05:44 -05:00
|
|
|
};
|
|
|
|
|
2022-03-04 15:48:42 -05:00
|
|
|
const pullTranslations = async function () {
|
|
|
|
try {
|
|
|
|
const values = await async.mapLimit(Object.keys(locales), CONCURRENCY_LIMIT, getLocaleData);
|
|
|
|
|
|
|
|
const source = values.find(elt => elt.locale === 'en').translations;
|
|
|
|
values.forEach(function (translation) {
|
|
|
|
validateTranslations({locale: translation.locale, translations: translation.translations}, source);
|
|
|
|
const file = JSON.stringify(translation.translations, null, 4);
|
|
|
|
fs.writeFileSync(
|
|
|
|
`${OUTPUT_DIR}/${translation.locale}.json`,
|
|
|
|
file
|
|
|
|
);
|
|
|
|
});
|
|
|
|
} catch (err) {
|
2018-11-25 18:05:44 -05:00
|
|
|
console.error(err); // eslint-disable-line no-console
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2022-03-04 15:48:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
pullTranslations();
|