如何在函数内本地化字符串?

时间:2014-05-27 11:49:59

标签: wordpress localization

我在解决如何本地化此函数返回的字符串时遇到了一些问题。在函数内部使字符串可以翻译的正确方法是什么?

add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);
function add_search_box_to_menu( $items, $args ) {
    if( $args->theme_location == 'mobile' )
        return $items."<li class='menu-header-search'><form id='searchform' method='get'><input type='text' name='s' id='s' placeholder='This placeholder text should be translatable'></form></li>";

    return $items;
}

这是我最初认为可行的方法,但没有:

<li class='menu-header-search'><form id='searchform' method='get'><input type='text' name='s' id='s' placeholder='" . __('This placeholder text should be translatable', 'my_textdomain') . "'></form></li>";

1 个答案:

答案 0 :(得分:0)

您可以使用__() function翻译字符串,即使在函数中也是如此。你的代码看起来像这样:

add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);
function add_search_box_to_menu( $items, $args ) {
    if( $args->theme_location == 'mobile' )
        return $items."<li class='menu-header-search'><form id='searchform' method='get'><input type='text' name='s' id='s' placeholder='".__('This placeholder text should be translatable')."'></form></li>";

    return $items;
}
相关问题