Quick Tip: Using the Mysteriously Named _e, _n and __ Functions

Quick Tip: Using the Mysteriously Named _e, _n and __ Functions

You might have met these functions during your WordPress career. These can be used for translating purposes. Here are some quick examples about using them.


Why Use Them?

The fact is WordPress can be used and translated into any languages of the world. If you want a webpage layout which can handle multiple languages you can do that with the default translating system built into WordPress. You don’t even have to write complex PHP code or a plugin for that. Just use these methods and make the appropriate language files.


Where to Use Them?

The places to use these functions are the PHP files of themes and plugins. For starters I recommend using them only in themes, but later on you can explore using them in plugins as well.


Gettext Files

For translating texts WordPress uses the gettext translation framework. This data is stored in POT (Portable Object Template), PO (Portable Object) and MO (Machine Object) files. You can create these files with the Open Source poEdit and GNU gettext. The default language files for the WordPress interface are in the wp-content folder. More information can be read in the Translation article in the WordPress Codex.

../wp-content/languages/uk.mo
../wp-content/languages/uk.po

Examples

_e is used for simple text while _n can be used for the plural form of a word, you can even define different forms for different numbers of an object or thing.

<!-- Making a h1 heading -->
<h1><?php _e("apple"); ?></h1>

<!-- Sample paragraph -->
<p><?php _n("piece", "pieces", 3); ?></p>

For Developers

If you are a PHP developer and may want to write a plugin or other useful thing related to your WordPress project you can use the __ function, which returns the translated version of the given string. This returned string can be integrated in whatever WordPress code you want.

// return the translation of apple in a German sentence
echo "Das ist ein " . __("apple") . "!"; 

References

So the main difference between __ and _e is that the latter one echoes the result to the webpage. __ serves as the alias of the translate() function. These functions can be found in wp-includes/l10n.php.

Reference pages for the functions here:

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • Pingback: Quick Tip: Using the Mysteriously Named _e, _n and __ Functions | Qtiva

  • Pingback: Quick Tip: Using the Mysteriously Named _e, _n and __ Functions | Qoozon

  • http://elektroelch.net Latz

    The “For Developers” example is the worst example for the use of the “__” function ever! It’s a weird mixture of German text that is not internationalized and a single word that is internationalized. Even if it’s only an example it should make some sense. You will hopefully never see such code in a plugin. The only reasonable use of the __ function in this case is:

    _e(‘This is an apple!’)

    You explained the _e function and a paragraph later you do no us it. Why?

    _e(‘This is an’ . ‘ apple’ . ‘!’);

    You also should take a look of the usage of double and single quotes. In all your examples it’s not necessary to use the double quotes and the text is unnecessary evaluated.

  • http://unserkaiser.com Kaiser

    Don’t forget to add a textdomain string to all those functions. Else you have a high chance that your translation collides with another crappy coded (mu-)plugin, dropin or theme…

    • http://maorchasen.com Maor Chasen

      Agree on this one 100%. Loading a textdomain prior to making any use of the l18n is not even a best practice, it’s a must. It’s worthless teaching about __, _e, _x and its relatives if the basics are not being taught. Just a thought …

  • http://nddery.ca Nicolas

    There is also _x() which allow you to define a context for translation.

    “Quite a few times, there will be collisions with similar translatable text found in more than two places but with different translated context.” (http://phpdoc.wordpress.org/trunk/WordPress/i18n/_wp-includes—l10n.php.html#function_x). I found that especially when translating to French (probably mainly because I mostly translate to French) having the context is useful.

  • http://www.customicondesign.com custom icon design

    I dont know why wordpress need so additional function, because I know many people just need English website. No needs another language. When I researched the wordpress, I find wordpress have many additional function is useless to many people. but it all include the function in include files. So I suggestion wordpress have many versions. For mini version, just have some core function and we can add some other functions manual.

  • http://colinfmurphy.com Colin Murphy

    One area I didnt see mentioned in teh article is the use of variables for translating. Below is an example using the php function printf and the WordPress function __()