There are times you don’t want to translate the whole installation of WooCommerce just for renaming one short string. There are times you need to rename a label or a little thing and don’t feel like installing a bloated translation plugin.
It doesn’t matter whether you want to rename a string in the original language (English, usually), or change the default translated string in a different language. Either way, and thankfully, there’s a little PHP snippet that will work for you instantly. Enjoy!
PHP Snippet: How to Translate or Rename a Single String of Text (WooCommerce Plugin)
Please note that the ‘woocommerce’ === $domain part in the below snippet gives you exclusive access to WooCommerce plugin strings. With that line, you can’t translate strings generated by your theme or other plugins. Feel free to remove it, but be careful because you may end up translating a lot of additional stuff that maybe you didn’t really want to.
Also, the ! is_admin() check is making sure that translation is only executing on the frontend, so the WordPress dashboard will be excluded. This is a good performance trick, also.
Finally, please note the difference between $translated and $untranslated: if you run a store in English language, you can use any of the two because $translated = $untranslated.
/**
* @snippet Translate a String in WooCommerce (English to English)
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 4.5
* @donate $9 https://tutoraspire.com
*/
add_filter( 'gettext', 'tutoraspire_translate_woocommerce_strings', 999, 3 );
function tutoraspire_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $translated ) {
case 'Sale!':
$translated = 'On Offer';
break;
case 'Product Description':
$translated = 'Product Specifications';
break;
// ETC
}
}
return $translated;
}
On the other hand, if you run a mono-lingual store in a language that is not English, $translated should be the string in your language while $untranslated is the original string in English – which means that if you want to do a renaming from Spanish to Spanish, you should use $untranslated. Kinda complex to explain, so I recommend trial/error.
/**
* @snippet Translate a String in WooCommerce (English to Spanish)
* @how-to Get tutoraspire.com FREE
* @author Tutor Aspire
* @compatible WooCommerce 4.6
* @donate $9 https://tutoraspire.com
*/
add_filter( 'gettext', 'tutoraspire_translate_woocommerce_strings', 999, 3 );
function tutoraspire_translate_woocommerce_strings( $translated, $untranslated, $domain ) {
if ( ! is_admin() && 'woocommerce' === $domain ) {
switch ( $untranslated ) {
case 'Sale!' :
$translated = 'Rebaja!';
break;
case 'Product Description' :
$translated = 'Descripción del producto';
break;
// ETC
}
}
return $translated;
}
How to Find The Correct WooCommerce String
Sometimes the code above does not work because you’ve selected the wrong string of text. It’s not as straight forward as it seems, because “frontend” strings (the ones you see on the website as a user) might differ from “backend” ones (the ones inside the code).
For this reason, you should always be familiar with the WooCommerce PHP files and search for the string before applying the snippet above. Here are some examples of the CORRECT strings you should translate:
// an easy one to translate
'Pay for order'
// a weird one, note the %s placeholder for product name
// on the frontend you would see the product name...
// ...but on the backend you see this:
'Sorry, "%s" is no longer in stock so this order cannot be paid for. We apologize for any inconvenience caused.'