Skip to content

Commit

Permalink
MCKIN-19840 EOC (End of course Journal) - None of the strings are tra…
Browse files Browse the repository at this point in the history
…nslated in any language (#30)

Add support for translating JS strings.
xitij2000 authored Aug 4, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 9314fd6 + 757ff22 commit 40d36f3
Showing 5 changed files with 47 additions and 5 deletions.
25 changes: 23 additions & 2 deletions eoc_journal/eoc_journal.py
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@

import webob
from django.conf import settings
from django import utils

from lxml import html
from lxml.etree import XMLSyntaxError, ParserError
@@ -30,7 +31,7 @@
from .completion_api import CompletionApiClient
from .course_blocks_api import CourseBlocksApiClient
from .pdf_generator import get_style_sheet
from .utils import _, normalize_id
from .utils import _, normalize_id, DummyTranslationService

try:
from django.contrib.auth.models import User
@@ -61,6 +62,7 @@ def provide_pb_answer_list(xblock_instance):
return result


@XBlock.needs("i18n")
@XBlock.needs('user')
class EOCJournalXBlock(StudioEditableXBlockMixin, XBlock):
"""
@@ -158,6 +160,11 @@ class EOCJournalXBlock(StudioEditableXBlockMixin, XBlock):
'custom_font',
)

@property
def i18n_service(self):
""" Obtains translation service """
return self.runtime.service(self, "i18n") or DummyTranslationService()

def student_view_data(self, context=None):
"""
JSON representation of data shown to students.
@@ -218,17 +225,31 @@ def student_view(self, context=None):

fragment = Fragment()
fragment.add_content(
loader.render_template("templates/eoc_journal.html", context)
loader.render_django_template('templates/eoc_journal.html',
context=context,
i18n_service=self.i18n_service)
)
fragment.add_css_url(
self.runtime.local_resource_url(self, "public/css/eoc_journal.css")
)
fragment.add_javascript_url(self.get_translation_content())
fragment.add_javascript_url(
self.runtime.local_resource_url(self, "public/js/eoc_journal.js")
)
fragment.initialize_js("EOCJournalXBlock")
return fragment

def get_translation_content(self):
"""
Returns URL containing translations for user's language.
"""
try:
return self.runtime.local_resource_url(self, 'public/js/translations/{lang}/textjs.js'.format(
lang=utils.translation.to_locale(utils.translation.get_language()),
))
except IOError:
return self.runtime.local_resource_url(self, 'public/js/translations/en/textjs.js')

@XBlock.handler
def serve_pdf(self, request, _suffix):
"""
6 changes: 5 additions & 1 deletion eoc_journal/templates/eoc_journal.html
Original file line number Diff line number Diff line change
@@ -145,7 +145,11 @@ <h4>{{ pdf_report_link_heading }}</h4>
{% if display_key_takeaways %}
<div class="eoc-key-takeaways">
<div class="title">
<h4>Key Takeaways</h4>
<h4>
{% filter force_escape %}
{% trans "Key Takeaways" %}
{% endfilter %}
</h4>
</div>
{% if key_takeaways_pdf_url %}
<p>
17 changes: 17 additions & 0 deletions eoc_journal/utils.py
Original file line number Diff line number Diff line change
@@ -33,8 +33,25 @@ def build_jwt_edx_client(url, scopes, user, expires_in, append_slash=True):
return EdxRestApiClient(url, append_slash=append_slash, jwt=jwt)


def ngettext_fallback(text_singular, text_plural, number):
""" Dummy `ngettext` replacement to make string extraction tools scrape strings marked for translation """
if number == 1:
return text_singular
else:
return text_plural


class NotConnectedToOpenEdX(Exception):
"""
Exception to raise when not connected to OpenEdX.
"""
pass


class DummyTranslationService(object): # pylint: disable=too-few-public-methods
"""
Dummy drop-in replacement for i18n XBlock service
"""
_catalog = {}
gettext = _
ngettext = ngettext_fallback
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
git+https://github.com/edx/xblock-utils.git@v1.0.2#egg=xblock-utils==1.0.2
xblock-utils==1.2.2
git+https://github.com/open-craft/problem-builder@27e2f4ba5afdefcb6a4e5fd793969e1e626a0abe#egg=xblock-problem-builder
edx-rest-api-client==1.6.0
reportlab==3.1.44
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ def package_data(pkg, roots):

setup(
name='xblock-eoc-journal',
version='0.6',
version='0.7',
description='End of Course Journal XBlock',
packages=[
'eoc_journal',

0 comments on commit 40d36f3

Please sign in to comment.