How to translate a string in Woocommerce ?

In this tutorial we’ll learn how to quickly translate a string in Woocommerce. With the help of this functionality we can replace any string with any other string. This is a general purpose functionality and can be used for multiple purposes.

To see what it can do lets have an example, this will be the cart screen when there is no product.

woocommerce

Here you can see the empty cart message i.e. “Your cart is currently empty.”. Now let’s say I want to replace it with the text ‘Empty Cart’. To do this add the following lines of code at the end of functions.php file of your theme.

add_filter('gettext', 'translate_text');

add_filter('ngettext', 'translate_text');

function translate_text($translated) {

     $translated = str_ireplace('Your cart is currently empty', 'Empty cart', $translated);

     return $translated;

}

Save the file and refresh the empty cart page. You’ll see the cart message has been changed.

woocommerce

In this way, you can replace almost every string with any other.

Waqas

I hope you enjoy reading this blog post. If you want my team to do WooCommerce Maintenance for you, click here.

1 thought on “How to translate a string in Woocommerce ?”

Leave a Comment

Your email address will not be published. Required fields are marked *