gettext does not translate when called in ajax

it is too late but for public use:

/* if qTranslate is installed */
/* set front locale for ajax calls requested from front-end */

function set_locale_for_frontend_ajax_calls() {

    if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX
        && substr( $_SERVER['HTTP_REFERER'], 0, strlen( admin_url() ) ) != admin_url() ) {

        load_theme_textdomain( 'your-theme-domain-name', get_template_directory() . '/languages’ );
    }
}

add_action( 'admin_init', 'set_locale_for_frontend_ajax_calls' );

add_action('wp_head','jsURLs');

function jsURLs(){

    global $q_config;

    ?><script type="text/javascript">
        /* <![CDATA[ */
        var ajaxurl      = "<?php echo admin_url('admin-ajax.php?lang='.$q_config['language']); ?>";
        /* ]]> */
    </script><?php

}

it works for me if qTranslate is installed
but if not following maybe work:

/* if qTranslate is not installed */
/* set front locale for ajax calls requested from front-end */

function set_locale_for_frontend_ajax_calls() {

    if ( is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX
        && substr( $_SERVER['HTTP_REFERER'], 0, strlen( admin_url() ) ) != admin_url() ) {
        setlocale(LC_ALL, $_GET['lang']);
        load_theme_textdomain( 'your-theme-domain-name', get_template_directory() . '/languages’ );
    }
}

add_action( 'admin_init', 'set_locale_for_frontend_ajax_calls' );

add_action('wp_head','jsURLs');

function jsURLs(){

    global $q_config;

    ?><script type="text/javascript">
        /* <![CDATA[ */
        var ajaxurl      = "<?php echo admin_url('admin-ajax.php?lang='.get_locale()); ?>";
        /* ]]> */
    </script><?php

}

Leave a Comment