WP_Scripts::print_translations( string $handle, bool $echo = true )

Prints translations set for a specific handle.


Description Description


Parameters Parameters

$handle

(string) (Required) Name of the script to add the inline script to. Must be lowercase.

$echo

(bool) (Optional) Whether to echo the script instead of just returning it.

Default value: true


Top ↑

Return Return

(string|false) Script on success, false otherwise.


Top ↑

Source Source

File: wp-includes/class.wp-scripts.php

	public function print_translations( $handle, $echo = true ) {
		if ( ! isset( $this->registered[ $handle ] ) || empty( $this->registered[ $handle ]->textdomain ) ) {
			return false;
		}

		$domain = $this->registered[ $handle ]->textdomain;
		$path   = $this->registered[ $handle ]->translations_path;

		$json_translations = load_script_textdomain( $handle, $domain, $path );

		if ( ! $json_translations ) {
			// Register empty locale data object to ensure the domain still exists.
			$json_translations = '{ "locale_data": { "messages": { "": {} } } }';
		}

		$output = <<<JS
( function( domain, translations ) {
	var localeData = translations.locale_data[ domain ] || translations.locale_data.messages;
	localeData[""].domain = domain;
	wp.i18n.setLocaleData( localeData, domain );
} )( "{$domain}", {$json_translations} );
JS;

		if ( $echo ) {
			printf( "<script%s>\n%s\n</script>\n", $this->type_attr, $output );
		}

		return $output;
	}

Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.