WP_Scripts::print_translations()publicWP 5.0.0

Prints translations set for a specific handle.

Метод класса: WP_Scripts{}

Хуков нет.

Возвращает

Строку|false. Script on success, false otherwise.

Использование

global $wp_scripts;
$wp_scripts->print_translations( $handle, $display );
$handle(строка) (обязательный)
Name of the script to add the inline script to. Must be lowercase.
$display(true|false)
Whether to print the script instead of just returning it.
По умолчанию: true

Список изменений

С версии 5.0.0 Введена.

Код WP_Scripts::print_translations() WP 6.6.2

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

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

	if ( isset( $this->registered[ $handle ]->translations_path ) ) {
		$path = $this->registered[ $handle ]->translations_path;
	}

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

	if ( ! $json_translations ) {
		return false;
	}

	$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 ( $display ) {
		wp_print_inline_script_tag( $output, array( 'id' => "{$handle}-js-translations" ) );
	}

	return $output;
}