无法在Django中切换语言环境

时间:2022-08-26 23:18:41

I have file locale/es/LC_MESSAGES/django.mo (and .po), ran makemessages and compilemessages. Definitely all messages are translated

我有文件区域设置/ es / LC_MESSAGES / django.mo(和.po),运行makemessages和compilemessages。绝对是所有消息都被翻译

in settings.py have:

在settings.py中有:

USE_I18N = True
LANGUAGE_CODE = 'es'

Still django stubbornly takes strings from the english .po file... Why can that be?

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

There must be some gotcha... Thanks.

必须有一些问题...谢谢。

EDIT this appears to be the case only when LocaleMiddleware is active.

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

2 个解决方案

#1


9  

According to the django docs

根据django文档

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

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

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

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.

If you only need one language, 'es', you can disable the middleware. If you really need LocaleMiddleware active, try this recipe to override the headers from the client's browser http://www.djangosnippets.org/snippets/218/:

如果您只需要一种语言'es',则可以禁用中间件。如果您确实需要LocaleMiddleware处于活动状态,请尝试使用此配方覆盖客户端浏览器http://www.djangosnippets.org/snippets/218/中的标题:

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

#2


0  

I use this in my settings :

我在我的设置中使用它:

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

So you should use something like 'es-ES'

所以你应该使用'es-ES'之类的东西

#1


9  

According to the django docs

根据django文档

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

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

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

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.

If you only need one language, 'es', you can disable the middleware. If you really need LocaleMiddleware active, try this recipe to override the headers from the client's browser http://www.djangosnippets.org/snippets/218/:

如果您只需要一种语言'es',则可以禁用中间件。如果您确实需要LocaleMiddleware处于活动状态,请尝试使用此配方覆盖客户端浏览器http://www.djangosnippets.org/snippets/218/中的标题:

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

#2


0  

I use this in my settings :

我在我的设置中使用它:

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

So you should use something like 'es-ES'

所以你应该使用'es-ES'之类的东西