wppaste
WordPress

WP_Script_Modules::print_script_module_translations()

Since
7.0.0
Source
wp-includes/class-wp-script-modules.php:383
Prints translations for all enqueued script modules.

Description

Outputs inline <script> tags that call wp.i18n.setLocaleData() with the translated strings for each script module. This must run before the script modules execute.

Auto-detects the text domain and translation path for each module from its source URL. Modules whose text domain or path differs from the defaults can opt into a specific domain/path via WP_Script_Modules::set_translations().

Compatibility

WordPress
since 7.0.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in 2 of the 5 tracked releases, added in 7.0.0, and compiles on PHP 7.4 through 8.6-dev.

Performance profile

How much work a call to WP_Script_Modules::print_script_module_translations() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
9–10

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Script_Modules::print_script_module_translations() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always9–10->get_sorted_dependencies()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev649–106
8.5649–106
8.4649–1061 more instruction than PHP 8.3
8.3639–106
8.2639–106
8.1639–106
7.4639–106

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 7

Source code

	public function print_script_module_translations(): void {		// Collect all module IDs that will be on the page (enqueued + their dependencies).		$module_ids = $this->get_sorted_dependencies( $this->queue ); 		$set_locale_data_js_function = <<<'JS'		( domain, translations ) => {			const localeData = translations.locale_data[ domain ] || translations.locale_data.messages;			localeData[""].domain = domain;			wp.i18n.setLocaleData( localeData, domain );		}		JS; 		foreach ( $module_ids as $id ) {			$domain = $this->registered[ $id ]['textdomain'] ?? 'default';			$path   = $this->registered[ $id ]['translations_path'] ?? ''; 			$json_translations = load_script_module_textdomain( $id, $domain, $path ); 			if ( ! $json_translations ) {				continue;			} 			$output    = sprintf(				'( %s )( %s, %s );',				$set_locale_data_js_function,				wp_json_encode( $domain, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ),				$json_translations			);			$script_id = "wp-script-module-translation-data-{$id}";			$output   .= "\n//# sourceURL=" . rawurlencode( $script_id ); 			// Ensure wp-i18n is printed; the inline script below relies on wp.i18n.setLocaleData().			if ( ! wp_script_is( 'wp-i18n', 'done' ) ) {				wp_scripts()->do_items( array( 'wp-i18n' ) );			} 			wp_print_inline_script_tag( $output, array( 'id' => $script_id ) );		}	}

Changelog

Introduced in 7.0.0. Unchanged from 7.0.4 through 7.1.0.

  1. 7.0.4
  2. 7.1.0

Signature, return type and hooks compared across 2 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-script-modules.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.