如何判断 Django 忽略 Accept-Language 标头的原因?

发布于 2024-08-09 09:59:01 字数 868 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2024-08-16 09:59:01

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 if django_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 the LANGUAGE_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!

嘿看小鸭子会跑 2024-08-16 09:59:01

取自此页面,您可以从请求中删除 HTTP_ACCEPT_LANGUAGE 并依靠 LocaleMiddleware

class ForceDefaultLanguageMiddleware(object):
    """
    Ignore Accept-Language HTTP headers

    This will force the I18N machinery to always choose settings.LANGUAGE_CODE
    as the default initial language, unless another one is set via sessions or cookies

    Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'],
    namely django.middleware.locale.LocaleMiddleware
    """

    def process_request(self, request):
        if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
            del request.META['HTTP_ACCEPT_LANGUAGE']

Taken from this page, you can remove the HTTP_ACCEPT_LANGUAGE from the request and fall back on the LocaleMiddleware:

class ForceDefaultLanguageMiddleware(object):
    """
    Ignore Accept-Language HTTP headers

    This will force the I18N machinery to always choose settings.LANGUAGE_CODE
    as the default initial language, unless another one is set via sessions or cookies

    Should be installed *before* any middleware that checks request.META['HTTP_ACCEPT_LANGUAGE'],
    namely django.middleware.locale.LocaleMiddleware
    """

    def process_request(self, request):
        if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
            del request.META['HTTP_ACCEPT_LANGUAGE']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文