Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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" %}

like image 244
dlmccoy Avatar asked Aug 14 '11 00:08

dlmccoy


People also ask

How does Django translation work?

Django then provides utilities to extract the translation strings into a message file. This file is a convenient way for translators to provide the equivalent of the translation strings in the target language. Once the translators have filled in the message file, it must be compiled.

How does Django discover language preferences?

This header is sent by your browser and tells the server which language(s) you prefer, in order by priority. Django tries each language in the header until it finds one with available translations. Failing that, it uses the global LANGUAGE_CODE setting.

Which internationalization function marks a string as a translation string without actually translating it at that moment?

utils. translation. ugettext_lazy() to translate strings lazily – when the value is accessed rather than when the ugettext_lazy() function is called. In this example, ugettext_lazy() stores a lazy reference to the string – not the actual translation.


1 Answers

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.

like image 177
pista329 Avatar answered Jan 02 '23 07:01

pista329