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?

up vote 10 down vote accepted

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.

  • 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 '15 at 14:05
  • Oh, yes, that is correct. I didn't know the exact text to translate. – cybmeta May 14 '15 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 '16 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 at 12:53

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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