Reputation: 51
I've had problem with ajax requests that my requests translating __()
functions didn't work.
Googled it from everywhere.
Everywhere was as an answer that url parameter in the following form ?lang=fi
should be added for the requests.
Upvotes: 1
Views: 1968
Reputation: 51
It turned out that if you are using Polylang your ajax requests url must be in the following format /fi
. Note for the default language this shouldn't be defined at all.
Solved the problem with this change. I hope this helps someone.
Upvotes: 2
Reputation: 21
I know am late to the party but I couldn't find any answers anywhere that will solve this problem and Libla's answer it's just right.
Just a little context:
I have a multilingual Woocommerce website and all translations were working great except for the checkout "order review" that is loading with Ajax. This part after Ajax is showing in the website language (the one you have set up in settings).
All of that is because the Ajax call ignores the "?lang=" parameter (probably just the Polylang plugin, I never checked with other translation plugins).
Instead, Ajax should contain the language parameter (like in the browser www.yourwebsite.com/fi).
To solve that, I've added a filter (in functions.php)
/** Fix ajax handler translation */
add_filter( 'woocommerce_get_script_data', 'ajax_handler_fix_translation' );
if ( ! function_exists( 'ajax_handler_fix_translation' ) ) {
function ajax_handler_fix_translation( $params ) {
/** Get the current language */
$locale = determine_locale();
/** Take just the first part of the $locale */
$lang = ( ! empty( $locale ) ) ? strstr( $locale, '_', true ) : '';
if ( empty( $lang ) ) {
/** If there is no $lang parameter, just return to standard */
return $params;
}
if ( isset( $params['wc_ajax_url'] ) ) {
$params['wc_ajax_url'] = '/'.$lang.$params['wc_ajax_url'];
}
return $params;
}
}
Upvotes: 2