1. Carl Meyer
  2. django-modeltranslation


eschler  committed 9322bd0

Fixed verbose names of translated fields are not translated. Resolves issue 24 (thanks to carl.j.meyer).

  • Participants
  • Parent commits 430eba2
  • Branches default

Comments (0)

Files changed (4)


View file
  • Ignore whitespace
+  FIXED: Verbose names of translated fields are not translated.
+         (thanks to carl.j.meyer, resolves issue 24).
   FIXED: Race condition between model import and translation registration in
          production by ensuring that models are registered for translation
          before TranslationAdmin runs.

File modeltranslation/fields.py

View file
  • Ignore whitespace
 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):
         self.name = 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/tests.py

View file
  • Ignore whitespace
 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
+from modeltranslation import translator
 # TODO: tests for TranslationAdmin
-from modeltranslation import translator
 settings.LANGUAGES = (('de', 'Deutsch'),
                       ('en', 'English'))
 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/utils.py

View file
  • Ignore whitespace
 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():
     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."""
     def __init__(self, name, initial_val=""):