jessemiller / HamlPy

A converter of HAML like templates into Django templates.
MIT License
444 stars 92 forks source link

How do we use the templatize function ? #96

Open altimore opened 12 years ago

altimore commented 12 years ago

Hello,

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 ./manage.py makemessages -a
bcoughlan commented 12 years ago

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!

culebron commented 12 years ago

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
fizista commented 11 years ago

There is a very simple solution:

django-admin.py makemessages --settings=[project.settings] -a -e haml,html

Where:

cordery commented 11 years ago

I've been digging into this quite a bit.

Background

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 https://code.djangoproject.com/ticket/20811

Getting makemessages to work properly

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

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 https://gist.github.com/cordery/6315117

altimore commented 11 years ago

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