Use site language when making Store API requests in Cart/Checkout (#51244)

* Use correct langauge by reseting the locale param

* use typescript
This commit is contained in:
Seghir Nadir 2024-09-13 11:42:21 +02:00 committed by GitHub
parent 29ed02836a
commit 1b28b0f1ca
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 44 additions and 1 deletions

View File

@ -2,3 +2,4 @@
* Internal dependencies * Internal dependencies
*/ */
import './store-api-nonce'; import './store-api-nonce';
import './remove-user-locale';

View File

@ -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 );

View File

@ -23,7 +23,7 @@ try {
* *
* @return {boolean} Returns true if this is a store request. * @return {boolean} Returns true if this is a store request.
*/ */
const isStoreApiRequest = ( options ) => { export const isStoreApiRequest = ( options ) => {
const url = options.url || options.path; const url = options.url || options.path;
if ( ! url || ! options.method || options.method === 'GET' ) { if ( ! url || ! options.method || options.method === 'GET' ) {
return false; return false;

View File

@ -0,0 +1,4 @@
Significance: patch
Type: fix
Use the correct language in Cart/Checkout when the user language is different from the store.