1

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.

  • I have tested your code. It's working fine. – mukto90 May 11 '17 at 5:18
  • So why might it not be doing the job on my site @mukto90? – Steve May 11 '17 at 6:04
  • 1
    What about a later priority? add_filter('gettext', 'aad_translate_words_array', 20 ); – hwl Jun 28 '17 at 11:24
  • 2
    Note that this is very unefficient as it stands, as there could be thousands of such calls. It's not clear what is being translated (context?), but maybe there are other ways? – birgire Jun 28 '17 at 11:32
  • 1
    Don't be offended, but I must ask. Do you display your text using gettext functions, like echo __( 'Place Name' ); or _e( 'Place Name' );? If you do, it has to work. Tested it. – Frank P. Walentynowicz Jun 28 '17 at 12:04
8
+25

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/

1

as a default, in WP there is the following: add_action( 'after_setup_theme', your_theme_setup_function() );

inside that setup function there should be the following call: load_theme_textdomain( your_theme_domain, get_template_directory() . '/languages' );

it appears that the 'after_setup_theme' call makes that functions inside 'functions.php' itself don't recognize the domain.

solved it by moving the call: load_theme_textdomain( your_theme_domain, get_template_directory() . '/languages' ); to the root of 'functions.php'

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.