Django Translation - 适用于测试客户端,但不适用于浏览器
我不明白为什么 django 翻译(i18n)不起作用。我在代码中使用英语,并编写了法语翻译。奇怪的是,当我使用 django.test.client.Client 时它有效,但当我使用网络浏览器时它不起作用。
我尝试在浏览器设置中将 Accept-Language 设置为 fr,并且还尝试从请求标头中删除键 HTTP_ACCEPT_LANGUAGE 。
我已通过 django.conf.urls.i18n 中的 setlang 视图将语言设置为 fr。
当我这样做时:
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
即使我的页面中应该转换的字符串仍然是英文,我也成功获得了 fr。
我的设置中有这样的内容:
USE_I18N = True
#Probably ignored because of the django_language cookie
LANGUAGE_CODE = 'fr'
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'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.transaction.TransactionMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
)
我已经编译了我的翻译,并且它们可以工作,因为我在使用测试客户端时得到了预期的输出。
我很确定问题是浏览器发送到 django 的东西影响了翻译,但我不知道是什么。我比较了这两个版本,两个版本中对语言的唯一引用都是 fr。
有人对我可以尝试什么有什么建议吗?
我对调试器进行了更多研究,发现 _active 中的目录在使用浏览器时不包含我的翻译,但在使用测试客户端时包含我的翻译。什么可能导致这种行为?
I can't figure why the django translation (i18n) doesn't work. I use english in the code and I've written translations for french. Strangely, it works when I use django.test.client.Client but not when I use a web browser.
I've tried to set the Accept-Language to fr in my browser settings and I've also tried to delete the key HTTP_ACCEPT_LANGUAGE from the request header.
I've set the language to fr through the setlang view found in django.conf.urls.i18n.
When I do:
{% load i18n %}
{% get_current_language as LANGUAGE_CODE %}
I successfully get fr, even if the string that should be converted in my page is still in english.
I have this in my settings:
USE_I18N = True
#Probably ignored because of the django_language cookie
LANGUAGE_CODE = 'fr'
MIDDLEWARE_CLASSES = (
'django.middleware.cache.UpdateCacheMiddleware',
'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.transaction.TransactionMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
)
I've compiled my translation and they work since I get the expected output when using the test client.
I'm pretty sure the problem is something that the browser send to django that affects the translation, but I can't figure what. I've compared the two and the only references to a language are fr in both version.
Anyone have any suggestion on what I could try?
I did more reseach with the debugger and I found that the catalog in _active doesn't contain my translations when using the browser, but does when using the test client. What could cause that behavior?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,虽然 2016 年即将到来,但在测试客户端中添加 Accept-Language 标头仍然不起作用。但您可以手动激活语言。请参阅下面的代码:
更多详细信息您可以阅读 在这里。
希望这有帮助!
Unfortunatelly, while 2016 is coming soon, adding Accept-Language header inside test client still not work. But you can activate language manually. See a code below:
More details you can read here.
Hope this helps!