translate_with_gettext_context() WordPress Function

The WordPress function translate_with_gettext_context() is used to translate a string with a given context. This function is similar to the WordPress function translate(), but with an additional argument for the context. This function is useful for when a string can have different meanings depending on the context in which it is used. For example, the string "you" can be translated to "tu" in Spanish, but "you" can also be translated to "vos" in Spanish, depending on the context. The translate_with_gettext_context() function allows you to specify the context in which the string is used, so that the correct translation can be displayed.

translate_with_gettext_context( string $text, string $context, string $domain = 'default' ) #

Retrieve the translation of $text in the context defined in $context.


Description

If there is no translation, or the text domain isn’t loaded, the original text is returned.

_Note:_ Don’t use translate_with_gettext_context() directly, use _x() or related functions.


Top ↑

Parameters

$text

(string)(Required)Text to translate.

$context

(string)(Required)Context information for the translators.

$domain

(string)(Optional) Text domain. Unique identifier for retrieving translated strings.

Default value: 'default'


Top ↑

Return

(string) Translated text on success, original text on failure.


Top ↑

Source

File: wp-includes/l10n.php

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

	/**
	 * Filters text with its translation based on context information.
	 *
	 * @since 2.8.0
	 *
	 * @param string $translation Translated text.
	 * @param string $text        Text to translate.
	 * @param string $context     Context information for the translators.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );

	/**
	 * Filters text with its translation based on context information 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 $context     Context information for the translators.
	 * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
	 */
	$translation = apply_filters( "gettext_with_context_{$domain}", $translation, $text, $context, $domain );

	return $translation;
}


Top ↑

Changelog

Changelog
VersionDescription
5.5.0Introduced gettext_with_context-{$domain} filter.
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.