__()
Retrieve translation of the 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).
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' );
Changelog
Since 2.1.0 | Introduced. |
__() code WP 6.3
function __( $text, $domain = 'default' ) { return translate( $text, $domain ); }