I18N: Add translation support for script modules#77214
I18N: Add translation support for script modules#77214manzoorwanijk wants to merge 3 commits intotrunkfrom
Conversation
Add polyfill for `wp_set_script_module_translations()` and update build templates to call it for script modules that depend on `wp-i18n`. Script modules have no mechanism to load i18n translation data, leaving strings in ES modules untranslated regardless of site language. This affects admin pages built as script modules like Connectors and Fonts. Changes: - Add polyfill in lib/compat/wordpress-7.1/script-modules.php with wp_set_script_module_translations(), load_script_module_textdomain(), and gutenberg_print_script_module_translations() - Update routes-registration.php.template to set translations for route content and route modules that use wp-i18n - Update module-registration.php.template to set translations for @wordpress/* script module packages that use wp-i18n All calls guarded with function_exists() for forward compatibility with the Core implementation. See https://core.trac.wordpress.org/ticket/65015.
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Flaky tests detected in cf9468d. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/24244336065
|
Move the script modules translation polyfill from the wordpress-7.1 compat directory to wordpress-7.0 so the fix targets the 7.0 release, which is where the new admin pages (Connectors, Fonts) using script modules were introduced. Also moves the backport changelog entry accordingly. See https://core.trac.wordpress.org/ticket/65015.
What?
See https://core.trac.wordpress.org/ticket/65015
Related PR: WordPress/wordpress-develop#11543
Adds translation support for script modules so that strings using
__()from@wordpress/i18ninside script modules are properly translated.Why?
Script modules (ES modules registered via
wp_register_script_module()) have no mechanism to load i18n translation data. Classic scripts usewp_set_script_translations()to load JSON translation files and callwp.i18n.setLocaleData(), but no equivalent exists for script modules.This means strings in any script module remain in English regardless of the site language. In WordPress 7.0+, this affects the new admin pages built as script modules — Connectors and Fonts — where strings like "Set up", "If the connector you need is not listed", and "All of your API keys and credentials are stored here" are never translated.
The Core side of this fix adds
wp_set_script_module_translations()and the underlying infrastructure toWP_Script_Modules. This PR provides:wp_set_script_module_translations()for modules and routes that usewp-i18nHow?
Polyfill (
lib/compat/wordpress-7.0/script-modules.php)wp_set_script_module_translations()— stores text domain and path per module ID in a global arrayload_script_module_textdomain()— resolves the module's source URL to a relative path, computes an MD5 hash, and loads the JSON translation file (same logic asload_script_textdomain())gutenberg_print_script_module_translations()— hooked toadmin_print_footer_scripts(priority 11) andwp_footer(priority 21), outputs inline<script>tags callingwp.i18n.setLocaleData()after classic scripts likewp-i18nare loaded but before deferred ES modules executegutenberg_get_script_module_src()— helper to get a module's raw source URL, usingWP_Script_Modules::get_registered_src()when available or falling back to reflection for older WP versionsAll functions are guarded with
function_exists()checks so they yield to Core's implementation when available.Build template updates
routes-registration.php.template— after registering each route content/route script module, callswp_set_script_module_translations()if the module's asset file listswp-i18nin its dependenciesmodule-registration.php.template— same pattern for@wordpress/*script module packagesAll template calls are guarded with
function_exists( 'wp_set_script_module_translations' )for safety.Testing Instructions
Language packs don't include JSON translation files for script modules yet, so testing requires a small mu-plugin to provide test translations.
1. Set up the test environment:
npm run buildtest-script-module-translations.phpin the repo root.wp-env.override.jsonin the repo root to map the mu-plugin:{ "mappings": { "wp-content/mu-plugins/test-script-module-translations.php": "./test-script-module-translations.php" } }npm run wp-env stop && npm run wp-env starttest-script-module-translations.php
2. Verify the Connectors page:
/wp-admin/options-connectors.php)3. Verify infrastructure in page source:
js-module-translations<script>tags with IDs likewp/routes/connectors-home/content-js-module-translationsappear after the classic scripts and before the<script type="module">tagwp.i18n.setLocaleData()calls with the translation data4. Clean up:
test-script-module-translations.phpand.wp-env.override.json(or remove the mapping), then restart the environmentTesting Instructions for Keyboard
No UI changes — this is infrastructure-only. Keyboard navigation is unaffected.
Screenshots or screencast
Use of AI Tools
This PR was authored with assistance from Claude Code (Claude Opus 4.6).