Iulia Cazan
  • Home
  • My Plugins
    • Image Regenerate & Select Crop
    • Latest Post Shortcode
    • WPINS – Custom Google Maps for WordPress
    • Sticky Header 2020
    • Login IP & Country Restriction
    • Easy Populate Posts
    • Emails Catch All
    • Easy Replace Image
  • Tips & Tricks
  • Shop
    • Premium PluginsIf you are looking for a more sophisticated approach, for specific WordPress tools and customization, you are in the right place. If you cannot find what you are looking for or you need something more complex or specific, contact me for a quotation.
    • Image Regenerate & Select Crop ExtensionsIf you are looking for a more sophisticated approach, for specific WordPress tools and customization, you are in the right place. If you cannot find what you are looking for or you need something more complex or specific, contact me for a quotation.
    • My account
    • Cart
    • Checkout
    • Customer Support
    • Logout
  • Blog
  • Contact
Home • Tips & Tricks • How to fix translation in multi-language website + WooCommerce + AJAX checkout

How to fix translation in multi-language website + WooCommerce + AJAX checkout

I have been working recently on a multi-language website that is using WooCommerce > 3.5 and for some of the functionalities that were populated or updated with AJAX calls (for example the checkout page content like the payment methods, the terms and conditions, the cart items summary, etc.) the texts were only rendered in the default language, regardless of the user locale settings.

How to fix translation in multi-language website + WooCommerce + AJAX checkout

A Small and Quick Fix

I spent some time debugging and did some research, here is a small and quick fix that did the trick. I added this in my theme functions.php file, but it can be added in a plugin also.

add_filter( 'woocommerce_get_script_data', 'fix_ajax_handler_translation' );
if ( ! function_exists( 'fix_ajax_handler_translation' ) ) {
	/**
	 * Fix the lost translations on AJAX calls made in WordPress >= 5.0 with WooCommerce >= 3.5
	 * on checkout pages for example.
	 *
	 * @param array $params Script data.
	 * @return array
	 */
	function fix_ajax_handler_translation( $params ) {
		$locale = determine_locale();
		$lang   = ( ! empty( $locale ) ) ? strstr( $locale, '_', true ) : '';
		if ( empty( $lang ) ) {
			// Fail-fast, no need to check further. Fallback to the defaults.
			return $params;
		}
		if ( isset( $params['wc_ajax_url'] ) ) {
			$params['wc_ajax_url'] = $params['wc_ajax_url'] . '&lang=' . $lang;
		}
		return $params;
	}
}

Please note that if you use WordPress < 5.0 the native function determine_locale does not exist (it was introduced with 5.0), but you can use it as an alternative for get_user_locale.

I hope this helps anyone that encounters the same issue.

If you would like to support my work, consider making a donation, buy me a coffee, or share this on your feed.
A huge thanks in advance!

Are you interested in more programming tips and tricks?

Google reCAPTCHA, Google reCAPTCHA v3

How to Integrate Google reCAPTCHA v3 in Forms that Use AJAX Validation

I recently had to update one of the sites that is…

https://iuliacazan.ro/how-to-integrate-google-recaptcha-v3-in-forms-that-use-ajax-validation/
Acceptance Testing, Bucharest WordPress Meetup

Brief Introduction to Acceptance Testing

Last month I survived my first public presentation, at Bucharest WordPress Meetup…

https://iuliacazan.ro/brief-introduction-to-acceptance-tests/
align bottom, align top

Free CSS and HTML Grid Generator – Demo and Code Snippet

This is a useful online tool for both designers and programmers…

https://iuliacazan.ro/free-css-and-html-grid-generator-demo-and-code-snippet/
Default Template, Elementor

How to Automatically Set a Specific Template to New WordPress Posts – Snippet Included

Applying by default a specific…

https://iuliacazan.ro/how-to-automatically-set-a-specific-template-to-new-wordpress-posts-snippet-included/
February 26, 2019
,
Tips & Tricks,
Multi-language, Snippets, Translations, WooCommerce, WordPress
← Latest Post Shortcode 8.7
An Arctic Expedition to Northern Norway →
Iulia Cazan

I am a full-stack developer. I love WordPress and I enjoy creating custom web applications of all types.
© Copyright Iulia Cazan 2015-2023.

  • WordPress
  • Instagram
  • Facebook
  • Twitter
  • Home
  • My Plugins
  • Tips & Tricks
  • Online Tools
  • Shop
  • My Travels
  • Privacy Policy
  • Contact
  • Terms & Conditions
Iulia Cazan
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

Strictly Necessary Cookies

Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.

3rd Party Cookies

This website uses Google Analytics to collect anonymous information such as the number of visitors to the site and the most popular pages. Keeping this cookie enabled helps us to improve our website.

Also, in some pages, we embed YouTube videos that can help with showing different features of our plugins.

Please enable Strictly Necessary Cookies first so that we can save your preferences!