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:
parent
29ed02836a
commit
1b28b0f1ca
|
@ -2,3 +2,4 @@
|
|||
* Internal dependencies
|
||||
*/
|
||||
import './store-api-nonce';
|
||||
import './remove-user-locale';
|
||||
|
|
|
@ -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 );
|
|
@ -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;
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
Significance: patch
|
||||
Type: fix
|
||||
|
||||
Use the correct language in Cart/Checkout when the user language is different from the store.
|
Loading…
Reference in New Issue