尝试本地化我的应用程序时遇到问题。它有两种语言版本:英语和德语。当浏览器的语言设置为英语(美国)并且在我的设置文件中设置为“de”时,就会出现问题,反之亦然。有些字段以英语显示,有些则以德语显示。我的模型包含 CharField、DecimalField 和 DateField 字段类型。
模型.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)
表格.py:
class TestForm(ModelForm):
test_date = forms.DateField(label=_('Booking date'), widget=AdminDateWidget)
设置.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.uploadhandler.TemporaryFileUploadHandler',
'django.middleware.transaction.TransactionMiddleware',
'pagination.middleware.PaginationMiddleware',
)
英语是浏览器设置的语言。字段 test_number 和 test_price 的标签显示为德语,而 test_date 的标签显示为英语。如果我从 models.py 中删除 _('Test number') 并将其添加为 forms.py 中的标签属性,它就可以工作。这不是另一种方式吗?