Is there a way like switch_to_blog()
for switching the language in WordPress.
Something like
global $locale
$currentLanguage = $locale;
switch_to_language('de_DE');
//do some action with german localisation
switch_to_language($currentLanguage);
Is this possible in general with WordPress?
So I finally found the solution. The function is called load_textdomain()
This is how it's done on my side. Keep in mind to define LANGUAGE_PATH
and the language you would like to switch to in $new_language
. $your_domain
is the text domain of your plugin/theme
//will output "Good Morning"
_e('Good Morning', $your_domain);
global $locale;
//save the current language for later
$current_language = $locale;
$new_language = 'DE_de';
//load the new text domain
load_textdomain( $your_language_domain, LANGUAGE_PATH.'/'.$your_domain.'-'.$new_language.'.mo' );
//do some action with the new localisation
//will output "Guten Morgen"
_e('Good Morning', $your_domain);
//go back to the previous language
load_textdomain( $your_language_domain , LANGUAGE_PATH.'/'.$your_domain.'-'.$current_language.'.mo' );
Took a while to find this method in the core. Read more about that function on the codex site
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With