【问题标题】:Change or translate specific texts in Woocommerce在 Woocommerce 中更改或翻译特定文本
【发布时间】:2014-12-02 21:59:46
【问题描述】:

我在网上找到了一个解决方案,但它似乎不起作用。

它说要编辑我几天前所做的以下文件,但不知何故它仍然无法正常工作。

/wp-content/plugins/woocommerce/templates/single-product/related.php

如果我 FTP 到服务器,文件显示如下:

if ( $products->have_posts() ) : ?>

<div class="related products">

    <h2><?php _e('You may also like', 'woocommerce' ); ?></h2>

但是网页仍然显示“相关产品”而不是“您可能还喜欢”

由于某种原因,这没有发生或在某处被覆盖。

有什么想法吗?

【问题讨论】:

    标签: php wordpress woocommerce translation gettext


    【解决方案1】:

    我为子functions.php找到了这个:http://speakinginbytes.com/2013/10/gettext-filter-wordpress/

    /**
     * Change text strings
     *
     * @link http://codex.wordpress.org/Plugin_API/Filter_Reference/gettext
     */
    function my_text_strings( $translated_text, $text, $domain ) {
        switch ( $translated_text ) {
            case 'Related Products' :
                $translated_text = __( 'Check out these related products', 'woocommerce' );
                break;
        }
        return $translated_text;
    }
    add_filter( 'gettext', 'my_text_strings', 20, 3 );
    

    在这里运行良好:https://mo-sound.com/en/shop/ball-speaker-classic-white/

    【讨论】:

    • 这是正确的解决方案,一个函数覆盖。不是主题更改,因为主题文件也可能会更新。
    • 这是正确的代码。如果您的网站是多语言的,您需要将 switch ( $translated_text ) 替换为 switch ( $text )
    • 在搜索字符串中可能需要匹配HTML实体和区分大小写... > case "You may also like…" :
    • 今天这个功能不起作用..我试过了,但不起作用
    • 使用“相关产品”,因为翻译器区分大小写。
    【解决方案2】:

    覆盖默认模板的最佳方法是将文件复制到当前主题内名为/woocommerce/single-product 的文件夹中。对该文件进行更改。

    通常覆盖 Woocommerce 模板文件,例如

    /wp-content/plugins/woocommerce/templates/&lt;foldername&gt;/&lt;filename&gt;

    你把文件复制到

    /wp-content/&lt;your-theme&gt;/woocommerce/&lt;foldername&gt;/&lt;filename&gt;

    【讨论】:

    • 我怀疑这就是答案。 @user3710926 你不应该直接编辑 WooCommerce 核心文件。
    • 澄清一下;该特定文件应以woocommerce/single-product/related.php 的形式复制到您的主题文件夹中。您维护插件模板文件夹中的文件夹层次结构。
    【解决方案3】:

    这是user5217948 的代码,其中包含来自Frithir 的所需案例更新:

    // CHANGE RELATED PRODUCTS TEXT
    function my_text_strings( $translated_text, $text, $domain ) {
        switch ( $translated_text ) {
            case 'Related products' :
                $translated_text = __( 'You may also like...', 'woocommerce' );
                break;
        }
        return $translated_text;
    }
    add_filter( 'gettext', 'my_text_strings', 20, 3 );
    

    此代码适用于 WooCommerce 3.3.1 和 WordPress 4.9.4(均在 2018 年 2 月左右)。


    .

    【讨论】:

      【解决方案4】:

      对使用其他语言的 woocommerce/wordpress 的用户的友好提示

      您必须将“相关产品”替换为以您的语言显示的文本。就我而言,相关产品翻译为“productos relacionados”

      functions.php 文件中包含的代码

      function my_text_strings( $translated_text, $text, $domain ) {
      switch ( $translated_text ) {
          case 'Productos relacionados' :
              $translated_text = __( 'Completá el look', 'woocommerce' );
              break;
      }
      return $translated_text;
      }
      add_filter( 'gettext', 'my_text_strings', 20, 3 );
      

      【讨论】:

      • 谢谢你,你让我度过了一个美好的夜晚。我正在尝试使用原始美国翻译进行此切换,因为在函数编码中,它通常与原始美国术语有关。但是在这里,我还通过使用我对“相关产品”术语的法语翻译来实现它。话虽如此,我不得不在非重音字符中更改重音字符。法语翻译是“Produits visibleés”。但是在代码的切换部分:case 'Produits visibleés' 不起作用。任何想法如何在 php 中使用重音字符串作为比较或切换条件来管理语句?
      【解决方案5】:

      2020 年更新

      现在您可以使用更准确的过滤器,而不是建议的搜索和替换方法。

      您将此 sn-p 添加到您的 functions.php 文件中,在您的子主题中。

      这似乎需要 WooCommerce 3.9.0

      /*-------------- Change related products text -------------*/
      add_filter( 'woocommerce_product_related_products_heading', 'single_related_text' );
      
      function single_related_text(){
          return __('These products might be cool', 'woocommerce');
      }
      

      【讨论】:

        【解决方案6】:

        一点 PHP sn-p。将 PHP sn-ps 放在您的子主题 functions.php 文件的底部:wp-content/themes/mythemename/functions.php

        add_filter( 'gettext', 'mythemename_translate_woocommerce_strings', 999, 3 );
        
        function mythemename_translate_woocommerce_strings( $translated, $text, $domain ) {
        
            $translated = str_ireplace( 'text EN', 'text translated PT_BR', $translated );
        
            return $translated;
        }
        

        【讨论】:

          【解决方案7】:

          我刚刚在我自己的一个网站上成功使用了以下代码。它需要放在您的子主题的functions.php 中。将“Your Custom Text Here”替换为您希望使用的字词。

          add_filter( 'gettext', 'wtd_related_products_text', 20, 3 );
          function wtd_related_products_text( $translated_text, $text, $domain ) {
          switch ( $translated_text ) {
          case 'Related products' :
          $translated_text = __( 'Your Custom Text Here', 'woocommerce' );
          break;
          }
          return $translated_text;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-17
            • 2020-02-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多