restore_current_locale()WP 4.7.0

Restores the original locale of the current request and its translations.

The function reverses all locale changes made through switch_to_locale() and clears the switch stack.

When restoring, it updates translations, the global $wp_locale object, and the translation system locale. The change_locale and restore_previous_locale actions then fire.

Use restore_previous_locale() when you only need to reverse the last switch and return to the previous locale.

No Hooks.

Returns

string|false.

  • string - the restored original locale code.
  • false - the locale switcher is unavailable or the locale has not been switched.

Usage

restore_current_locale();

Examples

#1 Temporarily switch the language

Generate translated text, then restore the original locale:

$recipient_locale = 'de_DE';

if ( switch_to_locale( $recipient_locale ) ) {
	try {
		$subject = __( 'Your order is ready', 'my-plugin' );
	} finally {
		restore_current_locale();
	}
}

Notes

Global. WP_Locale_Switcher. $wp_locale_switcher WordPress locale switcher object.

Changelog

Since 4.7.0 Introduced.

restore_current_locale() code WP 7.1.1

function restore_current_locale() {
	/* @var WP_Locale_Switcher $wp_locale_switcher */
	global $wp_locale_switcher;

	if ( ! $wp_locale_switcher ) {
		return false;
	}

	return $wp_locale_switcher->restore_current_locale();
}