Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions pontoon/pretranslation/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ def pretranslate(project: Project, paths: set[str] | None):
if locale_entity in translated_entities or locale_resource not in tr_pairs:
continue

entity_desc = (
f"entity {entity.pk} ({entity.resource.path}) in {locale.code}"
)

try:
pretranslation = get_pretranslation(entity, locale)
except ValueError as e:
log.info(f"Pretranslation error: {e}")
log.info(f"Pretranslation error for {entity_desc}: {e!r}")
continue

failed_checks = run_checks(
Expand All @@ -132,13 +136,19 @@ def pretranslate(project: Project, paths: set[str] | None):
entity, locale, preserve_placeables=True
)
except ValueError as e:
log.info(f"Pretranslation error: {e}")
log.info(f"Pretranslation error for {entity_desc}: {e!r}")
continue

string, author_key = pretranslation
_, value, properties = parse_source_string_to_json(
entity.resource.format, string
)
try:
_, value, properties = parse_source_string_to_json(
entity.resource.format, string
)
except ValueError as e:
log.error(
f"Unparsable pretranslation for {entity_desc}: {e!r}: {string!r}"
)
continue

t = Translation(
entity=entity,
Expand Down
41 changes: 41 additions & 0 deletions pontoon/pretranslation/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,44 @@ def test_which_strings_to_pretranslate(
assert len(non_rejected.translation_set.filter(string="pretranslation")) == 0
assert len(rejected_by_human.translation_set.filter(string="pretranslation")) == 1
assert len(rejected_by_machine.translation_set.filter(string="pretranslation")) == 0


@patch("pontoon.pretranslation.tasks.run_checks")
@patch("pontoon.pretranslation.tasks.get_pretranslation")
@pytest.mark.django_db
def test_pretranslate_skips_unparsable_pretranslation(
gt_mock, checks_mock, project_a, locale_a, gt_user, caplog
):
"""
An entity whose pretranslation doesn't parse must not abort the pretranslation
of other entities.
"""
project_a.pretranslation_enabled = True
project_a.save()

resource = ResourceFactory.create(
project=project_a, path="resource.ftl", format="fluent"
)
TranslatedResourceFactory.create(resource=resource, locale=locale_a)
ProjectLocaleFactory.create(
project=project_a,
locale=locale_a,
pretranslation_enabled=True,
)

EntityFactory.create(resource=resource, string="key-a = A\n", order=0)
EntityFactory.create(resource=resource, string="key-b = B\n", order=1)

checks_mock.return_value = {}
gt_mock.side_effect = [
("key-a = unescaped { brace\n", "gt"),
("key-b = GT\n", "gt"),
]

with caplog.at_level("ERROR", logger="pontoon.pretranslation.tasks"):
pretranslate_task(project_a.pk)

assert "Unparsable pretranslation" in caplog.text
assert [t.string for t in Translation.objects.filter(user=gt_user)] == [
"key-b = GT\n"
]