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

Commit

Permalink
Merge pull request #124 from msaqib52/i18n-support
Browse files Browse the repository at this point in the history
add I18n support to js templates
  • Loading branch information
sarina committed Dec 2, 2020
2 parents 49ca683 + 654abe4 commit 486f5d8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -66,7 +66,7 @@ def is_requirement(line):


README = open(os.path.join(os.path.dirname(__file__), 'README.rst')).read()
VERSION = '2.1.1'
VERSION = '2.1.2'

if sys.argv[-1] == 'tag':
print("Tagging the version on github:")
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_resources.py
Expand Up @@ -105,6 +105,23 @@
</script>\
""".format(expected_filled_template)

expected_filled_translated_js_template = u"""\
<script type='text/template' id='example-unique-id'>
{}
</script>\
""".format(expected_translated_template)

expected_filled_not_translated_js_template = u"""\
<script type='text/template' id='example-unique-id'>
{}
</script>\
""".format(expected_not_translated_template)

expected_filled_localized_js_template = u"""\
<script type='text/template' id='example-unique-id'>
{}
</script>\
""".format(expected_localized_template)

another_template = u"""\
<explanation>This is an even simpler xml template.</explanation>
Expand Down Expand Up @@ -199,6 +216,27 @@ def test_render_js_template(self):
s = loader.render_js_template("data/simple_django_template.txt", example_id, example_context)
self.assertEqual(s, expected_filled_js_template)

def test_render_js_template_translated(self):
loader = ResourceLoader(__name__)
s = loader.render_js_template("data/trans_django_template.txt",
example_id,
context=example_context,
i18n_service=MockI18nService())
self.assertEqual(s, expected_filled_translated_js_template)

# Test that the language changes were reverted
s = loader.render_js_template("data/trans_django_template.txt", example_id, example_context)
self.assertEqual(s, expected_filled_not_translated_js_template)

def test_render_js_template_localized(self):
# Test that default template tags like l10n are loaded
loader = ResourceLoader(__name__)
s = loader.render_js_template("data/l10n_django_template.txt",
example_id,
context=example_context,
i18n_service=MockI18nService())
self.assertEqual(s, expected_filled_localized_js_template)

def test_load_scenarios(self):
loader = ResourceLoader(__name__)
scenarios = loader.load_scenarios_from_path("data")
Expand Down
4 changes: 2 additions & 2 deletions xblockutils/resources.py
Expand Up @@ -85,14 +85,14 @@ def render_template(self, template_path, context=None):
)
return self.render_django_template(template_path, context)

def render_js_template(self, template_path, element_id, context=None):
def render_js_template(self, template_path, element_id, context=None, i18n_service=None):
"""
Render a js template.
"""
context = context or {}
return u"<script type='text/template' id='{}'>\n{}\n</script>".format(
element_id,
self.render_django_template(template_path, context)
self.render_django_template(template_path, context, i18n_service)
)

def load_scenarios_from_path(self, relative_scenario_dir, include_identifier=False):
Expand Down

0 comments on commit 486f5d8

Please sign in to comment.