mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-18 17:41:09 +00:00
Fixed verbose names of translated fields are not translated. Resolves issue 24 (thanks to carl.j.meyer).
This commit is contained in:
parent
fd78a0957f
commit
76e04858cd
4 changed files with 27 additions and 12 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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=""):
|
||||
|
|
|
|||
Loading…
Reference in a new issue