Django i18n 不工作
我无法让翻译显示在我的模板中。这是我所做的:
- 在我的模板文件 (index.html) 中,我加载了 i18n 并在需要翻译的地方使用了
trans
标记。 - 在我的项目目录中,我运行
./manage.py makemessages -l es
- 我的 django.po 文件位于
./conf/locale/es/LC_MESSAGES/django.po
- 我用翻译编辑了这个文件。
- 然后我运行
./manage.pycompilemessages
- 我在 settings.py 中将我的
LANGUAGE_CODE
设置为“es” - 添加了
django.middleware.locale.LocaleMiddleware
到 SessionMiddleware 和 CommonMiddleware 之间的MIDDLEWARE_CLASSES
。 - 重新启动 Django,刷新我的页面。
只有英文表示仍在显示。我在这里做错了什么?我错过了什么吗?
I cannot get translations to show up for my templates. Here is what I have done:
- In my template file (index.html), I have loaded i18n and used the
trans
tag where I want translations. - In my project directory, I rand
./manage.py makemessages -l es
- My django.po file is in
./conf/locale/es/LC_MESSAGES/django.po
- I edited this file with the translations.
- I then ran
./manage.py compilemessages
- I set my
LANGUAGE_CODE
to 'es' in settings.py - Added
django.middleware.locale.LocaleMiddleware
toMIDDLEWARE_CLASSES
in between SessionMiddleware and CommonMiddleware. - Restarted Django, refreshed my page.
Only the english representations are still showing. What am I doing wrong here? Did I miss something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过在设置文件中设置
LOCALE_PATHS
?这为我解决了这个问题...有关更多信息,请查看我为类似问题编写的答案或django的官方文档LOCALE_PATHS
设置。Have you tried setting
LOCALE_PATHS
in your settings file? That fixed it for me...for more info check out an answer I wrote for a similar question or django's official documentation for theLOCALE_PATHS
setting.Django 的
LocaleMiddleware
尝试根据给定请求确定区域设置,并相应地替换LANGUAGE_CODE
。它执行以下步骤:HTTP_ACCEPT_LANGUAGE
标头确定语言。如果不可能,它将转到 4。LANGUAGE_CODE
设置作为给定的语言环境。在除 4 之外的所有步骤中,它将检查您的
LANGUAGES
设置中是否给出了语言,该设置是表示语言代码和语言标题的 2 元组的元组。确保设置/覆盖默认值,因为默认值将是所有可用 Django 语言的元组。如果您的 Django 站点有多种语言:
请确保您的会话或 cookie 设置正确。您可以使用 Django 的内部视图来设置语言 (
django.views.i18n.set_language
)。如果您只有一种语言:
如果您只有西班牙语,您通常不需要为此烦恼。只需确保设置了
LANGUAGES
和LANGUAGE_CODE
即可。如果LANGUAGES
中未提供,它不会尝试将页面语言设置为您的浏览器语言。此外,您应该确保USE_I18N = True
。不过,这应该是默认值,因此至少请确保您没有在某处将其设置为 False。Django's
LocaleMiddleware
tries to determine the locale from the given request and replaces theLANGUAGE_CODE
accordingly. It goes through the following steps:django_language
. Set the language to the given locale in this session. If not existing it goes to 2.HTTP_ACCEPT_LANGUAGE
header. If not possible it will go to 4.LANGUAGE_CODE
setting as given locale.In all steps except 4 it will check if the language is given in your
LANGUAGES
setting, which is a tuple of 2-tuples representing language code and language title. Make sure you set/overwrite the default, since the default would be a tuple of all available Django languages.If you have multiple languages in your Django site:
Make sure your session or cookies are set correctly. You could use Django's internal view for setting the language (
django.views.i18n.set_language
).If you got only one language:
If you only have Spanish you usually don't need to bother with this. Just make sure
LANGUAGES
andLANGUAGE_CODE
is set. It will not attempt to set the pages language to your browsers language if it is not provided inLANGUAGES
. Additionally you should make sure thatUSE_I18N = True
. That should be the default though, so make at least sure you didn't set it toFalse
somewhere.好的,我发现了问题...我的 po 文件位于
./conf/locale
中,而不仅仅是./locale
中。我创建 ./conf/locale 的原因是当我运行 django-admin.py makemessages 时,它指示我这样做。但是,如果它位于您的本地项目目录中,那么它只是简单的
./locale
。Ok, I found the problem... My po files were in
./conf/locale
instead of just./locale
.The reason I created
./conf/locale
was because when I randdjango-admin.py makemessages
, it instructed me to do so. However, if it's in your local project directory, then it is just simply./locale
.