From 2afb005070649ef8a95c7ab316206c89b408018c Mon Sep 17 00:00:00 2001 From: Kyle D McCormick Date: Tue, 24 Nov 2026 14:13:51 -0500 Subject: [PATCH] fix: translate Problem, Video and Discussion templates with the block catalog The Problem block rendered problem.html, problem_ajax.html, status_span.html and the capa input templates with django.template.loader.render_to_string; the Discussion block did the same for its inline template; the Video block rendered video.html through ResourceLoader without an i18n service. {% trans %} in all of those looks strings up in Django's global catalog, which only holds what openedx-platform extracts from its own source. They are translated today only because the platform still carries built-in copies of these blocks, and because the platform's own extraction job has been failing since January, so the global catalog is a frozen snapshot that still contains them. Fixing either removes them. Problem and Discussion keep rendering through Django's engine, which their templates need for {% include %} and which caches compiled templates. Their Django apps (already registered through the Open edX plugin-app entry points so that the app-directories loader finds the templates) now also ship a `templatetags/xblock_i18n.py` library that exposes the XBlock i18n tags, `xblock.utils.templatetags.i18n`, by name. The templates `{% load xblock_i18n %}` instead of `{% load i18n %}`, and each block renders through one method that puts its i18n service in the context under the key those tags read. Video has one template with no includes, so it passes the service to ResourceLoader like the other blocks here. The Problem templates move under `templates/xblock_problem/`, since app-directories template names are global across installed apps and names like problem.html and textline.html invite collisions. The Discussion studio template moves next to the other Discussion templates. The Discussion student view also binds _() to the service: the module-level _() is the no-op extraction marker, so the sign-in message was never translated at all. No translatable strings are added or removed. Co-Authored-By: Claude Fable 5.1 --- src/xblock_discussion/apps.py | 4 +- src/xblock_discussion/block.py | 23 ++++++-- .../_discussion_inline.html | 2 +- .../_discussion_inline_studio.html | 2 +- .../_thread_list_template.html | 2 +- .../templatetags/__init__.py | 3 + .../templatetags/xblock_i18n.py | 10 ++++ .../tests/test_discussion.py | 58 ++++++++++++++++++ src/xblock_problem/block.py | 24 ++++++-- src/xblock_problem/capa/apps.py | 7 ++- src/xblock_problem/capa/checker.py | 2 +- .../{ => xblock_problem}/annotationinput.html | 4 +- .../chemicalequationinput.html | 2 +- .../{ => xblock_problem}/choicegroup.html | 4 +- .../{ => xblock_problem}/choicetext.html | 4 +- .../{ => xblock_problem}/clarification.html | 0 .../{ => xblock_problem}/codeinput.html | 4 +- .../{ => xblock_problem}/crystallography.html | 2 +- .../designprotein2dinput.html | 2 +- .../drag_and_drop_input.html | 2 +- .../{ => xblock_problem}/editageneinput.html | 2 +- .../{ => xblock_problem}/editamolecule.html | 2 +- .../{ => xblock_problem}/filesubmission.html | 0 .../formulaequationinput.html | 2 +- .../{ => xblock_problem}/imageinput.html | 2 +- .../{ => xblock_problem}/jsinput.html | 2 +- .../{ => xblock_problem}/mathstring.html | 0 .../{ => xblock_problem}/optioninput.html | 2 +- .../{ => xblock_problem}/problem.html | 20 +++---- .../{ => xblock_problem}/problem_ajax.html | 0 .../problem_notifications.html | 2 +- .../{ => xblock_problem}/schematicinput.html | 2 +- .../{ => xblock_problem}/solutionspan.html | 0 .../{ => xblock_problem}/status_span.html | 0 .../{ => xblock_problem}/textline.html | 2 +- .../{ => xblock_problem}/vsepr_input.html | 0 .../capa/templatetags/__init__.py | 3 + .../capa/templatetags/xblock_i18n.py | 10 ++++ src/xblock_problem/capa/tests/helpers.py | 2 +- src/xblock_problem/tests/test_capa_block.py | 59 ++++++++++++++++--- src/xblock_video/block.py | 4 +- src/xblock_video/tests/test_video.py | 34 +++++++++++ src/xblocks_core/tests/__init__.py | 3 + src/xblocks_core/tests/i18n.py | 50 ++++++++++++++++ test_settings.py | 7 ++- 45 files changed, 313 insertions(+), 58 deletions(-) rename src/xblock_discussion/templates/{ => discussion_templates}/_discussion_inline_studio.html (97%) create mode 100644 src/xblock_discussion/templatetags/__init__.py create mode 100644 src/xblock_discussion/templatetags/xblock_i18n.py rename src/xblock_problem/capa/templates/{ => xblock_problem}/annotationinput.html (93%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/chemicalequationinput.html (88%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/choicegroup.html (91%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/choicetext.html (96%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/clarification.html (100%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/codeinput.html (93%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/crystallography.html (93%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/designprotein2dinput.html (92%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/drag_and_drop_input.html (90%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/editageneinput.html (93%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/editamolecule.html (93%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/filesubmission.html (100%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/formulaequationinput.html (94%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/imageinput.html (93%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/jsinput.html (94%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/mathstring.html (100%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/optioninput.html (89%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/problem.html (80%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/problem_ajax.html (100%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/problem_notifications.html (97%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/schematicinput.html (83%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/solutionspan.html (100%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/status_span.html (100%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/textline.html (96%) rename src/xblock_problem/capa/templates/{ => xblock_problem}/vsepr_input.html (100%) create mode 100644 src/xblock_problem/capa/templatetags/__init__.py create mode 100644 src/xblock_problem/capa/templatetags/xblock_i18n.py create mode 100644 src/xblocks_core/tests/__init__.py create mode 100644 src/xblocks_core/tests/i18n.py diff --git a/src/xblock_discussion/apps.py b/src/xblock_discussion/apps.py index 508dee16..e68c4f20 100644 --- a/src/xblock_discussion/apps.py +++ b/src/xblock_discussion/apps.py @@ -25,7 +25,9 @@ class DiscussionAppConfig(AppConfig): dynamically injects this module into `INSTALLED_APPS` at runtime. This automatically exposes our `templates/` directory to the platform natively - without manual intervention. + without manual intervention. It also makes our `templatetags/` package loadable, which + the templates need for `{% load xblock_i18n %}`, the tags that translate through the + block's i18n service. References: diff --git a/src/xblock_discussion/block.py b/src/xblock_discussion/block.py index 38be8578..9dac92d3 100644 --- a/src/xblock_discussion/block.py +++ b/src/xblock_discussion/block.py @@ -14,13 +14,14 @@ from xblock.completable import XBlockCompletionMode from xblock.core import XBlock from xblock.fields import UNIQUE_ID, Scope, String -from xblock.utils.resources import ResourceLoader from xblock.utils.studio_editable import StudioEditableXBlockMixin from xblocks_core.legacy_utils.xml_utils import LegacyXmlMixin log = logging.getLogger(__name__) -loader = ResourceLoader(__name__) + +# Where the ``{% load xblock_i18n %}`` template tags look for the block's i18n service. +I18N_SERVICE_CONTEXT_KEY = "_i18n_service" Text = markupsafe.escape # pylint: disable=invalid-name @@ -154,6 +155,9 @@ def student_view(self, context=None): if not self.is_visible: return fragment + # Translate with this block's own catalog. The module-level ``_`` only marks strings. + _ = self.runtime.service(self, "i18n").gettext + self.add_resource_urls(fragment) login_msg = "" @@ -196,7 +200,7 @@ def student_view(self, context=None): "PLATFORM_NAME": settings.PLATFORM_NAME, "enable_discussion_home_panel": settings.FEATURES.get("ENABLE_DISCUSSION_HOME_PANEL", False), } - fragment.add_content(render_to_string("discussion_templates/_discussion_inline.html", context)) + fragment.add_content(self._render_template("discussion_templates/_discussion_inline.html", context)) fragment.initialize_js("DiscussionInlineBlock") @@ -211,9 +215,20 @@ def author_view(self, context=None): "discussion_id": self.discussion_id, "is_visible": self.is_visible, } - fragment.add_content(loader.render_django_template("templates/_discussion_inline_studio.html", context)) + fragment.add_content(self._render_template("discussion_templates/_discussion_inline_studio.html", context)) return fragment + def _render_template(self, template_name, context): + """ + Render one of this block's Django templates, found through the ``xblock_discussion`` Django app. + + Their ``{% load xblock_i18n %}`` tags translate through this block's i18n service, so the strings + come from the block's own catalog rather than Django's global one. + """ + return render_to_string( + template_name, {**context, I18N_SERVICE_CONTEXT_KEY: self.runtime.service(self, "i18n")} + ) + def student_view_data(self): """ Returns a JSON representation of the student_view of this XBlock. diff --git a/src/xblock_discussion/templates/discussion_templates/_discussion_inline.html b/src/xblock_discussion/templates/discussion_templates/_discussion_inline.html index 52944eaa..62ef216a 100644 --- a/src/xblock_discussion/templates/discussion_templates/_discussion_inline.html +++ b/src/xblock_discussion/templates/discussion_templates/_discussion_inline.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load xblock_i18n %} {% include "discussion_templates/_underscore_templates.html" %} {% include "discussion_templates/_thread_list_template.html" %} diff --git a/src/xblock_discussion/templates/_discussion_inline_studio.html b/src/xblock_discussion/templates/discussion_templates/_discussion_inline_studio.html similarity index 97% rename from src/xblock_discussion/templates/_discussion_inline_studio.html rename to src/xblock_discussion/templates/discussion_templates/_discussion_inline_studio.html index b315e724..877d520d 100644 --- a/src/xblock_discussion/templates/_discussion_inline_studio.html +++ b/src/xblock_discussion/templates/discussion_templates/_discussion_inline_studio.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load xblock_i18n %}

