Skip to content

Commit

Permalink
Fix translated form labels using current language
Browse files Browse the repository at this point in the history
Fixes #87
  • Loading branch information
julianwachholz committed Apr 7, 2024
1 parent 9aadcc0 commit f6465f5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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

0 comments on commit f6465f5

Please sign in to comment.