Posted on

WooCommerce: How to Translate / Rename any String

There are times you don’t want to translate the whole installation of WordPress or one of its plugins such as WooCommerce just for renaming something in the same. There are times you need to rename a label or a little thing and don’t feel like installing a bloated translation plugin just for one line of PHP. Thankfully, here’s a little WordPress snippet which will work for you instantly.

Translate a single string in WooCommerce/WordPress

WooCommerce / WordPress PHP Snippet: How to Translate a String of Text


/**
 * @snippet       Translate a String in WooCommerce
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @sourcecode    https://businessbloomer.com/?p=162
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.4
 */

add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 999 );

function bbloomer_translate_woocommerce_strings( $translated ) {

// STRING 1
$translated = str_ireplace( 'Sale!', 'ON OFFER', $translated );

// STRING 2
$translated = str_ireplace( 'Product Description', 'Product Specifications', $translated );

// ETC.

return $translated;
}

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance πŸ™‚

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares πŸ™‚ Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

61 thoughts on “WooCommerce: How to Translate / Rename any String

  1. The snippet worked for me. However, I replaced ‘bbloomer’ with my initials and it broke the entire site. I then spent 2 hours thinking what went wrong. πŸ™‚

    1. πŸ˜€ hope you sorted that out Yavor!

  2. Not worked for me.. Inserted into functions.php, but simply not translated string for me. Im not sure if is theme related or something else, but not worked. This is code i inserted:

    add_filter( 'gettext', 'bbloomer_translate_woocommerce_strings', 999 );
     
    function bbloomer_translate_woocommerce_strings( $translated ) {
     
    // STRING 1
    $translated = str_ireplace( 'Follow Us', 'SUIVEZ-NOUS', $translated );
    
    return $translated;
    }

    to translate string into footer widget..

    1. Hey Dean, thanks so much for your comment! Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

  3. Hi Rodolfo,

    Great support!
    A simple question: How do I translate a specific text string in the login page?
    Thank you very much in advance.

    Regards Erik

    1. Hey Erik, this snippet should work! What have you tried so far?

  4. Hi! can somebody post just a simple example how to add a second string to the code, I dont have a clue how to achieve this with switch statement …

    1. Hey Rob, I just updated the snippet for you πŸ™‚

  5. Hi Rodolfo

    Brilliant simple solution to all the others i’ve used in the past.

    Do have a question, i have ‘Cart’ to change to ‘Booking’ in my secondary header, the text changes onload but then changes back to ‘Cart’ as soon at the entire page loads.

    The menu item is added by the theme options itself if that’s of any issue

    It’s an odd one!

    1. i answered my own problem!

    2. Ahah excellent πŸ™‚

  6. Hi Rodolfo,
    I tried your code, but it doesn’t work for me… I pasted it in fuctions.php along with the code below to translate the shop and checkout. Any ideas why? I am trying to translate the page in English and Spanish.

    function get_woocommerce_shop_polylang() {
    return pll_get_post(get_option(‘woocommerce_shop_page_id’ ) );
    }
    add_filter(‘woocommerce_get_shop_page_id’, “get_woocommerce_shop_polylang”);

    function get_woocommerce_checkout_polylang() {
    return pll_get_post(get_option(‘woocommerce_checkout_page_id’ ) );
    }
    add_filter(‘woocommerce_checkout_page_id’, “get_woocommerce_checkout_polylang”);

    Thank you for your help!

    1. Hey Paloma! If you’re using Polylang you don’t need this snippet as well – it should be sufficient!

  7. Once more a great help! Thanks. However it does not seem to work with special characters. if I put the german word “WΓ€hle” the whole phrase just disapears completly. “Wahle” works fine.
    Any idea how to work around that?
    Thanks

    1. Hey Luise, thanks for your comment. I’m not 100% sure here. Maybe give https://wordpress.org/plugins/say-what/ plugin a go.

  8. Hi Rodolfo Melogli,

    I tried your code ( 4-02-2018 ) & it didn’t work, put that in child theme’s functions.php.

    trying to convert ( VAT Inc. ) to German.

    hope you could help.

    Thanks in Advance

    1. Hey K, I take you managed to work this out πŸ™‚

  9. Thank you!

    I have tried everything and wasted around 1 day trying to fix this problem.

    And yet, your simple code works!

  10. Thank you so much! works like a charm.

  11. Thank you!! This saved me a lot of time digging through my theme.

  12. It worked on all of the woo commerce. But it is not translating the Tax Names. Say If I have a tax name “GST Handling Fee” and I want to change it to ‘HST Expedition Fee’. It is not working.

    1. Hey Dev, thanks for your comment! You can do that from the WooCommerce Tax settings πŸ™‚

    1. Ah, thank you πŸ™‚

  13. Hi Rodolfo,

    Thank you for all the cool videos.

    I have a multilingual Woocommerce site (4 languages). I use polylang, Hyyan WooCommerce Polylang Integration and Loco Translate to manage the different languages.

    I would like to completely change the text of the Woocommerce emails (for example the one for a new user).
    This would involve changing the customer-new-account.php template file and all the related strings in the .po files.

    What would be the best approach to do this especially considering that the plugin could be updated in the future?
    Thanks,
    Massimo

    1. Massimo, my snippet is to be used for a string or 2, not for a full translation. Your translation plugins should allow you to translate emails as well, so maybe take a look at their “PRO” version, maybe this is not included in the freemium

  14. Just what I needed! A big thank you for that! πŸ™‚
    Works excellent!

  15. This saved me some trouble with translating some mystical plugin parts, thank you.
    But the thing about is, it searches for ALL strings on your website, not considering the context at all. Let say you I want translate one word like the preposition “of”. Now, the letters “of” may appear in hundreds of words on the website – like ” loft” or “soft” etc. They will all be replaced, which is of course not desirable.
    How can I possibly prevent this unwanted translations?

    Best regards and thanks!

    1. Filip, thanks for your comment! Yes, you should use this snippet very carefully… Instead of translating “of”, which is way too generic, you should for example translate “bag OF strawberries”, which is unique πŸ™‚

  16. Hey,
    i used this code several times before and it worked,
    but now i’m using woocommerce subscription plugin,
    and it did not work,
    I want to translate: year, week, day.
    any other possible way ?
    here is my code:

    add_filter(‘gettext’, ‘translate_reply’);
    add_filter(‘ngettext’, ‘translate_reply’);
    function translate_reply($translated) {
    $translated = str_ireplace(‘year’, ‘Χ©Χ Χ”’, $translated);
    return $translated;
    }

    1. Hey Itayko, not sure here πŸ™‚ Unfortunately I cannot provide custom work here via the blog comments.

  17. Hi!
    The code works well. But I’m wondering how to use it only on English version of my site (home language is Polish). I’m having troubles with Loco Translate and WPML costs $$$ πŸ™‚
    I have created additional custom tab on single product page (thanks to your youtube tutorial!) like this:

    $tabs[‘my_custom_tab’] = array( “title” => __( ‘Karta katalogowa’, ‘woocommerce’ ),

    and I want “Karta katalogowa” to be translated to “Datasheet” when I’m on English version of my site.
    Can I base on other automatic translation and wrap your above translation code in somethig like “if description_tab name is “Description” -> translate, else -> do not translate.
    What do you think? How to refer to other tab title?

    Thanks πŸ™‚

    1. Hey Jacek, thanks for your comment πŸ™‚

      Once you create the string, you can definitely use a translation plugin such as SayWhat, Polylang or other to target that in your own language.

      This snippet is to translate something in a one-language website πŸ™‚

  18. Great! It worked and was real easy! Much appreciated

    1. Awesome! Thanks Arthur πŸ™‚

  19. Hi. This code break whole page when applied to functions.php

    1. Hey Adam, thanks for your comment! This snippet works, I use it on many websites – so maybe you made a syntax error. What error do you get if you enable wp_debug?

  20. Thanks! !

  21. Hi i tried this and it worked just perfectly, question is; Can i edit more than 1 string if yes, how can i do that?

    1. Excellent, thanks Ifeanyi! Yes, that is possible with basic PHP – look into the SWITCH CASE PHP documentation and you should figure a way to achieve what you need πŸ™‚ Hope this helps!

  22. Works perfectly, thank you Rodolfo!

    1. Cool, cheers for the feedback Noelle πŸ™‚

  23. Hi Rodolfo,
    Sorry to bother you again, you pointed me to this article after answering my query/comment on the Woocommerce Emails Visual Hooks page.
    Can I translate more than one string, i.e) from two different Woocommerce Emails within the same function or do I need to repeat your snippet from above for each translation string?

    1. Yes, definitely. You can add the PHP “switch / case” inside the function and have a list of phrases (cases) with their relative translations. I can’t provide this here as this is custom work but hopefully I’ve sent you in the right direction πŸ™‚

    2. Hi,

      Sorry to bother you again. I’m trying to change the text for the completed order email when the customer has chosen the ‘Collect In Store’ shipping option in the cart.
      Is this possible with string translation or is there a different approach?.

    3. Hey Andy, yes, it should be possible. Have you tried it yet?

    4. No because I dont know how to specify/target the collect in store shipping option.

    5. You’ll have to search for that string through the files (with NotePad++ or similar for example), and then use the exact PHP string they use in the snippet πŸ™‚

    6. I’m not sure your understanding the question. The string I would want to customize is in the Completed Order Email, this string “Hi there. Your recent order on %s has been completed. Your order details are shown below for your reference:”

      But I want to change this string specifically for when a customer has bought something via Collect in Store. So when the admin marks it as complete, a custom completed order email is sent to that customer.

    7. Gotcha πŸ™‚ Yes, this is possible of course – but unfortunately this is custom development work and I cannot provide this solution on the blog right now.

      If you would like to get a quote for the fix, feel free to get in touch. Thanks for your understanding!

      ~R

  24. This way was working previously. On latest WooCommerce it does not work anymore. Any suggestions? Thank you.

    1. Fansi, thanks for your feedback πŸ™‚ This is a WordPress filter, so I find it strange it’s related to WooCommerce latest update. Can you check again and let me know? Thank you!

  25. doesnt work for me. Why is this?

    1. Mira, thanks for your feedback. What string are you trying to translate, can you tell me the link of your website page?

  26. Very nice – I wander if there is much performance issue translating this way or some other reason editing the po would be better..

    1. Yes, of course. I would recommend to translate max 10 strings or the PHP could slow down your website. Above 10 strings I would choose the “po” method! Thanks for your feedback!

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.