尝试翻译阻止此块并加载i18n:
{% load i18n %}
<p>{% trans "Welcome to our page" %}</p>
{% language 'ru' %}
<p>{% trans "Welcome to our page" %}</p>
{% endlanguage %}
settings.py
SE_I18N = True
USE_L10N = True
LANGUAGES = (
('en', _('English')),
('ru', _('Russian')),
)
LOCALE_PATHS = (
os.path.join(BASE_DIR, 'locale'),
)
TEMPLATES = [ {
'context_processors': [
'django.template.context_processors.i18n',
MIDDLEWARE = [
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
的.po
msgid "Welcome to our page"
msgstr "Приветствую вас!"
#: project/settings.py:172
msgid "English"
msgstr "Английский"
#: project/settings.py:173
msgid "Russian"
msgstr "Русский"
MO。
Report-Msgid-Bugs-To:
POT-Creation-Date: 2016-10-10 19:24+0000
PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE
Last-Translator: FULL NAME <EMAIL@ADDRESS>
Language-Team: LANGUAGE <LL@li.org>
Language:
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Английский Русский Приветствую вас!
urls.py
import os
from django.conf.urls import url, include
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^', include('account_management.urls')),
url(r'^admin/', admin.site.urls),
url(r'^i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += staticfiles_urlpatterns()
但结果是:
欢迎来到我们的页面 欢迎来到我们的页面
我用过这个文档 https://docs.djangoproject.com/en/1.10/topics/i18n/translation/
也许我忘记了什么?
答案 0 :(得分:0)
所以,当你需要i18n_patterns
时,你只有普通的网址,
因此,请将您的网址更改为
import os
from django.conf.urls import url, include
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^i18n/', include('django.conf.urls.i18n')),
]
urlpatterns += i18n_patterns(
url(r'^', include('account_management.urls')), # put here urls that you need to translate
)
urlpatterns += staticfiles_urlpatterns()
可在此处找到文档i18n_patterns
答案 1 :(得分:0)
您需要使用更新{% get_current_language as LANGUAGE_CODE %}
设置的LANGUAGE_CODE
标记。请参阅how-django-discovers-language-preference。
{% load i18n %}
<p>{% trans "Welcome to our page" %}</p>
{% language 'ru' %}
{% get_current_language as LANGUAGE_CODE %}
<p>{% trans "Welcome to our page" %}</p>
{% endlanguage %}
希望这有帮助。