【发布时间】:2016-11-21 20:09:08
【问题描述】:
我正在尝试在插件代码中获取当前语言。我尝试过使用 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 获取语言来解决这个问题,但该网站在 URL 中没有语言。
【问题讨论】:
-
我忘了说,网站使用 Polylang 语言插件。
-
糟糕,本来是想将其添加为评论,但会将其保留为答案。特别是如果它有效。
标签: php wordpress woocommerce locale cart