_x()WP 2.8.0

Retrieve translation for the specified text based on the specified context. Uses a translation file.

The function is needed to avoid confusion when the same text can be translated differently, based on the place (in what context) it is used.

_x() works just like __(), only diferense you need to specify the second parameter $context.

To display the result on the screen, use _ex(). It is an analogue of _e(), but with context parameter.

How to add context to Poedit?

The context string must be specified in .pot or .po file, so that during the translation it is clear in what context you need to translate the string (text).

To add context to Poedit. When adding _x as a key of search, add it so:

_x:1,2c

This is a pointer for Poedit, to look for the _x and get the first argument as a string for translations: msgid, and the second, as a string of the context msgctxt.

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

Used By: _ex()

No Hooks.

Return

String. Translated text or original text if the string could not be translated.

Usage

_x( $text, $context, $domain );
$text(string) (required)
Text to translate.
$context(string) (required)
Context information for the translators.
$domain(string)
Text domain. Unique identifier for retrieving translated strings.
Default: 'default'

Examples

0

#1 Translation of strings with context

Since the word Read can have different meanings, the second parameter explains what this word is mean in this case (in what context this word is used).

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

#2 How should look like .po file

To display the translation with context, you need to specify the second parameter in _x(). But you need to understand that this second parameter (string) is specified in .po file. This is what the .po file code looks like (msgctxt is the context clarify):

msgctxt "examination"
msgid "testing"
msgstr "Тестирование"

msgctxt "experiment"
msgid "testing"
msgstr "Испытания"

Output this translation in PHP:

echo '"testing" как "examination": ' . _x('testing', 'examination', 'myl10n');
echo '"testing" как "experiment": ' . _x('testing', 'experiment', 'myl10n');

// Получим:
// "testing" as "examination": Тестирование
// "testing" as "experiment": Испытания

This code implies that .mo file is connected by the function load_textdomain() and under the ID myl10n.

Changelog

Since 2.8.0 Introduced.

_x() code WP 6.5.2

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