Skip to content

feat: translate block JS strings with each block's own catalog - #93

Merged
farhan merged 3 commits into
openedx:mainfrom
farhan:farhan/js-block-i18n-catalogs
Nov 25, 2026
Merged

farhan merged 3 commits into
openedx:mainfrom
farhan:farhan/js-block-i18n-catalogs

Conversation

@farhan

@farhan farhan commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

In short: Some text in the ImageModal, SQL Grader, Submit and Compare and Free-text Response blocks is set by their JavaScript as hardcoded English, so learners never see it translated. This PR translates it with each block's own catalog.

What this PR does

Same pattern as openedx/xblocks-core#336 (Annotatable) and openedx/xblock-drag-and-drop-v2#365:

  • Each block sets i18n_js_namespace (XBlockImageModalI18N, XBlockSqlGraderI18N, XBlockSubmitAndCompareI18N, XBlockFreeTextResponseI18N), so openedx-platform builds a JS catalog for it.
  • build_fragment() loads that catalog with get_javascript_i18n_catalog_url() before the block's JS.
  • The JS defines a local gettext that reads the namespace and falls back to the global catalog.
Block Strings
imagemodal Zoom In, Zoom Out
sql_grader Empty result, Correct, Incorrect, Error: {error}
submit_and_compare Resubmit, (Loading...)
freetextresponse Submitting..., Saving...

Out of scope

  • audio, feedback and qualtricssurvey: their JS shows no text of its own. Their text is rendered and translated on the server.
  • The new strings will only be translated after they reach Transifex.

Testing

  • New unit tests per block: namespace, catalog loaded before the block JS, no catalog when the i18n service can't provide one.
  • make extract_translations: the 10 strings above are now extracted from JS. Only Zoom In, Correct and Incorrect exist in the published openedx-translations catalogs; the other 7 are new msgids. No test or vendor strings.
  • Manual testing in Tutor with an ar learner:
Check (Tutor dev, ar) Result
xblock.v1-i18n/<block>/ar.js generated for all 4 blocks (LMS and CMS), each defining XBlock*I18N ✅
Catalog loads before the block JS (LMS) ✅ e.g. imagemodal/ar.js → imagemodal/public/view.js
Studio unit page loads all 4 catalogs ✅ all 200
Image Modal: zoom out, JS sets Zoom In ✅ تكبير
SQL Grader result: Correct / Incorrect (mocked response) ✅ صحيح / غير صحيح
SQL Grader error: Error: {error} ✅ filled in, e.g. Error: near "SELEC": syntax error
New msgids (Zoom Out, Empty result, Error: {error}, Resubmit, (Loading...), Submitting..., Saving...) English fallback, as expected until Transifex
Submit and Compare and Free-text Response submit ✅ 200, no console errors
  • My platform checkout has no node_modules, so I initialized the blocks with XBlock.initializeBlocks.
  • Codejail isn't configured locally, so SQL Grader's submit_query response was mocked in the page.

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.09%. Comparing base (7ecabf8) to head (cf5071c).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #93      +/-   ##
==========================================
+ Coverage   82.68%   83.09%   +0.41%     
==========================================
  Files          48       48              
  Lines        1461     1497      +36     
  Branches      117      125       +8     
==========================================
+ Hits         1208     1244      +36     
  Misses        220      220              
  Partials       33       33              
Flag Coverage Δ
unittests 83.09% <100.00%> (+0.41%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

The JS of ImageModal, SQL Grader, Submit and Compare and Free-text
Response showed hardcoded English strings (zoom button, grading result,
Resubmit, Submitting/Saving notices), so learners never saw them
translated.

Each of these blocks now sets i18n_js_namespace, loads its JS
translation catalog before its own JS, and wraps those strings with a
local gettext that reads the block's namespace and falls back to the
global catalog. This is the same pattern as openedx/xblocks-core#336.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farhan
farhan force-pushed the farhan/js-block-i18n-catalogs branch from 28e1007 to cadfe67 Compare November 23, 2026 10:45
Codecov flagged the "no JS" branch of build_fragment() as untested.
These tests check that no catalog is loaded when the fragment has no
JS of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@farhan
farhan marked this pull request as ready for review November 24, 2026 06:28

@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, xblocks-extra built from fb8aa53, translations pulled with pull_xblock_translations):

  • All 4 catalogs load before the block JS in the LMS, and in Studio's author_view (all 200).
  • Image Modal Zoom In → تكبير and SQL Grader Correct/Incorrect → صحيح/غير صحيح come from the block catalog. The global gettext still returns English for them.
  • Submit and Compare and Free-text Response submit with no page errors. Their strings are new, so they show English for now.
  • 194 tests pass, and ruff is clean.

Two small things:

  1. Submit and Compare still has a hardcoded '(Loading...)' (see the inline comment).
  2. The description says 4 msgids are new, but make extract_translations on main and on this branch shows 6. Error: {error} (SQL Grader) and Saving... (Free-text Response) aren't in the published catalogs either. Only Zoom In, Correct and Incorrect already have translations.

Unrelated to this PR: importing a course with SQL Grader logs a search-indexing error (__proxy__ is not JSON serializable). XBlock's default index_dictionary() uses the display_name default as content_type, and SQL Grader's is a gettext_lazy string. Happy to open a separate issue.

Generated using Claude Code

your_answer.css('display', 'block');
expert_answer.css('display', 'block');
submit_button.val('Resubmit');
submit_button.val(gettext('Resubmit'));

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 block's JS also sets '(Loading...)' a few lines up (line 87, pre_submit()), which learners see on every submit:

problem_progress.text(gettext('(Loading...)'));

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, thanks. Fixed in cf5071c:

src/submit_and_compare/public/view.js:87

problem_progress.text(gettext('(Loading...)'));

Learners see "(Loading...)" on every submit, so it should come from
the block's catalog like the other JS strings.

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

farhan commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks for the review and for testing this, @irfanuddinahmad.

  1. (Loading...): fixed in cf5071c.
  2. You're right. I compared against the wrong catalog. With the fix there are 7 new msgids (Zoom Out, Empty result, Error: {error}, Resubmit, (Loading...), Submitting..., Saving...). Only Zoom In, Correct and Incorrect are already in openedx-translations. I've updated the description.

On the SQL Grader indexing error: agreed it's unrelated to this PR. A separate PR would be better than an issue, since the fix should be small.

@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 cf5071c: (Loading...) is now extracted and goes through the block catalog, the description matches the extraction, and all 4 blocks still work in Tutor (ar). LGTM.

Generated using Claude Code

@farhan
farhan merged commit c35a768 into openedx:main Nov 25, 2026
8 checks passed
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.

2 participants