From 42a5285abddad3c3fd79ae3b4b1a7c309308b61a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Mon, 15 Jan 2024 14:58:46 +0100 Subject: [PATCH] Use correct Google Translate AutoML locale (#3060) For locales that use the same locale code in Google Translate (e.g. es-AR and es-ES both use es), we now use the correct locale code in Machinery Google Translate if AutoML is enabled. --- pontoon/machinery/tests/test_views.py | 2 +- pontoon/machinery/views.py | 10 +++------- translate/src/api/machinery.ts | 2 +- 3 files changed, 5 insertions(+), 9 deletions(-) diff --git a/pontoon/machinery/tests/test_views.py b/pontoon/machinery/tests/test_views.py index e80d54f6bb..058dd6c1aa 100644 --- a/pontoon/machinery/tests/test_views.py +++ b/pontoon/machinery/tests/test_views.py @@ -84,7 +84,7 @@ def test_view_google_translate( m.post("https://translation.googleapis.com/language/translate/v2", json=data) response = member.client.get( url, - {"text": "text", "locale": google_translate_locale.google_translate_code}, + {"text": "text", "locale": google_translate_locale.code}, ) assert response.status_code == 200 diff --git a/pontoon/machinery/views.py b/pontoon/machinery/views.py index 7ffe4caf31..163b1d36c8 100644 --- a/pontoon/machinery/views.py +++ b/pontoon/machinery/views.py @@ -156,16 +156,12 @@ def google_translate(request): """Get translation from Google machine translation service.""" try: text = request.GET["text"] - locale_code = request.GET["locale"] - - if not locale_code: - raise ValueError("Locale code is empty") + locale = Locale.objects.get(code=request.GET["locale"]) - locale = Locale.objects.filter(google_translate_code=locale_code).first() - if not locale: + if not locale.google_translate_code: raise ValueError("Locale code not supported") - except (MultiValueDictKeyError, ValueError) as e: + except (Locale.DoesNotExist, MultiValueDictKeyError, ValueError) as e: return JsonResponse( {"status": False, "message": f"Bad Request: {e}"}, status=400, diff --git a/translate/src/api/machinery.ts b/translate/src/api/machinery.ts index ebb3357753..c1f2e8c792 100644 --- a/translate/src/api/machinery.ts +++ b/translate/src/api/machinery.ts @@ -128,7 +128,7 @@ export async function fetchGoogleTranslation( const url = '/google-translate/'; const params = { text: original, - locale: locale.googleTranslateCode, + locale: locale.code, }; const { translation } = (await GET_(url, params)) as {