如何判断 Django 忽略 Accept-Language 标头的原因?
我有一个 Django 应用程序(在 Google App Engine 上),我希望将其国际化。
settings.py:
USE_I18N = True
LANGUAGE_CODE = 'en'
# Restrict supported languages (and JS media generation)
LANGUAGES = (
('en', 'English'),
('fr', 'French'),
)
MIDDLEWARE_CLASSES = (
'ragendja.middleware.ErrorMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# i18n
'django.middleware.locale.LocaleMiddleware',
...
我已在 locale/fr/LC_MESSAGES 中为我的应用程序生成了 .po 和 .mo 文件(尽管不是在全局级别)。
我将浏览器接受语言标题设置为“fr”,而 Django 会忽略它。当我查看 request.LANGUAGE_CODE 时,它始终是“en”。
我可以判断浏览器是正确的,因为我访问其他一些支持 i18n 的网站并且它返回法语。
如何找到 Django 认为我的设置缺少的内容?
我看到这个问题,它对我没有帮助。
我正在 Google App Engine 上使用应用程序引擎补丁 1.0.2.2 运行 Django 1.0。
I have a Django app (on Google App Engine) that I wish to internationalize.
settings.py:
USE_I18N = True
LANGUAGE_CODE = 'en'
# Restrict supported languages (and JS media generation)
LANGUAGES = (
('en', 'English'),
('fr', 'French'),
)
MIDDLEWARE_CLASSES = (
'ragendja.middleware.ErrorMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
# i18n
'django.middleware.locale.LocaleMiddleware',
...
I have generated .po and .mo files for my app in locale/fr/LC_MESSAGES (though not at the global level).
I set my browser Accept-Language heading to "fr" and Django ignores it. When I look at request.LANGUAGE_CODE it is always "en".
I can tell the browser is proper because I visit some other i18n-aware site and it returns French.
How do I find what Django thinks is missing about my setup?
I saw this question and it didn't help me.
I am running Django 1.0 using app engine patch 1.0.2.2 on Google App Engine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Django 根据 i18n 执行操作有一定的顺序。
首先它检查
LANGUAGE_CODE
。这是站点范围的语言,如果没有其他设置,这就是用户获得的语言。其次,由于您添加了
LocaleMiddleware
,它会检查是否在用户会话中设置了django_language
。我建议清除数据库中的会话信息或创建一个全新的用户来尝试。第三,它检查是否设置了
django_language
cookie(或者实际上,cookie 的名称由LANGUAGE_COOKIE_NAME
定义)。我建议删除这个cookie。第四,它查找
Accept-Language
HTTP 标头。这就是您的浏览器设置的用武之地。祝您好运!
There's a certain order that Django does things in terms of i18n.
First it checks
LANGUAGE_CODE
. It's the site-wide language and if nothing else is set, this is the language the user gets.Second, since you've added the
LocaleMiddleware
, it checks ifdjango_language
is set in the user session. I would suggest clearing the session information in the DB or creating a completely new user to try with.Third, it checks if there's a
django_language
cookie set (or, actually, the name of the cookie is defined by theLANGUAGE_COOKIE_NAME
). I would suggest deleting this cookie.Fourth, it looks for the
Accept-Language
HTTP header. Which is where your browser setting comes in.Good luck!
取自此页面,您可以从请求中删除
HTTP_ACCEPT_LANGUAGE
并依靠LocaleMiddleware
:Taken from this page, you can remove the
HTTP_ACCEPT_LANGUAGE
from the request and fall back on theLocaleMiddleware
: