16

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?

3 Answers 3

23

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.

5
  • 1
    That worked! I had to do "Categorie: %s" and "Sectie: %s" because the text has to be exactly as in the translation file.
    – Florian
    May 14, 2015 at 14:05
  • Oh, yes, that is correct. I didn't know the exact text to translate.
    – cybmeta
    May 14, 2015 at 14:51
  • 1
    Great piece of code – just be aware that the gettext filter won’t catch translations that use a "context" string, those will need gettext_with_context.
    – Manu
    May 24, 2016 at 10:44
  • Thanks for this great solution. As I am building a child theme and willing to override some of the parent theme's translations, I substituted the proposed function body with the following line return $domain != 'child-domain' && ( $new = __( $original, 'child-domain' ) ) != $original ? $new : $translated;. In this way, I can housekeep the overriding translations in the child theme's PO file instead of having them cabled in the function code.
    – Franco
    Jun 17, 2018 at 12:53
  • Thanks cybmeta! I used this technique to change core text with a plugin I published in the repo. I credited this post in the code. wordpress.org/plugins/…
    – John Dee
    Jan 8, 2019 at 10:49
3

I was about to use the "add_filter( 'gettext'..." solution suggested above but then read at https://developer.wordpress.org/reference/hooks/gettext/ that this solution can have quite a negative performance impact. I suggest to therefore go with alternative solutions where possible. In my case I can copy a theme template file to my child theme and edit the string in the template file. This can be a great solution but it of course doesn't help for strings that don't appear in template files.

PS: I meant to add this as a comment but unfortunately I'm not allowed to do so yet.

2

It's also possible to use get_the_archive_title filter in your functions.php:

function archive_title_modify( $title ) {
    return str_replace('Categorie: ', 'Sectie: ', $title);
}
add_filter('get_the_archive_title', 'archive_title_modify');

The advantage is that it's called only once per page, instead on every translated string as with gettext filter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.