Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change hardcoded WooCommerce strings: Use a PO and MO file to change some hardcoded WooCommerce strings. The same concept works for other plugins and themes. https://www.damiencarbery.com/2018/12/change-hardcoded-woocommerce-strings/
<?php
/*
Plugin Name: Change hardcoded WooCommerce strings
Plugin URI: https://www.damiencarbery.com/2018/12/change-hardcoded-woocommerce-strings/
Description: Use a PO and MO file to change some hardcoded WooCommerce strings. The same concept works for other plugins and themes.
Author: Damien Carbery
Author URI: https://www.damiencarbery.com
Version: 0.1
*/
class LoadCustomMOFile {
private $loadmo_called;
public function __construct() {
$this->loadmo_called = false;
$this->init();
}
public function init(){
add_filter( 'load_textdomain_mofile', array( $this, 'load_mofile' ), 10, 2 );
add_filter( 'override_load_textdomain', array( $this, 'stop_mofile_loading' ), 10, 3 );
}
function load_mofile( $mofile, $domain ) {
if ( 'woocommerce' == $domain ) {
$this->loadmo_called = true; // Note that the custom MO file has been loaded.
return plugin_dir_path( __FILE__ ) . 'woocommerce-en_US.mo';
}
return $mofile;
}
function stop_mofile_loading( $false, $domain, $mofile ) {
if ( 'woocommerce' == $domain ) {
if ( $this->loadmo_called ) {
return true; // Prevent looking for other WooCommerce MO files.
}
else {
return $false;
}
}
}
}
$load_custom_mo_file = new LoadCustomMOFile();
<?php
// Determine what locale WordPress is currently running.
add_action( 'wp_head', 'dcwd_what_locale' );
add_action( 'admin_head', 'dcwd_what_locale' ); // Run in admin too.
function dcwd_what_locale() {
echo '<!-- Locale: ' . get_locale() . ' -->';
}
[04-Dec-2018 11:08:42 UTC] Mofile: /wp-content/languages/woocommerce/woocommerce-en_US.mo
[04-Dec-2018 11:08:42 UTC] Mofile: /wp-content/languages/plugins/woocommerce-en_US.mo
[04-Dec-2018 11:08:43 UTC] Mofile: /wp-content/languages/woocommerce/woocommerce-en_US.mo
[04-Dec-2018 11:08:43 UTC] Mofile: /wp-content/languages/plugins/woocommerce-en_US.mo
#. Translators: %s Product title.
#: includes/class-wc-form-handler.php:558 includes/class-wc-form-handler.php:562
msgid "%s removed."
msgstr "You have removed %s. Are you sure?"
#: includes/class-wc-form-handler.php:559
msgid "Undo?"
msgstr "Add it back to the cart"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment