Programmatically choose language for translation

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.

tech