【问题标题】:Translating Wordpress (WooCommerce plugin) using php filters使用 php 过滤器翻译 Wordpress(WooCommerce 插件)
【发布时间】:2019-05-22 06:55:04
【问题描述】:

我正在尝试使用 php 过滤器(add_filter() 函数)将我的 WooCommerce 页面翻译成我的母语。 我有一个例子,我拿了一个页面的钩子,用它来轻松地翻译一个按钮,没有任何问题。

add_filter('ultimate_woocommerce_auction_bid_button_text', 'woo_custom_bid_button_text');
function woo_custom_bid_button_text() {
  return __( 'Přihodit', 'woo_ua' );
}

“Přihodit”是我的母语“Place bid”的翻译。

问题是当我想翻译“出价”按钮前面的部分时

这是 WooCommerce 源代码:

            <div class="quantity buttons_added">
            <label for="uwa_your_bid"><?php _e('Bid Value', 'woo_ua') ?>:</label>
            <input type="number" name="uwa_bid_value" data-auction-id="<?php echo esc_attr( $product_id ); ?>"
            value=""min="<?php echo $product->woo_ua_bid_value()  ?>"
            step="any" size="<?php echo strlen($product->get_woo_ua_current_bid())+2 ?>" title="bid"  class="input-text qty  bid text left">
            </div>

我已经尝试过使用与上面相同的方法:

add_filter('ultimate_woocommerce_auction_before_bid_button', 'woo_custom_text_before_bid_input'`enter code here`);
function woo_custom_text_before_bid_input() {
  return _e('Příhoz', 'woo_ua');
}

但是我不能让它工作,因为没有特定的钩子可以使用。提前感谢任何回复或解决方案。

【问题讨论】:

    标签: php wordpress woocommerce hook translation


    【解决方案1】:

    你可以使用gettext filter

    add_filter( 'gettext', function( $translated_text, $text, $domain ){
    
        if ( $text == 'Bid Value' )
            $translated_text = 'Příhoz';
    
        return $translated_text;
    
    }, 10, 3 );
    

    【讨论】:

    • 此代码隐藏了网站上的所有内容。我的意思是现在所有的文本和文章都不见了。连翻译都没有。
    • 我编辑了我的答案。也许你从那里复制了旧代码?
    • 好吧.. 我根本看不懂代码,但它确实有效。你是一个活生生的救星!非常感谢!也许你能帮我一个忙并解释代码本身吗?例如我不明白为什么最后一行包含 ", 10, 3 );"那只是 add_filter() 语法中的 PRIORITY 和 ACCEPTED ARGS 吗?
    • 是的。这些参数是优先级和接受的参数。你可以在这里找到更多细节developer.wordpress.org/reference/functions/add_filter
    • 是的。我已经多次阅读该文档以了解 add_filter() 函数。只是要求确定。再次感谢:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-23
    • 2015-02-11
    • 2020-04-03
    • 2021-09-18
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多