我有文件 locale/es/LC_MESSAGES/django.mo (和 .po),运行 makemessages 和compilemessages。绝对所有消息都已翻译
在settings.py中有:
USE_I18N = True
LANGUAGE_CODE = 'es'
django 仍然顽固地从英语 .po 文件中获取字符串......为什么会这样呢?
一定有一些问题......谢谢。
EDIT仅当 LocaleMiddleware 处于活动状态时才会出现这种情况。
根据 django 文档
http://docs.djangoproject.com/en/dev/topics/i18n/#id2
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”,则可以禁用中间件。
如果您确实需要激活 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']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)