Anonymous avatar Anonymous committed 9322bd0

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

Comments (0)

Files changed (4)

+  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.

modeltranslation/fields.py

 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.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)

modeltranslation/tests.py

 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)
 
 
 
         inst.delete()
 
+    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

modeltranslation/utils.py

 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=""):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.