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.
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.
_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>
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") . "!";
So the main difference between
_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:
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post