Skip to content

Batch actions: Add the ability to pretranslate strings - #4455

Merged
mathjazz merged 13 commits into
mozilla:mainfrom
MundiaNderi:pretranslate-2790
Nov 2, 2026
Merged

mathjazz merged 13 commits into
mozilla:mainfrom
MundiaNderi:pretranslate-2790

Conversation

@MundiaNderi

@MundiaNderi MundiaNderi commented Aug 27, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #2790

This is a draft pr because I want to context switch

Allows contributors to manually batch pretranslate strings for locales supported by translation engines.

@MundiaNderi MundiaNderi changed the title Fixes #2790 Batch actions: Add the ability to pretranslate strings Aug 27, 2026
@codecov-commenter

codecov-commenter commented Aug 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 72.53521% with 78 lines in your changes missing coverage. Please review.
✅ Project coverage is 85.06%. Comparing base (a35a291) to head (8041f13).
⚠️ Report is 15 commits behind head on main.

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

@flodolo

flodolo commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

I haven't looked at the code, but

Teams might want to benefit from pretranslation capabilities on projects for which pretranslation is not enabled (by the admins).

This should not be available to contributors. I would go as far as to limit this to members of the pretranslator group.

@MundiaNderi MundiaNderi self-assigned this Aug 27, 2026
@mathjazz

Copy link
Copy Markdown
Collaborator

This should not be available to contributors. I would go as far as to limit this to members of the pretranslator group.

What do you mean by "pretranslator group"?

Batch actions in general are only available to translators.

@flodolo

flodolo commented Aug 27, 2026

Copy link
Copy Markdown
Collaborator

What do you mean by "pretranslator group"?

There is a pretranslators group used to gate access to the API.

Batch actions in general are only available to translators.

In this case, I don't think it should. We want visibility on which projects have pretranslation enabled, and check quality.

@mathjazz

Copy link
Copy Markdown
Collaborator

Could you elaborate in more detail a bit on how you envision that to work? How would I as a translator join the group?

In this case, I don't think it should. We want visibility on which projects have pretranslation enabled, and check quality.

Why is that not possible if pretranslation is available to translators or managers?

@flodolo

flodolo commented Aug 28, 2026

Copy link
Copy Markdown
Collaborator

Can we move this conversation to the issue?

How would I as a translator join the group?

They wouldn't, that's a manually curated group, because the associated API has a cost and can be abused.

Pretranslation is enabled on a locale+project base with strict criteria. Why would we suddenly change this, through batch actions of all places?
https://mozilla-l10n.github.io/localizer-documentation/tools/pretranslation.html#opt-in-guidelines

Why is that not possible if pretranslation is available to translators or managers?

Pretranslation is not available to translators or managers. Pretranslation is enabled on a project and locale, and by default will translate all resources in the project: what's the goal of having a batch action?

@flodolo

flodolo commented Sep 16, 2026

Copy link
Copy Markdown
Collaborator

@MundiaNderi
You mentioned you had errors locally. Could you add them?

@flodolo

flodolo commented Sep 16, 2026 •

Copy link
Copy Markdown
Collaborator

Looks like it was posted in the wrong PR? #4406 (comment)

ValueError at /batch-edit-translations/

A @fluent-fn attribute is required for :platform

Exception Location: /usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py, line 507, in function_ref

