Skip to content

Commit f4b6800

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

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
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.4.0 (2024-01-15)
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.4.0"

drag_and_drop_v2/drag_and_drop_v2.py

Lines changed: 28 additions & 10 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."),
@@ -320,10 +322,13 @@ def _learner_raw_score(self):
320322
return correct_count / float(total_count)
321323

322324
@staticmethod
323-
def _get_statici18n_js_url():
325+
def _get_deprecated_i18n_js_url():
324326
"""
325-
Returns the Javascript translation file for the currently selected language, if any found by
327+
Returns the deprecated JavaScript translation file for the currently selected language, if any found by
326328
`pkg_resources`
329+
330+
This method returns pre-OEP-58 i18n files and is deprecated in favor
331+
of `get_javascript_i18n_catalog_url`.
327332
"""
328333
lang_code = translation.get_language()
329334
if not lang_code:
@@ -335,6 +340,19 @@ def _get_statici18n_js_url():
335340
return text_js.format(lang_code=code)
336341
return None
337342

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

355-
statici18n_js_url = self._get_statici18n_js_url()
356-
if statici18n_js_url:
357-
js_urls.append(statici18n_js_url)
358-
359373
for css_url in css_urls:
360374
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
361375
for js_url in js_urls:
362376
fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url))
363377

378+
statici18n_js_url = self._get_statici18n_js_url()
379+
if statici18n_js_url:
380+
fragment.add_javascript_url(statici18n_js_url)
381+
364382
self.include_theme_files(fragment)
365383

366384
fragment.initialize_js('DragAndDropBlock', self.student_view_data())
@@ -458,15 +476,15 @@ def studio_view(self, context):
458476
'public/js/drag_and_drop_edit.js',
459477
]
460478

461-
statici18n_js_url = self._get_statici18n_js_url()
462-
if statici18n_js_url:
463-
js_urls.append(statici18n_js_url)
464-
465479
for css_url in css_urls:
466480
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
467481
for js_url in js_urls:
468482
fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url))
469483

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

0 commit comments

Comments
 (0)