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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27259838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:16:13  来源:igfitidea点击:

Change or translate specific texts in Woocommerce

phpwordpresswoocommercetranslationgettext

提问by Mike Young

I found a solution online for this but it does not seem to work.

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

It says to edit the below file which I did a few days ago but somehow it is not working still.

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

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

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

Which if I FTP to the server the file shows the below:

如果我 FTP 到服务器,文件显示如下:

if ( $products->have_posts() ) : ?>

<div class="related products">

    <h2><?php _e('You may also like', 'woocommerce' ); ?></h2>

However the webpage still shows 'Related products' and not 'You may also like'

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

For some reason this is not taking place or being over ridden somewhere.

出于某种原因,这不会发生或在某处被超越。

Any ideas?

有任何想法吗?

回答by user5217948

i found this for the child functions.php: http://speakinginbytes.com/2013/10/gettext-filter-wordpress/

我为子functions.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 );

it works well here: https://mo-sound.com/en/shop/ball-speaker-classic-white/

它在这里运行良好:https: //mo-sound.com/en/shop/ball-speaker-classic-white/

回答by RST

The best way to override default templates is to copy the file to a folder named /woocommerce/single-productinside your current theme. Make changes to that file.

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

In general to override Woocommerce template files like

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

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

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

you copy the file to

你把文件复制到

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

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

回答by MarkPraschan

Here is user5217948's code with the needed case update from Frithir:

这是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 );

This code works with WooCommerce 3.3.1 and WordPress 4.9.4 (both circa Feb 2018).

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



.

.

回答by Mariano Gianni

A friendly tip for those that have woocommerce/wordpress in another language

对于那些使用另一种语言的 woocommerce/wordpress 的人的友好提示

You would have to replace 'Related Products' to the text that is showing up on your language. In my case, related products translates to " productos relacionados "

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

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 );

} add_filter( 'gettext', 'my_text_strings', 20, 3 );

回答by Anders Lund

2020 Update

2020 更新

Instead of the suggested search-and-replace method, there is now a filter you can use, which is more accurate.

现在您可以使用更准确的过滤器,而不是建议的搜索和替换方法。

You add this snippet to your functions.php file, in your child theme.

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

It seems this requires WooCommerce 3.9.0

看来这需要 WooCommerce 3.9.0

/*-------------- Change related products text -------------*/
add_filter( 'woocommerce_product_related_products_heading', 'single_related_text' );

function single_related_text(){
    return __('These products might be cool', 'woocommerce');
}

回答by Fabiano Monteiro

A little PHP snippet. Place PHP snippets at the bottom of your child theme functions.php file: wp-content/themes/mythemename/functions.php

一个小的 PHP 片段。将 PHP 片段放在子主题 functions.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;
}