Traceback (most recent call last):
File "/usr/local/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.13/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/pontoon/batch/views.py", line 131, in batch_edit_translations
action_status = pretranslate_translations(user, locale, entities)
File "/app/pontoon/batch/actions.py", line 436, in pretranslate_translations
pretranslation = get_pretranslation(entity, locale)
File "/app/pontoon/pretranslation/pretranslate.py", line 85, in get_pretranslation
pt_res = pt.serialize(value, properties)
File "/app/pontoon/pretranslation/pretranslate.py", line 185, in serialize
return as_string(self.format, entry, fluent_escape_syntax=False)
File "/app/pontoon/sync/formats/init.py", line 62, in as_string
fluent_entry = fluent_astify_entry(
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 202, in fluent_astify_entry
value = fluent_astify_message(
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 312, in fluent_astify_message
selector = value(message.declarations, message.selectors[len(keys0) - 1])
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 528, in value
return expression(decl, decl[val.name], val.name)
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 444, in expression
return function_ref(decl, arg, expr)
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 507, in function_ref
raise ValueError(f"A @fluent-fn attribute is required for :{function}")

Exception Type: ValueError at /batch-edit-translations/
Exception Value: A @fluent-fn attribute is required for :platform

Can it be that you run the migration with an old version of moz-l10n, and that didn't fix existing strings with PLATFORM()?

See #4533 (comment)

@MundiaNderi

Copy link
Copy Markdown
Collaborator Author

Looks like it was posted in the wrong PR? #4406 (comment)

ValueError at /batch-edit-translations/

A @fluent-fn attribute is required for :platform

Exception Location: /usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py, line 507, in function_ref

Traceback (most recent call last):
File "/usr/local/lib/python3.13/site-packages/django/core/handlers/exception.py", line 55, in inner
response = get_response(request)
File "/usr/local/lib/python3.13/site-packages/django/core/handlers/base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/app/pontoon/batch/views.py", line 131, in batch_edit_translations
action_status = pretranslate_translations(user, locale, entities)
File "/app/pontoon/batch/actions.py", line 436, in pretranslate_translations
pretranslation = get_pretranslation(entity, locale)
File "/app/pontoon/pretranslation/pretranslate.py", line 85, in get_pretranslation
pt_res = pt.serialize(value, properties)
File "/app/pontoon/pretranslation/pretranslate.py", line 185, in serialize
return as_string(self.format, entry, fluent_escape_syntax=False)
File "/app/pontoon/sync/formats/init.py", line 62, in as_string
fluent_entry = fluent_astify_entry(
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 202, in fluent_astify_entry
value = fluent_astify_message(
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 312, in fluent_astify_message
selector = value(message.declarations, message.selectors[len(keys0) - 1])
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 528, in value
return expression(decl, decl[val.name], val.name)
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 444, in expression
return function_ref(decl, arg, expr)
File "/usr/local/lib/python3.13/site-packages/moz/l10n/formats/fluent/serialize.py", line 507, in function_ref
raise ValueError(f"A @fluent-fn attribute is required for :{function}")

Exception Type: ValueError at /batch-edit-translations/
Exception Value: A @fluent-fn attribute is required for :platform

Can it be that you run the migration with an old version of moz-l10n, and that didn't fix existing strings with PLATFORM()?

See #4533 (comment)

Ah yes - the 'wrong PR' was what I had open during the meeting. I will look into the new issue and see if there's any correlation, then revert, possibly tomorrow evening / Friday.

@MundiaNderi
MundiaNderi force-pushed the pretranslate-2790 branch 2 times, most recently from fce5ee6 to a1ea4ee Compare September 23, 2026 08:15
@MundiaNderi
MundiaNderi marked this pull request as ready for review September 23, 2026 08:47

@flodolo flodolo left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We have #4526 to align the background task with the API, but we should probably run bulk_run_checks() to avoid storing a pretranslation with warnings/errors.

Comment thread pontoon/batch/actions.py Resolved
Comment thread pontoon/batch/actions.py Resolved
@mathjazz

mathjazz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

We have #4526 to align the background task with the API, but we should probably run bulk_run_checks() to avoid storing a pretranslation with warnings/errors.

Yup - let's keep the validation pipeline used by the pretranslation task.

@mathjazz

mathjazz commented Oct 7, 2026

Copy link
Copy Markdown
Collaborator

One more thing we should change: batch pretranslation currently runs synchronously inside the atomic HTTP request, and it can easily time out while performing external MT calls; it should instead run in the background with an in-progress UI state.

Comment thread pontoon/batch/actions.py Resolved
Comment thread pontoon/batch/actions.py Resolved
@mathjazz

Copy link
Copy Markdown
Collaborator

One more thing we should change: batch pretranslation currently runs synchronously inside the atomic HTTP request, and it can easily time out while performing external MT calls; it should instead run in the background with an in-progress UI state.

This still needs to be addressed.

@mathjazz mathjazz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the update.

Found a few issue, see the outstanding comment about async and the inline comment.

Also, in pontoon/batch/views.py:183:update_translation_memory() we are adding pretranslations to TM. Pretranslations are unapproved machine output, so they shouldn't go into Translation Memory.

Comment thread pontoon/batch/actions.py Resolved
@MundiaNderi

Copy link
Copy Markdown
Collaborator Author

One more thing we should change: batch pretranslation currently runs synchronously inside the atomic HTTP request, and it can easily time out while performing external MT calls; it should instead run in the background with an in-progress UI state.

Sorry had missed this as I only checked in-line comments within the changes. But I have added that and will update the PR soon.
Screenshot 2026-10-20 at 13 19 59

@alvynabranches

This comment was marked as abuse.

Batch actions: Add the ability to pretranslate strings

This is a draft pr because I want to context switch

Allows contributors to manually batch pretranslate strings
for locales supported by translation engines.
candidate fails to parse, rather than failing the whole batch.
Mirrors the existing behavior in pretranslation/tasks.py
EditorMenu only renders when no entities are batch-selected, so
requestInProgress === 'pretranslate' can never coincide with it being
mounted. In-progress feedback already exists in Pretranslate.tsx.
pretranslation candidate or an unparsable one
Comment thread pontoon/batch/actions.py Resolved
Comment thread pontoon/batch/actions.py Resolved
@MundiaNderi
MundiaNderi requested a review from mathjazz October 22, 2026 09:45

@mathjazz mathjazz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for the update! Left a comment inline.

Comment thread pontoon/batch/tasks.py Resolved
Comment thread pontoon/batch/actions.py Resolved
Comment thread pontoon/batch/forms.py Resolved
Comment thread pontoon/batch/tests/test_views.py Resolved
Comment thread pontoon/base/user_utils.py Resolved
Comment thread pontoon/batch/actions.py Resolved
Comment thread pontoon/batch/views.py Resolved
- Recalculate TranslatedResource stats even when every pretranslation
  fails its checks, so error/warning counts don't go stale
- Report invalid entity PKs and invalid translation PKs separately
  instead of mixing them in one list; sum them for the task result
- Fall back to per-translation activation when the bulk update hits
  an IntegrityError
- Poll for the pretranslation status immediately, and extend the
  client timeout to 1 hour to match SYNC_TASK_TIMEOUT
- Allow superusers through IsPretranslator via can_pretranslate()
- Remove the unused `pretranslate` field from BatchActionsForm

@mathjazz mathjazz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Almost there - just a few comments left.

Comment thread translate/src/api/entity.ts Resolved
Comment thread pontoon/batch/actions.py Resolved
…ures

- Expose SYNC_TASK_TIMEOUT to the translate page via a data attribute and
  use it for the pretranslation polling window, falling back to 1 hour
- Remove the unused `pretranslate` param from the client API call
- Log a warning when a pretranslation cannot be activated after an
  IntegrityError
@MundiaNderi
MundiaNderi requested a review from mathjazz November 2, 2026 10:50

@mathjazz mathjazz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Tested locally and it looks good - great work!

Thanks for addressing the review feedback.

Please add a paragraph to the docs and note that the feature is only available to users with permissions and we can merge it.

@MundiaNderi
MundiaNderi requested a review from mathjazz November 2, 2026 13:22

@mathjazz mathjazz left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Almost there!

@mathjazz
mathjazz merged commit 172bcf9 into mozilla:main Nov 2, 2026
12 checks passed
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.

Add ability to manually pretranslate strings

5 participants