I have a multilanguage website (to do it I use both Polylang and Loco Translate).

My problem is that I want to send an email to our users, but I don't know in which language to send it until I retrive the record from the DB (so I can't simply set a different WP_LANGUAGE)

This is pseudocode for what I would like to do:

$wpdb->query($wpdb->prepare("SELECT userName,language FROM wp_order WHERE id =%s",$id_order));

$result = $wpdb->last_result;


   //SET LANGUAGE FOR TRANSLATION $result->language

   $emailBody= "Hi".$result->name." "._("text to translate","pluginname");

   /* ... generate email ... */

   wp_mail($email, $subject, $body, $headers);

1 Answer 1


After looking at the l10n.php I found a way to switch the textdomain using the unload_textdomain function.

So I created a simple switch

function switch_textdomain( $domain, $newLocale){
    This will only change the textdomain, not the locale.
    It will only affect what kind of language will be used for the _e(), _(), __() commands for the $domain

   unload_textdomain( $domain );

   /*mofile Folder
     this may have to be changed depending on what you want to switch*/
   $path = get_template_directory();
   $mofile = "{$path}/{$newLocale}.mo";

   return load_textdomain( $domain, $mofile );


This will not actually change the locale, but will only change the textdomain for the specified domain (your theme, or plugin).

Since in my case I only needed the translation this is good enough for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.