为什么 Django 语言本地化的 makemessages 函数会忽略 html 文件?
我正在尝试在项目上运行 Django 语言本地化,但 makemessages 总是忽略模板文件夹中的 html 模板。
我正在从项目根运行 python manage.py makemessages -a
,并且项目中任何位置的 .py
文件中标记为翻译的所有字符串均已成功添加到 .po
文件。
html 模板中的任何字符串,即 {{ trans "String_to_translate" }}
都会被忽略,并且不会添加到 .po 文件中,即使必要的模块已加载到模板顶部, {% load i18n %}
。
为了测试整个模板文件夹被排除在 makemessages 函数之外的可能性,我创建了一个 .py
文件并在其中包含一个用于翻译的字符串,并且它已成功添加到 .po< /代码> 文件。
话虽如此,有谁知道可能导致此问题的原因是什么?
I am trying to run the Django language localization on a project, but makemessages always ignores the html templates in my templates folder.
I am running python manage.py makemessages -a
from the project root, and all of the strings that are marked for translation inside .py
files anywhere in the project are successfully added to the .po
file.
Any of the strings in the html templates, i.e., {{ trans "String_to_translate" }}
are ignored and not added to the .po file even though the necessary module is loaded at the top of the template, {% load i18n %}
.
To test the possibility that the whole template folder was excluded from the makemessages function, I made a .py
file and included a string for translation there, and it was successfully added to the .po
file.
With all of that being said, does anyone know what could possibly be causing this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
templates
文件夹需要位于INSTALLED_APPS
中列出的应用中,或者位于TEMPLATE_DIRS
中列出的目录中 - 在您的settings.py
文件Your
templates
folder either needs to be in an app that has been listed inINSTALLED_APPS
or in a directory that has been listed inTEMPLATE_DIRS
- in yoursettings.py
file尝试为
app
文件夹中的templates
文件夹创建符号链接。然后使用符号链接开关从应用程序文件夹中调用 makemessagesdjango-admin.py makemessages --all --symlinks
makemessages
忽略TEMPLATE_DIRS
和已安装_APPS
。 Templates dir 需要位于您的应用程序文件夹内,并且需要从您的应用程序文件夹内调用makemessages
。Try creating symlink for your
templates
folder in yourapp
folder. Then call makemessages from your app folder with symlink switchdjango-admin.py makemessages --all --symlinks
makemessages
ignoresTEMPLATE_DIRS
andINSTALLED_APPS
. Templates dir needs to be inside your app folder andmakemessages
needs to be called from inside your app folder.将
{{ trans "string" }}
语法更改为{% trans "string" %}
。此答案是作为问题编辑发布的questions/7054082/why-would-the-makemessages-function-for-django-language-localization-ignore-html">为什么 Django 语言本地化的 makemessages 函数会忽略 html 文件?由 OP dlmccoy 根据 CC BY-SA 3.0 编写。
Change the syntax of
{{ trans "string" }}
to{% trans "string" %}
.This answer was posted as an edit to the question Why would the makemessages function for Django language localization ignore html files? by the OP dlmccoy under CC BY-SA 3.0.