mirror of
https://github.com/scratchfoundation/scratch-l10n.git
synced 2025-01-03 11:25:51 -05:00
fix(scripts): add retries, correct formatting for editor
retries flaky transifex api calls up to 5 times; converts message with description to plain message for editor translations
This commit is contained in:
parent
dd2ed99ece
commit
137b6e3d09
4 changed files with 46 additions and 21 deletions
|
@ -72,8 +72,17 @@ const downloadResource = async function (projectSlug, resourceSlug, localeCode,
|
|||
*/
|
||||
const txPull = async function (project, resource, locale, mode = 'default') {
|
||||
const url = await downloadResource(project, resource, locale, mode);
|
||||
const buffer = await download(url);
|
||||
return JSON.parse(buffer.toString());
|
||||
let buffer;
|
||||
for (let i = 0; i < 5; i++) {
|
||||
try {
|
||||
buffer = await download(url);
|
||||
return JSON.parse(buffer.toString());
|
||||
} catch (e) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`got ${e.message}, retrying after ${i + 1} failed attempt(s)`);
|
||||
}
|
||||
}
|
||||
throw Error('failed to pull after 5 retries');
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -48,7 +48,7 @@
|
|||
"dependencies": {
|
||||
"@babel/cli": "^7.1.2",
|
||||
"@babel/core": "^7.1.2",
|
||||
"@transifex/api": "3.0.0",
|
||||
"@transifex/api": "4.2.5",
|
||||
"babel-plugin-react-intl": "^3.0.1",
|
||||
"download": "^8.0.0",
|
||||
"transifex": "1.6.6"
|
||||
|
|
|
@ -52,11 +52,20 @@ const getLocaleData = async function (locale) {
|
|||
const pullTranslations = async function () {
|
||||
try {
|
||||
const values = await batchMap(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);
|
||||
// if translation has message & description, we only want the message
|
||||
let txs = {};
|
||||
for (const key of Object.keys(translation.translations)) {
|
||||
const tx = translation.translations[key];
|
||||
if (tx.message) {
|
||||
txs[key] = tx.message;
|
||||
} else {
|
||||
txs[key] = tx;
|
||||
}
|
||||
}
|
||||
validateTranslations({locale: translation.locale, translations: txs}, source);
|
||||
const file = JSON.stringify(txs, null, 4);
|
||||
fs.writeFileSync(
|
||||
`${OUTPUT_DIR}/${translation.locale}.json`,
|
||||
file
|
||||
|
|
|
@ -46,21 +46,28 @@ const getLocaleData = async function (item) {
|
|||
const locale = item.locale;
|
||||
const resource = item.resource;
|
||||
let txLocale = localeMap[locale] || locale;
|
||||
|
||||
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
|
||||
};
|
||||
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) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(`got ${e.message}, retrying after ${i + 1} attempts`);
|
||||
}
|
||||
}
|
||||
throw Error('failed to pull translations after 5 retries');
|
||||
};
|
||||
|
||||
const expandResourceFiles = (resources) => {
|
||||
|
|
Loading…
Reference in a new issue