Skip to content

Commit b5895e5

Browse files
committed
Pretranslate: run checks and retry with preserved placeables
1 parent d387b86 commit b5895e5

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

‎pontoon/batch/actions.py‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from pontoon.base.models.translation import TranslationQuerySet
1515
from pontoon.base.user_utils import get_pretranslation_authors
1616
from pontoon.batch import utils
17+
from pontoon.checks.libraries import run_checks
18+
from pontoon.checks.utils import bulk_run_checks
1719
from pontoon.messaging.notifications import send_badge_notification
1820
from pontoon.pretranslation.pretranslate import get_pretranslation
1921
from pontoon.translations.utils import parse_source_string_to_json
@@ -445,6 +447,20 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
445447
invalid_entity_pks.append(entity.pk)
446448
continue
447449

450+
failed_checks = run_checks(
451+
entity, locale.code, pretranslation[0], use_tt_checks=False
452+
)
453+
454+
if failed_checks:
455+
try:
456+
pretranslation = get_pretranslation(
457+
entity, locale, preserve_placeables=True
458+
)
459+
except ValueError as e:
460+
logger.info(f"Pretranslation error for entity {entity.pk}: {e!r}")
461+
invalid_entity_pks.append(entity.pk)
462+
continue
463+
448464
string, author_key = pretranslation
449465

450466
try:
@@ -475,9 +491,11 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
475491
)
476492

477493
changed_translations = Translation.objects.bulk_create(translations_to_create)
478-
changed_translations_qs = Translation.objects.filter(
479-
pk__in=[t.pk for t in changed_translations]
480-
)
494+
495+
translation_pks = {t.pk for t in changed_translations}
496+
bulk_run_checks(Translation.objects.for_checks().filter(pk__in=translation_pks))
497+
498+
changed_translations_qs = Translation.objects.filter(pk__in=translation_pks)
481499

482500
count, translated_resources, changed_entities = utils.get_translations_info(
483501
changed_translations_qs, locale
@@ -505,6 +523,7 @@ def pretranslate_translations(user, locale: Locale, entities: QuerySet[Entity]):
505523
else None,
506524
"changed_translation_pks": changed_translation_pks,
507525
"invalid_translation_pks": invalid_entity_pks,
526+
"badge_update": {},
508527
}
509528

510529

‎pontoon/batch/tests/test_utils.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,3 +275,26 @@ def test_pretranslate_translations_skips_existing_pretranslations(
275275

276276
assert Translation.objects.filter(entity=entity).count() == 1
277277
assert result["count"] == 0
278+
279+
280+
@pytest.mark.django_db
281+
@patch("pontoon.batch.actions.run_checks")
282+
@patch("pontoon.batch.actions.get_pretranslation")
283+
def test_pretranslate_translations_retries_on_failed_checks(
284+
get_pretranslation_mock,
285+
run_checks_mock,
286+
):
287+
get_pretranslation_mock.return_value = ("key = pretranslated value", "gt")
288+
run_checks_mock.return_value = {"clWarnings": ["some warning"]}
289+
290+
project = ProjectFactory(slug="project6", name="Project6")
291+
resource = ResourceFactory(project=project, path="resource.ftl", format="fluent")
292+
entity = EntityFactory(resource=resource, string="key1 = value1")
293+
locale = Locale.objects.get(code="en-ZA")
294+
295+
pretranslate_translations(
296+
UserFactory(), locale, Entity.objects.filter(pk=entity.pk)
297+
)
298+
299+
assert get_pretranslation_mock.call_count == 2
300+
get_pretranslation_mock.assert_called_with(entity, locale, preserve_placeables=True)

0 commit comments

Comments
 (0)