_x( string $text, string $context, string $domain = ‘default’ ): string

Retrieves translated string with gettext context.

Description

Quite a few times, there will be collisions with similar translatable text found in more than two places, but with different translated context.

By including the context in the pot file, translators can translate the two strings differently.

Parameters

$textstringrequired
Text to translate.
$contextstringrequired
Context information for the translators.
$domainstringoptional
Text domain. Unique identifier for retrieving translated strings.
Default 'default'.

Default:'default'

Return

string Translated context string without pipe.

Source

function _x( $text, $context, $domain = 'default' ) {
	return translate_with_gettext_context( $text, $context, $domain );
}

Changelog

VersionDescription
2.8.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    Example

    $translated = _x( 'Read', 'past participle: books I have read', 'textdomain' );

    Since the string ‘Read’ on its own could have one of several different meanings in English, context is given so that translators know that they should be supplying a short term that means “Books I have read.”

  2. Skip to note 5 content

    Example

    /**
     * Translates a string with context.
     *
     * @param string $text    The string to be translated.
     * @param string $context The context for the translation.
     * @param string $domain  Optional. The text domain for the translation. Default is 'default'.
     *
     * @return string The translated string.
     */
    function my_custom_translation_function( $text, $context, $domain = 'default' ) {
        // Translate the string with context
        $translated_text = _x( $text, $context, $domain );
    
        // Do some additional processing or modifications if needed
    
        return $translated_text;
    }

    Inside the function, the _x() function is called with the provided parameters to perform the translation. The resulting translated string is stored in the $translated_text variable.

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