From 71b90a918df27e5ba6fa32a4a6912d74630b89a3 Mon Sep 17 00:00:00 2001 From: polesye Date: Wed, 28 May 2014 16:03:14 +0300 Subject: [PATCH] BLD-725: ChoiceGroup i18n. --- common/lib/capa/capa/inputtypes.py | 26 +++++++++++++------ .../lib/capa/capa/templates/choicetext.html | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 13627ae0f6..39c79f8f26 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -433,6 +433,7 @@ class ChoiceGroup(InputTypeBase): tags = ['choicegroup', 'radiogroup', 'checkboxgroup'] def setup(self): + i18n = self.capa_system.i18n # suffix is '' or [] to change the way the input is handled in --as a scalar or vector # value. (VS: would be nice to make this less hackish). if self.tag == 'choicegroup': @@ -445,16 +446,20 @@ class ChoiceGroup(InputTypeBase): self.html_input_type = "checkbox" self.suffix = '[]' else: - raise Exception("ChoiceGroup: unexpected tag {0}".format(self.tag)) + _ = i18n.ugettext + # Translators: 'ChoiceGroup' is an input type and should not be translated. + msg = _("ChoiceGroup: unexpected tag {tag_name}").format(tag_name=self.tag) + raise Exception(msg) - self.choices = self.extract_choices(self.xml) - self._choices_map = dict(self.choices) # pylint: disable=attribute-defined-outside-init + self.choices = self.extract_choices(self.xml, i18n) + self._choices_map = dict(self.choices,) # pylint: disable=attribute-defined-outside-init @classmethod def get_attributes(cls): + _ = lambda text: text return [Attribute("show_correctness", "always"), Attribute('label', ''), - Attribute("submitted_message", "Answer received.")] + Attribute("submitted_message", _("Answer received."))] def _extra_context(self): return {'input_type': self.html_input_type, @@ -462,7 +467,7 @@ class ChoiceGroup(InputTypeBase): 'name_array_suffix': self.suffix} @staticmethod - def extract_choices(element): + def extract_choices(element, i18n): """ Extracts choices for a few input types, such as ChoiceGroup, RadioGroup and CheckboxGroup. @@ -474,12 +479,17 @@ class ChoiceGroup(InputTypeBase): """ choices = [] + _ = i18n.ugettext for choice in element: if choice.tag != 'choice': - raise Exception( - "[capa.inputtypes.extract_choices] Expected a tag; got %s instead" - % choice.tag) + msg = u"[capa.inputtypes.extract_choices] {error_message}".format( + # Translators: '' is a tag name and should not be translated. + error_message=_("Expected a tag; got {given_tag} instead").format( + given_tag=choice.tag + ) + ) + raise Exception(msg) choices.append((choice.get("name"), stringify_children(choice))) return choices diff --git a/common/lib/capa/capa/templates/choicetext.html b/common/lib/capa/capa/templates/choicetext.html index 6991ede721..b0db04ad4e 100644 --- a/common/lib/capa/capa/templates/choicetext.html +++ b/common/lib/capa/capa/templates/choicetext.html @@ -1,3 +1,4 @@ +<%! from django.utils.translation import ugettext as _ %> <% element_checked = False %> % for choice_id, _ in choices: <%choice_id = choice_id %> @@ -62,7 +63,7 @@ % if show_correctness == "never" and (value or status not in ['unsubmitted']): -
${submitted_message}
+
${_(submitted_message)}
%endif % if msg: ${msg|n}