Skip to content

Commit 665b80a

Browse files
authored
feat(): Add support for locale to the js sdk (medusajs#14306)
* feat(): Add support for locale to the js sdk * Create great-icons-thank.md
1 parent 7b4dda5 commit 665b80a

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

.changeset/great-icons-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@medusajs/js-sdk": patch
3+
---
4+
5+
feat(): Add support for locale to the js sdk

packages/core/js-sdk/src/client.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "./types"
1111

1212
export 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.
1516
const 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) {

packages/core/js-sdk/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class Medusa {
1818
this.store = new Store(this.client)
1919
this.auth = new Auth(this.client, config)
2020
}
21+
22+
setLocale(locale: string) {
23+
this.client.setLocale(locale)
24+
}
25+
26+
getLocale() {
27+
return this.client.locale
28+
}
2129
}
2230

2331
export default Medusa

0 commit comments

Comments
 (0)