translate()WP 2.2.0

Gets the translation of the specified text.

If the translation could not be obtained or the translation file could not be loaded, the original text will be returned.

This is a low-level function and is not intended for direct use! Use wrappers: __() or _e().

Hooks from the function

Returns

String. Original or translated text.

Usage

translate( $text, $domain );
$text(string) (required)
Text to be translated.
$domain(string)
Translation domain (.mo file with translation).
Default: 'default'

Examples

1

#1 Demo

$domain = 'my-textdomain';
$text = 'Some text to translate';

echo translate( $text, $domain );

Changelog

Since 2.2.0 Introduced.
Since 5.5.0 Introduced gettext-{$domain} filter.

translate() code WP 6.9

function translate( $text, $domain = 'default' ) {
	$translations = get_translations_for_domain( $domain );
	$translation  = $translations->translate( $text );

	/**
	 * Filters text with its translation.
	 *
	 * @since 2.0.11
	 *
	 * @param string $translation Translated text.
	 * @param string $text        Text to translate.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( 'gettext', $translation, $text, $domain );

	/**
	 * Filters text with its translation for a domain.
	 *
	 * The dynamic portion of the hook name, `$domain`, refers to the text domain.
	 *
	 * @since 5.5.0
	 *
	 * @param string $translation Translated text.
	 * @param string $text        Text to translate.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( "gettext_{$domain}", $translation, $text, $domain );

	return $translation;
}