Vladimir Štus
Vladimir Štus

Reputation: 217

WordPress plugin localization, load_plugin_textdomain doesn't work

Ok i built plugin for contact form, I wanna add translation for it. In my main plugin files i add this code

function ap_action_init() {
    // Localization
    load_plugin_textdomain('prijava_forma', false, dirname(plugin_basename(__FILE__))."/languages";
}

// Add actions
add_action('init', 'ap_action_init');

in my file where contact form is written i have this

_e( 'Prva','prijava_forma' );

In my language folder I added the .mo and .po files created with Poedit.

Also I defined WPLANG in config.php and changed the language in the dashboard.

But i get no translation. Where could be problem, i am new to this?

Upvotes: 1

Views: 5032

Answers (3)

larshetfield
larshetfield

Reputation: 51

Also make sure that your string stands for itself. Do not append anything to the string, do this after the gettext function instead.

Wrong:

return __('Please translate me'.'(666)','your-textdomain');

Right:

return __('Please translate me','your-textdomain').'(666)';

Upvotes: 0

Sébastien Serre
Sébastien Serre

Reputation: 295

It may be also caused on hook where the functions is attached and where are the po/mo files.

On the Init Hooks, load_plugin_textdomain() returned true but string were not translated.

I changed the action to plugins_loaded as my po/mo were in a folder inside a Custom plugins.

Upvotes: 0

Nabil Kadimi
Nabil Kadimi

Reputation: 10394

There are many possible causes:

  • .mo file not readable or not found at all (.po file is not used by WordPress by the way)
  • The string you are expecting is not translated yet
  • Wrong .mo file name, valid names are ar.mo, fr_FR.mo..., invalid ones are br_BR.mo, arabic.po, AR.mo, ar_AR.mo... So make sure you get this one right.
  • for plugins the name will be the concatenation for the text domain, a dash and the locale: myplugin-ru_RU.mo

Check what load_plugin_textdomain() returned, if the .mo file was loaded, it should return true, in which case the next step would be to check that you are not missing the textdomain parameter in your __(), _e() and similar functions.

More on WordPress localization

Upvotes: 4

Related Questions