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

Translation form: customizable field labels #73

Open
wants to merge 1 commit into
base: master
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
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")),
}
Comment on lines +21 to +24
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the more compact version, this would become

Suggested change
self.labels = {
"fallback": getattr(options, "fallback_label", _("fallback language")),
"translation": getattr(options, "translation_label", _("translation language")),
}
labels = getattr(options, "labels", {})
self.labels = {
"fallback": labels.get("fallback_label", _("fallback language")),
"translation": labels.get("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"
Comment on lines +251 to +252
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or do we prefer something more compact like:

Suggested change
fallback_label = "default language"
translation_label = "new language"
labels = {"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