Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So far I've been handling some translations in Wordpress and tried to read the official gettext documentation but won't get the point of one maybe simple thing: What's the differences between those startings like __(, _e(, etc.? And even more: which others are there beside? Thanks in advance!

Frank

share|improve this question
    
Thank all of you for your elaborate answers! I really appreciate this here! –  Circuit Circus Jul 9 '12 at 8:26
    
...Maybe one further question to this: Can the entry of the text-domain be left out? I upright started bring it to every string I wanted to translate but then recognized some examples in the Wordpress codex which don't contain it at all... –  Circuit Circus Jul 9 '12 at 16:00
    
... Okay, now I came across it in this codex page - sorry for this redundant question :-) –  Circuit Circus Jul 9 '12 at 16:32
add comment

3 Answers 3

up vote 11 down vote accepted

__ (double underscore) is the base translate function. It translates a string and returns it as a string.

_e does the same as __, but echo's the result immediately.

_x is the contextual translate function. It has a second option to provide context to people doing the translation.

_ex is the same as _x, but echo's the result.

Example of using _x:

$string = _x( 'Buffalo', 'an animal', 'plugin-domain' );
$string = _x( 'Buffalo', 'a city in New York', 'plugin-domain' );
$string = _x( 'Buffalo', 'a verb meaning to confuse somebody', 'plugin-domain' );

Sometimes the same string can be different in other languages. Providing context to the translators can help them pick the right words.

Shortcut functions:

  • esc_attr__ : Equivalent to __ but also runs the result through esc_attr.
  • esc_html__ : Equivalent to __ but also runs the result through esc_html.
  • esc_attr_e : Equivalent to _e but also runs the result through esc_attr.
  • esc_html_e : Equivalent to _e but also runs the result through esc_html.
  • esc_attr_x : Equivalent to _x but also runs the result through esc_attr.
  • esc_html_x : Equivalent to _x but also runs the result through esc_html.

_n is the pluralization handler. Example:

$string = sprintf( _n(
        'You have %d taco.', 
        'You have %d tacos.', 
        $number, 
        'plugin-domain'), 
    $number );

In that example, there's two ways to say the number of tacos, depending on if it's singular or not. The first use of $number tells the _n function which version to use. The second use of $number happens in the sprintf, to replace the %d with the actual number in the string.

There is no echo function equivalent for _n, but there is a function named _nx. It's a combination of _n and _x. Pluralization and context.

_n_noop is a special one. It's used for translating pluralized strings, but not actually performing the translation immediately. This is useful if you want to make the strings centralized but actually do the work elsewhere. The function that actually does the work elsewhere is translate_nooped_plural.

Example:

$holder = _n_noop('You have %d taco.', 'You have %d tacos.', 'plugin-domain');
// ... later ...
$string = sprintf( translate_nooped_plural( $holder, $count ), $count );

This isn't used much, but can be handy for organization. If you put all your strings in one file, for example, then reference them elsewhere, this wouldn't be possible with just _n, you need something like _n_noop to do that.

_nx_noop is the same as _n_noop, but also can take a context for the translators, same as _x.

Note that you can put the domain into either the noop function call, or into the translate_nooped_plural function call. Whichever makes more sense for your organization. If both have a domain, then the one passed to the noop call wins.

number_format_i18n is the equivalent to PHP's built-in number_format, but it adds in the handling for things like decimals and so on, which are different in other locales.

date_i18n is the equivalent to PHP's built-in date, with all the pertinent handling there as well. Month names, day names, etc.

Also, never break the laws. Just a reminder. :)

share|improve this answer
    
Wow, this is really a good explanation of it! Thank you very much for helping! Now I see clear. –  Circuit Circus Jul 9 '12 at 8:15
add comment

__(), _e() and _x(), _ex()

__() and _e() are essentially both a wrapper of translate() (do not use directly) and almost the same.

The difference lies in that __() returns the translated string and _e() echoes it. Both need to be fed a string as a required parameter and usually, though optional, also a textdomain.

Analogously, there's _x() and _ex(), which let you specify a context that can describe where the string appears. If your project includes more than a few tens of translatable strings, using context makes a lot of sense.

Also, note the existence of _n() and _nx() for plurals.

Example of common usage

$output = '<label for="some_field">' .
        _x( 'Some Information.', 'Some Form Field', 'your-text-domain' ) .
    '</label>' .
    '<input type="text" name="some_field" value="" />' .
    '<p class="description">' .
        _x( 'Here you can enter some info.', 'Some Form Field', 'your-text-domain' ) .
    '</p>';

return $output;

Parameters

__( $text, $domain )
_e( $text, $domain )
_x( $text, $context, $domain )
_ex( $text, $context, $domain )
_n( $single, $plural, $number $domain )
_nx( $single, $plural, $number, $context, $domain )

All parameters but $number are strings. All but $domain are required.

Further flexibility with variables and sprintf()

If your strings will contain variable numbers or words, use sprintf():

$stars = get_post_meta( $post->ID, 'rating', true );
$title = get_the_title( $post->ID );

$output = '<p>' .
        sprintf(
            _x(
                'The movie titled %2$s received a %1$d star rating.',
                'Movie Description',
                'your-text-domain'
            ),
            $stars,
            $title
        ) .
    '</p>';

return $output;

Additional resources

Some additional resources for the upcoming WordPress I18n Ninja:

share|improve this answer
    
Also check out "Localization" under "Miscellaneous" in the codex for a complete breakdown of functions & detailed explanations. –  TheDeadMedic Jul 7 '12 at 18:30
    
& this is the place where wp-polyglots meet. –  brasofilo Jul 7 '12 at 18:36
    
@Johannes Pille: I didn't notice your answer until I'd posted mine, should I delete my post? –  Jeremy Jared Jul 7 '12 at 18:40
    
@JeremyJared No, why? Further info can't be a bad thing, can it?! I think your answer is well thought through. +1 from me. –  Johannes Pille Jul 7 '12 at 18:42
    
@TheDeadMedic Edited into "additional resources". –  Johannes Pille Jul 7 '12 at 18:43
add comment

I'm not an expert on translations, but the WordPress Codex Page has good documentation and explains the reason to use each instance.

From the codex pages:

__()

Is used when the message is passed as an argument to another function; _e() is used to write the message directly to the page. More detail on these two functions:

__('message')

Searches the localization module for the translation of 'message', and passes the translation to the PHP return statement. If no translation is found for 'message', it just returns 'message'.

_e('message')

Searches the localization module for the translation of 'message', and passes the translation to the PHP echo statement. If no translation is found for 'message', it just echoes 'message'.

Note that if you are internationalizing a Theme or Plugin, you should use a "Text Domain".

The gettext framework takes care of most of WordPress. However, there are a few places in the WordPress distribution where gettext cannot be used:

  • The main WordPress README file -- it's a static HTML file, not a PHP file, so it cannot be run through the gettext functions.
  • A few error messages are generated very early in the WordPress loading cycle, before gettext is loaded.

Link to the Codex Page

Additional info regarding when gettext doesn't work

Hopefully that answers your question, if not let us know and maybe someone else can help or I can do some more research.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.