string translation in functions.php not working

I used the following code in functions.php to translate some text:

add_filter('gettext', 'aad_translate_words_array');
add_filter('ngettext', 'aad_translate_words_array');
function aad_translate_words_array( $translated ) {
     $words = array(
        // 'word to translate' = > 'translation'
        'Place Name' => 'Artist Name',
        'Add Your Place' => 'Add Your Artist Practice'
     );
     $translated = str_ireplace(  array_keys($words),  $words,  $translated );
     return $translated;
}

This code is not producing any text changes.

Why is this the case? Help appreciated.

2 Answers
2

The code you have is correct and will handle the wording even if the case does not match.

Your problem is probably that wherever Place Name is being output, it is not being passed through a WordPress translation function, __( 'Place Name' ) or _e( 'Place Name' );

Either that, or what you’re trying to translate is being dynamically generated … you gave zero details as to where Place Name is originating from, and as such, there’s nothing we can do besides tell you the code is correct.

If you’re not comfortable with using PHP to do this, you should use a plugin like Say What:
https://wordpress.org/plugins/say-what/

Here’s a tutorial I put together a while back on changing wording on any WordPress site:
https://plugins.smyl.es/docs-kb/how-to-change-button-or-other-text-on-entire-wordpress-site/

Leave a Comment