Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
zerolab committed Dec 20, 2024
1 parent ee1f5b0 commit 98836ef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@
</div>
<p class="help">{{ field.help_text }}</p>
</li>
{% elif field.name == 'use_machine_translation' %}
<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>
{% else %}
<li>{% include "wagtailadmin/shared/field.html" %}</li>
{% endif %}
Expand Down
7 changes: 2 additions & 5 deletions wagtail_localize/tests/test_update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.auth.models import Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ValidationError
from django.forms.widgets import CheckboxInput, HiddenInput
from django.forms.widgets import CheckboxInput
from django.test import TestCase, override_settings
from django.urls import reverse
from wagtail.models import Locale, Page, PageViewRestriction
Expand Down Expand Up @@ -632,10 +632,7 @@ def test_update_translations_form_without_machine_translator(self):

self.assertEqual(response.status_code, 200)

self.assertIsInstance(
response.context["form"].fields["use_machine_translation"].widget,
HiddenInput,
)
self.assertNotIn("use_machine_translation", response.context["form"].fields)

def test_update_translations_form_with_machine_translator(self):
response = self.client.get(
Expand Down
15 changes: 7 additions & 8 deletions wagtail_localize/views/update_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,40 @@
HAS_MACHINE_TRANSLATOR = get_machine_translator() is not None

if HAS_MACHINE_TRANSLATOR:
PUBLISH_TRANSLATIONS_HELP = (
PUBLISH_TRANSLATIONS_HELP = gettext_lazy(
"Apply the updates and publish immediately. The changes will use "
"the original language until translated unless you also select "
'"Use machine translation".'
)
else:
PUBLISH_TRANSLATIONS_HELP = (
PUBLISH_TRANSLATIONS_HELP = gettext_lazy(
"Apply the updates and publish immediately. The changes will use "
"the original language until translated."
)

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(PUBLISH_TRANSLATIONS_HELP),
help_text=PUBLISH_TRANSLATIONS_HELP,
required=False,
)
use_machine_translation = forms.BooleanField(
label=gettext_lazy("Use machine translation"),
help_text=gettext_lazy(USE_MACHINE_TRANSLATION_HELP),
help_text=gettext_lazy("Apply machine translations to the incoming changes."),
required=False,
)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if not HAS_MACHINE_TRANSLATOR:
self.fields["use_machine_translation"].widget = forms.HiddenInput()
del self.fields["use_machine_translation"]

def clean(self):
cleaned_data = super().clean()
if cleaned_data.get("use_machine_translation") and not HAS_MACHINE_TRANSLATOR:
if not HAS_MACHINE_TRANSLATOR and cleaned_data.get("use_machine_translation"):
raise ValidationError(_("A machine translator could not be found."))
return cleaned_data


class UpdateTranslationsView(SingleObjectMixin, TemplateView):
Expand Down

0 comments on commit 98836ef

Please sign in to comment.