WordPress at a glance

locale filter-hook . WP 1.5.0

Allows you to change the name of the locale (en_US), which WordPress gets in too many places around its code, via the function get_locale().

Usage

add_filter( 'locale', 'filter_function_name_9802' );
function filter_function_name_9802( $locale ){
	// filter...

	return $locale;
}
$locale(string)
The locale ID. Example: en_US, ru_RU.

Examples

#1 Change the localization language

This example shows how to change the localization language in WordPress based on the language variable passed in $_GET query parameter:

add_filter( 'locale', 'set_my_locale' );
function set_my_locale( $lang ) {
	if( 'ru' == $_GET['language'] )
		return 'ru_RU';
	else
		return $lang;
}

Where the hook is used

Code of filter-hook locale

Code fragment from: wp-includes/l10n.php VER 4.9.8
...
 * @global string $wp_local_package
 *
 * @return string The locale of the blog or from the {@see 'locale'} hook.
 */
function get_locale() {
	global $locale, $wp_local_package;

	if ( isset( $locale ) ) {
		/**
		 * Filters the locale ID of the WordPress installation.
		 *
		 * @since 1.5.0
		 *
		 * @param string $locale The locale ID.
		 */
		return apply_filters( 'locale', $locale );
	}

	if ( isset( $wp_local_package ) ) {
		$locale = $wp_local_package;
	}

	// WPLANG was defined in wp-config.
	if ( defined( 'WPLANG' ) ) {
		$locale = WPLANG;
	}

	// If multisite, check options.
	if ( is_multisite() ) {
		// Don't check blog option when installing.
		if ( wp_installing() || ( false === $ms_locale = get_option( 'WPLANG' ) ) ) {
...
Code fragment from: wp-includes/l10n.php VER 4.9.8
...
		if ( $ms_locale !== false ) {
			$locale = $ms_locale;
		}
	} else {
		$db_locale = get_option( 'WPLANG' );
		if ( $db_locale !== false ) {
			$locale = $db_locale;
		}
	}

	if ( empty( $locale ) ) {
		$locale = 'en_US';
	}

	/** This filter is documented in wp-includes/l10n.php */
	return apply_filters( 'locale', $locale );
}

/**
 * Retrieves the locale of a user.
 *
 * If the user has a locale set to a non-empty string then it will be
 * returned. Otherwise it returns the locale of get_locale().
 *
 * @since 4.7.0
 *
 * @param int|WP_User $user_id User's ID or a WP_User object. Defaults to current user.
 * @return string The locale of the user.
 */
function get_user_locale( $user_id = 0 ) {
	$user = false;
...
No comments
    Hello, !     Log In . Register