在Django Rosetta中看不到我的项目字符串

时间:2018-05-15 18:09:50

标签: python django rosetta

我已经在我的rosetta项目上安装并配置了Django我可以说它正在运行,因为我可以看到Django admin的多语言功能,我可以看到要翻译的字符串third partydjango个标签,但是当选择project标签时,我收到此消息:

Nothing to translate!
You haven't specified any languages in your settings file, or haven't yet generated a batch of translation catalogs.

Please refer to Django's I18N documentation for a guide on how to set up internationalization for your project.

我已在代码中使用gettext(),但我无法在rosetta中看到这些字符串。

这是在gettext()中使用models.py的示例:

from django.utils.translation import gettext as _

class Hotel(models.Model):

    ONE_STAR = '*'
    TWO_STARS = '**'
    THREE_STARS = '***'
    FOUR_STARS = '****'
    FIVE_STARS = '****'
    GRAND_TOURISM = 'GRAND_TOURISM'
    NA = 'NA'
    SPECIAL = 'SPECIAL'
    ECO = 'ECO'
    BOUTIQUE = 'BOUTIQUE'

    HOTEL_CATEGORY_CHOICES = (
        (ONE_STAR, _('*')),
        (TWO_STARS, _('**')),
        (THREE_STARS, _('***')),
        (FOUR_STARS, _('****')),
        (FIVE_STARS, _('*****')),
        (GRAND_TOURISM, _('Grand Tourism')),
        (NA, _('NA')),
        (SPECIAL, _('Special')),
        (ECO, _('Eco-Hotel')),
        (BOUTIQUE, _('Boutique-Hotel'))
    )

这是settings.py中的语言配置:

LOCALE_PATHS = (
    os.path.join(BASE_DIR, 'locale'),
)

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]    

LANGUAGE_CODE = 'en-us'
gettext = lambda s: s
LANGUAGES = (
    ('en', gettext('English')),
    ('de', gettext('German')),
    ('es', gettext('Spanish')),
)

USE_I18N = True

我不知道我错过了什么。

1 个答案:

答案 0 :(得分:0)

开发人员应该知道,当您添加语言的区域或特定的书面脚本形式时,用于发出消息的命令必须使用“ _”(下划线)作为区域分隔符,并且项目的settings.py必须使用“ -”(连字符)作为区域分隔符。

例如,假设我们要添加“ UK”版本的英语(繁体和简体中文)以及香港地区的中文(广东话)。以下是创建语言翻译文件的命令:

$ django-admin makemessages -l en_uk
$ django-admin makemessages -l zh_hk
$ django-admin makemessages -l zh_hans
$ django-admin makemessages -l zh_hant

项目的settings.py需要使用连字符来包含LANGUAGES元组:

LANGUAGES = (
    ('en-uk', _('English (UK)')),
    ('zh-hk', _('Cantonese')),
    ('zh-hans', _('Chinese (Simplified)')),
    ('zh-hant', _('Chinese (Traditional)')),
)