Add Language Support To Your WordPress Plugins

If you make WordPress Plugins, it’s real easy to add language/localization support to them.
Just follow these steps and you’re done.

Step 1

Define your text domain. That means that your translations won’t interfere with other plugins translations, or the other way around.

//In the beginning of your plugin main file
define(MY_TEXT_DOMAIN, 'my-plugin-text');

Step 2

Everywhere you have text, use __() (returns the translated string) or _e() (echoes the translated string) to generate the strings.

//If you had this before
//Change to this
<h2><?php _e('Hello', MY_TEXT_DOMAIN); ?></h2>

//Or if you had this
$string = 'Hello';
//Change to this
$string = __('Hello', MY_TEXT_DOMAIN);

Step 3

Make sure that you load your text domain when the plugin loads. This function below makes sure that you only load it once. You add this to your main plugin file.
This looks for a .mo-file in a subdir called “languages” in your plugin dir.

$my_translator_is_setup = false;
function myPluginSetLanguages()
    global $my_translator_is_setup;


            dirname( plugin_basename( __FILE__ ) ) . '/languages/'
    $my_translator_is_setup = true;

Step 4

Go translate! I use the plugin Codestyling Localization to make .po-files and generate .mo-files. Its real easy.