WP_Locale_Switcher::switch_to_locale()publicWP 4.7.0

Switches the translations according to the given locale.

Метод класса: WP_Locale_Switcher{}

Хуки из метода

Возвращает

true|false. True on success, false on failure.

Использование

$WP_Locale_Switcher = new WP_Locale_Switcher();
$WP_Locale_Switcher->switch_to_locale( $locale, $user_id );
$locale(строка) (обязательный)
The locale to switch to.
$user_id(int|false)
User ID as context.
По умолчанию: false

Список изменений

С версии 4.7.0 Введена.

Код WP_Locale_Switcher::switch_to_locale() WP 6.5.2

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.
	 *
	 * @param string    $locale  The new locale.
	 * @param false|int $user_id User ID for context if available.
	 */
	do_action( 'switch_locale', $locale, $user_id );

	return true;
}