WP在插件代码中获取语言环境



我试图在插件代码中获取当前语言。我已经尝试使用get_locale(),但它总是给我EN_US。我试图在WordPress代码参考上找到解决方案,但没有找到任何有效的方法。

有问题的是插件woocommerce,file wc-cart-runctions.php有行:

$added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );

$message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) );

我想得到这个结果:

if($language == 'hr') { $added_text = sprintf( _n( '%s je dodan u košaricu.', '%s su dodani u košaricu.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );
    } else { $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ); }
if($language == 'hr') { $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( home_url().'/kosarica' ), esc_html__( 'Pogledaj košaricu', 'woocommerce' ), esc_html( $added_text ) );
        } else { $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) ); }

通常,我会通过从URL获取语言来解决此问题,但是该网站在URL中没有语言。

如果您使用polylang插件,请参阅此站点:https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/..您可能需要使用应返回当前语言的pll_current_language()

   <?php
    $language = pll_current_language('slug');
    if($language == 'hr') { 
    $added_text = sprintf( _n( '%s je dodan u košaricu.', '%s su dodani u košaricu.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) );} else { 
    $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ); }
    if($language == 'hr') { 
    $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( home_url().'/kosarica' ), esc_html__( 'Pogledaj košaricu', 'woocommerce' ), esc_html( $added_text ) );} else { 
    $message   = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' ), esc_html( $added_text ) ); }

最新更新