【问题标题】:Translate Woocommerce Add to cart button & Cart widgetWoocommerce 添加到购物车按钮和购物车小部件
【发布时间】:2021-01-27 15:33:50
【问题描述】:

我在翻译 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 插件中的字符串中,但它没有翻译。

有人知道如何帮助我翻译网站上的按钮和购物车微件吗?

提前谢谢你。

【问题讨论】:

  • 去看看这个帖子:wordpress.stackexchange.com/questions/40456/… 你应该先删除init 动作然后添加你的动作
  • 我的操作是什么?
  • 在您指出的网站上,我正确地将 Order Now 显示为添加到购物车按钮的文本,不应该是这样吗?
  • 我正在尝试在阿尔巴尼亚语版本的网站中将“立即订购”翻译成阿尔巴尼亚语“Porosit tani”。

标签: php wordpress woocommerce translation polylang


【解决方案1】:

你可以试试这个功能:

// 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文件中。

【讨论】:

  • 此代码有效,是的。我要做的是翻译阿尔巴尼亚语版本网站上的“立即订购”按钮。我正在使用 polylang。
  • 我已经更新了我的答案。让我知道它是否有效。
  • 它确实有效,你太棒了!!我必须保存这个。谢谢你一百万!
猜你喜欢
  • 2022-11-24
  • 2018-10-30
  • 2018-05-08
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多