diff --git a/xblock/core.py b/xblock/core.py index e23b02ebd..c16b04680 100644 --- a/xblock/core.py +++ b/xblock/core.py @@ -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): """ diff --git a/xblock/test/test_runtime.py b/xblock/test/test_runtime.py index 6994d9b9e..fa5bf7d92 100644 --- a/xblock/test/test_runtime.py +++ b/xblock/test/test_runtime.py @@ -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):