Why would the makemessages function for Django language localization ignore html files?
Asked Answered
H

3

7

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?

Hardset answered 14/8, 2011 at 0:4 Comment(0)
B
4

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

Boyhood answered 14/8, 2011 at 9:32 Comment(8)
Both of those things are already configured, and the app works successfully with templates, except for the makemessages functionality on templates.Hardset
Say your django app is called app, there is a directory in that called templates? This .py file you created, where was it? Path? How did you refer to it in the code? Can you also paste the INSTALLED_APPS and TEMPLATE_DIRS settings here?Boyhood
Inside of myproject/app, there is indeed a templates directory, and templates are successfully found from inside of it by any views that need templates. Makemessages successfully finds strings to translate inside the normal views.py, like ugettext("String_to_translate"). I also created example.py inside of the templates folder to check if makemessages ever looked inside of that folder for anything, and it found ugettext("Another string") inside of that. TEMPLATE_DIRS =('/home/dlmccoy/webapps/projectname/myproject/app/templates',), INSTALLED_APPS = ( 'myproject.app', )Hardset
Wow, that's weird. Hard to say what the problem might be. You need to include {% load i18n %} in every template file, not just your "base" file that you are extending in your other template files. Are you doing that? Can you also remove the entry in TEMPLATE_DIRS (since the directory will be picked up if it is inside your app directory)?Boyhood
For the sake of testing, I set up a template without any importing of base files, and still no luck. I do include {% load i18n %} every time. I also removed the entry for TEMPLATE_DIRs with no change. In case this is also useful information, I tried manually editing the django.po file with translations, and the template still can't find them with {{ trans "String_to_translate" }}. Any other ideas?Hardset
Nope, sorry, no other ideas. Maybe you should put up a test case on bitbucket/github. Given the available information, I can't think of anything else.Boyhood
In one of the dumbest syntax errors in web programming, the solution was to write {% trans "String_to_translate %} rather than {{ trans "String_to_translate" }}. Thanks for putting up with me, rm.Hardset
Hmm, should have noticed that. The weirdest bugs usually end up being caused by something like this.Boyhood
P
4

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.

Prenotion answered 29/11, 2013 at 10:9 Comment(0)
P
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.

Pasadena answered 22/12, 2022 at 20:51 Comment(1)
That sounds like a typo to me...Roer

© 2022 - 2024 — McMap. All rights reserved.