Title: WP_Script_Modules::print_script_module_translations
Published: May 20, 2026

---

# WP_Script_Modules::print_script_module_translations()

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#description)
 * [Source](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#wp--skip-link--target)

Prints translations for all enqueued script modules.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#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()](https://developer.wordpress.org/reference/classes/WP_Script_Modules/set_translations/).

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#source)󠁿

    ```php
    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 ) );
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-script-modules.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/class-wp-script-modules.php#L383)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-script-modules.php#L383-L421)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#related)󠁿

| Uses | Description | 
| [load_script_module_textdomain()](https://developer.wordpress.org/reference/functions/load_script_module_textdomain/)`wp-includes/l10n.php` |

Loads the translation data for a given script module ID and text domain.

  | 
| [WP_Script_Modules::get_sorted_dependencies()](https://developer.wordpress.org/reference/classes/wp_script_modules/get_sorted_dependencies/)`wp-includes/class-wp-script-modules.php` |

Sorts the given script module identifiers based on their dependencies.

  | 
| [wp_print_inline_script_tag()](https://developer.wordpress.org/reference/functions/wp_print_inline_script_tag/)`wp-includes/script-loader.php` |

Prints an inline script tag.

  | 
| [wp_scripts()](https://developer.wordpress.org/reference/functions/wp_scripts/)`wp-includes/functions.wp-scripts.php` |

Initializes $wp_scripts if it has not been set.

  | 
| [wp_script_is()](https://developer.wordpress.org/reference/functions/wp_script_is/)`wp-includes/functions.wp-scripts.php` |

Determines whether a script has been added to the queue.

  | 
| [wp_json_encode()](https://developer.wordpress.org/reference/functions/wp_json_encode/)`wp-includes/functions.php` |

Encodes a variable into JSON, with some confidence checks.

  |

[Show 1 more](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wp_script_modules/print_script_module_translations/?output_format=md#changelog)󠁿

| Version | Description | 
| [7.0.0](https://developer.wordpress.org/reference/since/7.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwp_script_modules%2Fprint_script_module_translations%2F)
before being able to contribute a note or feedback.