Skip to content

Commit

Permalink
[RNMobile] Avoid "429 - Too Many Request" error when fetching transla…
Browse files Browse the repository at this point in the history
…tions (#51103)

* Fetch translations in batches

* Abort fetch translations process upon failure

* Avoid parsing response if translation request failed

* Retry translation download when getting error 429

* Revert fetch translations in batches

* Fix language URL

I noticed when testing that requests to the language URL were being redirected. For this reason, the URL has been updated to avoid this redirection.
  • Loading branch information
fluiddot authored Jun 1, 2023
1 parent 54e9eee commit 1d87f29
Showing 1 changed file with 52 additions and 12 deletions.
64 changes: 52 additions & 12 deletions packages/react-native-editor/bin/i18n-translations-download.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,54 @@ const supportedLocales = [
'zh-tw', // Chinese (Taiwan)
];

const MAX_RETRIES = 5;
const RETRY_DELAY = 2000;

const getLanguageUrl = ( locale, projectSlug ) =>
`https://translate.wordpress.org/projects/${ projectSlug }/dev/${ locale }/default/export-translations\?format\=json`;
`https://translate.wordpress.org/projects/${ projectSlug }/dev/${ locale }/default/export-translations/\?format\=json`;

const getTranslationFilePath = ( locale ) => `./data/${ locale }.json`;

const fetchTranslation = ( locale, projectSlug ) => {
let retryCount = MAX_RETRIES;
const localeUrl = getLanguageUrl( locale, projectSlug );
return fetch( localeUrl )
.then( ( response ) => response.json() )
.then( ( body ) => {
return { response: body, locale };
} )
.catch( () => {
console.error(
`Could not find translation file ${ localeUrl } for project slug ${ projectSlug }`
);
} );
const request = () =>
fetch( localeUrl )
.then( ( response ) => {
if ( ! response.ok ) {
const { status, statusText } = response;

// Retry when encountering "429 - Too Many Requests" error
if ( status === 429 && retryCount > 0 ) {
console.log(
`Translation file ${ localeUrl } for project slug ${ projectSlug } failed with error 429 - Too Many Requests, retrying (${ retryCount })...`
);
retryCount--;
return new Promise( ( resolve ) =>
setTimeout(
() => request().then( resolve ),
RETRY_DELAY
)
);
}

console.error(
`Could not find translation file ${ localeUrl } for project slug ${ projectSlug }`,
{ status, statusText }
);
return { locale, status, statusText };
}
return response.json();
} )
.then( ( body ) => {
return { response: body, locale };
} )
.catch( () => {
console.error(
`Could not find translation file ${ localeUrl } for project slug ${ projectSlug }`
);
} );
return request();
};

const fetchTranslations = ( {
Expand Down Expand Up @@ -104,7 +135,16 @@ const fetchTranslations = ( {
let extraTranslations = [];

return Promise.all( fetchPromises ).then( ( results ) => {
const fetchedTranslations = results.filter( Boolean );
const fetchedTranslations = results.filter(
( result ) => result.response
);

// Abort process if any translation can't be fetched
if ( fetchedTranslations.length !== supportedLocales.length ) {
process.exit( 1 );
return;
}

const translationFilePromises = fetchedTranslations.map(
( languageResult ) => {
return new Promise( ( resolve, reject ) => {
Expand Down

1 comment on commit 1d87f29

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 1d87f29.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5144794340
📝 Reported issues:

Please sign in to comment.