From e23d4f27d7fb133aea84c8f3cfdc7be24ef5aade Mon Sep 17 00:00:00 2001 From: Manzoor Wani Date: Fri, 10 Apr 2026 07:06:34 +0530 Subject: [PATCH 01/11] I18N: Add translation support for script modules. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/wp-includes/class-wp-script-modules.php | 111 +++++++++++++- src/wp-includes/l10n.php | 145 ++++++++++++++++++ src/wp-includes/script-modules.php | 24 +++ .../tests/script-modules/wpScriptModules.php | 111 ++++++++++++++ 4 files changed, 390 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 058e5f0eef0e6..e9c7af94a46bb 100644 --- a/src/wp-includes/class-wp-script-modules.php +++ b/src/wp-includes/class-wp-script-modules.php @@ -81,6 +81,17 @@ class WP_Script_Modules { */ private $modules_with_missing_dependencies = array(); + /** + * Holds translation data for script modules, keyed by script module identifier. + * + * Each entry contains 'domain' and 'path' keys for the text domain + * and the path to translation files respectively. + * + * @since x.y.z + * @var array + */ + private $translations = array(); + /** * Registers the script module if no script module with that script module * identifier has already been registered. @@ -328,6 +339,74 @@ public function deregister( string $id ) { unset( $this->registered[ $id ] ); } + /** + * Sets translated strings for a script module. + * + * Works similar to {@see WP_Scripts::set_translations()} but for script modules. + * The translations will be loaded and output as inline scripts before + * the script modules are printed, calling `wp.i18n.setLocaleData()`. + * + * @since x.y.z + * + * @param string $id The identifier of the script module. + * @param string $domain Optional. Text domain. Default 'default'. + * @param string $path Optional. The full file path to the directory containing translation files. + * @return bool True if the text domain was registered, false if the module is not registered. + */ + public function set_translations( string $id, string $domain = 'default', string $path = '' ): bool { + if ( ! isset( $this->registered[ $id ] ) ) { + return false; + } + + $this->translations[ $id ] = array( + 'domain' => $domain, + 'path' => $path, + ); + + return true; + } + + /** + * Prints translations for all enqueued script modules that have translations set. + * + * Outputs inline `