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

Fix translated form labels using current language #108

Merged
merged 2 commits into from
Apr 9, 2024
Merged
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
5 changes: 3 additions & 2 deletions modeltrans/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
build_localized_fieldname,
get_instance_field_value,
get_language,
get_translated_field_label,
)

SUPPORTED_FIELDS = (fields.CharField, fields.TextField)
Expand Down Expand Up @@ -76,9 +77,9 @@ def contribute_to_class(self, cls, name):
self.column = None

# Use a translated verbose name:
translated_field_name = gettext(self.original_field.verbose_name)
translated_field_name = self.original_field.verbose_name
if self.language is not None:
translated_field_name += " ({})".format(self.language.upper())
translated_field_name = get_translated_field_label(translated_field_name, self.language)
self.verbose_name = translated_field_name

setattr(cls, name, self)
Expand Down
6 changes: 6 additions & 0 deletions modeltrans/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from django.db.models.constants import LOOKUP_SEP
from django.db.models.fields.json import KeyTransform
from django.db.models.lookups import Transform
from django.utils.functional import keep_lazy_text
from django.utils.translation import get_language as _get_language

from .conf import get_available_languages, get_default_language
Expand All @@ -24,6 +25,11 @@ def split_translated_fieldname(field_name):
return (field_name[0:_pos], field_name[_pos + 1 :])


@keep_lazy_text
def get_translated_field_label(original_label, lang):
return original_label + " ({})".format(lang.upper())


def build_localized_fieldname(field_name, lang, ignore_default=False):
if lang == "id":
# The 2-letter Indonesian language code is problematic with the
Expand Down
Binary file added tests/app/locale/de/LC_MESSAGES/django.mo
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/app/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-07 11:39+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

#: app/models.py:203
msgid "title of the post"
msgstr "Titel des Beitrags"
Binary file added tests/app/locale/fr/LC_MESSAGES/django.mo
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/app/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-04-07 11:39+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"

#: app/models.py:203
msgid "title of the post"
msgstr "Titre de l'article"
6 changes: 5 additions & 1 deletion tests/app/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.db import models
from django.utils.translation import gettext_lazy

from modeltrans.conf import get_default_language
from modeltrans.fields import TranslationField
Expand Down Expand Up @@ -198,7 +199,10 @@ def __str__(self):


class Post(models.Model):
title = models.CharField(max_length=255)
title = models.CharField(
verbose_name=gettext_lazy("title of the post"),
max_length=255,
)
is_published = models.BooleanField(default=False)

i18n = TranslationField(fields=("title",))
Expand Down
20 changes: 20 additions & 0 deletions tests/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,26 @@ def test_setting_of_field_properties(self):
self.assertEqual(title_fr_field.required, True)
self.assertEqual(title_fr_field.widget.__class__, forms.widgets.Textarea)

def test_translated_field_labels(self):
"""Test that a field's verbose_name is translated to the currently active language."""
form_cls = modelform_factory(Post, fields="__all__")
form = form_cls()
self.assertEqual(form.fields["title"].label, "Title of the post")
self.assertEqual(form.fields["title_de"].label, "Title of the post (DE)")
self.assertEqual(form.fields["title_fr"].label, "Title of the post (FR)")

with override("de"):
form = form_cls()
self.assertEqual(form.fields["title"].label, "Titel des Beitrags")
self.assertEqual(form.fields["title_de"].label, "Titel des Beitrags (DE)")
self.assertEqual(form.fields["title_fr"].label, "Titel des Beitrags (FR)")

with override("fr"):
form = form_cls()
self.assertEqual(form.fields["title"].label, "Titre de l'article")
self.assertEqual(form.fields["title_de"].label, "Titre de l'article (DE)")
self.assertEqual(form.fields["title_fr"].label, "Titre de l'article (FR)")

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