WP_Locale_Switcher
- Since
- 4.7.0
- Source
wp-includes/class-wp-locale-switcher.php:15
Core class used for switching locales.
Compatibility
- WordPress
- since 4.7.0
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0).
Hooks and filters fired · 3
Every hook that fires from inside WP_Locale_Switcher, in the order it appears in the class, grouped by the method that fires it.
Properties · 3
$stackarrayprivate- Locale switching stack.
$original_localestringprivate- Original locale.
$available_languagesstring[]private- Holds all available languages.
Methods · 12
- __construct()Constructor.
- init()Initializes the locale switcher.
- switch_to_locale()Switches the translations according to the given locale.
- switch_to_user_locale()Switches the translations according to the given user's locale.
- restore_previous_locale()Restores the translations according to the previous locale.
- restore_current_locale()Restores the translations according to the original locale.
- is_switched()Whether switch_to_locale() is in effect.
- get_switched_locale()Returns the locale currently switched to.
- get_switched_user_id()Returns the user ID related to the currently switched locale.
- filter_locale()Filters the locale of the WordPress installation.
- load_translations()Load translations for a given locale.
- change_locale()Changes the site's locale to the given one.
Source code
#[AllowDynamicProperties]class WP_Locale_Switcher { /** * Locale switching stack. * * @since 6.2.0 * @var array */ private $stack = array(); /** * Original locale. * * @since 4.7.0 * @var string */ private $original_locale; /** * Holds all available languages. * * @since 4.7.0 * @var string[] An array of language codes (file names without the .mo extension). */ private $available_languages; /** * Constructor. * * Stores the original locale as well as a list of all available languages. * * @since 4.7.0 */ public function __construct() { $this->original_locale = determine_locale(); $this->available_languages = array_merge( array( 'en_US' ), get_available_languages() ); } /** * Initializes the locale switcher. * * Hooks into the {@see 'locale'} and {@see 'determine_locale'} filters * to change the locale on the fly. * * @since 4.7.0 */ public function init() { add_filter( 'locale', array( $this, 'filter_locale' ) ); add_filter( 'determine_locale', array( $this, 'filter_locale' ) ); } /** * Switches the translations according to the given locale. * * @since 4.7.0 * * @param string $locale The locale to switch to. * @param int|false $user_id Optional. User ID as context. Default false. * @return bool True on success, false on failure. */ public function switch_to_locale( $locale, $user_id = false ) { $current_locale = determine_locale(); if ( $current_locale === $locale ) { return false; } if ( ! in_array( $locale, $this->available_languages, true ) ) { return false; } $this->stack[] = array( $locale, $user_id ); $this->change_locale( $locale ); /** * Fires when the locale is switched. * * @since 4.7.0 * @since 6.2.0 The `$user_id` parameter was added. *Changelog
Introduced in 4.7.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/class-wp-locale-switcher.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.