
Use a PO and MO file to change some hardcoded WooCommerce strings. The same concept works for other plugins and themes.
WooCommerce provides a lot of actions, filters and templates to allow developers make extensive changes to WooCommerce online shops. Despite this, there are a few strings that are hardcoded in the core code. These strings are available for translation but what if you want to change them while keeping everything in English? (or your current active language)
How to ‘translate’ into English
WooCommerce provides a POT file (a template file for a PO file). This file has no translations in it (all the msgstr strings are empty). This is the starting point for our translation
I downloaded and installed the Poedit translations editor and opened the wp-content/plugins/woocommerce/i18n/languages/woocommerce.pot file in it. At the bottom of the screen I see this message:
A POT file is not available to edit – it must be copied to a PO file and it will be edited with the translations. When I click ‘Create new translation‘ it prompts me for the translation language. As I’m not changing the language I must determine what locale (aka ‘language‘) WordPress is running as.
When I run this it reports Locale: en_US aka English – United States. The ‘en_US‘ will be used in the filename of the .MO file that I will create (it’s not a requirement but it’s good for organisation).
I chose “English (United States)” and began my changes.
Change some strings and create the MO file
I went through the WooCommerce front end, viewing products, adding them to the cart and proceeding to checkout. Every string I examined could be changed via a filter or template file. The WooCommerce team have done an excellent job.
When I removed an item from the cart I found strings that cannot be changed with a filter or template file.
This message is made up of 2 strings: ‘%s removed‘ and ‘Undo?‘ from includes/class-wc-form-handler.php.
I changed these to be a little more personal:
I then compiled them into a MO file with Poedit. The MO file is quite small – only 601 bytes as it only contains the changed strings.
Where to put the MO file
Next is to figure out where to put the MO file so that WooCommerce will load it.
I searched the WooCommerce source for load_textdomain() and found it in includes/class-woocommerce.php. This function is defined in wp-includes/l10n.php and it has the ‘load_textdomain_mofile‘ filter to allow changing the path of the MO file that is loaded.
I renamed my MO file to woocommerce-en_US.mo. This is primarily to help me identify the file’s purpose and locale.
The load_textdomain() function is called multiple times. In my test installation it was called 22 times on one page load – called by WordPress, the theme, WooCommerce and the Query Monitor plugins.
Putting the code into a class allowed me set a member variable when the ‘load_textdomain_mofile‘ filter was called and the custom MO file loaded. I check this variable in the ‘override_load_textdomain‘ filter function and can return ‘true‘ to override the .mo file loading.
I wrote the following code to simply set the WooCommerce MO file to the woocommerce-en_US.mo file that I copied to the same directory as the plugin file.
Now when I remove an item from the cart I see the changed strings.
Note
This concept of changing a few strings with a custom MO file can be applied to other plugins (or themes) that do not make it as easy to change them as WooCommerce does.
This is a fantastic solution. Thanks man!