@@ -10,6 +10,7 @@ import {
1010} from "./types"
1111
1212export const PUBLISHABLE_KEY_HEADER = "x-publishable-api-key"
13+ export const LOCALE_STORAGE_KEY = "medusa_locale"
1314
1415// We want to explicitly retrieve the base URL instead of relying on relative paths that differ in behavior between browsers.
1516const getBaseUrl = ( passedBaseUrl : string ) => {
@@ -112,6 +113,18 @@ export class Client {
112113 private DEFAULT_JWT_STORAGE_KEY = "medusa_auth_token"
113114 private token = ""
114115
116+ private locale_ = ""
117+
118+ get locale ( ) {
119+ if ( hasStorage ( "localStorage" ) ) {
120+ const storedLocale = window . localStorage . getItem ( LOCALE_STORAGE_KEY )
121+ if ( storedLocale ) {
122+ return storedLocale
123+ }
124+ }
125+ return this . locale_
126+ }
127+
115128 constructor ( config : Config ) {
116129 this . config = { ...config , baseUrl : getBaseUrl ( config . baseUrl ) }
117130 const logger = config . logger || {
@@ -126,9 +139,20 @@ export class Client {
126139 debug : config . debug ? logger . debug : ( ) => { } ,
127140 }
128141
142+ if ( hasStorage ( "localStorage" ) ) {
143+ this . locale_ = window . localStorage . getItem ( LOCALE_STORAGE_KEY ) || ""
144+ }
145+
129146 this . fetch_ = this . initClient ( )
130147 }
131148
149+ setLocale ( locale : string ) {
150+ if ( hasStorage ( "localStorage" ) ) {
151+ window . localStorage . setItem ( LOCALE_STORAGE_KEY , locale )
152+ }
153+ this . locale_ = locale
154+ }
155+
132156 /**
133157 * `fetch` closely follows (and uses under the hood) the native `fetch` API. There are, however, few key differences:
134158 * - Non 2xx statuses throw a `FetchError` with the status code as the `status` property, rather than resolving the promise
@@ -226,10 +250,12 @@ export class Client {
226250 // We always want to fetch the up-to-date JWT token before firing off a request.
227251 const headers = new Headers ( defaultHeaders )
228252 const customHeaders = {
253+ "content-language" : this . locale ,
229254 ...this . config . globalHeaders ,
230255 ...( await this . getJwtHeader_ ( ) ) ,
231256 ...init ?. headers ,
232257 }
258+
233259 // We use `headers.set` in order to ensure headers are overwritten in a case-insensitive manner.
234260 Object . entries ( customHeaders ) . forEach ( ( [ key , value ] ) => {
235261 if ( value === null ) {
0 commit comments