I18n in WordPress

A few months ago – when I wasn’t as familiar with WordPress as today – I downloaded a WordPress Plugin that promised to equip single pages and posts with custom header images. I noticed, that all options in the admin area were in English, and since I set up the plugin for a friend’s website, I wanted to top off my favor by translating the English descriptions, so he wouldn’t have a hard time with them. Unfortunately the plugin was developed by an ignorant shortsighted American, so I had to put all hardcoded strings into WordPress’ translation functions myself – and translate it. A few days later (and I am not making this up) he published an update with a few minor changes and – silly me – I updated it, overwriting all alterations I worked so hard on!

My plea to all plugin and theme authors: Internationalize! It makes your plugin/theme so much more attractive for users. You don’t even have to translate it yourself. Just set it up in a way, that people can work with it – in their own languages.

In the following days, I digged deep into the possibilities of internationalization in WordPress and I would like to quickly introduce the most important functions:

Generally, it is always helpful to gather information in the WordPress Codex, since most of the issues are already covered there.

All translation functions can be found in your WordPress installation in the file wp-includes/l10n.php.
Let’s start with the functions:

__( 'string', 'domain' );
_e( 'string', 'domain' );

In both versions the first attribute is the string you want to translate and the second the name of the domain (Theme or Plugin). Th difference is, while __() returns the translated string, _e() echoes it.

If you have a look in the plugins I wrote, you’ll notice that I alway define a constant for the 'domain'-part. This way I have to change the domain only once, if ever necessary.

You can also add information about the context for translators, if you think it is necessary:

_c( 'string|description', 'domain' );

Here, WordPress explodes the string at the '|' character and only translates the first part. The translator also gets to see the descrition, though. Since version 2.8.x this is also possible with

_x( 'string', 'description', 'domain' );

while here the description is handled seperately.
As the last function with one letter we have:

_n( 'singular', 'plural', 'count', 'domain' );

You probably figured it out already – a function, checking the passed on count and returning the correct, translated string.

You can find many of these functions together with esc_attr() and esc_html() as well as _noop(). Since the purpose of this article is only a short overview, I will not get further into it, unless otherwise desired. We’ll see, maybe this will be only part 1 of an  i18n-series…

Leave a Reply