我的Django项目有问题,我使用Jinja2作为模板引擎;特别是使用django-jinja“提供不引人注目的Jinja2支持”:(。我的问题是模板中的字符串没有被翻译。Django识别正确的语言,也可以使用/i18n/setlang视图进行设置。
奇怪的是,Python中定义的字符串被翻译成:
# in settings/base.py
from django.utils.translation import ugettext_lazy as _
LANGUAGES = (
('en', _('English')),
('de', _('German')),
)
LANGUAGE_CODE = "en"
TIME_ZONE = "UTC"
USE_I18N = True
USE_L10N = True我有一个django.po位于locale/de/LC_MESSAGES/django.po,有一个对应的django.mo
#: PyLatein/settings/base.py:144
msgid "English"
msgstr "Englisch"
#: PyLatein/settings/base.py:145
msgid "German"
msgstr "Deutsch"
#: templates/about.jinja:9
msgid "About"
msgstr "Über"在我呈现的模板中,使用了正确的语言(LANGUAGE_CODE是正确的)和
if the language is en, then I can choose between "English" and "German",
if the language is de, then I can choose between "Englisch" and "Deutsch"!->因此正确翻译了这些,但是About仍然是About而不是Über,尽管在我使用的模板about.jinja中
<h1 class="page-header">{{ _('About') }}</h1>我的中间人是:
MIDDLEWARE_CLASSES = (
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.auth.middleware.SessionAuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django.middleware.security.SecurityMiddleware",
)我的模板定义是:
TEMPLATES = [
{
"BACKEND": "django_jinja.backend.Jinja2",
"DIRS": [osp.join(BASE_DIR, "templates")],
"APP_DIRS": True,
"OPTIONS": {
"match_extension": ".jinja",
"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",
],
"extensions": [
"jinja2.ext.do",
"jinja2.ext.loopcontrols",
"jinja2.ext.with_",
"jinja2.ext.i18n",
"jinja2.ext.autoescape",
"django_jinja.builtins.extensions.CsrfExtension",
"django_jinja.builtins.extensions.CacheExtension",
"django_jinja.builtins.extensions.TimezoneExtension",
"django_jinja.builtins.extensions.UrlsExtension",
"django_jinja.builtins.extensions.StaticFilesExtension",
"django_jinja.builtins.extensions.DjangoFiltersExtension",
],
"constants": {
"settings": settings,
},
"translation_engine": "django.utils.translation",
}
},
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"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",
],
},
},
]我使用Python 3、Django 1.9.8和django-jinja 2.2.0
发布于 2016-07-26 15:52:19
原来我需要指定LOCALE_PATHS
LOCALE_PATHS = (
osp.join(BASE_DIR, "locale"),
)我以某种方式假设,在默认情况下,它们被设置为"locale“(因为我在.py中读到了这篇文章)。
这就是为什么它没有翻译我的字符串。“英语”和“德语”这两个词仍然被翻译,因为django在$PYTHONPATH/django/conf/locale/<language>/LC_MESSAGES/django.(po|mo)中有一个后备语言环境。
https://stackoverflow.com/questions/38589229
复制相似问题