Send emails in user’s preferred language

TranslatePress displays front-end content in the preferred language of the user. If any emails are triggered by email action (such as registration, placing an order) then the email will be sent in the user’s language. However, if the email sent to the user is triggered by an admin action (or other users) then it might not arrive in the recipient’s preferred language. Translating WooCommerce emails in the preferred user’s language is entirely supported by TranslatePress, but if you are looking to provide support for another plugin/theme use the following code.

$user_language = trp_get_user_language( $user_id );
trp_switch_language( $user_language );

/* Load plugin textdomain in the user's language. Do this for all plugins you intend to use. 
 * Make sure the localization functions ( such as __() ) applied on the strings needed are called after this point. 
 * If they are already called and stored in a variable then they won't be in the user's language.

// generate localized strings
$message = __('Email content', 'my-domain');

// this text will be translated by TranslatePress as a regular string using existing manual translations (or machine translated if available)
$message .= 'User-inputted content written in the default language';

//send email using wp_mail function
wp_mail( $email, $subject, $message);

 * Restores previous language so that the rest of the page loading happens in the expected language, 
 * which is not necessarily the $user_language

// call the function to load textdomain back in the previous language

 * Example to load woocommerce textdomain in the user's language. 
function load_my_textdomain(){
   $locale = determine_locale();

   unload_textdomain( 'woocommerce' );
   load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
   load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );

The preferred language of the user is automatically stored and update when the logged in user visits any page in front-end on its last visit.

It can be viewed and changed in the default WP Edit Profile form. In order to stop the language from being updated automatically, you can check the Always use this language checkbox. The user will still be able to change it’s front-end language preference using the front-end language switcher.

Looking to go multilingual?

Get full access to TranslatePress and join 200.000+ website owners that grow their multilingual traffic.

Unlock PREMIUM Features

Or download FREE version