WooCommerce: Translation lost on AJAX call in Checkout page

I’m using WooCommerce for a site that I’m currently working on. And I’m creating it for a language other than English. I do the translations by means of the .po file.

The issue that I’m encountering is in the Checkout page. There is a table of order review which upon page loaded seems to be refreshed by means of AJAX call. Before the call the texts are all translated. But after the call the texts all reverted back to their original language (ie. English).

Read More

How do I retain the translation after the AJAX call?

enter image description here

Related posts

3 comments

  1. I think I fixed the issue with the following extended function in my plugin:

    function localize_backend( $locale ) {
        // set langauge if user is in admin area
        if( defined( 'WP_ADMIN' ) || ( isset( $_REQUEST['pwd'] ) && isset( $_REQUEST['kau-boys_backend_localization_language'] ) ) ){
            // ajax call from frontend
            if ( 'admin-ajax.php' == basename( $_SERVER[ 'SCRIPT_FILENAME' ] ) && strpos( admin_url(), $_SERVER[ 'HTTP_REFERER' ] ) === false ) {
                // if lang request param was set, change locale for AJAX response, else, don't overwrite locale (use frontend locale)
                if ( ! empty( $_REQUEST[ 'lang' ] ) ) {
                    $locale = $_REQUEST[ 'lang' ];
                }
            } else {
                $locale = backend_localization_get_locale();
            }
        }
    
        return $locale;
    }
    

    It will check if an AJAX call was made. If the call was coming from the frontend, it checks for a lang request param. If one could be found, it set’s the $locale to this value. Otherwise it doesn’t changes the locale to be backend setting, so it should be the locale from the frontend, defined in the wp-config.phpfile.

    Please update to version 2.1 of the plugin and check again.

    Thanks again for @Rarst and @cybnet for their hints.

  2. You have differents languages in your site, although your frontend is not multilingual, you serve one language in frontend and another in backend. Ajax in WordPress is processed in the “admin area” (wp-admin/admin-ajax.php), so the reponse of an ajax request will be in the language defined in the admin area. “English” in your case.

    I don’t know exactly how the plugin “Backend localization” works but other plugins that affect to the language need a language parameter in the ajax request. For example, to make qTranslate working in ajax, the lang param has to be added to the ajaxurl value in the javascript, something like ajaxurl+'?lang=es'. You will need to contact to developer of “Backend localization”

  3. Another possible fix is to add:

    $is_ajax = true; 
    

    at line 11 in an override version of review-order.php, which should be copied from:

    wp-contents/plugins/woocommerce/template/checkout/
    

    to:

    wp-contents/themes/{YOUR_THEME}/woocommerce/checkout/
    

Comments are closed.