WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/ 2

This page is named incorrectly due to a limitation of Mediawiki page naming. The function name is __() not _2().

Description

Retrieves the translated string from the translate().

Usage

<?php $translated_text __$text$domain ); ?>

Parameters

$text
(string) (required) Text to translate
Default: None
$domain
(string) (optional) Domain to retrieve the translated text
Default: 'default'

Return Values

(string) 
Translated text

Examples

Make a string inside your plugin or theme translatable:

$translated = __( 'Hello World!', 'mytextdomain' );

'mytextdomain' needs to be a unique text domain used throughout your plugin/theme. This should always be directly passed as a string literal as shown above, not a string assigned to a variable or constant. E.g., this is incorrect:

// ----- > so, such method is incorrect
$text_domain = 'mytextdomain';
$string = 'Hello World!';
$translated = __( $string, $text_domain );

This seems to work, but it will interfere in automatic parsing of your plugin/theme's files for translation. See these for more information:

-- If you want to manually change translation for that string using a hook, then use:

add_filter( 'gettext', 'my_changerr', 20, 3 );
function my_changerr( $translated_text, $text, $domain ) {
    if ( is_singular() ) {
        switch ( $translated_text ) {
            case 'Hello World!' :
                $translated_text = __( 'hiiiiiiii!', 'your_new_text_domain' );
                break;
        }
    }
    return $translated_text;
}

in this case, __( 'Hello World!', 'mytextdomain' ) returns hiiiiiiii!

Notes

  • See __() An alias of translate()
  • l10n is an abbreviation for localization.
  • The function name is two underscores in a row. In some fonts it looks like one long underscore. (Who says programmers don't have a sense of humor.)

Change Log

Source File

__() is located in wp-includes/l10n.php.

Related

L10n: translate(), __(), _e(), _n(), _x(), _ex(), _nx(), esc_attr__(), esc_attr_e(), esc_attr_x(), esc_html__(), esc_html_e(), esc_html_x(), _n_noop(), _nx_noop(), translate_nooped_plural()

See also index of Function Reference and index of Template Tags.