Skip to content
This repository was archived by the owner on Mar 17, 2022. It is now read-only.

Commit 89ca15d

Browse files
committed
fixes #401 email subject and title translations
fixes #401 email subject and title translations
1 parent 498bdc9 commit 89ca15d

File tree

1 file changed

+59
-49
lines changed

1 file changed

+59
-49
lines changed

src/Hyyan/WPI/Emails.php

+59-49
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Emails
3636
public function __construct()
3737
{
3838
if ('on' === Settings::getOption('emails', Features::getID(), 'on')) {
39-
add_filter('plugin_locale', array($this, 'correctLocal'), 100);
39+
add_filter( 'plugin_locale', array( $this, 'correctLocal' ), 999 );
4040

4141
// Register WooCommerce email subjects and headings in polylang strings translations table
4242
$this->registerEmailStringsForTranslation(); // called only after all plugins are loaded
@@ -113,36 +113,38 @@ public function registerEmailStringsForTranslation()
113113
'cancelled_order',
114114
'failed_order',
115115
), $this);
116+
$wc_emails = \WC_Emails::instance();
117+
$emails = $wc_emails->get_emails();
116118

117119
$this->default_settings = apply_filters(HooksInterface::EMAILS_DEFAULT_SETTINGS_FILTER, array(
118-
'new_order_subject' => __('[{site_title}] New customer order ({order_number}) - {order_date}', 'woocommerce'),
119-
'new_order_heading' => __('New customer order', 'woocommerce'),
120-
'customer_processing_order_subject' => __('Your {site_title} order receipt from {order_date}', 'woocommerce'),
121-
'customer_processing_order_heading' => __('Thank you for your order', 'woocommerce'),
122-
'customer_refunded_order_subject_partial' => __('Your {site_title} order from {order_date} has been partially refunded', 'woocommerce'),
123-
'customer_refunded_order_heading_partial' => __('Your order has been partially refunded', 'woocommerce'),
124-
'customer_refunded_order_subject_full' => __('Your {site_title} order from {order_date} has been refunded', 'woocommerce'),
125-
'customer_refunded_order_heading_full' => __('Your order has been fully refunded', 'woocommerce'),
126-
'customer_note_subject' => __('Note added to your {site_title} order from {order_date}', 'woocommerce'),
127-
'customer_note_heading' => __('A note has been added to your order', 'woocommerce'),
128-
'customer_invoice_subject_paid' => __('Your {site_title} order from {order_date}', 'woocommerce'),
129-
'customer_invoice_heading_paid' => __('Order {order_number} details', 'woocommerce'),
130-
'customer_invoice_subject' => __('Invoice for order {order_number} from {order_date}', 'woocommerce'),
131-
'customer_invoice_heading' => __('Invoice for order {order_number}', 'woocommerce'),
132-
'customer_completed_order_subject' => __('Your {site_title} order from {order_date} is complete', 'woocommerce'),
133-
'customer_completed_order_heading' => __('Your order is complete', 'woocommerce'),
134-
'customer_completed_order_subject_downloadable' => __('Your {site_title} order from {order_date} is complete - download your files', 'woocommerce'),
135-
'customer_completed_order_heading_downloadable' => __('Your order is complete - download your files', 'woocommerce'),
136-
'customer_new_account_subject' => __('Your account on {site_title}', 'woocommerce'),
137-
'customer_new_account_heading' => __('Welcome to {site_title}', 'woocommerce'),
138-
'customer_reset_password_subject' => __('Password Reset for {site_title}', 'woocommerce'),
139-
'customer_reset_password_heading' => __('Password Reset Instructions', 'woocommerce'),
140-
'customer_on_hold_order_subject' => __('Your {site_title} order receipt from {order_date}', 'woocommerce'),
141-
'customer_on_hold_order_heading' => __('Thank you for your order', 'woocommerce'),
142-
'cancelled_order_subject' => __('[{site_title}] Cancelled order ({order_number})', 'woocommerce'),
143-
'cancelled_order_heading' => __('Cancelled order', 'woocommerce'),
144-
'failed_order_subject' => __('[{site_title}] Failed order ({order_number})', 'woocommerce'),
145-
'failed_order_heading' => __('Failed order', 'woocommerce'),
120+
'new_order_subject' => __( $emails[ 'WC_Email_New_Order' ]->get_default_subject(), 'woocommerce' ),
121+
'new_order_heading' => __( $emails[ 'WC_Email_New_Order' ]->get_default_heading(), 'woocommerce' ),
122+
'customer_processing_order_subject' => __( $emails[ 'WC_Email_Customer_Processing_Order' ]->get_default_subject(), 'woocommerce' ),
123+
'customer_processing_order_heading' => __( $emails[ 'WC_Email_Customer_Processing_Order' ]->get_default_heading(), 'woocommerce' ),
124+
'customer_refunded_order_subject_partial' => __( $emails[ 'WC_Email_Customer_Refunded_Order' ]->get_default_subject( true ), 'woocommerce' ),
125+
'customer_refunded_order_heading_partial' => __( $emails[ 'WC_Email_Customer_Refunded_Order' ]->get_default_heading( true ), 'woocommerce' ),
126+
'customer_refunded_order_subject_full' => __( $emails[ 'WC_Email_Customer_Refunded_Order' ]->get_default_subject(), 'woocommerce' ),
127+
'customer_refunded_order_heading_full' => __( $emails[ 'WC_Email_Customer_Refunded_Order' ]->get_default_heading(), 'woocommerce' ),
128+
'customer_note_subject' => __( $emails[ 'WC_Email_Customer_Note' ]->get_default_subject(), 'woocommerce' ),
129+
'customer_note_heading' => __( $emails[ 'WC_Email_Customer_Note' ]->get_default_heading(), 'woocommerce' ),
130+
'customer_invoice_subject_paid' => __( $emails[ 'WC_Email_Customer_Invoice' ]->get_default_subject( true ), 'woocommerce' ),
131+
'customer_invoice_heading_paid' => __( $emails[ 'WC_Email_Customer_Invoice' ]->get_default_heading( true ), 'woocommerce' ),
132+
'customer_invoice_subject' => __( $emails[ 'WC_Email_Customer_Invoice' ]->get_default_subject(), 'woocommerce' ),
133+
'customer_invoice_heading' => __( $emails[ 'WC_Email_Customer_Invoice' ]->get_default_heading(), 'woocommerce' ),
134+
'customer_completed_order_subject' => __( $emails[ 'WC_Email_Customer_Completed_Order' ]->get_default_subject(), 'woocommerce' ),
135+
'customer_completed_order_heading' => __( $emails[ 'WC_Email_Customer_Completed_Order' ]->get_default_heading(), 'woocommerce' ),
136+
'customer_completed_order_subject_downloadable' => __( $emails[ 'WC_Email_Customer_Completed_Order' ]->get_default_subject(), 'woocommerce' ),
137+
'customer_completed_order_heading_downloadable' => __( $emails[ 'WC_Email_Customer_Completed_Order' ]->get_default_heading(), 'woocommerce' ),
138+
'customer_new_account_subject' => __( $emails[ 'WC_Email_Customer_New_Account' ]->get_default_subject(), 'woocommerce' ),
139+
'customer_new_account_heading' => __( $emails[ 'WC_Email_Customer_New_Account' ]->get_default_heading(), 'woocommerce' ),
140+
'customer_reset_password_subject' => __( $emails[ 'WC_Email_Customer_Reset_Password' ]->get_default_subject(), 'woocommerce' ),
141+
'customer_reset_password_heading' => __( $emails[ 'WC_Email_Customer_Reset_Password' ]->get_default_heading(), 'woocommerce' ),
142+
'customer_on_hold_order_subject' => __( $emails[ 'WC_Email_Customer_On_Hold_Order' ]->get_default_subject(), 'woocommerce' ),
143+
'customer_on_hold_order_heading' => __( $emails[ 'WC_Email_Customer_On_Hold_Order' ]->get_default_heading(), 'woocommerce' ),
144+
'cancelled_order_subject' => __( $emails[ 'WC_Email_Cancelled_Order' ]->get_default_subject(), 'woocommerce' ),
145+
'cancelled_order_heading' => __( $emails[ 'WC_Email_Cancelled_Order' ]->get_default_heading(), 'woocommerce' ),
146+
'failed_order_subject' => __( $emails[ 'WC_Email_Failed_Order' ]->get_default_subject(), 'woocommerce' ),
147+
'failed_order_heading' => __( $emails[ 'WC_Email_Failed_Order' ]->get_default_heading(), 'woocommerce' ),
146148
), $this);
147149

