Skip to content

fix: use each block's own translations for its JS and other strings - #336

Open
farhan wants to merge 12 commits into
openedx:mainfrom
farhan:farhan/332-i18n-js-namespace
Open

farhan wants to merge 12 commits into
openedx:mainfrom
farhan:farhan/332-i18n-js-namespace

Conversation

@farhan

@farhan farhan commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Closes #332.
Follow-up to #329 (merged 2026-11-17), which added per-block translation catalogs.

In short: once openedx-platform deletes its built-in copies of the Video, Problem, Annotatable and LTI blocks, some of their text would show in English for learners using another language. This PR makes that text use each block's own translations.

Background

What this PR does

Details (one commit each)

  1. JS (Video, Problem, Annotatable):
    • Each block sets i18n_js_namespace (XBlockVideoI18N, XBlockProblemI18N, XBlockAnnotatableI18N), so compile_xblock_translations builds a JS catalog for it.
    • Each view loads that catalog with get_javascript_i18n_catalog_url() before the block's own JS.
    • The JS reads gettext/ngettext from the namespace and falls back to the global catalog.
      • Video and Problem: an i18n_utils.js module is injected with webpack ProvidePlugin, so the existing call sites don't change.
      • Annotatable: its JS is inlined, so it defines a local gettext at the top of the file.
  2. Video extraction: tests/ is added to ignore_dirs. Before this, tests/views/bookmark_button.js strings were being sent to translators.
  3. Codejail errors (remote_exec.py):
    • CODE_JAIL_REST_SERVICE_REMOTE_EXEC is a pluggable (data) function, so it doesn't take a translator. It only marks its messages with gettext_noop.
    • safe_exec() takes an optional gettext and uses it to translate CodejailService* errors. The capa call sites pass the block's i18n service.
  4. LTI: lti_form.html is rendered with i18n_service.

Review follow-ups (new commits)

  1. Codejail errors: safe_exec now chains from err, which keeps the untranslated original. The intended non-literal gettext call gets a pylint translation-of-non-string disable.
  2. Problem drag-and-drop JS (capa/static/js/capa/drag_and_drop/base_image.js and draggable_events.js, 5 strings): a local gettext reads XBlockProblemI18N and falls back to the global catalog. These files are served from xblock_problem, not the platform.
  3. matlabinput.html: the inline JS gettext(...) was never extracted. MatlabInput._extra_context() now translates the message with the block's i18n service and passes it to the template (escapejs). {% trans %} would still use Django's global catalog.
  4. Tests:
    • Problem's i18n_utils.js is UMD, so Karma (which loads raw files, not the bundle) can cover both the namespace path and the fallback. Webpack still uses its CommonJS export.
    • Video and Problem now also check that the catalog loads before the block JS.
    • Tests for the Codejail status and parse errors and for Video's public_view catalog.
  5. Problem extraction: spec and spec_helpers are added to ignore_dirs, so the new spec's test strings aren't sent to translators.

Out of scope

Testing

  • make extract_translations compared against main:
    • Problem gains the Matlab message.
    • Video loses 3 strings that only came from its tests.
    • No other block changes, and there are no strings from tests or specs.
  • Manual, in openedx-platform (Tutor dev, master) with xblocks-core installed. I ran the steps below on xblocks-core main and on this branch, signed in as a learner whose language is ar.
    1. Run make pull_xblock_translations, then check that lms/static/js/xblock.v1-i18n/xblock_<block>/ar.js defines XBlock<Block>I18N for video, problem and annotatable.
    2. Open each block with the language set to ar. The catalog script should load before the block JS, and the strings should be translated.
Check main This PR
xblock_{video,problem,annotatable}/ar.js generated (LMS and CMS) ❌ none ✅ all 6, each defining XBlock*I18N
Block namespace defined on the page ❌ ✅
Catalog loads before the block JS ❌ no catalog ✅ e.g. xblock_video/ar.js → video-xblock.js
LTI launch button (lti_form.html) ❌ Press to Launch ✅ انقر للتشغيل

The platform checkout I used has no ar catalog of its own, so global gettext() returned English on both branches. All Arabic here comes from the block catalogs, and the slash-n-burn branches wouldn't change the result. @irfanuddinahmad's review covers the on-screen JS strings (Video controls, Problem status, Annotatable toggle), comparing this PR with 2.0.0.

Merge checklist:

  • Version bumped (N/A: semantic-release)
  • Changelog record added (N/A: semantic-release)
  • Documentation updated (not only docstrings)
  • Fixup commits are squashed away
  • Unit tests added/updated
  • Manual testing instructions provided
  • Noted any: Concerns, dependencies, migration issues, deadlines, tickets

🤖 Generated with Claude Code

