diff --git a/src/wp-includes/class-wp-script-modules.php b/src/wp-includes/class-wp-script-modules.php index 058e5f0eef0e6..2fbb92ee6d518 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 7.0.0 + * @var array + */ + private array $translations = array(); + /** * Registers the script module if no script module with that script module * identifier has already been registered. @@ -328,6 +339,80 @@ 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 7.0.0 + * + * @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 `