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

3 s
3

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

Leave a Comment