Skip to content

feat: load the XBlock i18n template tags from Django's own engine - #973

Draft
kdmccormick wants to merge 1 commit into
masterfrom
kdmccormick/i18n-tag-library-app
Draft

kdmccormick wants to merge 1 commit into
masterfrom
kdmccormick/i18n-tag-library-app

Conversation

@kdmccormick

@kdmccormick kdmccormick commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

Disclaimer: This PR was created by Claude Fable 5.1. It needs manual review by @kdmccormick before merge.

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 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.

Merge considerations

Supporting Info

Testing

  1. uv sync --group test --group quality, then DJANGO_SETTINGS_MODULE=xblock.test.settings pytest xblock web_fragments (or tox -e py312-django52). Expect all tests to pass, including the new xblock/test/django/test_templatetags.py and test_render_django_template_with_xblock_i18n_load.
  2. make quality (pycodestyle and pylint).
  3. Check the new tests catch the old behaviour: restore xblock/utils/templatetags/i18n.py from master and rerun test_templatetags.py. test_nothing_leaks_into_the_active_translation, test_restores_an_unset_active_translation and test_blocktrans_plural_uses_the_service should fail.
  4. In a Django project with "xblock.django" in INSTALLED_APPS, render {% load xblock_i18n %}{% trans "x" %} through render_to_string with an i18n service in the context under _i18n_service; the service's translation should be used, and django.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-suppression note in fields.py, and the three tests fail against the old implementation as expected. Step 4 is exercised end to end by the xblocks-core change.

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant