我有一个文件locale/es/LC_MESSAGES/django.mo(和.po),运行了makemessages和compilemessages。所有消息都已经翻译完成。
在settings.py中有:
USE_I18N = True
LANGUAGE_CODE = 'es'
为什么Django执意从英文.po文件中读取字符串?这其中一定有一些需要注意的地方...谢谢。
编辑 当LocaleMiddleware处于活动状态时,才会出现这种情况。
我有一个文件locale/es/LC_MESSAGES/django.mo(和.po),运行了makemessages和compilemessages。所有消息都已经翻译完成。
在settings.py中有:
USE_I18N = True
LANGUAGE_CODE = 'es'
为什么Django执意从英文.po文件中读取字符串?这其中一定有一些需要注意的地方...谢谢。
编辑 当LocaleMiddleware处于活动状态时,才会出现这种情况。
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.
[...]
如果只需要一种语言' es',你可以禁用中间件。如果你确实需要启用LocaleMiddleware,请尝试使用此方法覆盖来自客户端浏览器的标头:http://www.djangosnippets.org/snippets/218/:
*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.
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']
我在我的设置中使用这个:
TIME_ZONE = 'Europe/Paris'
LANGUAGE_CODE = 'fr-FR'
SITE_ID = 1
USE_I18N = True
所以你应该使用类似于“es-ES”的东西
if request.META.has_key('HTTP_ACCEPT_LANGUAGE'):
需要改为if 'HTTP_ACCEPT_LANGUAGE' in request.META:
。 - reinaldoluckman