src/Hyyan/WPI/Emails.php
/**
     * Translates Woocommerce email subjects and headings content to the order language.
     *
     * @param string   $formatted_string      Subject or heading not translated but already with token replacements
     * @param WC_Order $order       Order object
     * @param string   $string_type Type of string to translate <subject | heading>
     * @param string   $email_type  Email template
     *
     * @return string Translated string, returns the original $string on error
     */
Translates Woocommerce email subjects and headings content to the order language.
@param string   $formatted_string      Subject or heading not translated but already with token replacements
@param WC_Order $order       Order object
@param string   $string_type Type of string to translate 
@param string   $email_type  Email template

@return string Translated string, returns the original $string on error
Translates Woocommerce email subjects and headings content to the order language . @param string $formatted_string Subject or heading not translated but already with token replacements @param WC_Order $order Order object @param string $string_type Type of string to translate @param string $email_type Email template @return string Translated string returns the original $string on error
public function translateEmailStringToOrderLanguage( $formatted_string, $order, $string_type, $email_type ) 
	{
		$order_locale = ($order) ? pll_get_post_language( Utilities::get_orderid( $order ), 'locale' ) : '';
		if ( $order_locale == '' ) {
			$order_locale = pll_current_language( 'locale' );
			if ( ! ($order_locale) ) {
				return $formatted_string;
        }
      }
		$current_locale = pll_current_language( 'locale' );
		if ( ! $current_locale ) {
			$current_locale = get_locale();
		}
		$base_locale = ( is_multisite() ) ? get_site_option( 'WPLANG' ) : get_option( 'WPLANG' );
		if ( ! $base_locale ) {
			$base_locale = pll_default_language( 'locale' );
			if ( ! $base_locale ) {
				$base_locale = get_locale();
			}
		}
		$string_template = $this->getEmailSetting( $string_type, $email_type );
		if ( $string_template ) {
			$string_template_translated = pll_translate_string( $string_template, $order_locale );
			if ( $string_template_translated != $string_template ) {
				$string_template = $string_template_translated;
			} elseif ( $order_locale != $base_locale ) {
				if ( $order_locale != $current_locale ) {
					Utilities::switchLocale( $order_locale );
				}
				$string_template = $this->get_default_setting( $string_type );
			}
		} else {
			if ( $order_locale == $current_locale || ( ! is_admin()) ) {
				return $formatted_string;
      } else {
				if ( $order_locale != $current_locale ) {
					Utilities::switchLocale( $order_locale );
				}
				$string_template = $this->get_default_setting( $string_type );
			}
		}
      if ( ! $string_template ) {
            return $formatted_string;
      }
      if ( $order_locale != $current_locale ) {
          Utilities::switchLocale( $order_locale );
      }
      if ($order) {
        $find    = array();
        $replace = array();
        $find['order-date']   = '{order_date}';
        $find['order-number'] = '{order_number}';
        $find['site_title']   = '{site_title}';
        $replace['order-date']   = date_i18n(wc_date_format(), strtotime($order->get_date_created()));
        $replace['order-number'] = $order->get_order_number();
        $replace['site_title']   = get_bloginfo('name');
        $formatted_string = str_replace( apply_filters( HooksInterface::EMAILS_ORDER_FIND_REPLACE_FIND_FILTER, $find, $order ), apply_filters( HooksInterface::EMAILS_ORDER_FIND_REPLACE_REPLACE_FILTER, $replace, $order ), $string_template );
      }
		return $formatted_string;
    }
public function translateEmailStringToOrderLanguage ( $ formatted_string , $ order , $ string_type , $ email_type ) { $ order_locale = ( $ order ) ? pll_get_post_language ( Utilities :: get_orderid ( $ order ) , 'locale' ) : '' ; if ( $ order_locale == '' ) { $ order_locale = pll_current_language ( 'locale' ) ; if ( ! ( $ order_locale ) ) { return $ formatted_string ; } } $ current_locale = pll_current_language ( 'locale' ) ; if ( ! $ current_locale ) { $ current_locale = get_locale ( ) ; } $ base_locale = ( is_multisite ( ) ) ? get_site_option ( 'WPLANG' ) : get_option ( 'WPLANG' ) ; if ( ! $ base_locale ) { $ base_locale = pll_default_language ( 'locale' ) ; if ( ! $ base_locale ) { $ base_locale = get_locale ( ) ; } } $ string_template = $ this -> getEmailSetting ( $ string_type , $ email_type ) ; if ( $ string_template ) { $ string_template_translated = pll_translate_string ( $ string_template , $ order_locale ) ; if ( $ string_template_translated != $ string_template ) { $ string_template = $ string_template_translated ; } elseif ( $ order_locale != $ base_locale ) { if ( $ order_locale != $ current_locale ) { Utilities :: switchLocale ( $ order_locale ) ; } $ string_template = $ this -> get_default_setting ( $ string_type ) ; } } else { if ( $ order_locale == $ current_locale || ( ! is_admin ( ) ) ) { return $ formatted_string ; } else { if ( $ order_locale != $ current_locale ) { Utilities :: switchLocale ( $ order_locale ) ; } $ string_template = $ this -> get_default_setting ( $ string_type ) ; } } if ( ! $ string_template ) { return $ formatted_string ; } if ( $ order_locale != $ current_locale ) { Utilities :: switchLocale ( $ order_locale ) ; } if ( $ order ) { $ find = array ( ) ; $ replace = array ( ) ; $ find [ 'order-date' ] = '{order_date}' ; $ find [ 'order-number' ] = '{order_number}' ; $ find [ 'site_title' ] = '{site_title}' ; $ replace [ 'order-date' ] = date_i18n ( wc_date_format ( ) , strtotime ( $ order -> get_date_created ( ) ) ) ; $ replace [ 'order-number' ] = $ order -> get_order_number ( ) ; $ replace [ 'site_title' ] = get_bloginfo ( 'name' ) ; $ formatted_string = str_replace ( apply_filters ( HooksInterface :: EMAILS_ORDER_FIND_REPLACE_FIND_FILTER , $ find , $ order ) , apply_filters ( HooksInterface :: EMAILS_ORDER_FIND_REPLACE_REPLACE_FILTER , $ replace , $ order ) , $ string_template ) ; } return $ formatted_string ; }
Translates Woocommerce email subjects and headings content to the order language.
Translates Woocommerce email subjects and headings content to the order language .
//allow function to be called with no order to try to pick up pll locale for footer, from address and name //get both current language and base language which in admin could be different both from each other and from order language //if custom string is set in WooCommerce admin settings //check for a translation in polylang //if there is translation, use it // if there was no template translation, and other language needed // get the default for the language from the translation files // Switch language if current locale is not the same as the order //no template is set, woocommerce will be using language files //in this case if order is in current language or we are not in admin //then the starting string should already be correct // Switch language if current locale is not the same as the order // No setting in Polylang strings translations table nor default string // Switch language if current locale is not the same as the order // before starting variable replacements //perform standard replacements on template