1 ---
2 tags:
3 - translation
4 - storefront
5 products:
6 - translation
7 ---
8
9 import { Prerequisites, CodeTabs, CodeTab } from "docs-ui"
10
11 export const metadata = {
12 title: `Storefront Localization`,
13 }
14
15 # {metadata.title}
16
17 In this guide, you'll learn how to support multiple languages and locales in your storefront.
18
19 <Prerequisites
20 items={[
21 {
22 text: "Translation Module Configured",
23 link: "/commerce-modules/translation#configure-translation-module",
24 }
25 ]}
26 />
27
28 ## Overview
29
30 Medusa provides localization features through the [Translation Module](../../commerce-modules/translation/page.mdx). The Translation Module allows you to manage locales and translations for various resources in your store, such as products and categories.
31
32 Admin users can [specify supported locales](!user-guide!/settings/store#manage-store-locales), and [add translations for different resources](!user-guide!/settings/translations) from the Medusa Admin. Then, you can allow customers to choose their locale and show them translated content in your storefront.
33
34 Refer to the [Translation Module documentation](../../commerce-modules/translation/page.mdx) to learn more about translation concepts and features. This guide focuses on how to serve localized content in your storefront.
35
36 ---
37
38 ## Retrieve Supported Locales
39
40 You can retrieve the list of locales supported by your store using the [List Locales Store API route](!api!/store/locales/list-locales). You can display this list to customers, allowing them to select their preferred language.
41
42 For example:
43
44 <Note title="Tip">
45
46 Learn how to install and configure the JS SDK in the [JS SDK documentation](../../js-sdk/page.mdx).
47
48 </Note>
49
50 <CodeTabs group="store-request">
51 <CodeTab label="React" value="react">
52
53 ```tsx highlights={[["18"], ["19"], ["20"], ["21"], ["22"]]}
54 "use client" // include with Next.js 13+
55
56 import { useEffect, useState } from "react"
57 import { HttpTypes } from "@medusajs/types"
58 import { sdk } from "@/lib/sdk"
59
60 export default function Locales() {
61 const [loading, setLoading] = useState(true)
62 const [locales, setLocales] = useState<
63 HttpTypes.StoreLocale[]
64 >([])
65
66 useEffect(() => {
67 if (!loading) {
68 return
69 }
70
71 sdk.store.locale.list()
72 .then(({ locales: dataLocales }) => {
73 setLocales(dataLocales)
74 setLoading(false)
75 })
76 }, [loading])
77
78 return (
79 <div>
80 {loading && <span>Loading...</span>}
81 {!loading && locales.length === 0 && <span>No locales found.</span>}
82 {!loading && locales.length > 0 && (
83 <ul>
84 {locales.map((locale) => (
85 <li key={locale.code}>{locale.name}</li>
86 ))}
87 </ul>
88 )}
89 </div>
90 )
91 }
92 ```
93
94 </CodeTab>
95 <CodeTab label="JS SDK" value="js-sdk">
96
97 ```ts
98 sdk.store.locale.list()
99 .then(({ locales: dataLocales }) => {
100 // TODO use locales...
101 console.log(dataLocales)
102 })
103 ```
104
105 </CodeTab>
106 </CodeTabs>
107
108 The response has a `locales` field, which is an array of [locales](!api!/store/locales/schema).
109
110 Each locale has a `code` property that follows the [IETF BCP 47 standard](https://gist.github.com/typpo/b2b828a35e683b9bf8db91b5404f1bd1). For example, `en-US` represents American English, while `fr-FR` represents French (France).
111
112 ---
113
114 ## Retrieve Translations for Resources
115
116 Currently, you can retrieve translations using the [Store API routes](!api!/store) for product-related resources. The following Store API routes return translated content:
117
118 - `/store/products` and `/store/products/:id`
119 - `/store/product-variants` and `/store/product-variants/:id`
120 - `/store/product-categories` and `/store/product-categories/:id`
121 - `/store/collections` and `/store/collections/:id`
122 - `/store/product-types` and `/store/product-types/:id`
123 - `/store/product-tags` and `/store/product-tags/:id`
124
125 Future releases will expand translation support to additional resources.
126
127 By default, Medusa serves the original content of a resource. You can override the locale for a request to get the translated content if available.
128
129 ### Set Locale in JS SDK
130
131 The JS SDK has a `setLocale` method that you can use to set the locale for subsequent requests.
132
133 The JS SDK will automatically include the `x-medusa-locale` header in API requests. Also, if your application supports `localStorage`, the method sets the locale in the `medusa_locale` key of the `localStorage`.
134
135 For example, when a customer selects a locale in your storefront, you can set the locale in the JS SDK:
136
137 ```ts
138 sdk.setLocale("fr-FR")
139 // products content is fetched in French
140 const { products } = await sdk.store.product.list()
141 ```
142
143 In the above example, you set the locale to French using the `setLocale` method. It accepts the locale string in the [IETF BCP 47 standard](https://gist.github.com/typpo/b2b828a35e683b9bf8db91b5404f1bd1).
144
145 When you fetch the products, their content, such as title and description, is in French and falls back to the original content if translations aren't available.
146
147 You can also retrieve the currently set locale using the `getLocale` method:
148
149 ```ts
150 const currentLocale = sdk.getLocale()
151 console.log(currentLocale) // "fr-FR"
152 ```
153
154 ### Set Locale without JS SDK
155
156 If you're sending requests to the Store API without using the JS SDK, you can set the locale using either the `locale` query parameter or the `x-medusa-locale` header.
157
158 <CodeTabs group="set-locale">
159 <CodeTab label="Using Query Parameter" value="query-param">
160
161 ```bash
162 curl "http://localhost:9000/store/products?locale=fr-FR" \
163 -H 'x-publishable-api-key: {your_publishable_api_key}'
164 ```
165
166 </CodeTab>
167 <CodeTab label="Using Header" value="header">
168
169 ```bash
170 curl "http://localhost:9000/store/products" \
171 -H 'x-publishable-api-key: {your_publishable_api_key}' \
172 -H "x-medusa-locale: fr-FR"
173 ```
174
175 </CodeTab>
176 </CodeTabs>
177
178 Learn more in the [Store API reference](!api!/store/localization).
179
180 ### Set Locale in Server Environments
181
182 If you're making Store API requests from a server environment (such as server components or server actions in Next.js), you can use cookies to persist the selected locale across requests.
183
184 When a customer selects a locale in your storefront, set a cookie (for example, `_medusa_locale`) with the selected locale value:
185
186 ```ts
187 import { cookies } from "next/headers"
188
189 export const setLocale = async (locale: string) => {
190 const cookies_ = await cookies()
191 cookies_.set("_medusa_locale", locale, {
192 maxAge: 60 * 60 * 24 * 7,
193 })
194 }
195 ```
196
197 Then, create a utility that returns the locale from the cookie as a `x-medusa-locale` header:
198
199 ```ts
200 import { cookies } from "next/headers"
201
202 export const getLocaleHeader = async () => {
203 try {
204 const cookies_ = await cookies()
205 const locale = cookies_.get("_medusa_locale")?.value
206
207 return { "x-medusa-locale": locale }
208 } catch {
209 return {}
210 }
211 }
212 ```
213
214 Finally, use the utility to set the `x-medusa-locale` header in your Store API requests:
215
216 ```ts
217 import { getLocaleHeader } from "@/lib/locale"
218
219 export async function listProducts() {
220 const headers = {
221 ...(await getLocaleHeader()),
222 }
223
224 return sdk.store.product.list({}, { headers })
225 }
226 ```
227
228 ### Retrieve Translated Products
229
230 When you pass the locale while fetching products, their content will be in the specified locale if translations are available. Learn more in the [List Products](../products/list/page.mdx) guide.
231
232 ### Set Cart's Locale
233
234 When creating a cart, you should set the cart's locale to ensure that item contents are in the correct language. You can also update the cart's locale later if the customer changes their language preference.
235
236 Learn more in the [Create Cart](../cart/create/page.mdx) and [Update Cart](../cart/update/page.mdx) guides.