* Retrieves the current locale.
* If the locale is set, then it will filter the locale in the {@see 'locale'}
* filter hook and return the value.
* If the locale is not set already, then the WPLANG constant is used if it is
* defined. Then it is filtered through the {@see 'locale'} filter hook and
* the value for the locale global set and the locale is returned.
* The process to get the locale should only be done once, but the locale will
* always be filtered using the {@see 'locale'} hook.
* @global string $locale The current locale.
* @global string $wp_local_package Locale code of the package.
* @return string The locale of the blog or from the {@see 'locale'} hook.
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' ) ) {
// If multisite, check options.
// Don't check blog option when installing.
$ms_locale = get_site_option( 'WPLANG' );
$ms_locale = get_option( 'WPLANG' );
if ( false === $ms_locale ) {
$ms_locale = get_site_option( 'WPLANG' );
if ( false !== $ms_locale ) {
$db_locale = get_option( 'WPLANG' );
if ( false !== $db_locale ) {
if ( empty( $locale ) ) {
* Filters the locale ID of the WordPress installation.
* @param string $locale The locale ID.
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().
* @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 ) {
if ( 0 === $user_id && function_exists( 'wp_get_current_user' ) ) {
$user = wp_get_current_user();
} elseif ( $user_id instanceof WP_User ) {
} elseif ( $user_id && is_numeric( $user_id ) ) {
$user = get_user_by( 'id', $user_id );
return $locale ? $locale : get_locale();
* Determine the current locale desired for the request.
* @global string $pagenow
* @return string The determined locale.
function determine_locale() {
* Filters the locale for the current request prior to the default determination process.
* Using this filter allows to override the default logic, effectively short-circuiting the function.
* @param string|null $locale The locale to return and short-circuit. Default null.
$determined_locale = apply_filters( 'pre_determine_locale', null );
if ( ! empty( $determined_locale ) && is_string( $determined_locale ) ) {
return $determined_locale;
$determined_locale = get_locale();
$determined_locale = get_user_locale();
if ( isset( $_GET['_locale'] ) && 'user' === $_GET['_locale'] && wp_is_json_request() ) {
$determined_locale = get_user_locale();
if ( ! empty( $_GET['wp_lang'] ) && ! empty( $GLOBALS['pagenow'] ) && 'wp-login.php' === $GLOBALS['pagenow'] ) {
$determined_locale = sanitize_locale_name( wp_unslash( $_GET['wp_lang'] ) );
* Filters the locale for the current request.
* @param string $locale The locale.
return apply_filters( 'determine_locale', $determined_locale );
* Retrieve the translation of $text.
* If there is no translation, or the text domain isn't loaded, the original text is returned.
* *Note:* Don't use translate() directly, use __() or related functions.
* @since 5.5.0 Introduced gettext-{$domain} filter.
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text.
function translate( $text, $domain = 'default' ) {
$translations = get_translations_for_domain( $domain );
$translation = $translations->translate( $text );
* Filters text with its translation.
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
$translation = apply_filters( 'gettext', $translation, $text, $domain );
* Filters text with its translation for a domain.
* The dynamic portion of the hook, `$domain`, refers to the text domain.
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
$translation = apply_filters( "gettext_{$domain}", $translation, $text, $domain );
* Remove last item on a pipe-delimited string.
* Meant for removing the last item in a string, such as 'Role name|User role'. The original
* string will be returned if no pipe '|' characters are found in the string.
* @param string $string A pipe-delimited string.
* @return string Either $string or everything before the last pipe.
function before_last_bar( $string ) {
$last_bar = strrpos( $string, '|' );
if ( false === $last_bar ) {
return substr( $string, 0, $last_bar );
* Retrieve the translation of $text in the context defined in $context.
* If there is no translation, or the text domain isn't loaded, the original text is returned.
* *Note:* Don't use translate_with_gettext_context() directly, use _x() or related functions.
* @since 5.5.0 Introduced gettext_with_context-{$domain} filter.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text on success, original text on failure.
function translate_with_gettext_context( $text, $context, $domain = 'default' ) {
$translations = get_translations_for_domain( $domain );
$translation = $translations->translate( $text, $context );
* Filters text with its translation based on context information.
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
$translation = apply_filters( 'gettext_with_context', $translation, $text, $context, $domain );
* Filters text with its translation based on context information for a domain.
* The dynamic portion of the hook, `$domain`, refers to the text domain.
* @param string $translation Translated text.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
$translation = apply_filters( "gettext_with_context_{$domain}", $translation, $text, $context, $domain );
* Retrieve the translation of $text.
* If there is no translation, or the text domain isn't loaded, the original text is returned.
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text.
function __( $text, $domain = 'default' ) {
return translate( $text, $domain );
* Retrieve the translation of $text and escapes it for safe use in an attribute.
* If there is no translation, or the text domain isn't loaded, the original text is returned.
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text on success, original text on failure.
function esc_attr__( $text, $domain = 'default' ) {
return esc_attr( translate( $text, $domain ) );
* Retrieve the translation of $text and escapes it for safe use in HTML output.
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and returned.
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text.
function esc_html__( $text, $domain = 'default' ) {
return esc_html( translate( $text, $domain ) );
* Display translated text.
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
function _e( $text, $domain = 'default' ) {
echo translate( $text, $domain );
* Display translated text that has been escaped for safe use in an attribute.
* Encodes `< > & " '` (less than, greater than, ampersand, double quote, single quote).
* Will never double encode entities.
* If you need the value for use in PHP, use esc_attr__().
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
function esc_attr_e( $text, $domain = 'default' ) {
echo esc_attr( translate( $text, $domain ) );
* Display translated text that has been escaped for safe use in HTML output.
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and displayed.
* If you need the value for use in PHP, use esc_html__().
* @param string $text Text to translate.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
function esc_html_e( $text, $domain = 'default' ) {
echo esc_html( translate( $text, $domain ) );
* Retrieve translated string with gettext context.
* Quite a few times, there will be collisions with similar translatable text
* found in more than two places, but with different translated context.
* By including the context in the pot file, translators can translate the two
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated context string without pipe.
function _x( $text, $context, $domain = 'default' ) {
return translate_with_gettext_context( $text, $context, $domain );
* Display translated string with gettext context.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated context string without pipe.
function _ex( $text, $context, $domain = 'default' ) {
echo _x( $text, $context, $domain );
* Translate string with gettext context, and escapes it for safe use in an attribute.
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and returned.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text.
function esc_attr_x( $text, $context, $domain = 'default' ) {
return esc_attr( translate_with_gettext_context( $text, $context, $domain ) );
* Translate string with gettext context, and escapes it for safe use in HTML output.
* If there is no translation, or the text domain isn't loaded, the original text
* is escaped and returned.
* @param string $text Text to translate.
* @param string $context Context information for the translators.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string Translated text.
function esc_html_x( $text, $context, $domain = 'default' ) {
return esc_html( translate_with_gettext_context( $text, $context, $domain ) );
* Translates and retrieves the singular or plural form based on the supplied number.
* Used when you want to use the appropriate form of a string based on whether a
* number is singular or plural.
* printf( _n( '%s person', '%s people', $count, 'text-domain' ), number_format_i18n( $count ) );
* @since 5.5.0 Introduced ngettext-{$domain} filter.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param int $number The number to compare against to use either the singular or plural form.
* @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
* @return string The translated singular or plural form.
function _n( $single, $plural, $number, $domain = 'default' ) {
$translations = get_translations_for_domain( $domain );
$translation = $translations->translate_plural( $single, $plural, $number );
* Filters the singular or plural form of a string.
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param string $number The number to compare against to use either the singular or plural form.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
$translation = apply_filters( 'ngettext', $translation, $single, $plural, $number, $domain );
* Filters the singular or plural form of a string for a domain.
* The dynamic portion of the hook, `$domain`, refers to the text domain.
* @param string $translation Translated text.
* @param string $single The text to be used if the number is singular.
* @param string $plural The text to be used if the number is plural.
* @param string $number The number to compare against to use either the singular or plural form.
* @param string $domain Text domain. Unique identifier for retrieving translated strings.
$translation = apply_filters( "ngettext_{$domain}", $translation, $single, $plural, $number, $domain );