I18N: Add translation support for script modules#11543
I18N: Add translation support for script modules#11543manzoorwanijk wants to merge 2 commits intoWordPress:trunkfrom
Conversation
Add `wp_set_script_module_translations()` and supporting infrastructure to enable i18n for script modules (ES modules), mirroring the existing `wp_set_script_translations()` for classic scripts. Script modules registered via `wp_register_script_module()` currently have no way to load translation data, leaving strings untranslated on pages like Connectors and Fonts that are built as script modules. New public API: - `wp_set_script_module_translations()` in script-modules.php - `load_script_module_textdomain()` in l10n.php New methods on `WP_Script_Modules`: - `set_translations()` — stores text domain per module - `get_registered_src()` — public accessor for module source URL - `print_script_module_translations()` — outputs inline setLocaleData() calls after classic scripts load but before modules execute See #65015.
|
Hi @manzoorwanijk! 👋 Thank you for your contribution to WordPress! 💖 It looks like this is your first pull request to No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description. Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this GitHub repository mirrors. Please feel free to open pull requests to work on any contribution you are making. More information about how GitHub pull requests can be used to contribute to WordPress can be found in the Core Handbook. Please include automated tests. Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook. If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook. The Developer Hub also documents the various coding standards that are followed:
Thank you, |
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
|
Thanks for the PR! Can you expose which AI tool do you use in PR? |
Updated. Thanks |
Add two integration tests verifying the full translation flow: - test_print_script_module_translations_outputs_set_locale_data covers the happy path, asserting that the inline script contains the expected module ID, the wp.i18n.setLocaleData() call, and the translated string. - test_print_script_module_translations_includes_dependencies covers translations for dependency modules (not just directly enqueued ones). Both tests use the pre_load_script_translations filter to provide mock translation data inline, so no fixture JSON files are needed. See #65015.
Summary
Adds translation support for script modules (
WP_Script_Modules), mirroring the existingwp_set_script_translations()infrastructure for classic scripts.Script modules (ES modules registered via
wp_register_script_module()) currently have no way to load i18n translation data. This means any strings using__()from@wordpress/i18ninside script modules remain untranslated. This affects the new admin pages in WordPress 7.0+ that are built as script modules, including Connectors and Fonts.Trac ticket: https://core.trac.wordpress.org/ticket/65015
Changes
New public API
wp_set_script_module_translations( $id, $domain, $path )— registers a text domain for a script module (inscript-modules.php)load_script_module_textdomain( $id, $domain, $path )— loads translation JSON files for a script module, resolving the file path from the module's source URL (inl10n.php)WP_Script_Modulesclass changesset_translations()method — stores text domain and translation path per moduleget_registered_src()method — public accessor for a module's raw source URL (used byload_script_module_textdomain())print_script_module_translations()method — outputs inline<script>tags callingwp.i18n.setLocaleData()for each enqueued module with translationsprint_script_module_translations()at priority 11 onadmin_print_footer_scripts(after classic scripts loadwp-i18nat priority 10) and priority 21 onwp_footer(afterwp_print_footer_scriptsat priority 20)Automatic translation setup
wp_default_script_modules()now callswp_set_script_module_translations()for all@wordpress/*script modules that listwp-i18nin their dependenciesTests
set_translations(),get_registered_src(),wp_set_script_module_translations(), andprint_script_module_translations()How it works
The flow mirrors classic script translations:
wp_set_script_module_translations()is called after registering a script module, storing the text domain and optional pathprint_script_module_translations()iterates over all enqueued modules (and their dependencies) that have translations setload_script_module_textdomain()which resolves the module's source URL to a relative path, computes an MD5 hash, and looks for the corresponding JSON translation file (same naming convention as classic scripts)<script>callingwp.i18n.setLocaleData()with the translated stringsRelated
wp_set_script_module_translations()for route and package script modules, and adds a polyfill for WP < 7.1.Test plan
Automated tests
Run the script module tests, which now include end-to-end coverage for translation printing:
The new tests
test_print_script_module_translations_outputs_set_locale_dataandtest_print_script_module_translations_includes_dependenciesuse thepre_load_script_translationsfilter to provide mock translations (no fixture files needed) and assert that the rendered inline script contains the expectedwp.i18n.setLocaleData()call with the translated data.Manual verification (optional)
Language packs don't include JSON translation files for the new script modules yet, so manual end-to-end verification requires a small mu-plugin to provide mock translations. Paired with the companion Gutenberg PR (#77214), no
wp_set_script_module_translations()registration is needed — the build templates call it automatically.1. Set up the test environment:
src/wp-content/mu-plugins/test-script-module-translations.php(create themu-pluginsdirectory if it doesn't exist):test-script-module-translations.php
2. Verify:
/wp-admin/options-connectors.php)js-module-translations— inline<script>tags with IDs likewp/routes/connectors-home/content-js-module-translationsshould appear after the classic scripts3. Clean up:
src/wp-content/mu-plugins/test-script-module-translations.phpUse of AI Tools
AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus 4.6
Used for: Implementation, tests, and PR description were authored with AI assistance; reviewed and tested by me.