What’s the point on gettext syntax?

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

Related posts

Leave a Reply

3 comments

  1. __ (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. 🙂

  2. __(), _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:

  3. 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.