From 76e04858cd543b5bad5584d7863e74f2f4607a9c Mon Sep 17 00:00:00 2001 From: Dirk Eschler Date: Wed, 26 May 2010 09:14:00 +0000 Subject: [PATCH] Fixed verbose names of translated fields are not translated. Resolves issue 24 (thanks to carl.j.meyer). --- CHANGELOG.txt | 2 ++ modeltranslation/fields.py | 16 +++++++--------- modeltranslation/tests.py | 15 ++++++++++++--- modeltranslation/utils.py | 6 ++++++ 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 9cba01a..01887e0 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,5 @@ + 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. diff --git a/modeltranslation/fields.py b/modeltranslation/fields.py index 5498d6b..6aa8bd6 100644 --- a/modeltranslation/fields.py +++ b/modeltranslation/fields.py @@ -2,7 +2,8 @@ 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): @@ -42,14 +43,11 @@ 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) diff --git a/modeltranslation/tests.py b/modeltranslation/tests.py index 3e0f39b..b8fafcf 100644 --- a/modeltranslation/tests.py +++ b/modeltranslation/tests.py @@ -7,17 +7,18 @@ from django.test import TestCase from django.utils.thread_support import currentThread from django.utils.translation import get_language from django.utils.translation import trans_real - -# TODO: tests for TranslationAdmin +from django.utils.translation import ugettext_lazy from modeltranslation import translator +# TODO: tests for TranslationAdmin + 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) @@ -75,6 +76,14 @@ class ModelTranslationTest(TestCase): 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 diff --git a/modeltranslation/utils.py b/modeltranslation/utils.py index aeb57d1..8f7713c 100644 --- a/modeltranslation/utils.py +++ b/modeltranslation/utils.py @@ -4,6 +4,7 @@ from django.conf import settings 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(): @@ -30,6 +31,11 @@ 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.""" def __init__(self, name, initial_val=""):