WP_Locale_Switcher::switch_to_locale() WordPress Method

The WP_Locale_Switcher::switch_to_locale() method allows you to change the current language for the Wordpress site. This is useful if you need to display content in a different language or if you want to use Wordpress in a language other than the default.

WP_Locale_Switcher::switch_to_locale( string $locale ) #

Switches the translations according to the given locale.


Parameters

$locale

(string)(Required)The locale to switch to.


Top ↑

Return

(bool) True on success, false on failure.


Top ↑

Source

File: wp-includes/class-wp-locale-switcher.php

	public function switch_to_locale( $locale ) {
		$current_locale = determine_locale();
		if ( $current_locale === $locale ) {
			return false;
		}

		if ( ! in_array( $locale, $this->available_languages, true ) ) {
			return false;
		}

		$this->locales[] = $locale;

		$this->change_locale( $locale );

		/**
		 * Fires when the locale is switched.
		 *
		 * @since 4.7.0
		 *
		 * @param string $locale The new locale.
		 */
		do_action( 'switch_locale', $locale );

		return true;
	}


Top ↑

Changelog

Changelog
VersionDescription
4.7.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.