How to Translate a WordPress Plugin

Step 1: Internationalizing a Plugin

Internationalizing a plugin essentially means making it ready for translations. This involves wrapping translatable strings in special functions which will be used later to load the correct translations.

The function we’ll be using is __(). This function takes two arguments: the text to translate and the text domain, it looks something like this in action:

<?php $greeting = __( 'Hello There!', 'text-domain' ); ?>

The function returns the translation for the marked string if it exists. If it doesn’t, the original string is returned.

It uses the current language to determine the needed translation. You can switch languages in WordPress in the Settings section, whatever setting is selected there will be applied to all translations.

The text domain is the final missing piece of the puzzle. A text domain is a way to group translations that belong together. The theme you are running will have its own text domain and so will all your plugins. This makes it easy to differentiate between them.

Note that for plugins the text domain should be the name of the plugin’s folder. For example, the text domain could be my-awesome-plugin. To internationalize our plugin all we need to do is wrap our strings in the __() function.

Step 2: Implementing Translations

We’ve now marked translation ready strings, but there are no translations anywhere in sight. We need to tell WordPress where translations for our plugin reside. The standard is a lang directory within our plugin. Create that folder now and use the following code in the main plugin file to let WordPress know where to look for translations.

add_action('plugins_loaded', 'load_textdomain');
function load_textdomain() {
    load_plugin_textdomain( 'my-awesome-plugin', false, dirname( plugin_basename(__FILE__) ) . '/lang/' );

Step 3: Creating a Translation

  1. Buy, download,install and launch Poedit. You will need the professional version because it lets you create pot files and also translate the plugin headers.

Learning resources