开发者

Localization of Django application only applies to forms.py and not to models.py

I have a problem when trying to localize my application. It is available in two languages: english and german. The problem appears when the browser has the language set english(United States) and in my settings file is set to 'de' and vice-versa. Some fields appear in english, others in german. My model contains CharField, DecimalField and DateField field types.

models.py:

from django.db import models  
from django.utils.translation import ugettext as _  

class Test(models.Model):  
    test_number = models.CharField(_('Test number'), max_length=20)  
    test_date = models.DateField()  
    test_price = models.DecimalField(_('Test price'), max_digits=16, decimal_places=2, null=True, blank=True)  

forms.py:

class TestForm(ModelForm):
    test_date = forms.DateField(label=_('Booking date'), widget=AdminDateWidget)

settings.py

USE_L10N = True  
USE_I18N = True  

TIME_ZONE = 'Europe/Berlin'  
LANGUAGE_CODE = 'de'

TEMPLATE_CONTEXT_PROCESSORS = (  
    "django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media",
    "django.core.context_processors.request",    
)  

MIDDLEWARE_CLASSES = (  
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.core.files.uploadhandler.MemoryFileUploadHandler',
    'django.core.files.uploadhandl开发者_开发问答er.TemporaryFileUploadHandler',
    'django.middleware.transaction.TransactionMiddleware',
    'pagination.middleware.PaginationMiddleware',    
)

English is the language set the browser.The labels of the fields test_number and test_price appear in german and the label of test_date in english. If I remove _('Test number') from models.py and added it as label attribute in forms.py it works. Isn't it another way of doing this?


Changing the declaration "from django.utils.translation import ugettext as _" to "from django.utils.translation import ugettext_lazy as _" seems to solve the problem.


Doublecheck your .po file: it shouldn't have any 'fuzzy' status.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