WordPress at a glance

get_locale() WP 1.5.0

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

The second and subsequent calls, the function will return $locale 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().

Is the basis for: determine_locale()
✈ 1 time = 0.000015s = very fast | 50000 times = 0.05s = speed of light
Hooks from the function
Return

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

Usage

get_locale();

Examples

#1 Get the current locale

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

#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' );

#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
  • Global. String. $wp_local_package

Changelog

Since 1.5.0 Introduced.

Code of get locale: wp-includes/l10n.php WP 5.2.2

<?php
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' ) ) ) {
			$ms_locale = get_site_option( 'WPLANG' );
		}

		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 );
}

Related Functions

From tag: locale (site language)

More from tag: localize (l10n i18n Internationalization localization)

More from category: Localization

No comments
    Hello, !     Log In . Register