WooCommerce: Translation lost on AJAX call in Checkout page [closed]

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).

How do I retain the translation after the AJAX call?

enter image description here

3 Answers
3

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.

Leave a Comment