最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

text - string translation in functions.php not working

programmeradmin2浏览0评论

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
  • I have tested your code. It's working fine. – mukto90 Commented May 11, 2017 at 5:18
  • So why might it not be doing the job on my site @mukto90? – Steve Commented May 11, 2017 at 6:04
  • 1 What about a later priority? add_filter('gettext', 'aad_translate_words_array', 20 ); – hwl Commented Jun 28, 2017 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 Commented Jun 28, 2017 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 Commented Jun 28, 2017 at 12:04
Add a comment  | 

2 Answers 2

Reset to default 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/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'

发布评论

评论列表(0)

  1. 暂无评论