load_plugin_textdomain not working
Asked Answered
C

6

7

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?

Cookgeneral answered 19/4, 2012 at 19:20 Comment(0)
P
13

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 ;)

Pyralid answered 15/3, 2015 at 22:56 Comment(2)
dplus-de_DE.mo and .po work, but it's because it matches the first parameter of the load_plugin_textdomain function. load_plugin_textdomain('dplus',....) -> dplus-de_DE.mo/poIntercourse
yes, you´re right. Is not the name of the plugin, is the textdomain (in the plugins I've tried was the same, but it's righ, it could be different)Pyralid
S
3

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/' );
Sulphone answered 2/5, 2012 at 7:23 Comment(0)
O
1

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();
Ortiz answered 20/4, 2012 at 10:9 Comment(3)
the init hook is a function called SaveSettings, i've tried writing your code in there but it didn't work.Cookgeneral
Oh and tried writing load_plugin_textdomain inside that. still the same. Howeverm my theme locallization dosen't work as well (It's premium so i guess it should) could this be related?Cookgeneral
Could be related. Can you post the localization code of your theme as well?Ortiz
C
1

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.

Cervantes answered 6/5, 2021 at 8:30 Comment(0)
O
-1

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.

Ortiz answered 20/4, 2012 at 6:21 Comment(0)
H
-1

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

Horton answered 2/7, 2015 at 7:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.