Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Let machine translators skip fields #819

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
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 @@ -1417,6 +1417,12 @@ def machine_translate(request, translation_id):

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