From ad391c2d57259bd5d5da6163375c8e8689f0b6e7 Mon Sep 17 00:00:00 2001 From: Omar Al-Ithawi Date: Sun, 12 Nov 2023 20:35:26 +0300 Subject: [PATCH] feat: use XBlockI18NService js translations --- drag_and_drop_v2/drag_and_drop_v2.py | 33 +++++++++++++++++++++------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/drag_and_drop_v2/drag_and_drop_v2.py b/drag_and_drop_v2/drag_and_drop_v2.py index ceef8d37e..5280924da 100644 --- a/drag_and_drop_v2/drag_and_drop_v2.py +++ b/drag_and_drop_v2/drag_and_drop_v2.py @@ -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."), @@ -319,11 +321,12 @@ def _learner_raw_score(self): correct_count, total_count = self._get_item_stats() return correct_count / float(total_count) - @staticmethod - def _get_statici18n_js_url(): + def get_deprecated_statici18n_js_url(self): """ Returns the 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_i18n_js_url`. """ lang_code = translation.get_language() if not lang_code: @@ -334,6 +337,14 @@ def _get_statici18n_js_url(): if pkg_resources.resource_exists(loader.module_name, text_js.format(lang_code=code)): return text_js.format(lang_code=code) return None + + def get_i18n_js_url(self): + """ + Return the JavaScript translation file provided by the XBlockI18NService. + """ + if get_locale_static_url_func := getattr(self.i18n_service, 'get_javascript_locale_static_url', None): + return get_locale_static_url_func(self) + return None @XBlock.supports("multi_device") # Enable this block for use in the mobile app via webview def student_view(self, context): @@ -352,9 +363,12 @@ 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 statici18n_js_url := self.get_i18n_js_url(): + fragment.add_javascript_url(statici18n_js_url) + else: + depreacted_statici18n_js_url = self.get_deprecated_statici18n_js_url() + if depreacted_statici18n_js_url: + js_urls.append(depreacted_statici18n_js_url) for css_url in css_urls: fragment.add_css_url(self.runtime.local_resource_url(self, css_url)) @@ -458,9 +472,12 @@ def studio_view(self, context): 'public/js/drag_and_drop_edit.js', ] - statici18n_js_url = self._get_statici18n_js_url() - if statici18n_js_url: - js_urls.append(statici18n_js_url) + if statici18n_js_url := self.get_i18n_js_url(): + fragment.add_javascript_url(statici18n_js_url) + else: + depreacted_statici18n_js_url = self.get_deprecated_statici18n_js_url() + if depreacted_statici18n_js_url: + js_urls.append(depreacted_statici18n_js_url) for css_url in css_urls: fragment.add_css_url(self.runtime.local_resource_url(self, css_url))