Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@
</div>
<p class="help">{{ field.help_text }}</p>
</li>
{% elif field.name == 'use_machine_translation'%}
{% if machine_translator %}
<li class="use-machine-translation-field">
<div class="field boolean_field checkbox_input">
<div class="field-content">
<div class="input">
<input type="checkbox" name="{{ field.name }}" id="{{ field.auto_id }}">
<label class="use-machine-translation-label" for="{{ field.id_for_label }}">{{ field.label }}</label>
</div>
</div>
</div>
<p class="help">{{ field.help_text }}</p>
</li>
{% endif %}
{% else %}
<li>{% include "wagtailadmin/shared/field.html" %}</li>
{% endif %}
Expand Down
70 changes: 69 additions & 1 deletion wagtail_localize/tests/test_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
from wagtail.models import Locale, Page, PageViewRestriction
from wagtail.test.utils import WagtailTestUtils

from wagtail_localize.models import StringSegment, Translation, TranslationSource
from wagtail_localize.models import (
StringSegment,
StringTranslation,
Translation,
TranslationSource,
)
from wagtail_localize.test.models import NonTranslatableSnippet, TestSnippet

from .utils import assert_permission_denied, make_test_page
Expand Down Expand Up @@ -612,3 +617,66 @@ def test_post_update_page_translation_will_run_update_target_view_restrictions(
)
# one call from the first post, and one from above
self.assertEqual(update_target_view_restrictions.call_count, 2)

def test_post_update_page_translation_with_publish_translations_and_use_machine_translation(
self,
):
self.en_blog_post.test_charfield = "Edited blog post"
self.en_blog_post.save_revision().publish()

response = self.client.post(
reverse(
"wagtail_localize:update_translations",
args=[self.page_source.id],
),
{
"publish_translations": "on",
"use_machine_translation": "on",
},
)

self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_blog_index.id])
)

# The FR version should be translated (Dummy translator will reverse) and updated
self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "post blog Edited")

def test_post_update_page_translation_with_use_machine_translation(self):
self.en_blog_post.test_charfield = "Edited blog post"
self.en_blog_post.save_revision().publish()

response = self.client.post(
reverse(
"wagtail_localize:update_translations",
args=[self.page_source.id],
),
{
"use_machine_translation": "on",
},
)

self.assertRedirects(
response, reverse("wagtailadmin_explore", args=[self.en_blog_index.id])
)

self.fr_blog_post.refresh_from_db()
self.assertEqual(self.fr_blog_post.test_charfield, "Test content")

# Check that the translation is done, awaiting publication
fr = Locale.objects.get(language_code="fr")
translation_source = TranslationSource.objects.get_for_instance_or_none(
self.en_blog_post
)
translation = translation_source.translations.get(target_locale=fr)

string_segment = translation.source.stringsegment_set.get(
string__data="Edited blog post"
)
string_translation = StringTranslation.objects.get(
translation_of_id=string_segment.string_id,
locale=fr,
context_id=string_segment.context_id,
)
self.assertEqual(string_translation.data, "post blog Edited")
14 changes: 10 additions & 4 deletions wagtail_localize/views/edit_translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1370,12 +1370,11 @@ def upload_pofile(request, translation_id):
return redirect(next_url)


@require_POST
def machine_translate(request, translation_id):
def apply_machine_translation(translation_id, user):
translation = get_object_or_404(Translation, id=translation_id)

instance = translation.get_target_instance()
if not user_can_edit_instance(request.user, instance):
if not user_can_edit_instance(user, instance):
raise PermissionDenied

translator = get_machine_translator()
Expand Down Expand Up @@ -1426,12 +1425,19 @@ def machine_translate(request, translation_id):
"data": translations[string].data,
"translation_type": StringTranslation.TRANSLATION_TYPE_MACHINE,
"tool_name": translator.display_name,
"last_translated_by": request.user,
"last_translated_by": user,
"has_error": False,
"field_error": "",
},
)
return True
return False


@require_POST
def machine_translate(request, translation_id):
if apply_machine_translation(translation_id, request.user):
translator = get_machine_translator()
messages.success(
request,
_("Successfully translated with {}.").format(translator.display_name),
Expand Down
29 changes: 26 additions & 3 deletions wagtail_localize/views/update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,35 @@
from wagtail.snippets.models import get_snippet_models
from wagtail.utils.version import get_main_version

from wagtail_localize.machine_translators import get_machine_translator
from wagtail_localize.models import TranslationSource
from wagtail_localize.views.edit_translation import apply_machine_translation
from wagtail_localize.views.submit_translations import TranslationComponentManager


if get_machine_translator():
PUBLISH_TRANSLATIONS_HELP = (
"Select this to publish immediately. Changes will be published "
"in the original language unless you also select use machine translation."
)
else:
PUBLISH_TRANSLATIONS_HELP = (
"Select this to publish immediately. Changes will be published "
"in the original language until you translate and publish them."
)

USE_MACHINE_TRANSLATION_HELP = "Apply machine translations to the incoming changes."


class UpdateTranslationsForm(forms.Form):
publish_translations = forms.BooleanField(
label=gettext_lazy("Publish immediately"),
help_text=gettext_lazy(
"This will apply the updates and publish immediately, before any new translations happen."
),
help_text=gettext_lazy(PUBLISH_TRANSLATIONS_HELP),
required=False,
)
use_machine_translation = forms.BooleanField(
label=gettext_lazy("Use machine translation"),
help_text=gettext_lazy(USE_MACHINE_TRANSLATION_HELP),
required=False,
)

Expand Down Expand Up @@ -109,6 +128,7 @@ def get_context_data(self, **kwargs):
enabled=True
).select_related("target_locale")
],
"machine_translator": get_machine_translator(),
"last_sync_date": self.object.last_updated_at,
"form": self.get_form(),
"next_url": self.get_success_url(),
Expand All @@ -133,6 +153,9 @@ def form_valid(self, form):
self.object.update_from_db()

enabled_translations = self.object.translations.filter(enabled=True)
if form.cleaned_data["use_machine_translation"]:
for translation in enabled_translations.select_related("target_locale"):
apply_machine_translation(translation.id, self.request.user)

if form.cleaned_data["publish_translations"]:
for translation in enabled_translations.select_related("target_locale"):
Expand Down