翻译 Woocommerce 添加到购物车按钮和购物车小部件

时间:2021-01-27 15:33:50

标签: php wordpress woocommerce translation polylang

我在翻译 woocommerce 上的“添加到购物车”按钮时遇到问题。

我的网站是:http://test.mk/OPA 该网站是阿尔巴尼亚语和英语。第一语言是英语。

我已经安装了 Polylang Pro 插件并尝试使用 Loco translate。

我在functions.php中使用以下代码更改了添加到购物车按钮的“名称”:

 //To change add to cart text on single product page
     add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' ); 
function woocommerce_custom_single_add_to_cart_text() {
return __( 'Order Now', 'woocommerce' ); 
}

// To change add to cart text on product archives(Collection) page
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );  
function woocommerce_custom_product_add_to_cart_text() {
    return __( 'Order Now', 'woocommerce' );
}

所以,我真正需要翻译的是立即订购。

我使用以下代码添加了字符串:

add_action('init', function() {
  pll_register_string('woocommerce_product_add_to_cart_text', 'Order Now', 'WooCommerce');
});

它在 polylang 插件的字符串中,但它没有翻译。

有人知道如何帮助我翻译同样在网站上的按钮和购物车 wiget 吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

你可以试试这个功能:

// change the text of the add to cart button according to the language
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 99, 1 ); 
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text', 99, 1 );
function woocommerce_custom_single_add_to_cart_text( $text ) {

    // returns the current language "Polylang"
    switch ( pll_current_language() ) {
        case 'en':
            return __( 'Order Now', 'woocommerce' );
        case 'sq':
            return __( 'Porosit tani', 'woocommerce' );
    }

    return $text;
}

更改“添加到购物车”按钮的文本根据网站的当前语言

此功能应该可以工作,但我相信您应该搜索 Polylang 插件文档以正确翻译字符串。

代码必须插入到活动主题的functions.php文件中。