问题
I am trying to run the Django language localization on a project, but makemessages always ignores the html templates in my templates folder.
I'm 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? Thanks in advance for your help!
EDIT: Solution comprised solely of changing the syntax of {{ trans "string" }}
to {% trans "string" %}
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/7054082/why-would-the-makemessages-function-for-django-language-localization-ignore-html