Skip to content

Commit 9f4cbdf

Browse files
committed
feat: use XBlockI18NService js translations
1 parent e8df964 commit 9f4cbdf

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

drag_and_drop_v2/drag_and_drop_v2.py

Lines changed: 25 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 is returns pre-OEP-58 i18n files and will be 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,14 @@ def _get_statici18n_js_url():
335340
return text_js.format(lang_code=code)
336341
return None
337342

343+
def get_javascript_i18n_catalog_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+
return url_getter_func(self)
349+
return None
350+
338351
@XBlock.supports("multi_device") # Enable this block for use in the mobile app via webview
339352
def student_view(self, context):
340353
"""
@@ -352,15 +365,16 @@ def student_view(self, context):
352365
'public/js/drag_and_drop.js',
353366
]
354367

355-
statici18n_js_url = self._get_statici18n_js_url()
356-
if statici18n_js_url:
357-
js_urls.append(statici18n_js_url)
358-
359368
for css_url in css_urls:
360369
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
361370
for js_url in js_urls:
362371
fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url))
363372

373+
if static_i18n_js_url := self.get_javascript_i18n_catalog_url():
374+
fragment.add_javascript_url(static_i18n_js_url)
375+
elif deprecated_i18n_js_url := DragAndDropBlock.get_deprecated_i18n_js_url():
376+
fragment.add_javascript_url(self.runtime.local_resource_url(self, deprecated_i18n_js_url))
377+
364378
self.include_theme_files(fragment)
365379

366380
fragment.initialize_js('DragAndDropBlock', self.student_view_data())
@@ -458,15 +472,16 @@ def studio_view(self, context):
458472
'public/js/drag_and_drop_edit.js',
459473
]
460474

461-
statici18n_js_url = self._get_statici18n_js_url()
462-
if statici18n_js_url:
463-
js_urls.append(statici18n_js_url)
464-
465475
for css_url in css_urls:
466476
fragment.add_css_url(self.runtime.local_resource_url(self, css_url))
467477
for js_url in js_urls:
468478
fragment.add_javascript_url(self.runtime.local_resource_url(self, js_url))
469479

480+
if static_i18n_js_url := self.get_javascript_i18n_catalog_url():
481+
fragment.add_javascript_url(static_i18n_js_url)
482+
elif deprecated_i18n_js_url := self.get_deprecated_i18n_js_url():
483+
fragment.add_javascript_url(self.runtime.local_resource_url(self, deprecated_i18n_js_url))
484+
470485
# Do a bit of manipulation so we get the appearance of a list of zone options on
471486
# items that still have just a single zone stored
472487

0 commit comments

Comments
 (0)