gpt4 book ai didi

django - 无法在Django中切换语言环境

转载 作者:行者123 更新时间:2023-12-04 04:01:30 26 4
gpt4 key购买 nike

我有语言环境/es/LC_MESSAGES/django.mo(和.po),运行makemessages和compilemessages。绝对所有消息均已翻译

在settings.py中有:

USE_I18N = True
LANGUAGE_CODE = 'es'

django仍然顽固地从英语.po文件中获取字符串...为什么会这样呢?

必须有一些陷阱...谢谢。

编辑仅当LocaleMiddleware处于事件状态时,才出现这种情况。

最佳答案

根据Django文档

http://docs.djangoproject.com/en/dev/topics/i18n/#id2

LocaleMiddleware tries to determine the user's language preference by following this algorithm:

* First, it looks for a django_language key in the current user's session.

* Failing that, it looks for a cookie.

[...]

*Failing that, it looks at the Accept-Language HTTP header. This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django > tries each language in the header  until it finds one with available translations.

* Failing that, it uses the global LANGUAGE_CODE setting.


如果只需要一种语言“es”,则可以禁用中间件。
如果您确实需要激活LocaleMiddleware,请尝试以下食谱来覆盖客户端浏览器 http://www.djangosnippets.org/snippets/218/的 header :
enter code here

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']

关于django - 无法在Django中切换语言环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1230315/

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