【问题标题】:WP get locale in plugin codeWP 在插件代码中获取语言环境
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我试过了,它在主题文件中有效,但在插件文件中无效。
【解决方案2】:
   <?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 ) ); }

【讨论】:

  • 我只需要翻译这两个句子。老板不会让我为此购买插件:/
  • 这些代码在主题文件中工作,但不在插件文件中。非常感谢你们的努力,你们所有人!我将不得不做其他事情来获取有关插件文件中当前语言的信息。再次感谢您,感谢您的努力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-17
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多