I have a problem with my Django Project, I use Jinja2 as template engine; specifically using django-jinja "providing unobtrusive Jinja2 support" :(. My problem is that the strings in the template are not translated. Django recognizes the correct language, which can also be set by using the /i18n/setlang view.
What's weird is that the strings defined in Python ARE translated:
# 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
I have a django.po
located in locale/de/LC_MESSAGES/django.po
with a corresponding 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"
In my rendered template, the correct language is used (LANGUAGE_CODE
is correct) and
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"!
-> so those are translated correctly, but About
is still About
and not Über
, although in the template about.jinja
I used
<h1 class="page-header">{{ _('About') }}</h1>
My middlewares are:
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",
)
And my template definition is:
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",
],
},
},
]
I use Python 3, Django 1.9.8 and django-jinja 2.2.0