全部。我有一些問題與語言切換的非常簡單的應用:Django語言沒有切換模板
settings.py
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.staticfiles',
)
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',
)
ROOT_URLCONF = 'lr.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.i18n',
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
],
},
},
]
urls.py
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^auth/(?P<cmd>.*)$', auth, name='auth'),
url(r'^i18n/(?P<lang>.*)$', i18n, name='i18n'),
url(r'^admin/', include(admin.site.urls)),
]
views.py
class HomePageView(TemplateView):
"""
Template view for Home page
"""
template_name = 'home.html'
def home(request):
"""
Site title page
"""
return HomePageView.as_view()(request)
def i18n(request, **kwargs):
"""
Change locale processing
"""
from django.utils.translation import check_for_language
if 'lang' in kwargs:
lang = kwargs['lang']
if lang and check_for_language(lang):
if hasattr(request, 'session'):
request.session[LANGUAGE_COOKIE_NAME] = lang
if request.method == 'POST' \
and 'redirect' in request.POST:
# Place hidden input, named 'redirect' in login form in html template
# for making redirect to the same page after login/logout
return HttpResponseRedirect(request.POST['redirect'])
return HttpResponseRedirect('/')
頁模板空,它只是標題,登錄表單和ru/eng切換表單(只有三個單詞用於翻譯)。
不知何故,模板中的語言代碼(get_current_language)總是'en',除非我將'django.middleware.locale.LocaleMiddleware'放在MIDDLEWARE_CLASSES的頂部 - 在這種情況下,他總是'ru'。 我試過使用django.conf.urls.i18n,translation.activate()和許多其他的東西,但無濟於事。請告訴我一些,我做錯了什麼?這裏提供
整個項目代碼:https://www.dropbox.com/s/a7nojxpxpajcbmu/lr.zip?dl=0
雖然這可能是一個很好的答案,這將是更好,如果你的相關部分複製粘貼您的文章。鏈接可能會隨着時間而中斷謝謝。 – lrnzcig
感謝的答案,但我絕不錯過語言 LANGUAGE_CODE = '恩' 語言=( ( '儒',u'Рус。 '), (' 恩 'u'Eng。') ) 這裏是整個簡單的項目: https://www.dropbox.com/s/a7nojxpxpajcbmu/lr.zip?dl=0 –