Ready to get started?Download WordPress


Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

I18n for WordPress Developers

The plugin internationalization documentation is now located in the Plugin Developer Handbook.

The plugin localization documentation is now located in the Plugin Developer Handbook.

The theme internationalization documentation is now located in the Theme Developer Handbook.

The theme localization documentation is now located in the Theme Developer Handbook.


What is I18n?

Internationalization is the process of developing your plugin so it can easily be translated into other languages. Localization describes the subsequent process of translating an internationalized plugin.Internationalization is often abbreviated as i18n (because there are 18 letters between the i and the n) and localization is abbreviated as l10n (because there are 10 letters between the l and the n.)

Why internationalization is important?

Because WordPress is used all over the world, it is a good idea to prepare a WordPress plugin so that it can be easily translated into whatever language is needed. As a developer, you may not have an easy time providing localizations for all your users; You may not speak their language after all. However, any developer can successfully internationalize a theme to allow others to create a localization without the need to modify the source code itself.

Introduction to Gettext

WordPress uses the gettext libraries and tools for i18n. Gettext is an old and respectable piece of software, widely used in the open-source world.

Here is how it works in a few sentences:

  • Developers wrap translatable strings in special gettext functions
  • Special tools parse the source code files and extract the translatable strings into POT (Portable Objects Template) files
  • In the WordPress world, often POT files are fed to GlotPress, which is a collboration tool for translators
  • Translators translate and the result is a PO file (POT file, but with translations inside)
  • PO files are compiled to binary MO files, which give faster access to the strings at run-time

If you need to remember one thing: translatable strings are parsed from special function calls in the source-code, they are not obtained at run-time.

Note that if you look online, you'll see the _() function which refers to the native PHP gettext-compliant translation function, but instead with WordPress you should use the __() wordpress-defined PHP function.

Text Domains

If you're translating a plugin or a theme, you'll need to use a text domain to denote all text belonging to that plugin. This increases portability and plays better with already existing WordPress tools. The text domain must match the “slug” of the plugin.

The Text Domain needs to be added to the plugin header. The WordPress should internationalize your plugin or theme meta-data when it displays your plugin in the admin screens:

 * Plugin Name: My Plugin
 * Author: Otto
 * Text Domain: my-plugin

The text domain is a unique identifier, which makes sure WordPress can distinguish between all loaded translations. If your plugin is a single file called my-plugin.php or it is contained in a folder called my-plugin the domain name should be my-plugin. The text domain name must use dashes and not underscores.

In general, an application may use more than one large logical translatable module and a different MO file accordingly. A text domain is a handle of each module, which has a different MO file.

Strings for Translation

Translatable strings

In order to make a string translatable in your application you have to just wrap the original string in a __() function call:

$hello =  __( 'Hello, dear user!', 'my-text-domain' );

If your code should echo the string to the browser, use the _e function instead:

_e( 'Your Ad here', 'my-text-domain' )

The strings for translation are wrapped in a call to one of a set of special functions. The most commonly used one is __(). It just returns the translation of its argument:

echo '<h2>' . __( 'Blog Options', 'my-text-domain' ) . '</h2>';

Another simple one is _e(), which outputs the translation of its argument. Instead of writing:

echo __( 'Using this option you will make a fortune!', 'my-text-domain' );

you can use the shorter:

_e( 'Using this option you will make a fortune!', 'my-text-domain' );


echo 'We deleted $count spam messages.'

How would you i18n this line? Let's give it a try together:

_e( "We deleted $count spam messages.", 'my-text-domain' );

It won't work! Remember, the strings for translation are extracted from the sources, so the translators will see work on the phrase: "We deleted $count spam messages.". However in the application _e will be called with an argument like "We deleted 49494 spam messages." and gettext won't find a suitable translation of this one and will return its argument: "We deleted 49494 spam messages.". Unfortunately, it isn't translated correctly.

The solution is to use the printf family of functions. Especially helpful are printf and sprintf. Here is what the right solution of the spams count problem will look like:

printf( __( 'We deleted %d spam messages.', 'my-text-domain' ), $count );

Notice that here the string for translation is just the template "We deleted %d spam messages.", which is the same both in the source and at run-time.

