1

using the standard __('some-string', 'myplugin') for translations, strings are being correctly loaded for a Japanese (ja) locale in every function and script other than my AJAX functions.

my-plugin.php:

*Text Domain: myplugin
*Domain Path: /languages/
*/
add_action( 'init', 'myplugin_load_textdomain' );
function myplugin_load_textdomain() {
    load_plugin_textdomain( 'myplugin', false, basename( dirname( __FILE__ ) ) . '/languages' ); 
}
require_once(basename( dirname( __FILE__)."/classes/myplugin_handler.class.php");
global $myplugin_handler;
$myplugin_handler = new myplugin_handler();

myplugin_handler.class.php:

class myplugin_handler
{
    public function __construct()
    {
        add_action('wp_ajax_myplugin_ajax', array($this, 'myplugin_ajax_handler'));
        add_action('wp_ajax_nopriv_myplugin_ajax', array($this, 'myplugin_ajax_handler'));
    }

    public function myplugin_ajax_handler() {
        if ($_POST['action'] != 'myplugin_ajax') {
            wp_die(0);
        }
        if (!check_ajax_referer('myplugin-ajax-nonce', 'security')) {
            wp_die(0);
        }

        $closemsg = __('Close', 'myplugin');
        wp_die($closemsg);
    }
}

Close should translate to 閉じる but it does not. Where translation does work:

  • plugin function calls from the theme
  • plugin function calls from an admin screen
  • _e() and __() calls in embedded js in the footer
  • seemingly everywhere else besides AJAX functions

any help is appreciated

2 Answers 2

1

I think you just need to load the translations earlier to make them available for ajax hooks:

add_action( 'plugins_loaded', 'myplugin_load_textdomain' );
function myplugin_load_textdomain() {
    load_plugin_textdomain( 'myplugin', false, basename( dirname( __FILE__ ) ) . '/languages' ); 
} 
2
  • thanks for the tip but I should have clarified that I tried that hook, as well as after_setup_theme and neither of them resolved the problem
    – Evan
    Jan 30, 2018 at 13:42
  • It works here, I can not see any other problem in your code, so maybe the problem is anywhere else in you code base.
    – cybmeta
    Jan 30, 2018 at 15:50
0

It turns out I had a typo in the text domain for every call in my ajax function. For examples sake I entered the text domain as 'myplugin' in my question, but the real text domain is 'asumil-wishlist'. Instead of a hyphen I accidentally put an underscore, so __('somestring', 'asumil-wishlist') was __('somestring', 'asumil_wishlist').

Thanks anyways for the suggestions everyone.

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.