Puddinq.com sharing knowledge

load_plugin_textdomain is not working, how to debug

load_plugin_textdomain is not working, how to debug

Loading the textdomain for a custom plugin can feel like a nightmare. It either works, or you are endlessly trying to alter it untill it workd. wp_debug is no help and there you feel you do not have any way to debug it. Untill now:

add_action( 'plugins_loaded', 'puddinq_load_textdomain' );

/**
 * Load plugin textdomain.
 */
function puddinq_load_textdomain() {
    $path = dirname( plugin_basename(__FILE__)) . '/languages';
    $result = load_plugin_textdomain( dirname( plugin_basename(__FILE__)), false, $path );

    if (!$result) {
        $locale = apply_filters('plugin_locale', get_locale(), dirname( plugin_basename(__FILE__)));
        die("Could not find $path/" . dirname( plugin_basename(__FILE__)) . "-$locale.mo.");
    }
}

load_plugin_textdomain does return true or false and if it is false there is a way to see what it is looking for.

Below is a simular technique used for themes, and instead of a die a error log for your debug.txt

function load_my_theme_textdomain()
{

    $result = load_theme_textdomain('my_theme', get_template_directory() . '/languages');

    if ($result === false) {
        $locale = apply_filters('theme_locale', get_locale(), 'my_theme');
        error_log("Could not find " . get_template_directory() . "/languages/{$locale}.mo .");
    }
}
add_action('after_setup_theme', 'load_my_theme_textdomain');