WordPress is set to Dutch language. When I use get_the_archive_title()
my theme correctly outputs “Categorie: Category-name” on a category archive page. However I’d like that to read “Sectie: Category-name”.
I do not want to change the Dutch language file in the wp-content/languages folder, because that will be updated by WordPress updates.
I tried copying that translation file, altering the “category” translation and putting the new nl_NL.mo file into my-theme/languages. This did not have any effect.
How can I achieve a different translation for some strings without altering the core translation files?
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
.