WordPress at a glance

__() WP 2.1.0

Retrieve translation of specified text using a translation file (text domain).

If there is no translation, or the text domain isn't loaded, the original text is returned.

To display translation on the screen, use a similar function _e().

l10n is the abbreviation for localization (l + 10 letters + n).

Works based on: translate()
✈ 1 time = 0.000019s = very fast | 50000 times = 0.07s = speed of light PHP 7.1.2, WP 4.7.3

No Hooks.

Return

String. Translated text.

Usage

$text = __( $text, $domain );
$text(string) (required)
Text to translate.
$domain(string)
Translation file ID, specified during registration and connection of the translation file. If not specified, the default WordPress translation file will be used.
Default: 'default'

Examples

#1 Translate the string

Example of using the function in a basic WordPress theme:

$str = __( 'Comment:' );

If Russian localization file is used, it will return: "Комментарий:".

Since version 4.2, the WPLANG constant has been canceled and now the language is changed in the main site settings.

Before WordPress 4.2: Russian localization file means that the WPLANG constant is defined as ru_RU in wp-config.php file: define('WPLANG', 'ru_RU');. For such a translation, without specifying a $domain, the wp-content/languages/ru_RU.mo file will be used.

#2 String translation with specifying translation domain

To translate a string from own translation file, we need to specify second parameter $domain. It bind translation string to translation file. $domain and the translation file are registered with the load_textdomain() function;

$str = __( 'Comment:', 'mydomain' );

#3 Make a string inside your plugin or theme translatable

$translated = __( 'Hello World', 'text_domain' );

Hello World and text_domain should always be directly passed as a string literal as shown above, not a string assigned to a variable.

This is incorrect:

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

#4 Display translation

There is similar function _e(), which display the result, but not return it. For Example the following strings are the same:

_e( 'this is some message', 'text_domain' );

// is same as
echo __( 'this is a some message', 'text_domain' );

#5 Escaping text that contains links

To do it, use __() in combination with sprintf(). This will prevent the links from being changed by translators.

sprintf( 
	__( 'You can visit the page by clicking <a href="%s">here</a>.', 'text_domain' ), 
	'http://example.com'
);

Code of __: wp-includes/l10n.php VER 4.9.8

<?php
function __( $text, $domain = 'default' ) {
	return translate( $text, $domain );
}

Related Functions

From category: Localization

No comments
    Hello, !     Log In . Register