skip to Main Content

The code I used over the years (example below) hasn’t worked for awhile. Tried some other code posted on here, but no joy. It seems WC has changed how to filter/translate and each text changed needs to be separate. Is that correct? Originally had 11 text changes using this code…

Would appreciate some code to perform text changes in WC 5.0 Thanks!

add_filter('gettext', 'translate_text');
add_filter('ngettext', 'translate_text');

function translate_text($translated) {
$translated = str_ireplace('Products', 'Prints', $translated);
$translated = str_ireplace('Product', 'Print', $translated);
$translated = str_ireplace('Product Categories', 'Prints', $translated);
return $translated;
}

3

Answers


  1. Chosen as BEST ANSWER

    Here's the code that wound up working for me. Change the 2 ibc to the name you want or just leave it. Don't forget to change the name you wish to use instead of Print(s)

       add_filter( 'gettext', 'ibc_translate_woocommerce_strings', 999, 3 );
    
       function ibc_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
    
         if ( ! is_admin() && 'woocommerce' === $domain ) {
    
            switch ( $translated ) {
    
               case 'Products':
    
               $translated = 'Prints';
               break;
    
               case 'Product':
    
               $translated = 'Print';
               break;
    
               case 'Product Categories':
    
               $translated = 'Print';
               break;
    
             // ETC
       
       }
    
     }   
    
          return $translated;
    
     }
    

  2. The WordPress hooks gettext and ngettext haven’t changes since a while and work on translatable strings, work independently from WooCommerce versions

    The correct way to make it work with gettext and ngettext hooks is (simplifying a bit and adding some missing function arguments):

    add_filter( 'gettext', 'change_some_woocommerce_strings', 10, 3 );
    add_filter( 'ngettext', 'change_some_woocommerce_strings', 10, 3 );
    function change_some_woocommerce_strings( $translate_text, $original_text, $domain ) {
        if ( stripos( $original_text, 'Product') !== false || stripos( $original_text, 'Categories') !== false ) {
            $translate_text = str_ireplace( 
                array('Product categories', 'Products', 'Product'),
                array('Prints', 'Prints', 'Print'), 
            $original_text );
        }
    
        return $translate_text;
    }
    

    If some strings are not translated, it can be because they have some context added. In this case the hook gettext_with_context is required like:

    add_filter( 'gettext_with_context', 'change_some_woocommerce_strings_with_context', 10, 4 );
    function change_some_woocommerce_strings_with_context( $translate_text, $original_text, $context, $domain ) {
        if ( stripos( $original_text, 'Product') !== false || stripos( $original_text, 'Categories') !== false ) {
            $translate_text = str_ireplace( 
                array('Product categories', 'Products', 'Product'),
                array('Prints', 'Prints', 'Print'), 
            $original_text );
        }
    
        return $translate_text;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Tested and works.

    Login or Signup to reply.
  3. Maybe this can also help.
    When you are testing, you should empty the cart before refreshing the page to see the changes. At least this is how it worked for me with the mini-cart widget.
    Success to all!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search