WordPress I18n for developer – Marking strings with Text Domain

wordpress i18n tools

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:

Plugin Generate POT Area.png

Using the i18n tools

Download the WordPress i18n tools directory from SVN by issuing:

svn co

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> <output_file>

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. The output file is optional and will default to plugin-slug.pot in the current directory.

For example:

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

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
Facebook Comments

No comments

Trả lời