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

时间:2016-11-21 20:09:09

标签: php wordpress woocommerce locale cart

我想尝试在插件代码中获取当前语言。我试过get_locale()但它总是给我en_us。我试图找到WordPress代码引用的解决方案,但没有发现任何有用的东西。

问题是插件WooCommerce,文件wc-cart-functions.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中提取语言来解决此问题,但该网站在网址中没有语言。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

   <?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 ) ); }