在Woocommerce中更改或翻译特定文本


Change or translate specific texts in Woocommerce

>我在网上找到了解决方案,但它似乎不起作用。

说要编辑我几天前做的以下文件,但不知何故它仍然无法正常工作。

/

wp-content/plugins/woocommerce/templates/single-product/related.php

如果我通过FTP到服务器,文件将显示以下内容:

if ( $products->have_posts() ) : ?>
<div class="related products">
    <h2><?php _e('You may also like', 'woocommerce' ); ?></h2>

但是,网页仍然显示"相关产品",而不是"您可能也喜欢"

由于某种原因,这没有发生或在某个地方被覆盖。

有什么想法吗?

我发现子函数.php:http://speakinginbytes.com/2013/10/gettext-filter-wordpress/

/**
 * Change text strings
 *
 * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
 */
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related Products' :
            $translated_text = __( 'Check out these related products', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

它在这里效果很好:https://mo-sound.com/en/shop/ball-speaker-classic-white/

覆盖默认模板的最佳方法是将文件复制到当前主题中名为 /woocommerce/single-product 的文件夹中。对该文件进行更改。

通常要覆盖Woocommerce模板文件,例如

/wp-content/plugins/woocommerce/templates/<foldername>/<filename>

将文件复制到

/wp-content/<your-theme>/woocommerce/<foldername>/<filename>

这是 user5217948 的代码,其中包含来自 Frithir 的所需案例更新:

// CHANGE RELATED PRODUCTS TEXT
function my_text_strings( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'Related products' :
            $translated_text = __( 'You may also like...', 'woocommerce' );
            break;
    }
    return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

此代码适用于WooCommerce 3.3.1和WordPress 4.9.4(大约2018年2月)。


.

对于那些使用另一种语言的woocommerce/wordpress的人来说,这是一个友好的提示

您必须将"相关产品"替换为您的语言中显示的文本。就我而言,相关产品翻译为"productos relacionados"

要包含在函数中的代码.php文件

function my_text_strings( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
    case 'Productos relacionados' :
        $translated_text = __( 'Completá el look', 'woocommerce' );
        break;
}
return $translated_text;
}
add_filter( 'gettext', 'my_text_strings', 20, 3 );

> 2020 更新

现在有一个更

准确的过滤器,而不是建议的搜索和替换方法。

将此代码段添加到子主题中的函数.php文件中。

/*-------------- Change related products text -------------*/
add_filter( 'woocommerce_product_related_products_heading', 'single_related_text' );
function single_related_text(){
    return __('These products might be if your interest as well!', 'woocommerce');
}

一个小的PHP片段。将 PHP 代码段放在子主题函数的底部.php文件:wp-content/themes/mythemename/functions.php

add_filter( 'gettext', 'mythemename_translate_woocommerce_strings', 999, 3 );
function mythemename_translate_woocommerce_strings( $translated, $text, $domain ) {
    $translated = str_ireplace( 'text EN', 'text translated PT_BR', $translated );
    return $translated;
}

我刚刚在我自己的一个网站上成功地使用了以下代码。它需要放置在函数中.php在您的孩子主题中。将"此处的自定义文本"替换为您希望使用的字词。

add_filter( 'gettext', 'wtd_related_products_text', 20, 3 );
function wtd_related_products_text( $translated_text, $text, $domain ) {
switch ( $translated_text ) {
case 'Related products' :
$translated_text = __( 'Your Custom Text Here', 'woocommerce' );
break;
}
return $translated_text;
}