本文介绍了我怎么知道Django为什么忽略Accept-Language头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我希望国际化的Django应用程序(在Google App Engine上)。

settings.py:

  USE_I18N = True 
LANGUAGE_CODE ='en'

#限制支持的语言(和JS媒体生成)
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文件(尽管不是我设置浏览器的Accept-Language标题为fr,而Django忽略它。当我查看request.LANGUAGE_CODE时,它始终是en 。

我可以告诉浏览器是正确的,因为我访问了一些其他支持i18n的站点,并且它返回法语。



如何我发现Django认为我的设置缺少什么?



我看到,它并没有帮助我。



我使用应用程序引擎补丁1.0.2.2运行Django 1.0在Google App Engine上。

解决方案
$ b $有一定的顺序让Django在i18n方面做事。 b

首先检查 LANGUAGE_CODE 。它是整个网站的语言,如果没有其他设置,这是用户获得的语言。

第二,既然你已经添加了 LocaleMiddleware ,它检查用户会话中是否设置了 django_language 。第三,它检查是否有一个 django_language cookie集合(或实际上,cookie的名称由 LANGUAGE_COOKIE_NAME 定义)。我建议删除这个cookie。第四,它寻找 Accept-Language HTTP标头。这是您的浏览器设置的来源。



祝您好运!


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.

解决方案

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!

这篇关于我怎么知道Django为什么忽略Accept-Language头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 08:30