Skip to content

Commit

Permalink
Skip creating StringTranslations if the machine translators do not …
Browse files Browse the repository at this point in the history
…provide the translation (#819)
  • Loading branch information
tognee authored Dec 20, 2024
1 parent 97c2970 commit 7bfd881
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 0 deletions.
123 changes: 123 additions & 0 deletions wagtail_localize/tests/test_edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from wagtail.models import Locale, Page, Revision
from wagtail.test.utils import WagtailTestUtils

from wagtail_localize.machine_translators.dummy import translate_html
from wagtail_localize.models import (
OverridableSegment,
SegmentOverride,
Expand All @@ -43,6 +44,7 @@
TranslationLog,
TranslationSource,
)
from wagtail_localize.strings import StringValue
from wagtail_localize.test.models import (
Header,
NavigationLink,
Expand Down Expand Up @@ -90,6 +92,23 @@
]


# Patches for translation skipping tests
def patched_translate(source_locale, target_locale, strings):
result = {}
for string in strings:
if not all(ord(c) < 128 for c in string.data):
continue
result[string] = StringValue(translate_html(string.data))

return result


def patched_translate_html(html):
if not all(ord(c) < 128 for c in html):
return None
return translate_html(html)


class EditTranslationTestData(WagtailTestUtils):
def setUp(self):
self.login()
Expand Down Expand Up @@ -3400,6 +3419,110 @@ def test_machine_translate_page(self):
self.assertEqual(translation_3.tool_name, "Dummy translator")
self.assertEqual(translation_3.last_translated_by, self.user)

@patch(
"wagtail_localize.machine_translators.dummy.DummyTranslator.translate",
side_effect=patched_translate,
)
def test_machine_translate_page_with_translate_skip(self, mock_translate_html):
response = self.client.post(
reverse(
"wagtail_localize:machine_translate", args=[self.page_translation.id]
),
{
"next": reverse("wagtailadmin_pages:edit", args=[self.fr_page.id]),
},
)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[self.fr_page.id])
)

translation_1 = StringTranslation.objects.get(
translation_of__data="A char field",
context__path="test_charfield",
locale=self.fr_locale,
)

self.assertEqual(translation_1.data, "field char A")
self.assertEqual(
translation_1.translation_type, StringTranslation.TRANSLATION_TYPE_MACHINE
)
self.assertEqual(translation_1.tool_name, "Dummy translator")
self.assertEqual(translation_1.last_translated_by, self.user)

translation_2 = StringTranslation.objects.get(
translation_of__data='<a id="a1">This is a link</a>.',
context__path="test_richtextfield",
locale=self.fr_locale,
)

self.assertEqual(translation_2.data, '.<a id="a1">link a is This</a>')
self.assertEqual(
translation_2.translation_type, StringTranslation.TRANSLATION_TYPE_MACHINE
)
self.assertEqual(translation_2.tool_name, "Dummy translator")
self.assertEqual(translation_2.last_translated_by, self.user)

translation_3 = StringTranslation.objects.filter(
translation_of__data="Special characters: '\"!? セキレイ",
context__path="test_richtextfield",
locale=self.fr_locale,
).first()

self.assertIsNone(translation_3)

@patch(
"wagtail_localize.machine_translators.dummy.translate_html",
side_effect=patched_translate_html,
)
def test_machine_translate_page_with_translate_html_skip(self, mock_translate_html):
response = self.client.post(
reverse(
"wagtail_localize:machine_translate", args=[self.page_translation.id]
),
{
"next": reverse("wagtailadmin_pages:edit", args=[self.fr_page.id]),
},
)

self.assertRedirects(
response, reverse("wagtailadmin_pages:edit", args=[self.fr_page.id])
)

translation_1 = StringTranslation.objects.get(
translation_of__data="A char field",
context__path="test_charfield",
locale=self.fr_locale,
)

self.assertEqual(translation_1.data, "field char A")
self.assertEqual(
translation_1.translation_type, StringTranslation.TRANSLATION_TYPE_MACHINE
)
self.assertEqual(translation_1.tool_name, "Dummy translator")
self.assertEqual(translation_1.last_translated_by, self.user)

translation_2 = StringTranslation.objects.get(
translation_of__data='<a id="a1">This is a link</a>.',
context__path="test_richtextfield",
locale=self.fr_locale,
)

self.assertEqual(translation_2.data, '.<a id="a1">link a is This</a>')
self.assertEqual(
translation_2.translation_type, StringTranslation.TRANSLATION_TYPE_MACHINE
)
self.assertEqual(translation_2.tool_name, "Dummy translator")
self.assertEqual(translation_2.last_translated_by, self.user)

translation_3 = StringTranslation.objects.filter(
translation_of__data="Special characters: '\"!? セキレイ",
context__path="test_richtextfield",
locale=self.fr_locale,
).first()

self.assertIsNone(translation_3)

def test_machine_translate_snippet(self):
response = self.client.post(
reverse(
Expand Down
6 changes: 6 additions & 0 deletions wagtail_localize/views/edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,12 @@ def apply_machine_translation(translation_id, user, machine_translator):

with transaction.atomic():
for string, contexts in segments.items():
if (
translations.get(string) is None
or translations[string].data is None
):
# Don't create a translation if the machine can't provide
continue
for string_id, context_id in contexts:
StringTranslation.objects.get_or_create(
translation_of_id=string_id,
Expand Down

0 comments on commit 7bfd881

Please sign in to comment.