Quick Translation of any String

In this post you will learn how you can translate any text on your WordPress website with the help of PHP. You can translate any string, on the frontend and also on the backend. If a string is found, it will be changed EVERYWHERE, so you can’t just use it for one place. Be sure that the text you want to change is really a text and not an image.

In our example we want to change the “Dashboard” text on the backend dashboard to “Arrow Design Dashboard”.

 

This is how the Dashboard looks like before changing the code:

Quick Translation of any String

 

You can change the text by adding the following code to your functions.php file:

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

function arrow_design_translate_text($translated) {
$translated = str_ireplace('Dashboard', 'Arrow Design Dashboard', $translated);
return $translated;
}

This is how the Dashboard looks like after changing the code:

Quick Translation of any String

 

Code Explanation

str_ireplace()

This function is used to translate the string. The function is case-insensitive, if you need a case-sensitive function for this, use str_replace(). str_ireplace() has either 3 or 4 parameters, the last parameter is optional and this is the syntax: str_ireplace(find,replace,string,count)

 

gettext()

This function looks up a message in the current domain and the return value is a translated string.

 

ngettext()

This function is the plural version of gettext().

Conclusion

We hope this Quick Translation of any String post was helpful and you are now able to integrate this also on your website! You can find more articles here:

 

Read another Woocommerce post : ‘Make Billing and Shipping Fields Required

More information about the PHP functions:

str_ireplace()

gettext()

ngettext()

 

Arrow Design, based in Dublin, Ireland, provides quality website design services in Dublin and beyond at affordable prices. If you would like help with implementing the above code, or any wordpress website development project, contact us. We love website design and it shows! We provide custom wordpress plugin development, website design training and lots more.

We do it all, so you don’t have to!

Related Posts

Woocommerce Account Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Account Pages, like the Login/Register page, the Downloads page or the Orders page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce Single Product Page Hook Guide

In this article you will find a visual hook guide for the Woocommerce Single Product Page. This should help you to quickly and easily find the hook positions on the page.

Woocommerce PHP – Product Categories by Product ID

Woocommerce PHP – Product Categories by Product ID

In this tutorial you will learn how to check if a product is assigned to a tag, a category or a custom taxonomy. You can check if a product is is on the shop page, in the cart, in an order & more.

…We do more, so you can do less 🙂

 

Leave a message and we will be straight back to you!

Pin It on Pinterest

Share This