Translating plugins - the $domain parameter

Any half descent Wordpress plugin should be able to handle different languages. The gettext translation system that Wordpress uses is really easy to use, once one has the right tools, namely POEdit.

The function that loads the translation file is declared in WP as follows:

function load_plugin_textdomain($domain, $path = false)

Where $domain is the second parameter called in __($en_US_text, $domain) and _e($en_US_text, $domain). The difference between these two functions is that the _e() echoes the text, whereas __() just returns it. So, the $domain parameter defines the namespace within which the translation file (.mo) is valid, and this namespace must be specified for each translatable entity.

We load the .mo file in the plugin using the ‘init’ hook:

function p2m_translate(){
	load_plugin_textdomain('p2m-links', PLUGINDIR
            . '/' . dirname(plugin_basename (__FILE__)) );
}
add_action('init', 'p2m_translate');

In this example, the domain is ‘p2m-links’, which mean that translatable text should be expressed as:

__('Gobbledygook', 'p2m-links');
_e('Gobbledygook', 'p2m-links');

If no domain is specified in a plugin then Wordpress will look for translations in the .mo file contained in the wp-includes/languages directory.

Tags: , ,

Leave a Reply