gettext_with_context
Allows you to change translated text whose translation is requested with a translation context by the _x() function.
This is a filter for the _x() function.
Usage
add_filter( 'gettext_with_context', 'wp_kama_gettext_with_context_filter', 10, 4 );
/**
* Function for `gettext_with_context` filter-hook.
*
* @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.
*
* @return string
*/
function wp_kama_gettext_with_context_filter( $translation, $text, $context, $domain ){
// filter...
return $translation;
}
- $translation(string)
- Translated text. Marked as
msgstrin*.pofiles. - $text(string)
- Text to translate. Marked as
msgidin*.pofiles. - $context(string)
- Contextual information for translators. Marked as
msgctxtin*.pofiles. - $domain(string)
- Text domain. A unique identifier used to retrieve translated strings. Specified when the translation is registered.
Examples
#1 Change the name of the Pending post status
Change the phrase "Pending" in the Posts list to "Awaiting moderator review", but only in the required location.
The word "Pending" is used in different contexts:
#: wp-includes/post.php:342 msgctxt "post status" msgid "Pending" msgstr "Pending" #: wp-includes/post.php:392 msgctxt "request status" msgid "Pending" msgstr "Pending" #: wp-includes/post.php:928 msgid "Pending" msgstr "Pending"
In this case, the translation is requested through _x() as follows:
_x( 'Pending', 'post status' )
Because a huge number of translations pass through the filter and we need to change one particular phrase in one particular location, the example checks that:
- The word "Pending" is being translated.
- The requested context is
post status. - The requested domain is
default(the WordPress core translation).
add_filter( 'gettext_with_context', 'change_gettext_pending', 10, 4 );
function change_gettext_pending( $translation, $text, $context, $domain ) {
if ( $text === 'Pending' && $context === 'post status' && $domain === 'default' ) {
$translation = 'Awaiting moderator review';
}
return $translation;
}
Changelog
| Since 2.8.0 | Introduced. |
Where the hook is called
gettext_with_context
wp-includes/l10n.php 275
$translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );