2

I have installed and configured rosettaon my Djangoproject and I can say it is working because I can see multi-language features with Django adminand I can see strings to translate in third partyand djangotabs, but when a chose projecttab I get this message:

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()在 my中使用的示例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

我不知道我错过了什么。

4

1 回答 1

1

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

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

$ 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)')),
)
于 2019-05-03T04:29:02.317 回答