Support » Plugin: WordPress Language » AJAX request of plugin loads wrong textdomain on front

  • I have found an interesting issue with this otherwise lovely plugin.

    Settings: Front: Danish, Admin: English
    Locale in WP-config is Danish

    I’m making an AJAX-call in a plugin from the frontpage. Since admin-ajax.php ( all AJAX requests ) run in the admin scope of the WordPress installation, the English textdomain is loaded instead of the Danish one.

    If fixed this problem for now by adding this function to the locale filter. Source is a variable I pass with the AJAX request:

    function locale_ajax_check($locale)
    	{
    		if (is_admin() && defined( 'DOING_AJAX' ) && DOING_AJAX && isset($_REQUEST["source"]) && $_REQUEST["source"] == 'front')
    		{
    		   return get_option('wp_language_locale_front');
    
    		}
    		return $locale;
    	}

    Maybe there is a better way as future update though.

    https://wordpress.org/plugins/wordpress-language/

Viewing 1 replies (of 1 total)
  • i think correct way should be this, because of textdomain for translations:
    /* 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

    }

Viewing 1 replies (of 1 total)
  • The topic ‘AJAX request of plugin loads wrong textdomain on front’ is closed to new replies.