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.