Why load_plugin_textdomain() no longer worked…

After a recent upgrade of one of my WordPress plugins, Skype Online Status, one of the translators, Georg Adamsen, reported that his complete translation no longer worked. After testing for a bit, I soon discovered that none of the translations worked any more.

The plugin, like any other plugin that provides the possibility to include different languages in the form of .mo translation files, uses the recommended WordPress function load_plugin_textdomain() and it has been using it since a very long time…

Plain and simple, right at the top of the main plugin file:

// Internationalization
load_plugin_textdomain('skype-online-status', false, dirname(plugin_basename( __FILE__ )).'/languages');

So why would that suddenly not work any more?

I decided to take a look at the WordPress codex had to say on the subject of load_plugin_textdomain but found nothing new there. I double-checked the right path usage: still correct…

The only thing I could think of was that that function call at the top of the plugin file looked a bit naked to me. Yeah, you read correctly: naked! 😉

It’s a straight call to an internal WordPress function while I have grown accustomed to the ever growing number of action and filter hooks in WordPress. They are there for a reason, so I decided to move the load_plugin_textdomain function call to inside the plugins init function which is added to the WP init action… And presto! All was well again!

Apparently, the naked load_plugin_textdomain call at the top of the plugin file, fires too soon for WordPress to accept it. No error messages but also no working translations was the result.

Conclusion: Despite of the fact the WordPress Codex makes no mention of this, the moment load_plugin_textdomain() is called seems to be of utmost importance: it should be hooked to WP’s init action!

The correct method:

function my_plugin_init() {
// Internationalization first
load_plugin_textdomain('my-plugin', false, dirname(plugin_basename( __FILE__ )).'/languages');

// Then other init stuff
}

add_action('init','my_plugin_init');

UPDATE 2012-12-26
The above code does not kick in early enough for translation of widget titles. Turns out there is an earlier hook that can be used as mentioned on the now updated WordPress Codex page:

add_action('plugins_loaded','my_plugin_init');

After this hands-on discovery, I found some articles on the internet — it’s easy to find something once you know what you are looking for, isn’t it? 😉 — on the subject. One of them jumped right out in its comprehensiveness so I’d like to recommended it. For those that want to learn just about everything one needs to know when localizing a WordPress plugin: 6 Tips for Localizing Your WordPress Plugin

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.