Using WordPress ‘restore_current_locale()’ PHP function

The restore_current_locale() WordPress PHP function restores the translations according to the original locale.

Usage

To use the function, simply call it in your code like this:

restore_current_locale();

Parameters

There are no parameters for this function.

More information

See WordPress Developer Resources: restore_current_locale()

Examples

Temporarily change the locale and restore it

Restore the original locale after temporarily changing it for a specific task.

// Temporarily change the locale to French
switch_to_locale('fr_FR');

// Execute your task here (e.g., display a French message)

// Restore the original locale
restore_current_locale();

Display a translated string in a different language and then restore the original locale

// Save the current locale
$original_locale = get_locale();

// Temporarily switch to Spanish locale
switch_to_locale('es_ES');

// Display a translated string in Spanish
_e('Hello, World!', 'textdomain');

// Restore the original locale
restore_current_locale();

Translate a string in multiple languages

// Save the current locale
$original_locale = get_locale();

// Translate and display string in German
switch_to_locale('de_DE');
_e('Welcome to our website!', 'textdomain');

// Translate and display string in Italian
switch_to_locale('it_IT');
_e('Welcome to our website!', 'textdomain');

// Restore the original locale
restore_current_locale();

Send an email notification in the user’s language

// Get the user's preferred language
$user_locale = get_user_locale($user_id);

// Save the current locale
$original_locale = get_locale();

// Switch to the user's locale
switch_to_locale($user_locale);

// Set email content with translated strings
$subject = __('New Notification', 'textdomain');
$message = __('You have a new message.', 'textdomain');

// Send email
wp_mail($user_email, $subject, $message);

// Restore the original locale
restore_current_locale();

Display a translated date format in a different locale

// Save the current locale
$original_locale = get_locale();

// Temporarily switch to Japanese locale
switch_to_locale('ja');

// Display a translated date format in Japanese
$date_format = __('F j, Y', 'textdomain');
echo date_i18n($date_format, time());

// Restore the original locale
restore_current_locale();