From b0bfcc2954403b78b07e1e3d045ca7097c056838 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Wed, 17 Apr 2024 22:56:28 +0200 Subject: [PATCH] Do not make requests to non-configured Machinery services (#3181) Translate view makes requests to Google Translate and Microsoft Translator backend code even if API keys are not set. While the requests to external services are not made in this case, we should still prevent the redundant call to Pontoon backend. --- pontoon/translate/views.py | 5 +++++ translate/public/translate.html | 3 +++ translate/src/context/MachineryTranslations.tsx | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pontoon/translate/views.py b/pontoon/translate/views.py index 7c1aab6a7..bc04e6ba1 100644 --- a/pontoon/translate/views.py +++ b/pontoon/translate/views.py @@ -70,6 +70,11 @@ def translate(request, locale, project, resource): raise Http404 context = { + "is_google_translate_supported": bool(settings.GOOGLE_TRANSLATE_API_KEY), + "is_microsoft_translator_supported": bool( + settings.MICROSOFT_TRANSLATOR_API_KEY + ), + "is_systran_translate_supported": bool(settings.SYSTRAN_TRANSLATE_API_KEY), "locale": get_preferred_locale(request), "notifications": [], } diff --git a/translate/public/translate.html b/translate/public/translate.html index c4891e6a4..41ae15409 100644 --- a/translate/public/translate.html +++ b/translate/public/translate.html @@ -31,6 +31,9 @@
diff --git a/translate/src/context/MachineryTranslations.tsx b/translate/src/context/MachineryTranslations.tsx index 6dc060349..370e5d484 100644 --- a/translate/src/context/MachineryTranslations.tsx +++ b/translate/src/context/MachineryTranslations.tsx @@ -99,15 +99,24 @@ export function MachineryProvider({ // Only make requests to paid services if user is authenticated if (isAuthenticated) { - if (locale.googleTranslateCode) { + const root = document.getElementById('root'); + + const isGoogleTranslateSupported = + root?.dataset.isGoogleTranslateSupported === 'true'; + const isMicrosoftTranslatorSupported = + root?.dataset.isMicrosoftTranslatorSupported === 'true'; + const isSystranTranslateSupported = + root?.dataset.isSystranTranslateSupported === 'true'; + + if (isGoogleTranslateSupported && locale.googleTranslateCode) { fetchGoogleTranslation(plain, locale).then(addResults); } - if (locale.msTranslatorCode) { + if (isMicrosoftTranslatorSupported && locale.msTranslatorCode) { fetchMicrosoftTranslation(plain, locale).then(addResults); } - if (locale.systranTranslateCode) { + if (isSystranTranslateSupported && locale.systranTranslateCode) { fetchSystranTranslation(plain, locale).then(addResults); } }