How would you go about setting the correct language returned in AJAX calls, when you are not using any plugin?

All other strings in page work fine just by defining WPLANG to a proper locale.


function lpml_get_quiz_result_callback() 

    // do plenty of stuff before this 

    // this is always returned in original english language, even if I setlocale before or load textdomain manually.
    $message = __('Some english defined string', 'my_txt_domain');
    $status = true;

    echo json_encode(array('status' => $status, 'message' => $message));


add_action('wp_ajax_lpml_get_quiz_result', 'lpml_get_quiz_result_callback');
add_action('wp_ajax_nopriv_lpml_get_quiz_result', 'lpml_get_quiz_result_callback');

I checked these questions: q1, q2, but none applies / solves my case. :/

I tried setting locale and loading textdomain before using __() strings in ajax, but to no avail (tried it just before, and also in admin_init hook for DOING_AJAX), both did not work.

setlocale(LC_ALL, $_POST['lang']);

load_theme_textdomain( 'my_txt_domain', get_template_directory() . '/languages' );

Any tips appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.