【问题标题】:Localization of Django application only applies to forms.py and not to models.pyDjango 应用程序的本地化仅适用于 forms.py 而不适用于 models.py
【发布时间】:2010-10-11 11:17:47
【问题描述】:

我在尝试本地化我的应用程序时遇到问题。它有两种语言版本:英语和德语。当浏览器的语言设置为英语(美国)并且在我的设置文件中设置为“de”时,就会出现问题,反之亦然。一些字段以英语显示,其他字段以德语显示。我的模型包含 CharField、DecimalField 和 DateField 字段类型。

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.uploadhandler.TemporaryFileUploadHandler',
    'django.middleware.transaction.TransactionMiddleware',
    'pagination.middleware.PaginationMiddleware',    
)

English是浏览器设置的语言。字段test_number和test_price的标签显示为德语,test_date的标签显示为英文。如果我从 models.py 中删除 _('Test number') 并将其添加为 forms.py 中的标签属性,它就可以工作。这不是另一种方式吗?

【问题讨论】:

  • 请编辑您的问题以正确格式化您的代码。编辑问题时,页面右侧有格式指南。

标签: python django localization models modelform


【解决方案1】:

将声明“from django.utils.translation import ugettext as _”更改为“from django.utils.translation import ugettext_lazy as _”似乎可以解决问题。

【讨论】:

    【解决方案2】:

    仔细检查您的 .po 文件:它不应该有任何“模糊”状态。

    【讨论】:

    • 我已经仔细检查过了。没有模糊条目。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-22
    • 2013-02-14
    • 2014-11-10
    • 1970-01-01
    相关资源
    最近更新 更多