试图更改一些Woocommerce默认单词和句子

时间:2016-03-01 15:38:33

标签: wordpress woocommerce

我正在开发我的第一个Wordpress / Woocommerce网站,我想更改网站的默认Wordpress翻译中的一些单词,以便更好地适应业务类型。目前我在我的主题的functions.php文件中使用以下代码(Storefront的子主题):

add_filter('gettext', 'noProducto');

function noProducto($translated) {

    $default = array ('Descripción del producto', 'Productos relacionados');  

    $desired = array ('Descripción del inmueble', 'Inmuebles similares');    

$translated = str_ireplace($default, $desired , $translated);

return $translated;
}

它有效,但我已经读到这不是很有效并且可能导致性能问题。我想改用推荐的方法:

function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Descripción del producto' :
            $translated_text = __( 'Descripción del inmueble', 'woocommerce' );
            break;
        case 'Productos relacionados' :
            $translated_text = __( 'Inmuebles similares', 'woocommerce' );
            break;

    }
    return $translated_text;
};
add_filter( 'gettext', 'my_text_strings', 20, 3 );

然而,它对我不起作用,我不知道为什么。 我是Wordpress的新手,我正在努力学习良好的实践;所以任何暗示第二种方法可能不起作用的提示都将不胜感激。

1 个答案:

答案 0 :(得分:0)

试试这个。这将翻译文本。

function mycustom_filter_gettext( $translated, $original, $domain ) {

    // This is an array of original strings
    // and what they should be replaced with
    $strings = array(
               'Related Products'=>'products'

    );

    // See if the current string is in the $strings array
    // If so, replace its translation
    if ( isset( $strings[$original] ) ) {

        $translations = get_translations_for_domain( $domain );
        $translated = $translations->translate( $strings[$original] );
    }

    return $translated;
}

add_filter( 'gettext', 'mycustom_filter_gettext', 10, 3 );