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 (WordPress#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 and sethrubenstein committed Jul 13, 2023
1 parent 17027b5 commit 727fb33
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

0 comments on commit 727fb33

Please sign in to comment.