148150
// Register strings for translation and hook filters
@@ -668,31 +670,39 @@ public function translateEmailString($string, $string_type, $email_type)
668670
*/
669671
public function translateEmailStringToOrderLanguage($string, $order, $string_type, $email_type)
670672
{
671-
//allow function to be called with no order to try to pick up pll locale for footer, from address and name
672-
$order_language = ($order) ? pll_get_post_language(Utilities::get_orderid($order), 'locale') : '';
673-
if ($order_language == '') {
674-
$order_language = pll_current_language('locale');
675-
if (!($order_language)) {
676-
return $string;
677-
}
673+
//allow function to be called with no order to try to pick up pll locale for footer, from address and name
674+
$order_language = ($order) ? pll_get_post_language( Utilities::get_orderid( $order ), 'locale' ) : '';
675+
if ( $order_language == '' ) {
676+
$order_language = pll_current_language( 'locale' );
677+
if ( ! ($order_language) ) {
678+
return $string;
678679
}
679-
680-
// Get setting used to register string in the Polylang strings translation table
681-
$_string = $string; // Store original string to return in case of error
682-
if (false == ($string = $this->getEmailSetting($string_type, $email_type)) && !isset($this->default_settings[$email_type . '_' . $string_type])) {
683-
return $_string; // No setting in Polylang strings translations table nor default string found to translate
680+
}
681+
$locale = get_locale();
682+
$baseLocale = get_option( 'WPLANG' );
683+
684+
// Get setting used to register string in the Polylang strings translation table
685+
$_string = $string; // Store original string to return in case of error
686+
// Switch language
687+
if ( $order_language != $locale ) {
688+
$test = $this->getEmailSetting( $string_type, $email_type );
689+
if ( ! $test ) {
690+
$test = $this->default_settings[ $email_type . '_' . $string_type ];
684691
}
692+
if ( ! $test ) {
693+
return $_string; // No setting in Polylang strings translations table nor default string found to translate
694+
}
695+
$string = $test;
696+
$this->switchLanguage( $order_language );
685697

686-
// Switch language
687-
$this->switchLanguage($order_language);
688-
689-
if ($string) {
690-
// Retrieve translation from Polylang Strings Translations table
691-
$string = pll__($string);
698+
$test = pll_translate_string( $string, $order_language );
699+
if ( $test != $string ) {
700+
$string = $test;
692701
} else {
693-
// If no user translation found in Polylang Strings Translations table, use WooCommerce default translation
694-
$string = __($this->default_settings[$email_type . '_' . $string_type], 'woocommerce');
702+
// If no user translation found in Polylang Strings Translations table, use WooCommerce default translation
703+
$string = __( $this->default_settings[ $email_type . '_' . $string_type ], 'woocommerce' );
695704
}
705+
}
696706

697707
if ($order) {
698708
$find = array();

0 commit comments

Comments
 (0)