diff --git a/examples/jingo-project/locale/xx/LC_MESSAGES/messages.mo b/examples/jingo-project/locale/xx/LC_MESSAGES/messages.mo new file mode 100644 index 0000000..e8afa18 Binary files /dev/null and b/examples/jingo-project/locale/xx/LC_MESSAGES/messages.mo differ diff --git a/examples/jingo-project/locale/xx/LC_MESSAGES/messages.po b/examples/jingo-project/locale/xx/LC_MESSAGES/messages.po new file mode 100644 index 0000000..447b2af --- /dev/null +++ b/examples/jingo-project/locale/xx/LC_MESSAGES/messages.po @@ -0,0 +1,4 @@ +#, python-format +msgid "Broken %(string)s" +msgstr "Broken %(fuuu)s" + diff --git a/examples/jingo-project/settings.py b/examples/jingo-project/settings.py index 38aff49..21ecb55 100644 --- a/examples/jingo-project/settings.py +++ b/examples/jingo-project/settings.py @@ -1 +1,7 @@ +import os + +# Make filepaths relative to settings. +ROOT = os.path.dirname(os.path.abspath(__file__)) +path = lambda *a: os.path.join(ROOT, *a) + JINJA_CONFIG = {} diff --git a/fabfile.py b/fabfile.py index 58c26dc..2b45aa4 100644 --- a/fabfile.py +++ b/fabfile.py @@ -26,11 +26,12 @@ def doc(kind='html'): with cd('docs'): local('make clean %s' % kind) +def shell(): + local('django-admin.py shell') def test(): local('nosetests') - def updoc(): doc('dirhtml') rsync_project('p/%s' % NAME, 'docs/_build/dirhtml/', delete=True) diff --git a/jingo/__init__.py b/jingo/__init__.py index 2a2b001..80d5cae 100644 --- a/jingo/__init__.py +++ b/jingo/__init__.py @@ -4,8 +4,10 @@ import functools from django import http from django.conf import settings from django.template.context import get_standard_processors +from django.utils.translation import trans_real import jinja2 +import tower VERSION = (0, 3) __version__ = '.'.join(map(str, VERSION)) @@ -74,7 +76,14 @@ def render_to_string(request, template, context=None): # If it's not a Template, it must be a path to be loaded. if not isinstance(template, jinja2.environment.Template): template = env.get_template(template) - return template.render(**context) + try: + ret = template.render(**context) + except KeyError: + _lang = trans_real.get_language() + tower.deactivate_all() + ret = template.render(**context) + tower.activate(_lang) + return ret def load_helpers(): diff --git a/jingo/tests/test_views.py b/jingo/tests/test_views.py index 1787b5e..8f6aabc 100644 --- a/jingo/tests/test_views.py +++ b/jingo/tests/test_views.py @@ -1,6 +1,11 @@ -from mock import patch, sentinel +from mock import Mock, patch, sentinel +from nose.tools import eq_ +from test_helpers import render import jingo.views +from jingo import get_env +import jinja2 +import tower @patch('jingo.render') @@ -8,3 +13,15 @@ def test_direct_to_template(mock_render): request = sentinel.request jingo.views.direct_to_template(request, 'base.html', x=1) mock_render.assert_called_with(request, 'base.html', {'x': 1}) + + +def test_template_substitution_crash(): + tower.activate('xx') + + env = get_env() + + # The localized string has the wrong variable name in it + s = '{% trans string="heart" %}Broken {{ string }}{% endtrans %}' + template = env.from_string(s) + rendered = jingo.render_to_string(Mock(), template, {}) + eq_(rendered, 'Broken heart') diff --git a/requirements.txt b/requirements.txt index e63c0a1..cf34f4e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,5 @@ jinja2 nose mock -e svn+http://code.djangoproject.com/svn/django/trunk@12335#egg=Django +-e git://github.com/clouserw/tower.git#egg=tower fabric