Skip to content

Commit

Permalink
feat: use XBlockI18NService js translations
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarIthawi committed Jan 3, 2024
1 parent ad6ad2b commit 3a499d9
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions drag_and_drop_v2/drag_and_drop_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ class DragAndDropBlock(
SOLUTION_INCORRECT: None
}

i18n_js_namespace = 'DragAndDropI18N'

display_name = String(
display_name=_("Title"),
help=_("The title of the drag and drop problem. The title is displayed to learners."),
Expand Down Expand Up @@ -320,10 +322,13 @@ def _learner_raw_score(self):
return correct_count / float(total_count)

@staticmethod
def _get_statici18n_js_url():
def get_deprecated_i18n_js_url():
"""
Returns the Javascript translation file for the currently selected language, if any found by
Returns the deprecated JavaScript translation file for the currently selected language, if any found by
`pkg_resources`
This method is returns pre-OEP-58 i18n files and will be deprecated in favor
of `get_javascript_i18n_catalog_url`.
"""
lang_code = translation.get_language()
if not lang_code:
Expand All @@ -335,6 +340,19 @@ def _get_statici18n_js_url():
return text_js.format(lang_code=code)
return None

def _get_statici18n_js_url(self):
"""
Return the JavaScript translation file provided by the XBlockI18NService.
"""
if url_getter_func := getattr(self.i18n_service, 'get_javascript_i18n_catalog_url', None):
if javascript_url := url_getter_func(self):
return javascript_url

if deprecated_url := DragAndDropBlock.get_deprecated_i18n_js_url():
return self.runtime.local_resource_url(self, deprecated_url)

return None

@XBlock.supports("multi_device") # Enable this block for use in the mobile app via webview
def student_view(self, context):
"""
Expand All @@ -352,9 +370,8 @@ def student_view(self, context):
'public/js/drag_and_drop.js',
]

statici18n_js_url = self._get_statici18n_js_url()
if statici18n_js_url:
js_urls.append(statici18n_js_url)
if static_i18n_js_url := self.get_javascript_i18n_catalog_url():
js_urls.append(static_i18n_js_url)

for css_url in css_urls:
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
Expand Down

0 comments on commit 3a499d9

Please sign in to comment.