From 1b28b0f1ca2cdfa3bab7a4d13a299a5067f3af31 Mon Sep 17 00:00:00 2001 From: Seghir Nadir Date: Fri, 13 Sep 2024 11:42:21 +0200 Subject: [PATCH] Use site language when making Store API requests in Cart/Checkout (#51244) * Use correct langauge by reseting the locale param * use typescript --- .../assets/js/middleware/index.js | 1 + .../js/middleware/remove-user-locale.ts | 38 +++++++++++++++++++ .../assets/js/middleware/store-api-nonce.js | 2 +- .../fix-correct-language-in-frontend | 4 ++ 4 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce-blocks/assets/js/middleware/remove-user-locale.ts create mode 100644 plugins/woocommerce/changelog/fix-correct-language-in-frontend diff --git a/plugins/woocommerce-blocks/assets/js/middleware/index.js b/plugins/woocommerce-blocks/assets/js/middleware/index.js index f5d970a90d3..0d071b0b069 100644 --- a/plugins/woocommerce-blocks/assets/js/middleware/index.js +++ b/plugins/woocommerce-blocks/assets/js/middleware/index.js @@ -2,3 +2,4 @@ * Internal dependencies */ import './store-api-nonce'; +import './remove-user-locale'; diff --git a/plugins/woocommerce-blocks/assets/js/middleware/remove-user-locale.ts b/plugins/woocommerce-blocks/assets/js/middleware/remove-user-locale.ts new file mode 100644 index 00000000000..d53a7cb6666 --- /dev/null +++ b/plugins/woocommerce-blocks/assets/js/middleware/remove-user-locale.ts @@ -0,0 +1,38 @@ +/** + * External dependencies + */ +import apiFetch from '@wordpress/api-fetch'; +import { addQueryArgs } from '@wordpress/url'; + +/** + * Internal dependencies + */ +import { isStoreApiRequest } from './store-api-nonce'; + +/** + * Middleware to add the '_locale=site' query parameter from API requests. + * + * TODO: Remove once https://github.com/WordPress/gutenberg/issues/16805 is fixed and replace by removing userLocaleMiddleware middleware. + * + * @param {Object} options Fetch options. + * @param {Object} options.url The URL of the request. + * @param {Object} options.path The path of the request. + * @param {Function} next The next middleware or fetchHandler to call. + * @return {*} The evaluated result of the remaining middleware chain. + */ +const removeUserLocaleMiddleware = ( + options: { url?: string; path?: string }, + next: ( options: { url?: string; path?: string } ) => Promise< unknown > +): Promise< unknown > => { + if ( typeof options.url === 'string' && isStoreApiRequest( options ) ) { + options.url = addQueryArgs( options.url, { _locale: 'site' } ); + } + + if ( typeof options.path === 'string' && isStoreApiRequest( options ) ) { + options.path = addQueryArgs( options.path, { _locale: 'site' } ); + } + + return next( options ); +}; + +apiFetch.use( removeUserLocaleMiddleware ); diff --git a/plugins/woocommerce-blocks/assets/js/middleware/store-api-nonce.js b/plugins/woocommerce-blocks/assets/js/middleware/store-api-nonce.js index a5b121d8693..dc813d71653 100644 --- a/plugins/woocommerce-blocks/assets/js/middleware/store-api-nonce.js +++ b/plugins/woocommerce-blocks/assets/js/middleware/store-api-nonce.js @@ -23,7 +23,7 @@ try { * * @return {boolean} Returns true if this is a store request. */ -const isStoreApiRequest = ( options ) => { +export const isStoreApiRequest = ( options ) => { const url = options.url || options.path; if ( ! url || ! options.method || options.method === 'GET' ) { return false; diff --git a/plugins/woocommerce/changelog/fix-correct-language-in-frontend b/plugins/woocommerce/changelog/fix-correct-language-in-frontend new file mode 100644 index 00000000000..00edcbc0400 --- /dev/null +++ b/plugins/woocommerce/changelog/fix-correct-language-in-frontend @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Use the correct language in Cart/Checkout when the user language is different from the store.