Django makemessage命令不处理应用程序

时间:2016-01-30 20:43:45

标签: python django internationalization django-i18n

我正在尝试将django应用程序国际化。我按照教程,但当我跑...

./manage.py makemessages --all

... django只创建一个.po文件,其中包含我的settings.py的翻译(见下文)。它完全忽略了我的应用程序及其模板中的任何内容。

这是我的文件结构:

myproject
  |- myproject
      |- course
          |- templates
          |- static
          |- ...
          |- apps.py
          |- models.py
          |- views.py
          |- ...
      |- locale
      |- myproject
          |- settings.py
          |- ...
      |- manage.py

我的设置文件如下所示:

from django.utils.translation import ugettext_lazy as _

...

INSTALLED_APPS = [
     ...
     'course.apps.CourseConfig',
]

MIDDLEWARE_CLASSES = [
    ...
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    ...
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': False,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True


LANGUAGES = [
    ('en', _('English')),
    ('kr', _('Korean')),
    ('cn', _('Chinese')),
    ('pt', _('Portuguese')),
]

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

我的模板包含翻译文本:

{% extends "course/base.html" %}

{% load i18n %}    
{% trans "Back to modules" %}
...

如果我运行'makemessages'命令,那么.po文件中唯一包含的是settings.py文件中的语言名称,而不是模板变量或我在视图中用ugettext翻译的任何内容。 / p>

# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2016-01-30 20:25+0100\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"Language: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"

#: myproject/settings.py:146
msgid "English"
msgstr ""

#: myproject/settings.py:147
msgid "Korean"
msgstr ""

#: myproject/settings.py:148
msgid "Chinese"
msgstr ""

#: myproject/settings.py:149
msgid "Portuguese"
msgstr ""

任何人都可以告诉我缺少什么吗?该应用程序工作正常,所有模板都在运行时找到。非常感谢。

1 个答案:

答案 0 :(得分:0)

我终于找到了&#34;问题&#34;。我不得不进入我的django应用course的目录,然后再从那里运行../manage.py makemessages -l en等。然后,文件被创建,一切正常。 Django文档并没有明确表示我必须cd进入我的每个应用程序。