diff --git a/src/xblock_discussion/templates/discussion_templates/_thread_list_template.html b/src/xblock_discussion/templates/discussion_templates/_thread_list_template.html index f26180c8..21be4964 100644 --- a/src/xblock_discussion/templates/discussion_templates/_thread_list_template.html +++ b/src/xblock_discussion/templates/discussion_templates/_thread_list_template.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load xblock_i18n %} - {% include "status_span.html" with status=status status_id=id %} + {% include "xblock_problem/status_span.html" with status=status status_id=id %}

diff --git a/src/xblock_problem/capa/templates/jsinput.html b/src/xblock_problem/capa/templates/xblock_problem/jsinput.html similarity index 94% rename from src/xblock_problem/capa/templates/jsinput.html rename to src/xblock_problem/capa/templates/xblock_problem/jsinput.html index 56dec9cd..0c81ff81 100644 --- a/src/xblock_problem/capa/templates/jsinput.html +++ b/src/xblock_problem/capa/templates/xblock_problem/jsinput.html @@ -28,7 +28,7 @@ value="{{ value }}" />

-
{% include "status_span.html" with status=status status_id=id %}
+
{% include "xblock_problem/status_span.html" with status=status status_id=id %}
{% include "status_span.html" with status=status status_id=id %}
+
{% include "xblock_problem/status_span.html" with status=status status_id=id %}