farhan and others added 4 commits November 19, 2026 18:25
The blocks' JS called the global gettext(), which only looks up the
platform's djangojs catalog. Those strings stop translating once the
built-in copies are removed from openedx-platform. Setting
i18n_js_namespace makes compile_xblock_translations build a per-block
JS catalog, and each view now loads it before the block's own JS.
The JS reads gettext/ngettext from that namespace and falls back to
the global catalog.

Part of openedx#332.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
tests/views/bookmark_button.js was being extracted into the block's
catalog, which sent test-only strings to translators.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
remote_exec used Django's global gettext, which has no msgids for
xblock_problem once the platform copy is gone. The remote exec
function is pluggable through CODE_JAIL_REST_SERVICE_REMOTE_EXEC with
a (data) signature, so it now only marks the messages (gettext_noop).
safe_exec gets an optional gettext argument and translates
the Codejail service errors with it. The capa call sites pass the
block's i18n service.

Part of openedx#332.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without it, "Press to Launch" looks up Django's global catalog, which
loses the msgid once openedx-platform drops its LTI copy.

Part of openedx#332.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@irfanuddinahmad irfanuddinahmad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested locally (Tutor nightly, ar learner and author). The PR's files were overlaid on xblocks-core 2.0.0, and both bundles were rebuilt. The platform checkout has no ar catalog of its own, so any Arabic below comes from the block catalogs.

