Django 1.10 翻译

3

我正在尝试在Django中进行国际化/本地化。

当我尝试使用以下命令生成'.po'文件时,出现了错误:

./manage.py makemessages

以下是settings.py中的相关部分:

import os

from django.utils.translation import ugettext_lazy as _

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls.apps.PollsConfig'
]

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',
]

ROOT_URLCONF = 'sampleproject.urls'

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

WSGI_APPLICATION = 'sampleproject.wsgi.application'

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

LANGUAGES = [
    ('fi-FI', _('Finnish')),
    ('en', _('English')),
]

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

urls.py 中的相关部分

urlpatterns += i18n_patterns(
    url(r'^$', home, name='home'),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^polls/', include('polls.urls')),
)

这是追踪信息。

Traceback (most recent call last):
  File "./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
    utility.execute()
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/__init__.py", line 359, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/base.py", line 305, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/base.py", line 356, in execute
    output = self.handle(*args, **options)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 361, in handle
    potfiles = self.build_potfiles()
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 393, in build_potfiles
    self.process_files(file_list)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 488, in process_files
    self.process_locale_dir(locale_dir, files)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 507, in process_locale_dir
    build_file.preprocess()
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/core/management/commands/makemessages.py", line 113, in preprocess
    content = templatize(src_data, self.path[2:])
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/utils/translation/__init__.py", line 214, in templatize
    return _trans.templatize(src, origin)
  File "/project/Myapps/for_sample/lib/python3.4/site-packages/django/utils/translation/trans_real.py", line 670, in templatize
    "%s (%sline %d)" % (t.contents, filemsg, t.lineno)
SyntaxError: Translation blocks must not include other block tags: blocktrans count var|length as count (file htmlcov/_project_Myapps_for_sample_lib_python3_4_site-packages_django_templatetags_i18n_py.html, line 1073)

开发设置: Django 1.10 Python 3.4.5

由于这是我在SO上的第一个问题,如果有任何错误,请原谅 :) 提前感谢 :)


你的 htmlcov/_project_Myapps_for_sample_lib_python3_4_site-packages_django_templatetags_i18n_py.html html 模板长什么样?它显示有错误。 - akarilimano
是的,我刚刚解决了这个错误。当我运行coverage.py脚本时,生成了提到的那个文件。我删除了HTML报告,错误就消失了。但是,当我运行./manage.py makemessages命令时,没有生成.po文件。 - glider
使用 --keep-pot 选项运行。它“防止在创建 .po 文件之前删除生成的临时 .pot 文件。这对于调试可能会阻止最终语言文件创建的错误非常有用。” https://docs.djangoproject.com/en/1.10/ref/django-admin/#makemessages - akarilimano
是的,它起作用了,谢谢伙计 :) - glider
FYI - 你把你的SECRET_KEY发布到了全世界。你绝对不想让你的秘钥泄露出去。我猜这不是生产环境,但你仍然应该更改它。Philip James在DjangoCon US上进行了一次漂亮的5分钟闪电演讲,介绍了如何处理此类问题:https://speakerdeck.com/phildini/oops-i-committed-my-secret-key - FlipperPA
这只是我为了尝试覆盖率、翻译和Sphinx而创建的一个示例应用程序。根据您的建议,我已经删除了秘密密钥...感谢您的信息,FlipperPA。 - glider
1个回答

4
错误是由运行覆盖率脚本时生成的htmlcov文件夹引起的。删除该文件夹并执行以下命令以生成“.po”文件。
./manage.py makemessages -l fi
执行以下命令以生成“.mo”文件。
./manage.py compilemessages

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接