Skip to content

Commit 4b7ad8d

Browse files
committed
feat: use XBlockI18NService js translations
1 parent aca4b63 commit 4b7ad8d

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

Changelog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Drag and Drop XBlock changelog
44
Unreleased
55
---------------------------
66

7+
Version 3.3.1 (2024-01-12)
8+
---------------------------
9+
10+
* XBlockI18NService js translations support
11+
712
Version 3.3.0 (2023-10-24)
813
---------------------------
914

drag_and_drop_v2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
""" Drag and Drop v2 XBlock """
22
from .drag_and_drop_v2 import DragAndDropBlock
33

4-
__version__ = "3.3.0"
4+
__version__ = "3.3.1"

drag_and_drop_v2/drag_and_drop_v2.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ class DragAndDropBlock(
8080
SOLUTION_INCORRECT: None
8181
}
8282

83+
i18n_js_namespace = 'DragAndDropI18N'
84+
8385
display_name = String(
8486
display_name=_("Title"),
8587
help=_("The title of the drag and drop problem. The title is displayed to learners."),
@@ -319,11 +321,13 @@ def _learner_raw_score(self):
319321
correct_count, total_count = self._get_item_stats()
320322
return correct_count / float(total_count)
321323

322-
@staticmethod
323-
def _get_statici18n_js_url():
324+
def _get_deprecated_i18n_js_url(self):
324325
"""
325-
Returns the Javascript translation file for the currently selected language, if any found by
326+
Returns the deprecated JavaScript translation file for the currently selected language, if any found by
326327
`pkg_resources`
328+
329+
This method returns pre-OEP-58 i18n files and is deprecated in favor
330+
of `get_javascript_i18n_catalog_url`.
327331
"""
328332
lang_code = translation.get_language()
329333
if not lang_code:
@@ -335,6 +339,19 @@ def _get_statici18n_js_url():
335339
return text_js.format(lang_code=code)
336340
return None
337341

342+
def _get_statici18n_js_url(self):
343+
"""
344+
Return the JavaScript translation file provided by the XBlockI18NService.
345+
"""
346+
if url_getter_func := getattr(self.i18n_service, 'get_javascript_i18n_catalog_url', None):
347+
if javascript_url := url_getter_func(self):
348+
return javascript_url
349+
350+
if deprecated_url := self._get_deprecated_i18n_js_url():
351+
return self.runtime.local_resource_url(self, deprecated_url)
352+
353+
return None
354+
338355
@XBlock.supports("multi_device") # Enable this block for use in the mobile app via webview
339356
def student_view(self, context):
340357
"""
@@ -352,15 +369,15 @@ def student_view(self, context):
352369
'public/js/drag_and_drop.js',
353370
]
354371

355-
statici18n_js_url = self._get_statici18n_js_url()
356-
if statici18n_js_url:
357-
js_urls.append(statici18n_js_url)
358-
359372
for css_url in css_urls:
360373
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
361374
for js_url in js_urls:
362375
fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url))
363376

377+
statici18n_js_url = self._get_statici18n_js_url()
378+
if statici18n_js_url:
379+
fragment.add_javascript_url(statici18n_js_url)
380+
364381
self.include_theme_files(fragment)
365382

366383
fragment.initialize_js('DragAndDropBlock', self.student_view_data())
@@ -458,15 +475,15 @@ def studio_view(self, context):
458475
'public/js/drag_and_drop_edit.js',
459476
]
460477

461-
statici18n_js_url = self._get_statici18n_js_url()
462-
if statici18n_js_url:
463-
js_urls.append(statici18n_js_url)
464-
465478
for css_url in css_urls:
466479
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
467480
for js_url in js_urls:
468481
fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url))
469482

483+
statici18n_js_url = self._get_statici18n_js_url()
484+
if statici18n_js_url:
485+
fragment.add_javascript_url(statici18n_js_url)
486+
470487
# Do a bit of manipulation so we get the appearance of a list of zone options on
471488
# items that still have just a single zone stored
472489

0 commit comments

Comments
 (0)