-
-
Save dartrax/9387937ef9752ab3f70c4fde03c9d946 to your computer and use it in GitHub Desktop.
// Save the current language in post_meta when checkout is processed (used to identify correct Email language) | |
add_action('woocommerce_checkout_update_order_meta', 'dartrax_save_language_on_checkout', 10, 2 ); | |
function dartrax_save_language_on_checkout( $order_id, $posted ) { | |
if( ! class_exists('TRP_Translate_Press') ) return ''; | |
global $TRP_LANGUAGE; | |
update_post_meta( $order_id, 'order_language', $TRP_LANGUAGE ); | |
} | |
// Woocommerce Germanized Mails | |
add_action( 'woocommerce_gzd_shipment_status_draft_to_shipped_notification', 'dartrax_prepare_locale_for_Mail_with_shipment_id', 5, 1 ); | |
add_action( 'woocommerce_gzd_shipment_status_processing_to_shipped_notification', 'dartrax_prepare_locale_for_Mail_with_shipment_id', 5, 1 ); | |
add_action( 'woocommerce_gzd_return_shipment_status_draft_to_processing_notification', 'dartrax_prepare_locale_for_Mail_with_shipment_id', 5, 1 ); | |
add_action( 'woocommerce_gzd_return_shipment_status_requested_to_processing_notification', 'dartrax_prepare_locale_for_Mail_with_shipment_id', 5, 1 ); | |
add_action( 'woocommerce_gzd_return_shipment_status_processing_to_delivered_notification', 'dartrax_prepare_locale_for_Mail_with_shipment_id', 5, 1 ); | |
add_action( 'woocommerce_gzd_return_shipment_status_shipped_to_delivered_notification', 'dartrax_prepare_locale_for_Mail_with_shipment_id', 5, 1 ); | |
function dartrax_prepare_locale_for_Mail_with_shipment_id( $shipment_id ) { | |
// get order_id from shipment and proceed with that | |
if( $shipment = wc_gzd_get_shipment( $shipment_id ) ) | |
dartrax_prepare_locale_for_Mail_with_order_id( $shipment->get_order_id() ); | |
} | |
// Woocommerce Shipment Mails | |
add_action( 'woocommerce_order_status_processing_to_cancelled_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_on-hold_to_cancelled_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_pending_to_on-hold_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_failed_to_on-hold_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_cancelled_to_on-hold_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_cancelled_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_failed_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_on-hold_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_pending_to_processing_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_fully_refunded_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_partially_refunded_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_pending_to_failed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_on-hold_to_failed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_pending_to_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_failed_to_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
add_action( 'woocommerce_order_status_cancelled_to_completed_notification','dartrax_prepare_locale_for_Mail_with_order_id', 5, 1 ); | |
function dartrax_prepare_locale_for_Mail_with_order_id( $order_id ) { | |
// Get language code that may be stored in post meta data | |
$lang_code = get_post_meta($order_id, 'order_language', true); | |
if( $lang_code == '' || ! class_exists('TRP_Translate_Press') ) return ''; | |
// Set it as current active language for translatepress | |
global $TRP_LANGUAGE; | |
$TRP_LANGUAGE = $lang_code; | |
} | |
// Woocommerce Mails when resend by Admin | |
add_action( 'woocommerce_before_resend_order_emails','dartrax_prepare_locale_for_resend_Mails', 5, 2 ); | |
function dartrax_prepare_locale_for_resend_Mails( $order, $mail_type ) { | |
// First type belongs to WooCommerce, last two types belong to WooCommerce Germanized | |
if($mail_type == 'customer_invoice' || $mail_type == 'customer_paid_for_order' || $mail_type == 'customer_processing_order') | |
dartrax_prepare_locale_for_Mail_with_order_id( $order->get_id() ); | |
} | |
// Woocommerce Note to customer Mail | |
add_action( 'woocommerce_new_customer_note_notification','dartrax_prepare_locale_for_note_Mails', 5, 1 ); | |
function dartrax_prepare_locale_for_note_Mails( $note_and_order_id ) { | |
// get order_id from argument array and proceed with that | |
dartrax_prepare_locale_for_Mail_with_order_id( $note_and_order_id['order_id'] ); | |
} | |
// Prevent Woocommerce Germanized from filtering the Translatepress Shortcut when legal pages are used in emails | |
add_filter( 'woocommerce_gzd_email_attachment_content_shortcodes_allowed', function( $shortcodes_allowed ) { | |
array_push($shortcodes_allowed, "trp_language"); | |
return $shortcodes_allowed; | |
} ); | |
// Override Locale when WooCommerce sends an eMail | |
add_filter( 'woocommerce_email_setup_locale', function() { | |
if( ! class_exists('TRP_Translate_Press') ) return ''; | |
// Override translatepress 'locale'-function because that does not work in Admin interface | |
add_filter('locale','dartrax_force_trp_locale', 99999 + 1); | |
// Switch text domains to load the correct .po/.mo-file based translations | |
global $TRP_LANGUAGE; | |
switch_text_domains( $TRP_LANGUAGE ); | |
return false; | |
} ); | |
add_filter( 'woocommerce_email_restore_locale', function() { | |
// Undo overriding of translatepress' 'locale'-function | |
remove_filter('locale','dartrax_force_trp_locale', 99999 + 1); | |
return true; | |
} ); | |
// Override translatepress 'locale'-function because that does not deliver $TRP_LANGUAGE in Admin interface | |
function dartrax_force_trp_locale($locale) { | |
global $TRP_LANGUAGE; | |
return $TRP_LANGUAGE; | |
} | |
// Override 'plugin_locale'-function so Woocommerce and Woocommerze Germanized won't use the admin profile language | |
function dartrax_force_woo_locale($locale, $plugin) { | |
global $TRP_LANGUAGE; | |
return $plugin == 'woocommerce' || $plugin == 'woocommerce-germanized' ? $TRP_LANGUAGE : $locale; | |
} | |
// Switch to another text domain. Inspired by https://gist.github.com/Jon007/5b90e78289899bc28e9c39c12ef56e60 | |
function switch_text_domains( $language ) { | |
if ( class_exists( 'TRP_Translate_Press' ) && class_exists( 'WooCommerce' ) && class_exists( 'WooCommerce_Germanized' ) ) { | |
global $locale, $woocommerce, $woocommerce_germanized; | |
// unload plugin's textdomains | |
unload_textdomain( 'default' ); | |
unload_textdomain( 'woocommerce' ); | |
unload_textdomain( 'woocommerce-germanized' ); | |
// set locale to order locale | |
$locale = apply_filters( 'locale', $language ); | |
// Woocommerce and Woocommerce Germanized use the admin profile language instead of the side language. Override with the desired language | |
add_filter('plugin_locale', 'dartrax_force_woo_locale', 10, 2); | |
// (re-)load plugin's textdomain with order locale | |
load_default_textdomain( $language ); | |
$woocommerce->load_plugin_textdomain(); | |
$woocommerce_germanized->load_plugin_textdomain(); | |
$wp_locale = new \WP_Locale(); | |
// Clean up | |
remove_filter('plugin_locale', 'dartrax_force_woo_locale', 10); | |
} | |
} |
Hi @dartrax , thanks for your help.
WordPress version: 5.9
WooCommerce version: 6.2.0
TranslatePress - Developer: 1.0.9
TranslatePress - Multilingual: 2.2.0
Germanized for WooCommerce: 3.83.
WP main language: German (de_DE)
Admin language: English (en_US)
Hi @Nieder-hu,
finally I was able to reproduce your issue. The culprit is the admin user locale not being the sites default locale. The function $woocommerce->load_plugin_textdomain() uses determine_locale(), which returns the user locale if the user is the admin user instead of the site default locale (which was set to the order_language before). I've added a filter to the plugin_locale hook, please tell me if that fixes your problem.
Fix: When the admin profile language did not match the side default language, instead of the order language the admin profile language was used for the translations covered by the woocommerce .mo/.po-files.
Hi @dartrax , thanks it works well.
Fix: Since a recent update of WooCommerce Germanized, the trp_language conditional shortcode got ignored when used in legal pages included with mails. This resulted in mails in which, for example, the terms and conditions occurred several times in a row in all available languages. This is fixed now.
For everyone's interest:
I've used the new Mail localisation feature that was natively build into TranslatePress for a while now, and I did not encounter any problems.
Just this little peace of code is still necessary, if you want to use the TranslatePress' conditional shortcuts in Emails, because otherwise they get filtered by the Germanized Extension.
// Prevent Woocommerce Germanized from filtering the Translatepress Shortcut when legal pages are used in emails
add_filter( 'woocommerce_gzd_email_attachment_content_shortcodes_allowed', function( $shortcodes_allowed ) {
array_push($shortcodes_allowed, "trp_language");
return $shortcodes_allowed;
} );
Hi @Nieder-hu,
I‘d like to make a test environment with the same settings as you have to see if I can reproduce that issue. Can you give me the following information:
Wordpress Version
Woocommerce Version
Translatepress Version
Translatepress Business Version if installed
Germanized Version
Wordpress Main language in general settings
Admin user language in user profile
I‘m not sure if it has something to do with different Page default/Admin languages (a case that I did not test yet) or with plugin versions (I still use Woocommerce 5.x).