Skip to content

Commit

Permalink
Translation form: customizable field labels
Browse files Browse the repository at this point in the history
  • Loading branch information
danielvdp committed Jul 20, 2021
1 parent bdf7969 commit 7b53162
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
django-modeltrans change log
============================

## 0.7.1 (2021-07-20)
- Add customizable field labels to `TranslationModelForm`

## 0.7.0 (2021-06-30)
- Add `TranslationModelForm`

Expand Down
7 changes: 6 additions & 1 deletion docs/pages/translation-model-form.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,9 @@ The label of the field is adjusted to include the relevant language
and to designate the field as a translation or default fallback field, as follows:

- translation fields: "field name (NL, translation language)"
- fallback field: "field name (EN, default language)"
- fallback field: "field name (EN, fallback language)"

The labels "translation language" and "fallback language" are customizable using the `Meta` options:

- `fallback_label` (defaults to "fallback language")
- `translation_label` (defaults to "translation language")
9 changes: 8 additions & 1 deletion modeltrans/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ def __init__(self, options=None):
super().__init__(options)
self.languages = getattr(options, "languages", ["browser", "fallback"])
self.fallback_language = getattr(options, "fallback_language", None)
self.labels = {
"fallback": getattr(options, "fallback_label", _("fallback language")),
"translation": getattr(options, "translation_label", _("translation language")),
}


class TranslationModelFormMetaClass(forms.models.ModelFormMetaclass):
Expand Down Expand Up @@ -202,6 +206,7 @@ def __init__(self, *args, languages=None, fallback_language=None, **kwargs):

self.model_i18n_field = get_i18n_field(self._meta.model)
self.languages = languages or self._meta.languages
self.labels = self._meta.labels
self.i18n_fields = [
field for field in self.model_i18n_field.fields if field in self.base_fields.keys()
]
Expand Down Expand Up @@ -245,7 +250,9 @@ def set_translation_field_attributes(self):
if field_name != original_field_name:
language = field_name.replace(f"{original_field_name}_", "")
is_translation = language != self.fallback_language
label_text = _("translation language") if is_translation else _("default language")
label_text = (
self.labels["translation"] if is_translation else self.labels["fallback"]
)
label = f"{original_field.label} ({language.upper()}, {label_text})"
self.fields[field_name].label = label
self.fields[field_name].required = (
Expand Down
21 changes: 19 additions & 2 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_setting_of_field_properties(self):
"""Test that fields are set with the correct properties."""
with self.subTest("Browser (fallback) language"):
title_field = Form().fields["title"]
self.assertEqual(title_field.label, "Title (EN, default language)")
self.assertEqual(title_field.label, "Title (EN, fallback language)")
self.assertTrue(title_field.required)

with self.subTest("Custom (fallback) language"):
Expand All @@ -236,10 +236,27 @@ def test_setting_of_field_properties(self):
self.assertEqual(title_de_field.widget.__class__, forms.widgets.Textarea)

title_fr_field = form.fields["title_fr"]
self.assertEqual(title_fr_field.label, "Title (FR, default language)")
self.assertEqual(title_fr_field.label, "Title (FR, fallback language)")
self.assertEqual(title_fr_field.required, True)
self.assertEqual(title_fr_field.widget.__class__, forms.widgets.Textarea)

with self.subTest("Custom labels"):

class CustomLabelForm(TranslationModelForm):
class Meta:
model = Challenge
fields = ["title"]
languages = ["browser", "fallback"]
fallback_language = "fr"
fallback_label = "default language"
translation_label = "new language"

form = CustomLabelForm()
title_field = form.fields["title"]
self.assertEqual(title_field.label, "Title (EN, new language)")
title_field = form.fields["title_fr"]
self.assertEqual(title_field.label, "Title (FR, default language)")

def test_form_initial_values(self):
challenge = Challenge.objects.create(title="english", title_fr="french")
initial_data = {
Expand Down

0 comments on commit 7b53162

Please sign in to comment.