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 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.
Share Improve this question edited Jun 28, 2017 at 11:03 Steve asked May 11, 2017 at 3:40 SteveSteve 1,75719 gold badges66 silver badges115 bronze badges 5 |2 Answers
Reset to default 8 +25The 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/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/
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'
add_filter('gettext', 'aad_translate_words_array', 20 );
– hwl Commented Jun 28, 2017 at 11:24echo __( 'Place Name' );
or_e( 'Place Name' );
? If you do, it has to work. Tested it. – Frank P. Walentynowicz Commented Jun 28, 2017 at 12:04