为什么 Django 语言本地化的 makemessages 函数会忽略 html 文件?

发布于 2024-11-30 00:34:48 字数 504 浏览 1 评论 0原文

我正在尝试在项目上运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

路弥 2024-12-07 00:34:48

您的 templates 文件夹需要位于 INSTALLED_APPS 中列出的应用中,或者位于 TEMPLATE_DIRS 中列出的目录中 - 在您的settings.py 文件

Your templates folder either needs to be in an app that has been listed in INSTALLED_APPS or in a directory that has been listed in TEMPLATE_DIRS - in your settings.py file

北恋 2024-12-07 00:34:48

尝试为 app 文件夹中的 templates 文件夹创建符号链接。然后使用符号链接开关从应用程序文件夹中调用 makemessages django-admin.py makemessages --all --symlinks

cd /myproject
ln -s /myproject/templates /myproject/myapp/templates    
cd /myproject/myapp
django-admin.py makemessages --all --symlinks

makemessages 忽略 TEMPLATE_DIRS已安装_APPS。 Templates dir 需要位于您的应用程序文件夹内,并且需要从您的应用程序文件夹内调用 makemessages

Try creating symlink for your templates folder in your app folder. Then call makemessages from your app folder with symlink switch django-admin.py makemessages --all --symlinks

cd /myproject
ln -s /myproject/templates /myproject/myapp/templates    
cd /myproject/myapp
django-admin.py makemessages --all --symlinks

makemessages ignores TEMPLATE_DIRS and INSTALLED_APPS. Templates dir needs to be inside your app folder and makemessages needs to be called from inside your app folder.

心意如水 2024-12-07 00:34:48

{{ 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文