Commit eb3b8c5c by Ned Batchelder

TNL-2269 Compute language direction every time.

The old code would compute the language direction once when the template
was loaded.

TNL-2269
parent 4eaa12d9
...@@ -2,16 +2,13 @@ ...@@ -2,16 +2,13 @@
<%namespace name='static' file='static_content.html'/> <%namespace name='static' file='static_content.html'/>
<%! <%!
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.translation import get_language_bidi
from django.template.defaultfilters import escapejs from django.template.defaultfilters import escapejs
import json import json
dir_rtl = 'rtl' if get_language_bidi() else 'ltr'
%> %>
<!doctype html> <!doctype html>
<!--[if lte IE 9]><html class="ie9 lte9" lang="${LANGUAGE_CODE}"><![endif]--> <!--[if lte IE 9]><html class="ie9 lte9" lang="${LANGUAGE_CODE}"><![endif]-->
<!--[if !IE]><<!--><html lang="${LANGUAGE_CODE}"><!--<![endif]--> <!--[if !IE]><<!--><html lang="${LANGUAGE_CODE}"><!--<![endif]-->
<head dir="${dir_rtl}"> <head dir="${static.dir_rtl()}">
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title> <title>
...@@ -38,7 +35,7 @@ dir_rtl = 'rtl' if get_language_bidi() else 'ltr' ...@@ -38,7 +35,7 @@ dir_rtl = 'rtl' if get_language_bidi() else 'ltr'
<%block name="header_extras"></%block> <%block name="header_extras"></%block>
</head> </head>
<body class="${dir_rtl} <%block name='bodyclass'></%block> lang_${LANGUAGE_CODE}"> <body class="${static.dir_rtl()} <%block name='bodyclass'></%block> lang_${LANGUAGE_CODE}">
<%block name="view_notes"></%block> <%block name="view_notes"></%block>
<a class="nav-skip" href="#content">${_("Skip to main content")}</a> <a class="nav-skip" href="#content">${_("Skip to main content")}</a>
...@@ -57,7 +54,7 @@ dir_rtl = 'rtl' if get_language_bidi() else 'ltr' ...@@ -57,7 +54,7 @@ dir_rtl = 'rtl' if get_language_bidi() else 'ltr'
</script> </script>
<!-- view --> <!-- view -->
<div class="wrapper wrapper-view" dir="${dir_rtl}"> <div class="wrapper wrapper-view" dir="${static.dir_rtl()}">
<% online_help_token = self.online_help_token() if hasattr(self, 'online_help_token') else None %> <% online_help_token = self.online_help_token() if hasattr(self, 'online_help_token') else None %>
<%include file="widgets/header.html" args="online_help_token=online_help_token" /> <%include file="widgets/header.html" args="online_help_token=online_help_token" />
......
...@@ -27,6 +27,7 @@ except: ...@@ -27,6 +27,7 @@ except:
% endfor % endfor
%endif %endif
</%def> </%def>
<%def name='js(group)'> <%def name='js(group)'>
% if settings.FEATURES['USE_DJANGO_PIPELINE']: % if settings.FEATURES['USE_DJANGO_PIPELINE']:
${compressed_js(group)} ${compressed_js(group)}
...@@ -37,6 +38,15 @@ except: ...@@ -37,6 +38,15 @@ except:
%endif %endif
</%def> </%def>
## A language-direction indicator, suitable for use in class="" attributes,
## for example:
##
## <body class="${dir_rtl()}">
##
<%def name="dir_rtl()"><%
return 'rtl' if get_language_bidi() else 'ltr'
%></%def>
<%def name="include(path)"><% <%def name="include(path)"><%
from django.template.loaders.filesystem import _loader from django.template.loaders.filesystem import _loader
source, template_path = _loader.load_template_source(path) source, template_path = _loader.load_template_source(path)
......
...@@ -9,19 +9,40 @@ from django.test.utils import override_settings ...@@ -9,19 +9,40 @@ from django.test.utils import override_settings
@attr('shard_1') @attr('shard_1')
@override_settings(LANGUAGES=(('eo', 'Esperanto'),)) @override_settings(LANGUAGES=[('eo', 'Esperanto'), ('ar', 'Arabic')])
class I18nTestCase(TestCase): class I18nTestCase(TestCase):
""" """
Tests for i18n Tests for i18n
""" """
def assert_tag_has_attr(self, content, tag, attname, value):
"""Assert that a tag in `content` has a certain value in a certain attribute."""
regex = r"""<{tag} [^>]*\b{attname}=['"]([\w\d ]+)['"][^>]*>""".format(tag=tag, attname=attname)
match = re.search(regex, content)
self.assertTrue(match, "Couldn't find desired tag in %r" % content)
attvalues = match.group(1).split()
self.assertIn(value, attvalues)
def test_default_is_en(self): def test_default_is_en(self):
response = self.client.get('/') response = self.client.get('/')
self.assertIn('<html lang="en">', response.content) self.assert_tag_has_attr(response.content, "html", "lang", "en")
self.assertEqual(response['Content-Language'], 'en') self.assertEqual(response['Content-Language'], 'en')
self.assertTrue(re.search('<body.*class=".*lang_en">', response.content)) self.assert_tag_has_attr(response.content, "body", "class", "lang_en")
def test_esperanto(self): def test_esperanto(self):
response = self.client.get('/', HTTP_ACCEPT_LANGUAGE='eo') response = self.client.get('/', HTTP_ACCEPT_LANGUAGE='eo')
self.assertIn('<html lang="eo">', response.content) self.assert_tag_has_attr(response.content, "html", "lang", "eo")
self.assertEqual(response['Content-Language'], 'eo') self.assertEqual(response['Content-Language'], 'eo')
self.assertTrue(re.search('<body.*class=".*lang_eo">', response.content)) self.assert_tag_has_attr(response.content, "body", "class", "lang_eo")
def test_switching_languages_bidi(self):
response = self.client.get('/')
self.assert_tag_has_attr(response.content, "html", "lang", "en")
self.assertEqual(response['Content-Language'], 'en')
self.assert_tag_has_attr(response.content, "body", "class", "lang_en")
self.assert_tag_has_attr(response.content, "body", "class", "ltr")
response = self.client.get('/', HTTP_ACCEPT_LANGUAGE='ar')
self.assert_tag_has_attr(response.content, "html", "lang", "ar")
self.assertEqual(response['Content-Language'], 'ar')
self.assert_tag_has_attr(response.content, "body", "class", "lang_ar")
self.assert_tag_has_attr(response.content, "body", "class", "rtl")
<%namespace name='static' file='/static_content.html'/>
<%! from django.utils.translation import ugettext as _ %> <%! from django.utils.translation import ugettext as _ %>
<%! import mako.runtime %> <%! import mako.runtime %>
<% mako.runtime.UNDEFINED = '' %> <% mako.runtime.UNDEFINED = '' %>
<% <%
# set doc language direction
from django.utils.translation import get_language_bidi
dir_rtl = 'rtl' if get_language_bidi() else 'ltr'
document_body_class = document_body_class_append if document_body_class_append else '' document_body_class = document_body_class_append if document_body_class_append else ''
%> %>
<!DOCTYPE html> <!DOCTYPE html>
<html class="no-js" lang="en"> <html class="no-js" lang="en">
<head dir=-"${dir_rtl}"> <head dir=-"${static.dir_rtl()}">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
...@@ -21,7 +19,7 @@ ...@@ -21,7 +19,7 @@
<%include file="_assets-primary.html" /> <%include file="_assets-primary.html" />
</head> </head>
<body class="view-certificate view-valid-certificate ${dir_rtl} ${document_body_class}" data-view="valid-certificate"> <body class="view-certificate view-valid-certificate ${static.dir_rtl()} ${document_body_class}" data-view="valid-certificate">
<div class="wrapper-view"> <div class="wrapper-view">
${self.body()} ${self.body()}
......
...@@ -4,17 +4,14 @@ ...@@ -4,17 +4,14 @@
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.http import urlquote_plus from django.utils.http import urlquote_plus
from django.utils.translation import ugettext as _ from django.utils.translation import ugettext as _
from django.utils.translation import get_language_bidi
from microsite_configuration import microsite from microsite_configuration import microsite
from microsite_configuration import page_title_breadcrumbs from microsite_configuration import page_title_breadcrumbs
from branding import api as branding_api from branding import api as branding_api
dir_rtl = 'rtl' if get_language_bidi() else 'ltr'
%> %>
<!DOCTYPE html> <!DOCTYPE html>
<!--[if lte IE 9]><html class="ie ie9 lte9" lang="${LANGUAGE_CODE}"><![endif]--> <!--[if lte IE 9]><html class="ie ie9 lte9" lang="${LANGUAGE_CODE}"><![endif]-->
<!--[if !IE]><!--><html lang="${LANGUAGE_CODE}"><!--<![endif]--> <!--[if !IE]><!--><html lang="${LANGUAGE_CODE}"><!--<![endif]-->
<head dir="${dir_rtl}"> <head dir="${static.dir_rtl()}">
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta http-equiv="X-UA-Compatible" content="IE=edge">
% if responsive: % if responsive:
...@@ -131,8 +128,8 @@ dir_rtl = 'rtl' if get_language_bidi() else 'ltr' ...@@ -131,8 +128,8 @@ dir_rtl = 'rtl' if get_language_bidi() else 'ltr'
</head> </head>
<body class="${dir_rtl} <%block name='bodyclass'/> lang_${LANGUAGE_CODE}"> <body class="${static.dir_rtl()} <%block name='bodyclass'/> lang_${LANGUAGE_CODE}">
<div class="window-wrap" dir="${dir_rtl}"> <div class="window-wrap" dir="${static.dir_rtl()}">
<a class="nav-skip" href="<%block name="nav_skip">#content</%block>">${_("Skip to main content")}</a> <a class="nav-skip" href="<%block name="nav_skip">#content</%block>">${_("Skip to main content")}</a>
% if not disable_header: % if not disable_header:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment