How do we use the templatize function ? #96

altimore opened this Issue Aug 31, 2012 · 5 comments

5 participants



Great app you made here, i'm using it in most of my projects now.

I saw there is a templatize function in the project, it seems to link the haml processing to the translations utility.

After looking in the issues already here I found the #40 but i didn't understand how to use it.

My solution is for now is simple, but integrating yours would be a big enhancement.

grep -R trans templates --include="*\.haml" -h > templates/translateme.haml
hamlpy templates/translateme.haml templates/translateme.html
python ./ makemessages -a

I'm not familiar with the templatize function and I have been wondering what it's all about when looking through the code. Just going to tag @culebron on this to see if he can fill us in!


I've dug deeper in the code, and there is a separate class, whose name is hard-coded in the template engine, that converts templates into somithng python-like, where everything except {% trans '' %} becomes letter "X".

As I remember, you need to inherit the whole chain of classes from loader to template and parser, to replace a single function in Parser.

I also ended making a shell script:

find project -name '*.haml_trans' -delete
find project -name '*.haml' -exec bin/hamlpy {} {}_trans \;
./bin/django makemessages -a -e 'py,html,haml_trans' -i 'django-payokay'
find project -name '*.haml_trans' -delete

There is a very simple solution: makemessages --settings=[project.settings] -a -e haml,html


  • project.settings - Django configuration file where module "hamlpy" is configured properly.

I've been digging into this quite a bit.


Basically, when makemessages is run, django uses a function called templatize (django.utils.translation.trans_real.templatize) to turn the django template code into something xgettext can read.

Hamlpy monkeypatches that function in hamlpy.templatize to ensure that hamlpy reads the source and converts it first.

def decorate_templatize(func):
    def templatize(src, origin=None):
        hamlParser = hamlpy.Compiler()
        html = hamlParser.process(src.decode('utf-8'))
        return func(html.encode('utf-8'), origin)

    return templatize

If you do not trigger this monkeypatching, makemessages will not read your haml templates properly, particularly it will not parse blocktrans contents. I think a better fix would be if django used the template loaders when calling makemessages instead of just using open(). I've submitted an issue about this

Getting makemessages to work properly

To get makemessages to work (mostly, see next section) properly simply add the following line to your to your

from hamlpy import templatize

Alternately, you could write a wrapper for makemessages. You could also just add import hamlpy as hamlpy.init calls import templatize on line 1, but I like to keep things explicit when monkeypatching.

Getting makemessages to work with multiple template formats (haml, html, txt, etc)

You will notice that the monkeypatch does not try to first determine whether the file is haml or not. This means that non haml files may choke with errors like

SyntaxError: Translation blocks must not include other block tags:  (file templates/example.txt, line 2)

This is caused by templates like the following, where parts are being misread as haml. In the following example the hyphens are misinterpreted as a haml command even though this is a .txt not a .haml

{% blocktrans %}
-- Hello --
{% endblocktrans %}

To avoid this you will need to modify the monkeypatch by doing something like this gist


Thanks @cordery the import statement in works like a charm.
If possible, I would suggest to add it to the in the translation section so the new users don't have to make extensive search to get this right from the start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment