1 import { CodeTabs, CodeTab } from "docs-ui"
2
3 export const metadata = {
4 title: `${pageNumber} Translate Admin Customizations`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn how to add translations to your Medusa Admin widgets and UI routes.
10
11 <Note>
12
13 Translations for admin customizations are available from [Medusa v2.11.1](https://github.com/medusajs/medusa/releases/tag/v2.11.1).
14
15 </Note>
16
17 ## Translations in the Medusa Admin
18
19 The Medusa Admin dashboard supports [multiple languages](!user-guide!/tips/languages) for its interface. Medusa uses [react-i18next](https://react.i18next.com/) to manage translations in the admin dashboard.
20
21 <Note>
22
23 Medusa Admin translations apply to the interface only. To translate content like product data, check out the [Translations user guide](!user-guide!/settings/translations).
24
25 </Note>
26
27 When you create [Widgets](../widgets/page.mdx) or [UI Routes](../ui-routes/page.mdx) to customize the Medusa Admin, you can provide translations for the text content in your customizations, allowing users to view your customizations in their preferred language.
28
29 You can add translations for your admin customizations within your Medusa project or as part of a plugin.
30
31 ---
32
33 ## How to Add Translations to Admin Customizations
34
35 ### Step 1: Create Translation Files
36
37 Translation files are JSON files containing key-value pairs, where the key identifies a text string and the value is the translated text or a nested object of translations.
38
39 For example, to add English translations, create the file `src/admin/i18n/json/en.json` with the following content:
40
41 <Note>
42
43 English is the default language for Medusa Admin, so it's recommended to always include an English translation file.
44
45 </Note>
46
47 ![Directory structure showing where to place translation files](https://res.cloudinary.com/dza7lstvk/image/upload/v1761555573/Medusa%20Book/translations-json_m2pvet.jpg)
48
49 ```json title="src/admin/i18n/json/en.json"
50 {
51 "brands": {
52 "title": "Brands",
53 "description": "Manage your product brands"
54 },
55 "done": "Done"
56 }
57 ```
58
59 You can create additional translation files for other languages by following the same structure. For example, for Spanish, create `src/admin/i18n/json/es.json`:
60
61 ![Directory structure showing where to place translation files](https://res.cloudinary.com/dza7lstvk/image/upload/v1761555573/Medusa%20Book/translations-json-es_s1etna.jpg)
62
63 ```json title="src/admin/i18n/json/es.json"
64 {
65 "brands": {
66 "title": "Marcas",
67 "description": "Gestiona las marcas de tus productos"
68 },
69 "done": "Hecho"
70 }
71 ```
72
73 ### Step 2: Load Translation Files
74
75 Next, to load the translation files, create the file `src/admin/i18n/index.ts` with the following content:
76
77 ![Directory structure showing i18n index file](https://res.cloudinary.com/dza7lstvk/image/upload/v1761555573/Medusa%20Book/translations-index_cgrj0t.jpg)
78
79 ```ts title="src/admin/i18n/index.ts"
80 import en from "./json/en.json" with { type: "json" }
81 import es from "./json/es.json" with { type: "json" }
82
83 export default {
84 en: {
85 translation: en,
86 },
87 es: {
88 translation: es,
89 },
90 }
91 ```
92
93 The `src/admin/i18n/index.ts` file imports the JSON translation files. You must include the `with { type: "json" }` directive to ensure the JSON files load correctly.
94
95 The file exports an object that maps two-character language codes (like `en` and `es`) to their respective translation data.
96
97 ### Step 3: Use Translations in Admin Customizations
98
99 Finally, you can use the translations in your admin customizations by using the `useTranslation` hook from `react-i18next`.
100
101 <Note>
102
103 The `react-i18next` package is already included in the Medusa Admin's dependencies, so you don't need to install it separately. However, `pnpm` users may need to install it manually due to package resolution issues.
104
105 </Note>
106
107 For example, create the file `src/admin/widgets/product-brand.tsx` with the following content:
108
109 export const translationHighlights = [
110 ["3", "useTranslation", "Import `useTranslation` hook."],
111 ["6", "t", "Access translation function."],
112 ["10", "t", "Get translated text using keys."]
113 ]
114
115 ```tsx title="src/admin/widgets/product-brand.tsx" highlights={translationHighlights}
116 import { defineWidgetConfig } from "@medusajs/admin-sdk"
117 import { Button, Container, Heading } from "@medusajs/ui"
118 import { useTranslation } from "react-i18next"
119
120 const ProductWidget = () => {
121 const { t } = useTranslation()
122 return (
123 <Container className="p-0">
124 <div className="flex items-center justify-between px-6 py-4">
125 <Heading level="h2">{t("brands.title")}</Heading>
126 <p>{t("brands.description")}</p>
127 </div>
128 <div className="flex justify-end px-6 py-4">
129 <Button variant="primary">{t("done")}</Button>
130 </div>
131 </Container>
132 )
133 }
134
135 export const config = defineWidgetConfig({
136 zone: "product.details",
137 })
138
139 export default ProductWidget
140 ```
141
142 In the above example, you retrieve the `t` function from the `useTranslation` hook. You then use this function to get the translated text by providing the appropriate keys defined in your translation JSON files.
143
144 Nested keys are joined using dot notation. For example, `brands.title` refers to the `title` key inside the `brands` object in the translation files.
145
146 ### Test Translations
147
148 To test the translations, start the Medusa application with the following command:
149
150 ```bash npm2yarn
151 npm run dev
152 ```
153
154 Then, go to a product details page in the Medusa Admin dashboard. If your default language is set to English, you'll see the widget displaying text in English.
155
156 Next, [change the admin language](!user-guide!/settings/profile#edit-profile-details) to Spanish. The widget will now display the text in Spanish.
157
158 ---
159
160 ## Translation Auto-Completion
161
162 <Note>
163
164 Translation auto-completion is available from [Medusa v2.11.2](https://github.com/medusajs/medusa/releases/tag/v2.11.2).
165
166 </Note>
167
168 To enhance your development experience, you can set up auto-completion for translation keys. This will allow you to auto-complete translation keys from Medusa's default translations as well as your custom translations.
169
170 To set up auto-completion, create the file `src/admin/i18next.d.ts` with the following content:
171
172 ```ts title="src/admin/i18n/i18next.d.ts"
173 // Import Medusa keys
174 import type { Resources } from "@medusajs/dashboard"
175 // Import your custom translation keys
176 // For example, import the English translation file
177 import type enTranslation from "./i18n/en.json"
178 // add other imports for different languages if needed...
179 // import type esTranslation from "./i18n/es.json"
180
181 declare module "i18next" {
182 interface CustomTypeOptions {
183 fallbackNS: "translation"
184 resources: {
185 translation: Resources["translation"]
186 // Optional: add custom namespaces here
187 // For example, if you have a custom namespace called 'brands':
188 brands: typeof enTranslation & Resources["translation"]
189 }
190 }
191 }
192 ```
193
194 ---
195
196 ## How Translations are Loaded
197
198 When you load the translations with the `translation` key in `src/admin/i18n/index.ts`, your custom Medusa Admin translations are merged with the default Medusa Admin translations:
199
200 - Translation keys in your custom translations override the default Medusa Admin translations.
201 - The default Medusa Admin translations are used as a fallback when a key is not defined in your custom translations.
202
203 For example, consider the following widget and translation file:
204
205 <CodeTabs group="widget-translation">
206 <CodeTab label="Widget" value="widget">
207
208 ```tsx title="src/admin/widgets/product-brand.tsx"
209 import { defineWidgetConfig } from "@medusajs/admin-sdk"
210 import { Button, Container, Heading } from "@medusajs/ui"
211 import { useTranslation } from "react-i18next"
212
213 const ProductWidget = () => {
214 const { t } = useTranslation()
215 return (
216 <Container className="p-0">
217 <div className="flex items-center justify-between px-6 py-4">
218 {/* Output: Custom Brands Title */}
219 <Heading level="h2">{t("brands.title")}</Heading>
220 {/* Output: brands.description */}
221 <p>{t("brands.description")}</p>
222 </div>
223 <div className="flex justify-end px-6 py-4">
224 {/* Output: Custom Save */}
225 <Button variant="primary">{t("actions.save")}</Button>
226 {/* Output: Delete */}
227 <Button variant="primary">{t("actions.delete")}</Button>
228 </div>
229 </Container>
230 )
231 }
232
233 export const config = defineWidgetConfig({
234 zone: "product.details",
235 })
236
237 export default ProductWidget
238 ```
239
240 </CodeTab>
241 <CodeTab label="Translation File" value="translation-file">
242
243 ```json title="src/admin/i18n/json/en.json"
244 {
245 "brands": {
246 "title": "Custom Brands Title"
247 },
248 "actions": {
249 "save": "Custom Save"
250 }
251 }
252 ```
253
254 </CodeTab>
255 <CodeTab label="Loaded Translations" value="loaded-translations">
256
257 ```ts title="src/admin/i18n/index.ts"
258 import en from "./json/en.json" with { type: "json" }
259
260 export default {
261 en: {
262 translation: en,
263 },
264 // other languages...
265 }
266 ```
267
268 </CodeTab>
269 </CodeTabs>
270
271 The widget will render the following for each translation key:
272
273 - `brands.title`: Defined in your custom translation file, so it outputs `Custom Brands Title`.
274 - `brands.description`: Not defined in your custom translation file or the default Medusa Admin translations, so it outputs the key itself: `brands.description`.
275 - `actions.save`: Defined in your custom translation file, so it outputs `Custom Save`.
276 - `actions.delete`: Not defined in your custom translation file, so it falls back to the default Medusa Admin translation and outputs `Delete`.
277
278 ### Custom Translation Namespaces
279
280 To avoid potential key conflicts between your custom translations and the default Medusa Admin translations, you can use custom namespaces. This is particularly useful when developing plugins that add admin customizations, as it prevents naming collisions with other plugins or the default Medusa Admin translations.
281
282 To add translations under a custom namespace, change the `[language].translation` key in the `src/admin/i18n/index.ts` file to your desired namespace:
283
284 export const namespacesHighlights = [
285 ["6", "brands", "Use `brands` as custom namespace."],
286 ["9", "brands", "Use `brands` as custom namespace."]
287 ]
288
289 ```ts title="src/admin/i18n/index.ts" highlights={namespacesHighlights}
290 import en from "./json/en.json" with { type: "json" }
291 import es from "./json/es.json" with { type: "json" }
292
293 export default {
294 en: {
295 brands: en,
296 },
297 es: {
298 brands: es,
299 },
300 }
301 ```
302
303 The translation files will now be loaded under the `brands` namespace.
304
305 Then, in your admin customizations, specify the namespace when using the `useTranslation` hook:
306
307 ```tsx title="src/admin/widgets/product-brand.tsx" highlights={[["7"]]}
308 import { defineWidgetConfig } from "@medusajs/admin-sdk"
309 import { Button, Container, Heading } from "@medusajs/ui"
310 import { useTranslation } from "react-i18next"
311
312 // The widget
313 const ProductWidget = () => {
314 const { t } = useTranslation("brands")
315 return (
316 <Container className="p-0">
317 <div className="flex items-center justify-between px-6 py-4">
318 <Heading level="h2">{t("brands.title")}</Heading>
319 <p>{t("brands.description")}</p>
320 </div>
321 <div className="flex justify-end px-6 py-4">
322 <Button variant="primary">{t("done")}</Button>
323 </div>
324 </Container>
325 )
326 }
327
328 // The widget's configurations
329 export const config = defineWidgetConfig({
330 zone: "product.details",
331 })
332
333 export default ProductWidget
334 ```
335
336 Translations are now loaded only from the `brands` namespace without conflicting with other translation keys in the Medusa Admin.
337
338 ---
339
340 ## Translation Tips
341
342 ### Translation Organization
343
344 To keep your translation files organized, especially as they grow, consider grouping related translation keys into nested objects. This helps maintain clarity and structure.
345
346 It's recommended to create a nested object for each domain (for example, `brands`, `products`, etc...) and place related translation keys within those objects. This makes it easier to manage and locate specific translations.
347
348 For example:
349
350 ```json title="src/admin/i18n/json/en.json"
351 {
352 "brands": {
353 "title": "Brands",
354 "description": "Manage your product brands",
355 "actions": {
356 "add": "Add Brand",
357 "edit": "Edit Brand"
358 }
359 }
360 }
361 ```
362
363 You can then access these nested translations using dot notation, such as `brands.title` or `brands.actions.add`.
364
365 ### Variables in Translations
366
367 Translation values can include variables that are dynamically replaced at runtime. Variables are defined using double curly braces `{{variableName}}` in the translation files.
368
369 For example, in your translation file `src/admin/i18n/json/en.json`, define a translation with a variable:
370
371 ```json title="src/admin/i18n/json/en.json"
372 {
373 "welcome_message": "Welcome, {{username}}!"
374 }
375 ```
376
377 Then, in your admin customization, pass the variable value in the second object parameter of the `t` function:
378
379 ```tsx title="src/admin/widgets/welcome-widget.tsx"
380 t("welcome_message", { username: "John" })
381 ```
382
383 This will output: `Welcome, John!`
384
385 ### Pluralization
386
387 The `t` function supports pluralization based on a count value. You can define singular and plural forms in your translation files using the `_one`, `_other`, and `_zero` suffixes.
388
389 For example, in your translation file `src/admin/i18n/json/en.json`, define the following translations:
390
391 ```json title="src/admin/i18n/json/en.json"
392 {
393 "item_count_one": "You have {{count}} item.",
394 "item_count_other": "You have {{count}} items.",
395 "item_count_zero": "You have no items."
396 }
397 ```
398
399 Then, in your admin customization, use the key without the suffix and provide the `count` variable:
400
401 ```tsx title="src/admin/widgets/item-count-widget.tsx"
402 t("item_count", { count: itemCount })
403 ```
404
405 This will render one of the following based on the value of `itemCount`:
406
407 1. If `itemCount` is `0`, `item_count_zero` is used: `You have no items.`
408 2. If `itemCount` is `1`, `item_count_one` is used: `You have 1 item.`
409 3. If `itemCount` is greater than `1`, `item_count_other` is used: `You have X items.`
410
411 ### Element Interpolation
412
413 Your translation strings can include HTML or React element placeholders that are replaced with actual elements at runtime. This is useful for adding links, bold text, or other formatting within translated strings.
414
415 Elements to be interpolated are defined using angle brackets `<index></index>` in the translation files, where `index` is a zero-based index representing the element's position.
416
417 For example, in your translation file `src/admin/i18n/json/en.json`, define a translation with element placeholders:
418
419 ```json title="src/admin/i18n/json/en.json"
420 {
421 "terms_and_conditions": "Please read our <0>Terms and Conditions</0>."
422 }
423 ```
424
425 Then, in your admin customization, import the `Trans` component from `react-i18next` that allows you to interpolate elements:
426
427 ```tsx title="src/admin/widgets/terms-widget.tsx"
428 import { Trans } from "react-i18next"
429 ```
430
431 Finally, use the `Trans` component in the `return` statement to render the translation with the interpolated elements:
432
433 ```tsx title="src/admin/widgets/terms-widget.tsx"
434 <Trans
435 i18nKey="terms_and_conditions"
436 components={[
437 <a href="https://example.com/terms" className="text-blue-600 underline" />,
438 ]}
439 />
440 ```
441
442 The `components` prop is an array of React elements that correspond to the placeholders defined in the translation string. In this case, the `<0></0>` placeholder is replaced with the anchor `<a>` element.
443
444 #### Passing Variables with Element Interpolation
445
446 You can also pass translation variables to the `Trans` component as props. For example, to include a username variable:
447
448 ```tsx title="src/admin/widgets/welcome-widget.tsx"
449 <Trans
450 i18nKey="welcome_message"
451 username="John"
452 components={[
453 <strong />,
454 ]}
455 />
456 ```
457
458 The `username` prop replaces the `{{username}}` variable in the translation string, and the `<0></0>` placeholder is replaced with the `<strong>` element.
459
460 #### Using Namespaces with Element Interpolation
461
462 If you're loading translations from a custom namespace, specify the namespace in the `Trans` component using the `ns` prop:
463
464 ```tsx title="src/admin/widgets/product-brand.tsx"
465 <Trans
466 i18nKey="brands.count"
467 ns="brands"
468 count={5}
469 components={[<strong />]}
470 />
471 ```
472
473 The `ns` prop indicates that the translation should be loaded from the `brands` namespace.
474
475 #### Multiple Element Interpolation
476
477 You can interpolate multiple elements by defining multiple placeholders in the translation string and providing corresponding elements in the `components` array.
478
479 For example, define the following translation string:
480
481 ```json title="src/admin/i18n/json/en.json"
482 {
483 "welcome_message": "Hello, <0>{{username}}</0>! Please read our <1>Terms and Conditions</1>."
484 }
485 ```
486
487 Then, in your admin customization, you can use the `Trans` component with multiple elements:
488
489 ```tsx title="src/admin/widgets/welcome-widget.tsx"
490 <Trans
491 i18nKey="welcome_message"
492 username="John"
493 components={[
494 <strong />,
495 <a href="https://example.com/terms" className="text-blue-600 underline" />,
496 ]}
497 />
498 ```
499
500 The first placeholder `<0></0>` is replaced with the `<strong>` element, and the second placeholder `<1></1>` is replaced with the anchor `<a>` element.