Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
Make hamlpy module a Django app which can be loaded to ensure that te…
Browse files Browse the repository at this point in the history
…mplatize is patched
  • Loading branch information
rowanseymour committed Mar 17, 2017
1 parent 994eae4 commit 87ffdcf
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,17 @@ optional arguments:

### Create message files for translation

Just include your Haml templates along with all the other files which contain translatable strings, e.g.
HamlPy must first be included in Django's list of apps, i.e.

```python
INSTALLED_APPS = [
...
'hamlpy'
...
]
```

Then just include your Haml templates along with all the other files which contain translatable strings, e.g.

```bash
python manage.py makemessages --extension haml,html,py,txt
Expand Down
14 changes: 13 additions & 1 deletion hamlpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
from __future__ import unicode_literals

__version__ = '1.0.1'
from django.apps import AppConfig

__version__ = '1.0.1'

HAML_EXTENSIONS = ('haml', 'hamlpy')


class Config(AppConfig):
name = 'hamlpy'

def ready(self):
# patch Django's templatize method
from .template import templatize # noqa


default_app_config = 'hamlpy.Config'
4 changes: 1 addition & 3 deletions hamlpy/template/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from __future__ import unicode_literals

# load templatize module to patch Django's templatize function
from . import templatize # noqa

from .loaders import haml_loaders as _loaders


locals().update(_loaders)
6 changes: 3 additions & 3 deletions hamlpy/template/templatize.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from hamlpy.compiler import Compiler


def decorate_templatize(func):
def patch_templatize(func):
def templatize(src, origin=None):
# if the template has no origin then don't attempt to convert it because we don't know if it's Haml
if origin:
extension = os.path.splitext(origin.name)[1][1:].lower()
extension = os.path.splitext(origin)[1][1:].lower()

if extension in HAML_EXTENSIONS:
compiler = Compiler()
Expand All @@ -27,4 +27,4 @@ def templatize(src, origin=None):
return templatize


translation.templatize = decorate_templatize(translation.templatize)
translation.templatize = patch_templatize(translation.templatize)
11 changes: 7 additions & 4 deletions hamlpy/test/test_templatize.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@

import unittest

from django.template.base import Origin
from hamlpy import Config


class TemplatizeTest(unittest.TestCase):

def test_templatize(self):
# patching of Django's templatize happens when our app is loaded
Config.ready(None)

from django.utils.translation import templatize

# test regular Django tags
output = templatize('{% trans "Foo" %}{% blocktrans %}\nBar\n{% endblocktrans %}', origin=Origin('test.html'))
output = templatize('{% trans "Foo" %}{% blocktrans %}\nBar\n{% endblocktrans %}', origin='test.html')
self.assertRegexpMatches(output, r"gettext\(u?'Foo'\)")
self.assertRegexpMatches(output, r"gettext\(u?'\\nBar\\n'\)")

# test Haml tags with HTML origin
output = templatize('- trans "Foo"\n- blocktrans\n Bar\n', origin=Origin('test.haml'))
output = templatize('- trans "Foo"\n- blocktrans\n Bar\n', origin='test.haml')
self.assertRegexpMatches(output, r"gettext\(u?'Foo'\)")
self.assertRegexpMatches(output, r"gettext\(u?'\\n Bar\\n'\)")

# test Haml tags and HTML origin
self.assertNotIn('gettext', templatize('- trans "Foo"\n- blocktrans\n Bar\n', origin=Origin('test.html')))
self.assertNotIn('gettext', templatize('- trans "Foo"\n- blocktrans\n Bar\n', origin='test.html'))

# test Haml tags and no origin
self.assertNotIn('gettext', templatize('- trans "Foo"\n- blocktrans\n Bar\n'))

0 comments on commit 87ffdcf

Please sign in to comment.