Skip to content

Conversation

@functionzz
Copy link
Collaborator

@functionzz functionzz commented Nov 17, 2025

Description

This PR implements a Pretranslation API at api/v2/pretranslate/?format_string=FORMAT&locale=CODE. It utilizes existing pretranslation functionality in Pontoon and extends it as an authenticated service for those who need a pretranslation from either Translation Memory or AutoML.

Fixes #3830.

Additional Notes

Authentication for this endpoint is temporarily disabled for easier testing
Requires some testing on android and gettext resource formats.

@codecov-commenter
Copy link

codecov-commenter commented Nov 17, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.85%. Comparing base (737a932) to head (e8593da).

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@functionzz functionzz removed the API label Nov 17, 2025
Copy link
Collaborator

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

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

Nice work! Left some comments inline.

@functionzz functionzz requested a review from mathjazz November 20, 2025 18:37
@functionzz
Copy link
Collaborator Author

I also applied the same above env change for PRETRANSLATION_API_MAX_CHARS to the PERSONAL_ACCESS_TOKEN configuration if that is something that we want.

@functionzz functionzz requested a review from mathjazz November 24, 2025 21:29
@mathjazz
Copy link
Collaborator

The code looks good!

Why is the PR maked as Draft? Is there any functionality still missing?

@functionzz functionzz marked this pull request as ready for review November 25, 2025 14:17
Comment on lines +495 to +401
if not locale:
errors["locale"] = ["This field is required."]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder if we should also check if Locale.google_automl_model is set. That way Pretranslation API will only work for locales for which Pretranslation in Pontoon works, i.e. for the ones that have custom MT models.

@flodolo Thoughts?

@mathjazz
Copy link
Collaborator

You can test the PR on https://mozilla-pontoon-staging.herokuapp.com:

import requests

response = requests.post(
    "https://mozilla-pontoon-staging.herokuapp.com/api/v2/pretranslate/?locale=sl",
    data="Open",
    headers={
        "Content-Type": "text/plain",
        "Accept": "application/json",
        "Authorization": f"Bearer {TOKEN}",
    },
)

@mathjazz
Copy link
Collaborator

mathjazz commented Dec 1, 2025

Please add a flag in the UserProfile model or create a user group for users authorized to use pretranslation, and use that as another authentication step.

@functionzz functionzz requested a review from mathjazz December 2, 2025 17:01
@flodolo
Copy link
Collaborator

flodolo commented Dec 3, 2025

You can test the PR on mozilla-pontoon-staging.herokuapp.com:

import requests

response = requests.post(
    "https://mozilla-pontoon-staging.herokuapp.com/api/v2/pretranslate/?locale=sl",
    data="Open",
    headers={
        "Content-Type": "text/plain",
        "Accept": "application/json",
        "Authorization": f"Bearer {TOKEN}",
    },
)

Noting that this works on Heroku, but not on GCP (CSRF validation fails).

@flodolo
Copy link
Collaborator

flodolo commented Dec 9, 2025

This is the error displayed if we're missing the Google API keys. We probably need to fix this.

{"error":"An error occurred: Pretranslation for `test = { -brand-short-name } says:` to it not available.. Please verify the resource format and syntax."}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Pretranslation API

4 participants