Carl Meyer  committed 3fb0ff9

properly translate verbose_name of translated fields

  • Participants
  • Parent commits 430eba2
  • Branches field_name_translation

Comments (0)

Files changed (3)

File modeltranslation/

 from django.conf import settings
 from django.db.models.fields import Field, CharField
-from modeltranslation.utils import get_language, build_localized_fieldname
+from modeltranslation.utils import get_language, build_localized_fieldname, build_localized_verbose_name
 class TranslationField(Field):
                                                  language) = self.attname
-        # Copy the verbose name and append a language suffix (will e.g. in the
-        # admin). This might be a proxy function so we have to check that here.
-        if hasattr(translated_field.verbose_name, '_proxy____unicode_cast'):
-            verbose_name = \
-                translated_field.verbose_name._proxy____unicode_cast()
-        else:
-            verbose_name = translated_field.verbose_name
-        self.verbose_name = '%s [%s]' % (verbose_name, language)
+        # Copy the verbose name and append a language suffix
+        # (will show up e.g. in the admin).
+        self.verbose_name = build_localized_verbose_name(translated_field.verbose_name,
+                                                         language)
     def pre_save(self, model_instance, add):
         val = super(TranslationField, self).pre_save(model_instance, add)

File modeltranslation/

 from django.utils.thread_support import currentThread
 from django.utils.translation import get_language
 from django.utils.translation import trans_real
+from django.utils.translation import ugettext_lazy
 # TODO: tests for TranslationAdmin
 class TestModel(models.Model):
-    title = models.CharField(max_length=255)
+    title = models.CharField(ugettext_lazy('title'), max_length=255)
     text = models.TextField(null=True)
+    def test_verbose_name(self):
+        inst = TestModel.objects.create(title="Testtitle", text="Testtext")
+        self.assertEquals(unicode(inst._meta.get_field('title_de').verbose_name),
+                          u'Titel [de]')
+        inst.delete()
     def test_set_translation(self):
         self.failUnlessEqual(get_language(), "de")
         # First create an instance of the test model to play with

File modeltranslation/

 from django.core.exceptions import ValidationError
 from django.contrib.contenttypes.models import ContentType
 from django.utils.translation import get_language as _get_language
+from django.utils.functional import lazy
 def get_language():
 def build_localized_fieldname(field_name, lang):
     return '%s_%s' % (field_name, lang.replace('-', '_'))
+def _build_localized_verbose_name(verbose_name, lang):
+    return u'%s [%s]' % (verbose_name, lang)
+build_localized_verbose_name = lazy(_build_localized_verbose_name, unicode)
 class TranslationFieldDescriptor(object):
     """A descriptor used for the original translated field."""