From f6465f5c4807330a2057332ebefc62953b05ae3f Mon Sep 17 00:00:00 2001 From: Julian Wachholz Date: Wed, 7 Feb 2024 16:01:03 +0100 Subject: [PATCH] Fix translated form labels using current language Fixes #87 --- modeltrans/fields.py | 5 +++-- modeltrans/utils.py | 6 ++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/modeltrans/fields.py b/modeltrans/fields.py index 0be7915..5e27d6d 100644 --- a/modeltrans/fields.py +++ b/modeltrans/fields.py @@ -10,6 +10,7 @@ build_localized_fieldname, get_instance_field_value, get_language, + get_translated_field_label, ) SUPPORTED_FIELDS = (fields.CharField, fields.TextField) @@ -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) diff --git a/modeltrans/utils.py b/modeltrans/utils.py index 8ad611f..16e627a 100644 --- a/modeltrans/utils.py +++ b/modeltrans/utils.py @@ -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 @@ -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