I am trying to load localization for my plugin.

There is directory called languages in my plugin folder with .po and .mo files inside of it.

I have the following code in the plugin main file:

function myplugin_lang_init() {
    $domain = 'mydomain';
    // The "plugin_locale" filter is also used in load_plugin_textdomain()
    $locale = apply_filters('plugin_locale', get_locale(), $domain);
    load_textdomain($domain, WP_LANG_DIR.'/plugins/'.$domain.'-'.$locale.'.mo');
    load_plugin_textdomain($domain, FALSE, dirname(plugin_basename(__FILE__)).'/languages/');
// myplugin_lang_init(); // That works
add_action('plugins_loaded', 'myplugin_lang_init'); // I don't know why but it doesn't work

That works only if I call myplugin_lang_init() directly, but doest't work if I do add_action('plugins_loaded', 'myplugin_lang_init').

Exactly the same code always was working perfectly in other plugins I've ever developed.

Could you please have a look at it? Have I missed something?

Thanks in advance.

1 Answer 1


In case of someone see this,

Following worked for me with 4 changes :

  1. translated files only in plugin lang dir
  2. using "init" hook instead of "plugins_loaded" and be sure strings to translate are loaded after this one
  3. using get_user_locale() to be sure to show the right language for logged users (if not logged, this return get_locale())
  4. translations files names are like this : en_US.mo/po, fr_FR.mo/po...
function myplugin_lang_init() {
    $domain = 'mydomain';
    $plugin_dir_lang = plugin_dir_path( __FILE__ ) . 'languages'; // assume that current php file is in plugin root directory
    // The "plugin_locale" filter is also used in load_plugin_textdomain()
    $locale = apply_filters('plugin_locale', get_user_locale(), $domain); // use get_user_locale() instead of get_locale()
    load_textdomain($domain, $plugin_dir_lang . '/' . $locale . '.mo');
    load_plugin_textdomain($domain, FALSE, $plugin_dir_lang);
// myplugin_lang_init(); // That works
add_action('init', 'myplugin_lang_init'); // use init instead of plugins_loaded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.