is_locale_switched()
Checks if the locale (translation language) has been switched, whether the switch_to_locale() function has been used.
For more details on switching locales, read the description of switch_to_locale()
1 time — 0.000019 sec (very fast) | 50000 times — 0.02 sec (speed of light) | PHP 7.0.14, WP 4.7
No Hooks.
Returns
true|false.
true - if the locale is currently switched.
false - if it is not switched.
Usage
if( is_locale_switched() ){
// locale is switched
}
Examples
#1 Checking site language switching
Let's say our site runs in Russian (ru_RU) by default. But we switched the user's country by IP and switched the language to English (en_US). Now we need to tell the user that the language is switched. To do this, display a message, for example, in the header of the site (file header.php):
if( is_locale_switched() ){
echo 'The site language is switched to '. get_locale();
}
Notes
- Global. WP_Locale_Switcher. $wp_locale_switcher WordPress locale switcher object.
Changelog
| Since 4.7.0 | Introduced. |
is_locale_switched() is locale switched code WP 6.8.3
function is_locale_switched() {
/* @var WP_Locale_Switcher $wp_locale_switcher */
global $wp_locale_switcher;
return $wp_locale_switcher->is_switched();
}