eric.itzhak
eric.itzhak

Reputation: 16062

load_plugin_textdomain not working

Hey i'm trying to localize a plugin called Donate Plus ( which locallized technicly).

the plugin came with en_CA and de_DE files, i've tried creating a he_IL file without success. So i've tried with the de files came with the plugin but didn't work.

I've set the WPLANG in wp-config.php to de_DE yet that dosen't change the code.

this is the setting code :

load_plugin_textdomain( 'dplus', '/wp-content/plugins/donate-plus' );

And i did check that all the string are set to be localized.

Anyone has a clue?

Upvotes: 7

Views: 8959

Answers (6)

C.Christensen
C.Christensen

Reputation: 416

I had a similar issue where I was loading the translation files with the load_plugin_textdomain function from within a service class using PSR-4. This meant that the dirname( plugin_basename( __FILE__ ) ) string returned the wrong path.

  • The correct path is the relative path your-plugin/languages (assuming you are loading the translation files from the /languages directory).
  • Absolute paths such as /var/www/html/wp-content/plugins/my-plugin/languages won't work.

My plugins file structure looks something like this:

- my-plugin
    - assets
    - languages
    - services
        - Api
        - Base
            Translation.php
        - ...
        Plugin.php
    - vendor
    - views
    composer.json
    composer.lock
    index.php
    my-plugin.php
    uninstall.php

Since my Translation service is placed in the /services/Base/ directory, this worked for me:

$root = plugin_basename(dirname(__FILE__, 3));
load_plugin_textdomain( 'my-plugin', false, "$root/languages/");

Also, I used no action hook at all instead of init or plugins_loaded and fired the load_plugin_textdomain function at the beginning of the plugin, since the hooks don't fire early enough for the admin menu and action links to get translated.

Upvotes: 1

BinhPT
BinhPT

Reputation: 1

Use: load_textdomain( TEXT_DOMAIN , WP_PLUGIN_DIR .'/'.dirname( plugin_basename( FILE ) ) . '/languages/'. get_locale() .'.mo' );

Upvotes: -1

darthcharmander
darthcharmander

Reputation: 141

I just was with a similar isue, did you try to rename your files from de_DE.po and de_DE.mo to name-of-plugin-de_DE.mo and name-of-plugin-de_DE.po (changing name-of-plugin with yours, of course)?

dplus-de_DE.mo and dplus-de_DE.po It must work ;)

Upvotes: 13

Dennis
Dennis

Reputation: 31

load_plugin_textdomain takes three parameters. In your case it would be something like this (assuming the .po and .mo files are located in a subdir called 'languages')

load_plugin_textdomain( 'dplus', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );

Upvotes: 3

Sudar
Sudar

Reputation: 20000

I checked the source of DonatePlus Plugin and I found that the Plugin is doing localization wrongly.

The load_plugin_textdomain() call is made inside the DonatePlus classes constructor. But it should be present inside the 'init' hook. Trying adding the following code (which is at the of the file) inside the init function.

if( class_exists('DonatePlus') )
    $donateplus = new DonatePlus();

Upvotes: 1

Sudar
Sudar

Reputation: 20000

Where are all the .po and .mo files stored? Are they inside the /wp-content/plugins/donate-plus folder itself? If not then change the path or move the files.

Upvotes: -1

Related Questions