From d58352ea40564ebbffbaedf3533458b2acf837c7 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 --- Changelog.md | 5 ++++ drag_and_drop_v2/__init__.py | 2 +- drag_and_drop_v2/drag_and_drop_v2.py | 38 ++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Changelog.md b/Changelog.md index bc96176da..36c55d3e4 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,11 @@ Drag and Drop XBlock changelog Unreleased --------------------------- +Version 3.4.0 (2024-01-15) +--------------------------- + +* XBlockI18NService js translations support + Version 3.3.0 (2023-10-24) --------------------------- diff --git a/drag_and_drop_v2/__init__.py b/drag_and_drop_v2/__init__.py index 7189ac6e1..7b40bcd47 100644 --- a/drag_and_drop_v2/__init__.py +++ b/drag_and_drop_v2/__init__.py @@ -1,4 +1,4 @@ """ Drag and Drop v2 XBlock """ from .drag_and_drop_v2 import DragAndDropBlock -__version__ = "3.3.0" +__version__ = "3.4.0" diff --git a/drag_and_drop_v2/drag_and_drop_v2.py b/drag_and_drop_v2/drag_and_drop_v2.py index ceef8d37e..a385d6915 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."), @@ -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 returns pre-OEP-58 i18n files and is deprecated in favor + of `get_javascript_i18n_catalog_url`. """ lang_code = translation.get_language() if not lang_code: @@ -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 := self._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): """ @@ -352,15 +370,15 @@ 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) - for css_url in css_urls: fragment.add_css_url(self.runtime.local_resource_url(self, css_url)) for js_url in js_urls: fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url)) + statici18n_js_url = self._get_statici18n_js_url() + if statici18n_js_url: + fragment.add_javascript_url(statici18n_js_url) + self.include_theme_files(fragment) fragment.initialize_js('DragAndDropBlock', self.student_view_data()) @@ -458,15 +476,15 @@ 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) - for css_url in css_urls: fragment.add_css_url(self.runtime.local_resource_url(self, css_url)) for js_url in js_urls: fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url)) + statici18n_js_url = self._get_statici18n_js_url() + if statici18n_js_url: + fragment.add_javascript_url(statici18n_js_url) + # Do a bit of manipulation so we get the appearance of a list of zone options on # items that still have just a single zone stored