Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do we use the templatize function ? #96

Open
altimore opened this issue Aug 31, 2012 · 5 comments
Open

How do we use the templatize function ? #96

altimore opened this issue Aug 31, 2012 · 5 comments

Comments

@altimore
Copy link

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
Copy link
Collaborator

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
Copy link
Contributor

culebron commented Sep 6, 2012

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
Copy link
Contributor

fizista commented Jan 17, 2013

There is a very simple solution:

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

Where:

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

@cordery
Copy link
Contributor

cordery commented Aug 23, 2013

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

jessemiller added a commit that referenced this issue Aug 28, 2013
Fix to templatize decorator so it only hamlpy parses files with valid haml extensions see Issue #96
@altimore
Copy link
Author

altimore commented Oct 4, 2013

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants