Repository navigation
feat: load the XBlock i18n template tags from Django's own engine - #973
Draft
kdmccormick wants to merge 1 commit into
Draft
kdmccormick wants to merge 1 commit into
kdmccormick wants to merge 1 commit into
Conversation
Blocks whose Django templates are rendered by Django's engine (because
they {% include %} each other, or to get template caching) could not use
the XBlock trans/blocktrans tags: Django only finds tag libraries in
installed apps' templatetags packages, and xblock is not an app. So
their {% trans %} strings went to the host's global catalog instead of
the block's own.
Add the xblock.django app. With it in INSTALLED_APPS, templates can
{% load xblock_i18n %} and get the XBlock tags, which translate through
the i18n service found in the template context under "_i18n_service".
ResourceLoader.render_django_template accepts that load name too, so a
template can be rendered either way.
While here, fix how the tags apply the service. They merged the
service's catalog into Django's shared translation object for the
active language and then put back a copy taken before the node's first
render. From the second render of a cached template node on, that copy
was the live object, so the block's catalog stayed merged into the
host's translations for the rest of the process. The tags now install a
per-thread translation object that consults the service first and falls
back to the previous active translation, and restore the previous one
afterwards. Nothing shared is modified. As a side effect the service no
longer needs a `plural` attribute.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This was referenced Nov 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Blocks whose Django templates are rendered by Django's engine (because they
{% include %}each other, or to get template caching) could not use the XBlocktrans/blocktranstags: Django only finds tag libraries in installed apps'templatetagspackages, andxblockis not an app. So their{% trans %}strings went to the host's global catalog instead of the block's own.Add the
xblock.djangoapp. With it inINSTALLED_APPS, templates can{% load xblock_i18n %}and get the XBlock tags, which translate through the i18n service found in the template context under_i18n_service.ResourceLoader.render_django_templateaccepts that load name too, so a template can be rendered either way.While here, fix how the tags apply the service. They merged the service's catalog into Django's shared translation object for the active language and then put back a copy taken before the node's first render. From the second render of a cached template node on, that copy was the live object, so the block's catalog stayed merged into the host's translations for the rest of the process. The tags now install a per-thread translation object that consults the service first and falls back to the previous active translation, and restore the previous one afterwards. Nothing shared is modified. As a side effect the service no longer needs a
pluralattribute.Merge considerations
"xblock.django"toINSTALLED_APPS: feat: install xblock.django for the xblock_i18n template tags openedx-platform#39435 does that for openedx-platform, to go with the version bump. Nothing changes for hosts that don't.ResourceLoader(LTI, drag-and-drop, Annotatable, PDF, …): their strings no longer end up in the host's global catalog after the first render. The rendered output of those blocks is the same. The existingtest_render_django_template_translatedtest could not see the leak becauseResourceLoadercompiles a fresh template per call; with cached engines or templates it would have.librariesdict inrender_django_template.Supporting Info
ResourceLoaderonly, no includes), fix: translate Problem, Video and Discussion templates with the block catalog xblocks-core#347 (per-block copy of this library).{% trans %}resolves the active translation:django.utils.translation.trans_real.gettextreads the thread-local_active.value, which is what the newblock_translationcontext manager swaps.Testing
uv sync --group test --group quality, thenDJANGO_SETTINGS_MODULE=xblock.test.settings pytest xblock web_fragments(ortox -e py312-django52). Expect all tests to pass, including the newxblock/test/django/test_templatetags.pyandtest_render_django_template_with_xblock_i18n_load.make quality(pycodestyle and pylint).xblock/utils/templatetags/i18n.pyfrommasterand reruntest_templatetags.py.test_nothing_leaks_into_the_active_translation,test_restores_an_unset_active_translationandtest_blocktrans_plural_uses_the_serviceshould fail."xblock.django"inINSTALLED_APPS, render{% load xblock_i18n %}{% trans "x" %}throughrender_to_stringwith an i18n service in the context under_i18n_service; the service's translation should be used, anddjango.utils.translation.gettext("x")should be unaffected afterwards.I ran steps 1 to 3 locally (Python 3.13, Django 5.2): 1555 tests passed, quality clean except one pre-existing pylint
useless-suppressionnote infields.py, and the three tests fail against the old implementation as expected. Step 4 is exercised end to end by the xblocks-core change.