Skip to content

Commit

Permalink
MCKIN-24785. Platform issues - Instructor tool xblock strings for tra…
Browse files Browse the repository at this point in the history
  • Loading branch information
wasifarbisoft authored Sep 28, 2020
1 parent eb669fa commit c1415db
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
14 changes: 7 additions & 7 deletions problem_builder/instructor_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from xblock.fields import Dict, List, Scope, String
from xblock.fragment import Fragment
from xblockutils.resources import ResourceLoader
from .mixins import TranslationContentMixin
from .mixins import TranslationContentMixin, XBlockWithTranslationServiceMixin
from .utils import I18NService

loader = ResourceLoader(__name__)
Expand All @@ -50,7 +50,7 @@ def _(text):

@XBlock.needs("i18n")
@XBlock.wants('user')
class InstructorToolBlock(XBlock, I18NService, TranslationContentMixin):
class InstructorToolBlock(XBlock, I18NService, TranslationContentMixin, XBlockWithTranslationServiceMixin):
"""
InstructorToolBlock: An XBlock for instructors to export student answers from a course.
Expand Down Expand Up @@ -138,10 +138,10 @@ def student_view(self, context=None):
if not self.user_is_staff():
return Fragment(u'<p>This interface can only be used by course staff.</p>')
block_choices = {
_('Multiple Choice Question'): 'MCQBlock',
_('Multiple Response Question'): 'MRQBlock',
_('Rating Question'): 'RatingBlock',
_('Long Answer'): 'AnswerBlock',
self._('Multiple Choice Question'): 'MCQBlock',
self._('Multiple Response Question'): 'MRQBlock',
self._('Rating Question'): 'RatingBlock',
self._('Long Answer'): 'AnswerBlock',
}

html = loader.render_django_template('templates/html/instructor_tool.html', {
Expand Down Expand Up @@ -229,7 +229,7 @@ def start_export(self, data, suffix=''):
if user_id:
user_ids.append(user_id)
if not user_ids:
self.raise_error(404, _("Could not find any of the specified usernames."))
self.raise_error(404, self._("Could not find any of the specified usernames."))

if not root_block_id:
root_block_id = self.scope_ids.usage_id
Expand Down
17 changes: 8 additions & 9 deletions problem_builder/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import webob
from django import utils
from lazy import lazy
from .settings import PB_LANGUAGE_JS_DIRECTORY_MAP
from xblock.core import XBlock
from xblock.fields import UNIQUE_ID, Boolean, Float, Scope, String
from xblock.fragment import Fragment
Expand Down Expand Up @@ -300,20 +301,18 @@ class TranslationContentMixin:
"""
@staticmethod
def resource_string(path):

"""Handy helper for getting resources from our kit."""
data = pkg_resources.resource_string(__name__, path)
return data.decode("utf8")

def get_translation_content(self):
try:
# here we need to split the lang code and need to change - to _ and post - characters to
# upper case since we have local directories like ja_JP, etc instead of ja-jp, etc
language = utils.translation.get_language().split('-')
if len(language) == 2:
new_lang = language[0] + "_" + language[1].upper()
else:
new_lang = utils.translation.get_language()
return self.resource_string('public/js/translations/{lang}/textjs.js'.format(lang=new_lang))
language = utils.translation.get_language()
# the language code for some languages are not consistent
# with the js directory structure of problem builder
# i.e for polish language code is pl but js directory is pl_PL
# to coup this behaviour PB_LANGUAGES_JS_DIRECTORY_MAP is added to get the desired directory.
js_directory = PB_LANGUAGE_JS_DIRECTORY_MAP.get(language, 'en')
return self.resource_string('public/js/translations/{lang}/textjs.js'.format(lang=js_directory))
except IOError:
return self.resource_string('public/js/translations/en/textjs.js')
14 changes: 14 additions & 0 deletions problem_builder/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,17 @@
)
STATICI18N_ROOT = 'problem_builder/public/js'
STATICI18N_OUTPUT_DIR = 'translations'

PB_LANGUAGE_JS_DIRECTORY_MAP = {
'ar': 'ar',
'de-de': 'de_DE',
'en': 'en',
'es-419': 'es_419',
'fr': 'fr',
'fr-ca': 'fr_CA',
'ja-jp': 'ja_JP',
'pl': 'pl_PL',
'pt-br': 'pt_BR',
'zh-cn': 'zh_CN',
'ko-kr': 'ko_KR'
}
15 changes: 8 additions & 7 deletions problem_builder/tests/unit/test_instructor_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,26 @@ def setUp(self):
self.block = InstructorToolBlock(
self.runtime_mock, field_data=DictFieldData({}), scope_ids=scope_ids_mock
)
self.block._ = Mock(return_values=None)
self.block_choices = {
self.block._("Multiple Choice Question'"): 'MCQBlock',
self.block._("Multiple Response Question"): 'MRQBlock',
self.block._("Rating Question"): 'RatingBlock',
self.block._("Long Answer"): 'AnswerBlock'
}

def test_student_view_template_args(self):
"""
Check if `student_view` calls rendering method of template loader
with correct arguments.
"""
block_choices = {
'Multiple Choice Question': 'MCQBlock',
'Multiple Response Question': 'MRQBlock',
'Rating Question': 'RatingBlock',
'Long Answer': 'AnswerBlock',
}

with patch('problem_builder.instructor_tool.loader') as patched_loader:
patched_loader.render_django_template.return_value = u''
self.block.student_view()
self.service_mock.i18n_service = Mock(return_value=None)
patched_loader.render_django_template.assert_called_once_with('templates/html/instructor_tool.html', {
'block_choices': block_choices,
'block_choices': self.block_choices,
'course_blocks_api': COURSE_BLOCKS_API,
'root_block_id': self.course_id,
}, i18n_service=self.service_mock)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

# Constants #########################################################

VERSION = '4.1.3'
VERSION = '4.1.4'

# Functions #########################################################

Expand Down

0 comments on commit c1415db

Please sign in to comment.