Skip to content

Commit

Permalink
Merge pull request #335 from edx/asadiqbal08/WL-376
Browse files Browse the repository at this point in the history
WL-376 move i18n runtime logic in parent XBlock
  • Loading branch information
mattdrayer committed Mar 28, 2016
2 parents 0115220 + 2f8c69c commit 133d267
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions xblock/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def validate(self):
"""
return Validation(self.scope_ids.usage_id)

def ugettext(self, text):
"""
Translates message/text and returns it in a unicode string.
Using runtime to get i18n service.
"""
runtime_service = self.runtime.service(self, "i18n")
runtime_ugettext = runtime_service.ugettext
return runtime_ugettext(text)


class XBlockAside(XmlSerializationMixin, ScopedStorageMixin, RuntimeServicesMixin, HandlersMixin, SharedBlockBase):
"""
Expand Down
18 changes: 18 additions & 0 deletions xblock/test/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,24 @@ def test_service():
runtime.render(tester, 'student_view')


def test_ugettext_calls():
"""
Test ugettext calls in xblock.
"""
runtime = TestRuntime()
block = XBlockWithServices(runtime, scope_ids=Mock(spec=[]))
assert_equals(block.ugettext('test'), u'test')
assert_true(isinstance(block.ugettext('test'), unicode))

# NoSuchServiceError exception should raise if i18n is none/empty.
runtime = TestRuntime(services={
'i18n': None
})
block = XBlockWithServices(runtime, scope_ids=Mock(spec=[]))
with assert_raises(NoSuchServiceError):
block.ugettext('test')


@XBlock.needs("no_such_service_sub")
@XBlock.wants("another_not_service_sub")
class SubXBlockWithServices(XBlockWithServices):
Expand Down

0 comments on commit 133d267

Please sign in to comment.