Make WordPress Core


Ignore:
Timestamp:
04/27/2026 06:29:17 PM (7 weeks ago)
Author:
westonruter
Message:

I18N: Add translation support for script modules.

Add automatic translation loading for script modules (ES modules), so strings using __() and friends from @wordpress/i18n can be translated at runtime. This brings classic script i18n parity to script modules registered via wp_register_script_module(), which previously had no way to load translation data, leaving strings untranslated on screens like Connectors and Fonts that are built as script modules.

At the admin_print_footer_scripts and wp_footer actions, every enqueued script module and its dependencies are walked, the translation chunk is loaded for each, and an inline <script> calls wp.i18n.setLocaleData() so translations are available before deferred modules execute. Note there is currently a runtime dependency on the wp-i18n classic script, which is printed just-in-time if not already enqueued. This coupling is to be removed in a future release.

Public API:

  • WP_Script_Modules::set_translations() stores the text domain (and optional path) per registered module to override the text domain and path. A global wp_set_script_module_translations() function is added as a wrapper around wp_script_modules()->set_translations().
  • WP_Script_Modules::get_registered() obtains a registered module's data. See #60597.
  • WP_Script_Modules::print_script_module_translations() emits inline wp.i18n.setLocaleData() calls after classic scripts load but before modules execute.
  • load_script_module_textdomain() loads the translation data for a given script module ID and text domain.
  • The existing load_script_textdomain_relative_path filter gains a third $is_module parameter so callers can distinguish classic-script and script-module lookups when resolving translation paths.

PHPStan types are also added in WP_Script_Modules. See #64238.

Developed in https://github.com/WordPress/wordpress-develop/pull/11543

Props manzoorwanijk, westonruter, jsnajdr, jonsurrell, mukesh27, peterwilsoncc, 369work, desrosj, sabernhardt, nilambar, jorgefilipecosta, malayladu.
See #64238, #60597.
Fixes #65015.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/script-modules.php

    r62081 r62278  
    137137function wp_deregister_script_module( string $id ) {
    138138    wp_script_modules()->deregister( $id );
     139}
     140
     141/**
     142 * Overrides the text domain and path used to load translations for a script module.
     143 *
     144 * Translations for script modules are loaded automatically from the default
     145 * text domain and language directory. Use this function only when a module's
     146 * text domain differs from `'default'` or when translation files live outside
     147 * the standard location, for example plugin modules using their own text domain.
     148 *
     149 * @since 7.0.0
     150 *
     151 * @see WP_Script_Modules::set_translations()
     152 *
     153 * @param string $id     The identifier of the script module.
     154 * @param string $domain Optional. Text domain. Default 'default'.
     155 * @param string $path   Optional. The full file path to the directory containing translation files.
     156 * @return bool True if the text domain was registered, false if the module is not registered.
     157 */
     158function wp_set_script_module_translations( string $id, string $domain = 'default', string $path = '' ): bool {
     159    return wp_script_modules()->set_translations( $id, $domain, $path );
    139160}
    140161
Note: See TracChangeset for help on using the changeset viewer.