get_locale()WP 1.5.0

Sets the global variable $locale and gets the current site locale (for example, en_US).

For the second and subsequent calls, the function will return $locale global variable passed through the locale filter.

You can get the list of locales here.

Until version 4.0, the locale was defined by WPLANG constant within the wp-config.php. Now this constant is deprecated.

Usually this function is used by one of localization functions, like load_textdomain().

Since version 5.0, it's better to use a wrapper of this function: determine_locale().

1 time — 0.000015 sec (very fast) | 50000 times — 0.05 sec (speed of light)
Hooks from the function

Return

String. The locale of the blog or from the locale hook.

Usage

get_locale();

Examples

0

#1 Get the current locale

echo get_locale();
// output is `en_US`
0

#2 Set locale on the server

This example demonstrates how to set the LC_MONETARY locale. If the base locale is not set, then specify the 'en_US' locale this way:

setlocale( LC_MONETARY, get_locale() );
$my_local_settings = localeconv();

if( ! $my_local_settings['int_curr_symbol'] ){
	  setlocale( LC_MONETARY, 'en_US' );
}
0

#3 Get the value of the current locale

setlocale( LC_MONETARY, get_locale() );
$my_local_settings = localeconv();
print_r( $my_local_settings );

Notes

  • Global. String. $locale The current locale.
  • Global. String. $wp_local_package Locale code of the package.

Changelog

Since 1.5.0 Introduced.

get_locale() code WP 6.5.2

function get_locale() {
	global $locale, $wp_local_package;

	if ( isset( $locale ) ) {
		/** This filter is documented in wp-includes/l10n.php */
		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() ) {
			$ms_locale = get_site_option( 'WPLANG' );
		} else {
			$ms_locale = get_option( 'WPLANG' );
			if ( false === $ms_locale ) {
				$ms_locale = get_site_option( 'WPLANG' );
			}
		}

		if ( false !== $ms_locale ) {
			$locale = $ms_locale;
		}
	} else {
		$db_locale = get_option( 'WPLANG' );
		if ( false !== $db_locale ) {
			$locale = $db_locale;
		}
	}

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

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