pontoon icon indicating copy to clipboard operation
pontoon copied to clipboard

Pretranslation API

Open functionzz opened this issue 1 month ago • 8 comments

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.~

functionzz avatar Nov 17 '25 20:11 functionzz

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 78.86%. Comparing base (6e57308) to head (cbe6f55).

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

codecov-commenter avatar Nov 17 '25 20:11 codecov-commenter

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 avatar Nov 24 '25 20:11 functionzz

The code looks good!

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

mathjazz avatar Nov 25 '25 13:11 mathjazz

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 avatar Nov 26 '25 20:11 mathjazz

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.

mathjazz avatar Dec 01 '25 13:12 mathjazz

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 avatar Dec 03 '25 07:12 flodolo

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."}

flodolo avatar Dec 09 '25 15:12 flodolo

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."}
  • Please wrap locale code into back ticks.
  • Please make sure there is no double "..".

mathjazz avatar Dec 11 '25 16:12 mathjazz

Please rebase.

mathjazz avatar Dec 18 '25 13:12 mathjazz