gpt4 book ai didi

python - Django 语言更改被忽略,保持默认

转载 作者:行者123 更新时间:2023-11-28 20:23:06 25 4
gpt4 key购买 nike

我正在使用 Django 1.6,我觉得我错过了什么,但 cookie 设置为当前选择的语言,但显示语言保持默认。

对应代码:

settings.py

LANGUAGES = (
('hu', 'Hungarian'),
('en', 'English'),
)

TEMPLATE_CONTEXT_PROCESSORS = (
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request"
)

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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware'
)
LANGUAGE_CODE = 'en-US'
TIME_ZONE = 'CET'
USE_I18N = True
USE_L10N = True
USE_TZ = True

urls.py

urlpatterns = patterns('',
url(r'^i18n/', include('django.conf.urls.i18n')),
...
)

模板

{% extends 'base.html' %}
{% load i18n %}
...
<h4>{% trans "Modern Technologies" %}</h4>
...

我运行了 ma​​kemessages -a 来创建 lang 文件,安装了 rosetta 并编辑了语言。然后我运行了compilemessages。在 Chrome 中检查 cookie“django_language”是否设置正确。但实际文本仍然是默认的“现代技术”。

最佳答案

您的中间件顺序与 recommended by the documentation 不同:

To use LocaleMiddleware, add 'django.middleware.locale.LocaleMiddleware' to your MIDDLEWARE_CLASSES setting. Because middleware order matters, you should follow these guidelines:

  • Make sure it’s one of the first middlewares installed.
  • It should come after SessionMiddleware, because LocaleMiddleware makes use of session data. It should also come beforeCommonMiddleware because CommonMiddleware needs an activatedlanguage in order to resolve the requested URL.
  • If you use CacheMiddleware, put LocaleMiddleware after it.

所以你的中间件配置应该是这样的:

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.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

您还需要记住包含 LOCALE_PATHS在你的设置文件中设置:

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

关于python - Django 语言更改被忽略,保持默认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21551718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com