If you have more than one placeholder in a string, it is recommended that you use argument swapping. In this case, single quotes (') are mandatory : double quotes (") will tell php to interpret the $s as the s variable, which is not what we want.

printf( __( 'Your city is %1$s, and your zip code is %2$s.', 'my-text-domain' ), $city, $zipcode );

Here the zip code is being displayed after the city name. In some languages displaying the zip code and city in opposite order would be more appropriate. Using %s prefix in the above example, allows for such a case. A translation can thereby be written:

printf( __( 'Your zip code is %2$s, and your city is %1$s.', 'my-text-domain' ), $city, $zipcode );


Including HTML in translatable strings depends on the context. Include HTML if the string is not separated from any text surrounding it. Example of a link (separated from text surrounding it):

<div class="site-info">
  <a href="http://wordpress.org/" ><?php _e( 'Proudly powered by WordPress.', 'my-text-domain' ); ?></a>
</div><!-- .site-info -->

Example of a link in a paragraph (not separated from text surrounding it):

$url = 'http://example.com';
$link = sprintf( __( 'Check out this link to my <a href="%s">website</a> made with WordPress.', 'my-text-domain' ), esc_url( $url ) );
echo $link;


Let's get back to the spams example. What if we delete only one spam? The output will be: We deleted 1 spam messages., which is definitely not correct English, and would certainly be incorrect for many other languages as well.

In WordPress you can use the _n() function.

printf( _n( 'We deleted one spam message.', 'We deleted %d spam messages.', $count, 'my-text-domain' ), $count );

_n() accepts 4 arguments:

  • singular — the singular form of the string
  • plural — the plural form of the string
  • count — the number of objects, which will determine whether the singular or the plural form should be returned (there are languages, which have far more than 2 forms)

The text domain is the fourth optional parameter. The return value of the functions is the correct translated form, corresponding to the given count.

Disambiguation by context

Sometimes a single term is used in several contexts. Although it is one and the same word in English, it may need to be translated differently in some languages. For example, the word "Post" can be used both as a verb ("Click here to post your comment") and as a noun ("Edit this post"). In such cases, the _x() function should be used. It is similar to __(), but it has an additional second argument -- the context:

if ( false === $commenttxt ) $commenttxt = _x( 'Comment', 'noun' );
if ( false === $trackbacktxt ) $trackbacktxt = __( 'Trackback' );
if ( false === $pingbacktxt ) $pingbacktxt = __( 'Pingback' );
// some other place in the code
echo _x( 'Comment', 'column name' );

Using this method, we will see the string "Comment" for both of the original versions, but the translators will see two "Comment" strings for translation, each in the different contexts.

The text domain is the third optional parameter:

echo _x( 'Comment', 'column name', 'my-text-domain' );

Note that similarly to __(), _x() has an 'echo' version: _ex(). The previous example could be written as:

_ex( 'Comment', 'column name', 'my-text-domain' );

Use whichever you feel enhances legibility and ease-of-coding.

To add contexts to a string with plural form(s), use _nx(). If the translation needs escaping, use esc_attr_x() or esc_html_x().


Do you think translators will know how to translate a string like:

__( 'g:i:s a' )

? In this case you can add a clarifying comment in the source code. It has to start with the words translators: and to be the last PHP comment before the gettext call. Here is an example:

/* translators: draft saved date format, see http://php.net/date */
$draft_saved_date_format = __( 'g:i:s a' );

By adding a translators: comment you can write a "personal" message to the translators, so that they know how to deal with the string.

Newline characters

Gettext doesn't like \r (ASCII code: 13) in translatable strings, so please avoid it and use \n instead.

Empty strings

The empty string is reserved for internal Gettext usage and you must not try to internationalize the empty string. It also doesn't make any sense, because the translators won't see any context.

If you have a valid use-case to internationalize an empty string, add context to both help translators and be in peace with the Gettext system.

Handling JavaScript files

Use wp_localize_script() to add translated strings or other server-side data to a previously enqueued script.

wp_enqueue_script( 'script-handle', … );
wp_localize_script( 'script-handle', 'objectL10n', array(
	'speed'  => $distance / $time,
	'submit' => __( 'Submit', 'my-text-domain' ),
) );

Then in the JavaScript file, corresponding to script-handle you can use objectL10n.variable:

$('#speed').val('{speed} km/h'.replace('{speed}', objectL10n.speed));

I18n for widgets developed on 2.8+

WordPress 2.8+ uses a new Widget API, that only requires the widget developer to extend the standard widget class and some of its functions. With this API there is no init function. After the widget is coded using the widget(), form(), and update() methods, the widget must be registered. The textdomain is then loaded after the widget is registered.


// register Foo_Widget widget
function Foo_Widget_init() {
    return register_widget( 'Foo_Widget' );
add_action( 'widgets_init', 'Foo_Widget_init' );

$plugin_dir = basename( dirname( __FILE__ ) );
load_plugin_textdomain( 'foo_widget', null, $plugin_dir );

This example registers a widget named Foo_Widget, then sets the plugin directory variable and attempts to load the foo_widget-locale.po file.

Best Practices

Until we gather some WordPress-specific examples, use your time to read the short, but excellent article in the gettext manual. Summarized, it looks like this:

  • Decent English style—minimize slang and abbreviations.
  • Entire sentences—in most languages word order is different than that in English.
  • Split at paragraphs—merge related sentences, but do not include a whole page of text in one string.
  • Use format strings instead of string concatenation—sprintf(__('Replace %1$s with %2$s'), $a, $b); is always better than __('Replace ').$a.__(' with ').$b; .
  • Avoid unusual markup and unusual control characters—do not include tags that surround your text and do not leave URLs for translation, unless they could have a version in another language.
  • Do not leave leading or trailing whitespace in a translatable phrase.

Loading a Text Domain

The text domain name is also used to form the name of the MO file with your plugins' translations. You can load them by calling the function load_plugin_textdomain as early as the plugins_loaded action.

load_plugin_textdomain( $domain, $path_from_abspath, $path_from_plugins_folder )


function myplugin_init() {
 $plugin_dir = basename(dirname(__FILE__));
 load_plugin_textdomain( 'my-plugin', false, $plugin_dir );
add_action('plugins_loaded', 'myplugin_init');

This call tries to load my-plugin-{locale}.mo from your plugin's base directory. The locale is the language code and/or country code you defined in the constant WPLANG in the file wp-config.php. For example, the locale for German is 'de', and the locale for Danish is 'da_DK'. From the code example above the text domain is 'my-plugin' therefore the Danish MO and PO files should be named my-plugin-da_DK.mo and my-plugin-da_DK.po. For more information about language and country codes, see Installing WordPress in Your Language.

  • For WordPress 2.6 and up, the third parameter is the directory containing the .mo file, relative to the plugins directory. It must end with a trailing slash. If your plugin doesn't need compatibility with older versions of WordPress, you can leave the second parameter blank.
  • For versions lower than 2.6, the second parameter should be the directory containing the .mo file, relative to ABSPATH. The third parameter should be blank.

For themes the process is surprisingly similar:


Put this call in your functions.php and it will search your theme directory for locale.mo and load it (where locale is the current language, i.e. pt_BR.mo).

Watch Out

  • DO name your MO file as locale.mo (e.g., da_DK.mo)
  • DO NOT name your MO file as my_theme-da_DK.mo

Translate meta data

If you add a line like this to your plugin or theme header where the theme or plugin name is, WordPress should internationalize your plugin or theme meta-data when it displays your plugin in the admin screens:

Text Domain: my-text-domain

Marking strings with Text Domain

All the rules from above apply here, but there is one more. The additional rule states that you must add your domain as an argument to every __, _e and __ngettext call, otherwise your translations won't work.


  • __('String') should become __('String', 'text_domain')
  • _e('String') should become _e('String', 'text_domain')
  • _n('String', 'Strings', $count) should become _n('String', 'Strings', $count, 'text_domain')

Adding the domain by hand is a burden and that's why you can do it automatically:

  • If your plugin is registered in the official repository, go to your Admin page there and scroll to Add Domain to Gettext Calls.


php add-textdomain.php -i domain phpfile phpfile ...

After it's done, the domain will be added to all gettext calls in the files.

Translating Plugins and Themes

POT files

The first stage is to generate a .pot for your plugin or theme. This done by way of the xgettext utility as part of gettext. You will need to have the gettext package installed if you want to do this generation on-site. That is to say, if you want to use the WordPress i18n tools. If your plugin is part of the official repository you may use the Admin->Generate POT file tool from the repository administration interface to do it for you:


Using the i18n tools

Download the WordPress i18n tools directory from SVN by issuing:

svn co http://develop.svn.wordpress.org/trunk/tools/

Note that the scripts expect wp-includes/pomo/ to be accessible in the src directory relative to the parent of tools, which is to say that if your tools directory is a subdirectory of your WordPress root, the makepot.php script will look for ../../src/wp-includes/pomo/ (the way your file hierarchy would be if you checked out the entire trunk directory). And you may need to create a softlink from your actual WordPress root to src:

ln -s . src

(See Using Subversion to learn more about SVN.)

Now run the makepot.php script like this:

php makepot.php <target> <directory>

Target can be wp-plugin for a plugin and wp-theme for a theme. Let Directory be the full (absolute or relative) path to your plugin cq. theme directory. It is acceptable also to run the script from your theme/plugin directory.

For example:

php makepot.php wp-theme ../../src/wp-content/themes/my-theme
php ~/public_html/wordpress/tools/i18n/makepot.php wp-plugin .

Check whether the output .pot has appeared in the directory.

If you include your .pot file with your plugin or theme, instead of only supplying .mo (or .po) files, would-be translators of your plugin/theme will have an easy time getting started.

Using Grunt

If you use Grunt with your theme or plugin, you can use the grunt-pot plugin by Stephen Harris to generate a .pot file. See his site for instructions on integrating it into your project.

Example content

Each translatable string is formatted like this:

#: comments.php:28
msgid "Comments:"
msgstr ""

PO files

Every translator takes the WordPress .pot file and translates the msgstr sections to their own language. The result is a .po file with the same format as a .pot, but with translations and some specific headers.

MO files

From a resulting .po translation file a .mo file is compiled. This is a binary file which contains all the original strings and their translations in a format suitable for fast translation extraction. The conversion is done using the msgfmt tool:

msgfmt -o <output>.mo <input>.po

If you have a lot of .po files to convert at once, you can run it as a batch. For example, using a bash command:

# Find PO files, process each with msgfmt and rename the result to MO
for file in `find . -name "*.po"` ; do msgfmt -o ${file/.po/.mo} $file ; done