Skip to content

Commit

Permalink
Merge pull request #3880 from jpetto/bug-1222137-locale-based-templates
Browse files Browse the repository at this point in the history
[fix bug 1222137] Enable bedrock locale-based templates.
  • Loading branch information
pmclanahan committed Feb 19, 2016
2 parents 4805b16 + 62f0107 commit 93dd85e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
15 changes: 12 additions & 3 deletions lib/l10n_utils/__init__.py
Expand Up @@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import re
from os.path import splitext

from django.conf import settings
from django.http import HttpResponseRedirect
Expand Down Expand Up @@ -91,13 +92,21 @@ def render(request, template, context=None, **kwargs):

return response

localized_tmpl = '%s/templates/%s' % (request.locale, template)
# Render try #1: Look for l10n template in locale/{{ LANG }}/templates/
l10n_tmpl = '%s/templates/%s' % (request.locale, template)
try:
return django_render(request, localized_tmpl, context, **kwargs)
return django_render(request, l10n_tmpl, context, **kwargs)
except TemplateDoesNotExist:
# If not found, just go on and try rendering the parent template.
pass

# Render try #2: Look for locale-specific template in app/templates/
locale_tmpl = '.{}'.format(request.locale).join(splitext(template))
try:
return django_render(request, locale_tmpl, context, **kwargs)
except TemplateDoesNotExist:
pass

# Render try #3: Render originally requested/default template
return django_render(request, template, context, **kwargs)


Expand Down
53 changes: 52 additions & 1 deletion lib/l10n_utils/tests/test_template.py
Expand Up @@ -4,7 +4,8 @@

import os

from django.test import override_settings
from django.template import TemplateDoesNotExist
from django.test import RequestFactory, override_settings

from jingo import env
from jinja2 import FileSystemLoader
Expand Down Expand Up @@ -125,3 +126,53 @@ def test_render_no_locale(self, django_render, get_lang_path):
# Note: no .locale on request
# Should not cause an exception
render(request, '500.html')


@patch('lib.l10n_utils.template_is_active', Mock(return_value=True))
@patch('lib.l10n_utils.django_render')
class TestLocaleTemplates(TestCase):
def setUp(self):
self.rf = RequestFactory()

def test_enUS_render(self, django_render):
"""
en-US requests do not look for localized templates and should render the
originally requested template.
"""
request = self.rf.get('/')
request.locale = 'en-US'
render(request, 'firefox/new.html')
django_render.assert_called_with(request, 'firefox/new.html', ANY)

def test_default_render(self, django_render):
"""
Non en-US requests without l10n or locale template should render the
originally requested template.
"""
django_render.side_effect = [TemplateDoesNotExist, TemplateDoesNotExist,
True]
request = self.rf.get('/')
request.locale = 'de'
render(request, 'firefox/new.html')
django_render.assert_called_with(request, 'firefox/new.html', ANY)

def test_bedrock_locale_render(self, django_render):
"""
Non en-US requests with a locale-specific template should render the
locale-specific template.
"""
django_render.side_effect = [TemplateDoesNotExist, True]
request = self.rf.get('/')
request.locale = 'es-ES'
render(request, 'firefox/new.html')
django_render.assert_called_with(request, 'firefox/new.es-ES.html', ANY)

def test_l10n_render(self, django_render):
"""
Non en-US requests with an l10n template should render the l10n
template.
"""
request = self.rf.get('/')
request.locale = 'es-ES'
render(request, 'firefox/new.html')
django_render.assert_called_with(request, 'es-ES/templates/firefox/new.html', ANY)

0 comments on commit 93dd85e

Please sign in to comment.