0

I'm sure I'm making a dumb mistake, but I don't for the life of me know what it might be.

I recently converted one of my larger plugins to utilize I18n (my first time doing so for any of my plugins). The correct function calls are evidently being used since WP was able to generate a complete POT file through the admin section of my plugin. Just in case, here are some examples of my I18n calls:

__('Settings', 'plugin-domain')
_e('Select which one to use.', 'plugin-domain')
_n('The following ID is invalid: ', 'The following IDs are invalid: ', count($bad), 'plugin-domain')

My issue is getting the translation to actually happen now that everything is in place. I've got the plugin-domain-es_ES.[po/mo] files in a languages directory under my plugin's root and have added define('WPLANG', 'es_ES'); to my wp-config.php.

In the main plugin file, I've added the following to hook text domain loading:

add_action('plugins_loaded', array('PluginName', 'loadTextDomain'));
class PluginName {
    public static function loadTextDomain() {
        load_plugin_textdomain('plugin-domain', false, plugin_dir_path(__FILE__) . 'languages/');
    }
}

With this in place, no translation is taking place in the areas where it should be within my plugin. I've also verified that the hook is being reached. I've also tried a few other hooks, including admin_init and init, but nothing seems to work.

2 Answers 2

2

The problem I was encountering was a result of not realizing that the 3rd parameter for load_plugin_textdomain should be a relative path to the language directory from WP_PLUGIN_DIR. Since plugin_dir_path returns an absolute path, things broke.

Changing the hooked function as follows solved the problem:

add_action('plugins_loaded', array('PluginName', 'loadTextDomain'));
class PluginName {
    public static function loadTextDomain() {
        load_plugin_textdomain('plugin-domain', false, dirname(plugin_basename(__FILE__ )) . '/languages/');
    }
}
0

Dan, why don't you move your function outside of your class and just register your language file normally. I would try something like this

function loadTextDomain() {
    load_plugin_textdomain( 'plugin-domain', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

add_action( 'after_setup_theme', 'loadTextDomain' );

Apart from this, everything looks to be sound.

2
  • I prefer to keep all of my function declarations inside of classes to avoid unintended collisions with other plugins/themes. It also keeps all of the code nicely namespaced. Any of the hooks will accept anything that is callable.
    – Dan
    Commented Mar 28, 2014 at 5:00
  • 1
    Pieter, I undeleted your answer as it has the right path set.
    – kaiser
    Commented Mar 28, 2014 at 10:52

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.