Check This PR 2.0.0 (control)
Video controls (LMS) Arabic (تشغيل), catalog loads before video-xblock.js English
Annotatable toggle after click (JS) أظهِر الملاحظات التوضيحية Show Annotations
Problem dropdown status (JS unsubmitted) لم يتم التقديم, catalog loads before the bundle unsubmitted
LTI preview_handler انقر للتشغيل Press to Launch
Studio preview, scripted problem without a codejail service ...خدمة Codejail API غير متوفرة n/a (2.0.0 predates #222)
Studio previews (Video, Problem) load /static/studio/js/xblock.v1-i18n/.../ar.js no catalog

No JS errors. In every case, global gettext() returned English, so the translations aren't coming from the platform.

Still bypassing the block catalog (these files aren't in the diff, so I'm noting them here):

  1. capa/static/js/capa/drag_and_drop/*.js (5 strings). The platform has no copy: findstatic resolves them to site-packages/xblock_problem/capa/static, and master has no js/capa/ files. make extract_translations already puts these strings in the Problem catalog, but the JS calls the global gettext, so their translations are never shown. The Annotatable-style local gettext helper would fix it, and the catalog is already on the page. The "Out of scope" note in the description needs updating too.
  2. capa/templates/matlabinput.html:84 has an inline gettext("Your code is still being run. Refresh the page to see updates."). That string isn't extracted into any catalog. {% trans "..." as msg %} with {{ msg|escapejs }} would fix it.

Both are pre-existing gaps, so they aren't regressions from this PR. But Closes #332 would close #332 while they're still open, so either fix them here or keep #332 open.

Also: the version and changelog checklist items can be marked N/A, because semantic-release handles them.

Generated using Claude Code

except (CodejailServiceUnavailable, CodejailServiceStatusError, CodejailServiceParseError) as err:
if gettext is None or not err.args:
raise
raise type(err)(gettext(str(err.args[0]))) from err.__cause__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line fails the quality job: pylint E7610 translation-of-non-string. Translating at runtime is intended here (the messages are marked with gettext_noop), so a # pylint: disable=translation-of-non-string with a short comment is enough.

Nit: from err would keep the original exception in the chain, which helps debugging.

Generated using Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 63af35d8: added the translation-of-non-string disable with a comment, and switched to from err. quality should pass now (pylint src is clean locally).

Generated using Claude Code

CodeMirror: 'codemirror',
'edx.HtmlUtils': 'edx-ui-toolkit/js/utils/html-utils'
'edx.HtmlUtils': 'edx-ui-toolkit/js/utils/html-utils',
gettext: [i18nUtils, 'gettext'],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem's Karma tests don't cover this. karma.conf.js loads the raw static/js/*.js files with the spec_helpers/i18n.js global stub, not the webpack bundle, so i18n_utils.js never runs there (Video's Karma does run through webpack). I checked it in the browser instead, and it works (see the summary). A small spec for i18n_utils.js, covering both the namespace path and the fallback, would close the gap.

Generated using Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done in 81d4b7ca. Problem's Karma is 0.13, which can't load ES modules, so i18n_utils.js is now UMD: webpack still uses the CommonJS export, and Karma loads it as a script. spec/i18n_utils_spec.js covers both the namespace path and the global fallback, for gettext and ngettext (114 specs pass).

Generated using Claude Code

Comment thread src/xblock_video/block.py
"""
return Fragment(f'<div data-metadata="{json.dumps(self.editable_metadata_fields, cls=EdxJSONEncoder)}"></div>')

def _add_i18n_js_catalog(self, fragment):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: this method and its 4 tests are copied three times (Video, Problem, Annotatable). A single helper in xblocks_core.legacy_utils would remove the duplication. Also, only Annotatable tests that the catalog loads before the block JS in student_view; Video and Problem could get the same test.

Generated using Claude Code

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd keep the per-block copies. legacy_utils is marked "DO NOT USE IN NEW XBLOCKS", and xblocks_core/__init__.py asks to keep shared code minimal so each block stays independent (it suggests upstreaming to xblock instead). It's 10 lines, and each copy is tested with its block. If it grows, XBlock itself would be the better home. I took the other half: Video and Problem now check that the catalog loads before the block JS too (87451b9b).

Generated using Claude Code

farhan and others added 2 commits November 20, 2026 11:17
Chaining from the untranslated exception keeps the English message
and its cause for debugging. Translating a non-literal is intended
here: the messages are marked with gettext_noop in remote_exec.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The drag-and-drop input JS is served from xblock_problem, not the
platform, and called the global gettext(), so its strings were never
translated. It now reads Problem's namespaced catalog, which the view
already loads, and falls back to the global catalog.

Part of openedx#332.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@irfanuddinahmad

Copy link
Copy Markdown
Contributor

Correction to my review above: Closes #332 is fine. Checked against what #332 actually lists, #336 covers all three gaps (JS namespace, remote_exec, lti_form), and each one showed up translated in my local ar test.

The drag_and_drop JS and matlabinput.html items aren't part of #332. They're older gaps that the platform doesn't cause, and #332 already set Problem templates aside for a separate look. I've moved them to #340, together with the Problem templates rendered through render_to_string. They don't block this PR or the slash-n-burn PRs.

The quality failure at safe_exec.py:198 still needs fixing before merge.

Generated using Claude Code

farhan and others added 3 commits November 20, 2026 11:18
The message was an inline JS gettext() in matlabinput.html, so it was
never extracted into Problem's catalog and only looked up the global
one. Capa templates render with Django's global translation, so
{% trans %} would bypass the block catalog too. MatlabInput now
translates it with the block's i18n service and passes it to the
template.

Part of openedx#332.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Problem's Karma loads the raw static files instead of the webpack
bundle, so the namespaced lookup never ran in tests. The module is now
UMD: webpack still uses its CommonJS export, and Karma can load it as
a script and test both the block-catalog path and the fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Only Annotatable checked the order. The catalog defines the namespace
the bundle reads when it loads, so it must come first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farhan

farhan commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough testing, @irfanuddinahmad. Both gaps are fixed here, so Closes #332 holds:

  1. Drag-and-drop JS (a73739f5): you're right, the platform has no copy. base_image.js and draggable_events.js now use a local gettext that reads XBlockProblemI18N, with the global fallback.
  2. matlabinput.html (705fa856): I went with Python rather than {% trans %}. Capa templates render with Django's global translation, so {% trans %} would still bypass the block catalog. MatlabInput._extra_context() now translates the message with capa_system.i18n, and the template outputs it with escapejs. The msgid is now extracted into Problem's catalog.

I've updated the description: the out-of-scope note is corrected, the version and changelog items are N/A, and the new commits are listed.

Generated using Claude Code

These lines changed in this PR but had no tests: the remote_exec error
paths whose messages now use gettext_noop, and the catalog loading in
Video's public_view.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farhan
farhan marked this pull request as ready for review November 20, 2026 07:41
The new i18n_utils spec calls gettext/ngettext with test strings
("point"/"points"), which extraction picked up and would have sent to
translators.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@irfanuddinahmad irfanuddinahmad left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-tested at c18b9704 (ar, Tutor nightly, overlaid on 2.0.0):

  • All the earlier checks still pass: Video, Annotatable and Problem JS, the LTI form, the Studio codejail error, and the Studio catalogs.
  • The drag-and-drop image alt text is now translated from the block catalog; the global gettext still returns English.
  • The Matlab msgid is extracted into the Problem catalog.
  • The Problem spec/ folders no longer appear in the extraction.

Agreed on keeping the per-block helper copies, and on translating the Matlab message in Python. I've trimmed #340 to the remaining template item.

Generated using Claude Code

The view-ordering tests already cover adding the catalog URL, so the
separate "adds catalog url" tests are dropped. The two "no catalog"
cases are merged into one subTest, and Video's student and public view
tests share one helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farhan farhan changed the title fix: translate strings that bypass the block catalog (JS, codejail, lti_form) fix: use each block's own translations for its JS and other strings Nov 20, 2026
@farhan

farhan commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @feanil, @kdmccormick and @OmarIthawi, this PR is ready for review. A look when you get a chance would be great, thanks!

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.

i18n: strings that bypass the block catalog (JS namespace, remote_exec, lti_form)

2 participants