In addition to @maxime's answer, I found that the following method worked too. Simply change the $locale
global variable. This will fix most translations. If you want to specify the locale for specific text domains, then simply call the load_textdomain()
function pointing to the appropriate .mo
file.
$locale = 'en_US';
if ( file_exists( WP_LANG_DIR . '/' . $locale . '.mo' ) ) {
load_textdomain( 'my_domain', WP_LANG_DIR . '/' . $locale . '.mo' );
}
Now, in the above example, if your languages
folder contains the file en_US.mo
, you're good to go.
So when you call something like,
echo _e('Hello', 'my_domain');
the appropriate translations in your associated .po
file will be reflected.
You can find a detailed explanation at https://codex.buddypress.org/getting-started/customizing/customizing-labels-messages-and-urls/.
This is intended for BuddyPress, but the idea applies generally.
switch_to_locale()
andrestore_previous_locale()
?