WP_Script_Modules::print_script_module_translations()
- Since
- 7.0.0
- Source
wp-includes/class-wp-script-modules.php:383
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
- Scaling
- Scales with input
- Instructions
- 9–10
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with how much data it finds.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 64.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 9–10 | ->get_sorted_dependencies() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 64 | 9–10 | 6 | |
| 8.5 | 64 | 9–10 | 6 | |
| 8.4 | 64 | 9–10 | 6 | 1 more instruction than PHP 8.3 |
| 8.3 | 63 | 9–10 | 6 | |
| 8.2 | 63 | 9–10 | 6 | |
| 8.1 | 63 | 9–10 | 6 | |
| 7.4 | 63 | 9–10 | 6 |
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
- load_script_module_textdomain()Loads the translation data for a given script module ID and text domain.
- wp_json_encode()Encodes a variable into JSON, with some confidence checks.
- wp_script_is()Determines whether a script has been added to the queue.
- wp_scripts()Initializes $wp_scripts if it has not been set.
- wp_print_inline_script_tag()Prints an inline script tag.
- WP_Script_Modules::get_sorted_dependencies()Sorts the given script module identifiers based on their dependencies.
- wp_scripts()::do_items()
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.
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.