【问题标题】:Any suggestions how can I optimize this code? I need to translate some strings in 3 languages. Thanks [closed]任何建议如何优化此代码?我需要用 3 种语言翻译一些字符串。谢谢[关闭]
【发布时间】:2021-11-01 22:40:54
【问题描述】:

根据语言更改字符串。翻译字符串有更好的解决方案吗?有没有办法缩短代码。谢谢。

<?php

add_action( 'woocommerce_before_cart_table', 'cart_page_notice' );
 
function cart_page_notice() {
    $min_amount = 1000; //This is the amount of your free shipping threshold. Change according to your free shipping settings
    $current = WC()->cart->subtotal;
    if (( $current < $min_amount ) && ICL_LANGUAGE_CODE == 'ru') {
    $added_text = '<div class="woocommerce-message"><strong>Купите еще на ' . wc_price( $min_amount - $current ) . ' для бесплатной доставки</strong>'; // This is the message shown on the cart page
    $return_to = wc_get_page_permalink( 'shop' );
    if(ICL_LANGUAGE_CODE == 'ru'){  
    $notice = sprintf( '%s<a class="button" href="%s">%s</a>', $added_text, esc_url( $return_to ), 'Продолжить покупки</div>' ); // This is the text shown below the notification. Link redirects to the shop page
        echo $notice;
    }
    }if (( $current < $min_amount ) && ICL_LANGUAGE_CODE == 'en') {
    $added_text = '<div class="woocommerce-message"><strong>Buy ' . wc_price( $min_amount - $current ) . ' worth products more to get free shipping</strong>'; // This is the message shown on the cart page
    $return_to = wc_get_page_permalink( 'shop' );
    if(ICL_LANGUAGE_CODE == 'en'){  
    $notice = sprintf( '%s<a class="button" href="%s">%s</a>', $added_text, esc_url( $return_to ), 'Continue shopping</div>' ); // This is the text shown below the notification. Link redirects to the shop page
    }
        echo $notice;
    }elseif (( $current < $min_amount ) && ICL_LANGUAGE_CODE == 'tr') {
    $added_text = '<div class="woocommerce-message"><strong>Ücretsiz kargo için ' . wc_price( $min_amount - $current ) . ' daha satın alın</strong>'; // This is the message shown on the cart page
    $return_to = wc_get_page_permalink( 'shop' );
    if(ICL_LANGUAGE_CODE == 'tr'){  
    $notice = sprintf( '%s<a class="button" href="%s">%s</a>', $added_text, esc_url( $return_to ), 'Alışverişe devam</div>' ); // This is the text shown below the notification. Link redirects to the shop page
    }
    echo $notice;
}
}

【问题讨论】:

标签: php wordpress woocommerce code-snippets wpml


【解决方案1】:

您可以使用变量在 switch 语句中分配语言文本,以减少当前代码的重复:

switch(ICL_LANGUAGE_CODE) {
        case 'ru':
            $buyText = 'Купите еще на %s для бесплатной доставки';
            $continueText = 'Продолжить покупки';
        break;
        case 'tr':
            $buyText = 'Ücretsiz kargo için %s daha satın alın';
            $continueText = 'Alışverişe devam';
        break;
        default: //en
            $buyText = 'Buy %s worth products more to get free shipping';
            $continueText = 'Continue shopping';
        break;
    
    }
$buyText = sprintf($buyText, wc_price( $min_amount - $current ) );

然后您可以摆脱if 语句以根据上述变量输出一段代码:

if (( $current < $min_amount )) {
        $buyText = sprintf($buyText, $min_amount - $current );
        $added_text = '<div class="woocommerce-message"><strong>'.$buyText.'</strong>'; // This is the message shown on the cart page
        $return_to = wc_get_page_permalink( 'shop' );
    
        $notice = sprintf( '%s<a class="button" href="%s">%s</a>', $added_text, esc_url( $return_to ), $continueText ); // This is the text shown below the notification. Link redirects to the shop page
        echo $notice;
    }

这里是一个精简的例子,去掉了 wp 特定的函数,所以它可以在原生 PHP 中运行:https://3v4l.org/C9MCJ

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2021-06-20
    相关资源
    最近更新 更多