Can';Django中的t开关语言环境

Can';Django中的t开关语言环境,django,internationalization,Django,Internationalization,我有文件locale/es/LC_MESSAGES/django.mo(and.po),运行makemessages和compilemessages。肯定所有的信息都被翻译了 在settings.py中,您有: USE_I18N = True LANGUAGE_CODE = 'es' django仍然顽固地从english.po文件中获取字符串。。。为什么会这样 一定有什么问题。。。谢谢 编辑只有当LocaleMiddleware处于活动状态时才会出现这种情况。我在设置中使用此选项: TIME

我有文件locale/es/LC_MESSAGES/django.mo(and.po),运行makemessages和compilemessages。肯定所有的信息都被翻译了

在settings.py中,您有:

USE_I18N = True
LANGUAGE_CODE = 'es'
django仍然顽固地从english.po文件中获取字符串。。。为什么会这样

一定有什么问题。。。谢谢


编辑只有当LocaleMiddleware处于活动状态时才会出现这种情况。

我在设置中使用此选项:

TIME_ZONE = 'Europe/Paris'
LANGUAGE_CODE = 'fr-FR'
SITE_ID = 1
USE_I18N = True

因此,根据django文档,您应该使用类似“es”的东西

LocaleMiddleware尝试确定 用户的语言偏好由 遵循此算法:

* 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”,则可以禁用中间件。 如果确实需要LocalEMIDleware处于活动状态,请尝试以下方法覆盖客户端浏览器中的标题:


对于使用Python3.X的用户,
if-request.META.has_key('HTTP\u-ACCEPT\u-LANGUAGE'):
需要更改为
if-request.META中的'HTTP\u-ACCEPT\u-LANGUAGE'。
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']