{% if msg %}{{ msg|safe }}{% endif %} diff --git a/src/xblock_problem/capa/templates/problem.html b/src/xblock_problem/capa/templates/xblock_problem/problem.html similarity index 80% rename from src/xblock_problem/capa/templates/problem.html rename to src/xblock_problem/capa/templates/xblock_problem/problem.html index 05577302..f96b18b6 100644 --- a/src/xblock_problem/capa/templates/problem.html +++ b/src/xblock_problem/capa/templates/xblock_problem/problem.html @@ -1,4 +1,4 @@ -{% load i18n %} +{% load xblock_i18n %}

{% if demand_hint_possible %}
- {% include "problem_notifications.html" with notification_name='hint' notification_type='problem-hint' notification_icon='fa-question' notification_message='' %} + {% include "xblock_problem/problem_notifications.html" with notification_name='hint' notification_type='problem-hint' notification_icon='fa-question' notification_message='' %}
{% endif %}
@@ -121,30 +121,30 @@
- {% include "problem_notifications.html" with notification_type='warning' notification_icon='fa-exclamation-circle' notification_name='gentle-alert' notification_message='' %} + {% include "xblock_problem/problem_notifications.html" with notification_type='warning' notification_icon='fa-exclamation-circle' notification_name='gentle-alert' notification_message='' %} {% if answer_notification_type %} {% if answer_notification_type == 'correct' %} - {% include "problem_notifications.html" with notification_type='success' notification_icon='fa-check' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} + {% include "xblock_problem/problem_notifications.html" with notification_type='success' notification_icon='fa-check' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} {% endif %} {% if answer_notification_type == 'incorrect' %} - {% include "problem_notifications.html" with notification_type='error' notification_icon='fa-close' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} + {% include "xblock_problem/problem_notifications.html" with notification_type='error' notification_icon='fa-close' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} {% endif %} {% if answer_notification_type == 'partially-correct' %} - {% include "problem_notifications.html" with notification_type='success' notification_icon='fa-asterisk' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} + {% include "xblock_problem/problem_notifications.html" with notification_type='success' notification_icon='fa-asterisk' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} {% endif %} {% if answer_notification_type == 'submitted' %} - {% include "problem_notifications.html" with notification_type='general' notification_icon='fa-info-circle' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} + {% include "xblock_problem/problem_notifications.html" with notification_type='general' notification_icon='fa-info-circle' notification_name='submit' is_hidden=False notification_message=answer_notification_message %} {% endif %} {% endif %} {% if has_saved_answers %} - {% include "problem_notifications.html" with notification_type='warning' notification_icon='fa-save' notification_name='save' notification_message=save_message is_hidden=False %} + {% include "xblock_problem/problem_notifications.html" with notification_type='warning' notification_icon='fa-save' notification_name='save' notification_message=save_message is_hidden=False %} {% else %} - {% include "problem_notifications.html" with notification_type='warning' notification_icon='fa-save' notification_name='save' notification_message=save_message is_hidden=True %} + {% include "xblock_problem/problem_notifications.html" with notification_type='warning' notification_icon='fa-save' notification_name='save' notification_message=save_message is_hidden=True %} {% endif %} {# xss-lint: disable=django-trans-missing-escape #} {% trans "Answers are displayed within the problem" as notification_message %} {% with notification_message=notification_message|force_escape %} - {% include "problem_notifications.html" with notification_type='general' notification_icon='fa-info-circle' notification_name='show-answer' notification_message=notification_message is_hidden=True %} + {% include "xblock_problem/problem_notifications.html" with notification_type='general' notification_icon='fa-info-circle' notification_name='show-answer' notification_message=notification_message is_hidden=True %} {% endwith %}