Override default WordPress core translation

You could use gettext filter:

add_filter( 'gettext', 'cyb_filter_gettext', 10, 3 );
function cyb_filter_gettext( $translated, $original, $domain ) {

    // Use the text string exactly as it is in the translation file
    if ( $translated == "Categorie: %s" ) {
        $translated = "Sectie: %s";
    }

    return $translated;
}

If you need to filter a translation with context, use gettext_with_context filter:

add_filter( 'gettext_with_context', 'cyb_filter_gettext_with_context', 10, 4 );
function cyb_filter_gettext_with_context( $translated, $original, $context, $domain ) {

    // Use the text string exactly as it is in the translation file
    if ( $translated == "Categorie: %s" ) {
        $translated = "Sectie: %s";
    }

    return $translated;
}

A translation with context means that a context is given in the gettext function used to translate the string. For example, this is without context:

$translated = __( 'Search', 'textdomain' );

And this is with context:

$translated = _x( 'Search', 'form placeholder', 'textdomain' );

Similar filters are available for plural translations ([_n()][2] and [_nx()][2]): ngettext and ngettext_with_context.

Leave a Comment