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
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)
The WordPress hooks
gettext
andngettext
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
andngettext
hooks is (simplifying a bit and adding some missing function arguments):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:Code goes in functions.php file of the active child theme (or active theme). Tested and works.
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!