Skip to content

Commit

Permalink
Use correct Google Translate AutoML locale (mozilla#3060)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mathjazz authored Jan 15, 2024
1 parent c63b0c5 commit 42a5285
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pontoon/machinery/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions pontoon/machinery/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion translate/src/api/machinery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 42a5285

Please sign in to comment.