Raw
1 import { Prerequisites, CodeTabs, CodeTab, Table } from "docs-ui"
2
3 export const metadata = {
4 title: `Translate Custom Data Models`,
5 }
6
7 # {metadata.title}
8
9 In this chapter, you'll learn how to support translations for your custom data models using the Translation Module.
10
11 <Prerequisites
12 items={[
13 {
14 text: "Medusa v2.12.4 or later",
15 link: "https://github.com/medusajs/medusa/releases/tag/v2.12.4"
16 },
17 {
18 text: "Translation Module Configured",
19 link: "/commerce-modules/translation#configure-translation-module",
20 },
21 ]}
22 />
23
24 ## Summary
25
26 The Translation Module allows you to extend translation capabilities to custom data models in your Medusa application. Then, you can [manage translations from the Medusa Admin](!user-guide!/settings/translations), and serve translated resources in your configured locales.
27
28 By following this guide, you'll learn how to:
29
30 - Configure the Translation Module to support translations for your custom data models.
31 - Manage translations for your custom data models from the Medusa Admin.
32 - Serve translated resources in your configured locales.
33
34 ---
35
36 ## Prerequisites: Custom Data Model
37
38 This guide assumes you already have a custom [module](!docs!/learn/fundamentals/modules) with a data model. The guide will use a Blog Module with the following `Post` data model as an example:
39
40 ```ts title="src/modules/blog/models/post.ts"
41 import { model } from "@medusajs/framework/utils"
42
43 const Post = model.define("post", {
44 id: model.id().primaryKey(),
45 title: model.text(),
46 })
47
48 export default Post
49 ```
50
51 The module must also be registered in `medusa-config.ts`. For example:
52
53 ```ts title="medusa-config.ts"
54 module.exports = defineConfig({
55 // ...
56 modules: [
57 // other modules...
58 {
59 resolve: "./src/modules/blog",
60 },
61 ],
62 })
63 ```
64
65 ---
66
67 ## Step 1: Configure Translatable Data Models
68
69 <Note>
70
71 The `translatable` method is available since [Medusa v2.13.0](https://github.com/medusajs/medusa/releases/tag/v2.13.0). Previous versions required [configuring translatable data models](#deprecated-manage-from-medusa-configts) in `medusa-config.ts`, which is now deprecated.
72
73 </Note>
74
75 The first step is to configure the properties of your custom data models that you want to support translations for. This will allow admin users to translate the data model's records from the Medusa Admin, and manage whether the model and its properties are translatable.
76
77 For example, update your `Post` data model to mark its `title` property as translatable:
78
79 export const translatableModifierHighlights = [
80 ["5", "translatable", "Mark the title property as translatable."],
81 ]
82
83 ```ts title="src/modules/blog/models/post.ts" highlights={translatableModifierHighlights}
84 import { model } from "@medusajs/framework/utils"
85
86 const Post = model.define("post", {
87 id: model.id().primaryKey(),
88 title: model.text().translatable(),
89 })
90
91 export default Post
92 ```
93
94 When you add the `.translatable()` method to a property, the data model automatically appears in Settings → Translations in the Medusa Admin.
95
96 Next, go to [Step 2: Manage Translations from Medusa Admin](#step-2-manage-translations-from-medusa-admin).
97
98 ### Deprecated: Manage From medusa-config.ts
99
100 <Details summaryContent="Before Medusa v2.13.0">
101
102 If you're using a Medusa version prior to v2.13.0, you need to configure the Translation Module in `medusa-config.ts` to support translations for your custom data models.
103
104 Before proceeding, run the `build` command to ensure the generated types are up-to-date:
105
106 ```bash npm2yarn
107 npm run build
108 ```
109
110 This will allow you to benefit from auto-completion when configuring the Translation Module.
111
112 Next, in `medusa-config.ts`, add the `options.entities` property to the Translation Module configuration:
113
114 export const configHighlights = [
115 ["8", "entities", "The custom entities to support translations for."],
116 ["10", "type", "The name of the table for the custom data model."],
117 ["11", "fields", "The fields of the custom data model to support translations for."]
118 ]
119
120 ```ts title="medusa-config.ts" highlights={configHighlights}
121 module.exports = defineConfig({
122 // ...
123 modules: [
124 // other modules...
125 {
126 resolve: "@medusajs/medusa/translation",
127 options: {
128 entities: [
129 {
130 type: "post",
131 fields: ["title"],
132 },
133 ],
134 },
135 },
136 ],
137 })
138 ```
139
140 The `options.entities` option is an array of objects indicating the custom data models to support translations for. Each object has the following properties:
141
142 1. `type`: The name of the table where the custom data model is stored. This is the same value passed as the first parameter to `model.define`.
143 2. `fields`: An array of fields in the custom data model to support translations for.
144
145 </Details>
146
147 ---
148
149 ## Step 2: Manage Translations from Medusa Admin
150
151 After configuring the Translation Module, you can manage translations for your custom data models from the Medusa Admin.
152
153 Run the following command to start the Medusa application:
154
155 ```bash npm2yarn
156 npm run dev
157 ```
158
159 Then, open the Medusa Admin and go to Settings -> Translations. You should see your custom data model in the list of translatable resources.
160
161 ![Post data model highlighted in the list of translatable resources in the Medusa Admin](https://res.cloudinary.com/dza7lstvk/image/upload/v1767687396/Medusa%20Resources/CleanShot_2026-01-06_at_10.12.09_2x_w1hmua.png)
162
163 Click the Edit button for your custom data model to manage translations for its resources. You can manage translations for the configured locales and fields.
164
165 Learn more in the [Translations User Guide](!user-guide!/settings/translations).
166
167 ---
168
169 ## Step 3: Serve Translated Resources
170
171 Finally, you can serve translated resources for your custom data models in your configured locales. This section focuses on returning records with translated fields from API routes.
172
173 ### Pass Locale in API Requests
174
175 Medusa supports passing the desired locale in API requests to `/store` routes using either:
176
177 1. The `locale` query parameter.
178 2. The `x-medusa-locale` header.
179
180 For example, assuming you have a `/store/posts` API route, you can pass the locale in the request as follows:
181
182 <CodeTabs group="localization">
183 <CodeTab label="Query Parameter" value="query-parameter">
184 ```bash
185 curl "http://localhost:9000/store/posts?locale=fr-FR" \
186 -H 'x-publishable-api-key: {your_publishable_api_key}'
187 ```
188 </CodeTab>
189 <CodeTab label="Request Header" value="request-header">
190 ```bash
191 curl "http://localhost:9000/store/posts" \
192 -H 'x-publishable-api-key: {your_publishable_api_key}' \
193 -H 'x-medusa-locale: fr-FR'
194 ```
195 </CodeTab>
196 </CodeTabs>
197
198 <Note>
199
200 You must pass a publishable API key in the request header to store API routes. Learn more in the [Store API reference](!api!/store/publishable-api-key).
201
202 </Note>
203
204 If your API route isn't under the `/store` prefix, you must apply the `applyLocale` middleware. For example, add the middleware to the `src/api/middlewares.ts` file:
205
206 ```ts title="src/api/middlewares.ts"
207 import { applyLocale, defineMiddlewares } from "@medusajs/framework/http"
208
209 export default defineMiddlewares({
210 routes: [
211 {
212 matcher: "/posts",
213 middlewares: [applyLocale],
214 },
215 ],
216 })
217 ```
218
219 This allows you to pass the locale in the query parameter or request header for the `/posts` API route.
220
221 ### Handle Translations in API Routes
222
223 In your custom API routes, you can retrieve the request's locale from the `locale` property of the `MedusaRequest` object. Pass that property to [Query](!docs!/learn/fundamentals/query) to retrieve your data models with translated fields.
224
225 For example, to retrieve blog posts with translated titles in the `/store/posts` API route:
226
227 export const apiRouteHighlights = [
228 ["20", "locale", "Pass the request locale to the query options."],
229 ]
230
231 ```ts title="src/api/routes/store/posts.ts" highlights={apiRouteHighlights}
232 import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
233 import {
234 ContainerRegistrationKeys,
235 } from "@medusajs/framework/utils"
236
237 export async function GET(
238 req: MedusaRequest,
239 res: MedusaResponse
240 ) {
241 const query = req.scope.resolve(
242 ContainerRegistrationKeys.QUERY
243 )
244
245 const { data: posts } = await query.graph(
246 {
247 entity: "post",
248 fields: ["id", "title"],
249 },
250 {
251 locale: req.locale,
252 }
253 )
254
255 res.json({ posts })
256 }
257 ```
258
259 In this example, the `locale` option is set to `req.locale`. Medusa will set the `title` field of each post to its translated value if a translation is available for the requested locale. Otherwise, it returns the original value stored in the data model.
260
261 ---
262
263 ## Step 4: Test the Implementation
264
265 To test the implementation, start the Medusa application if you haven't already:
266
267 ```bash npm2yarn
268 npm run dev
269 ```
270
271 Then, send a request to your custom API route with the desired locale. For example:
272
273 ```bash
274 curl "http://localhost:9000/store/posts?locale=fr-FR" \
275 -H 'x-publishable-api-key: {your_publishable_api_key}'
276 ```
277
278 This should return the list of blog posts with their French translations for the `title` field if available:
279
280 ```json
281 {
282 "posts": [
283 {
284 "id": "post_123",
285 "title": "Titre de l'Article"
286 },
287 {
288 "id": "post_456",
289 "title": "Un Autre Titre"
290 }
291 ]
292 }
293 ```
294
295 ---
296
297 ## Pass Locale to useQueryGraphStep
298
299 If your API route executes a workflow and returns its result, you can retrieve data models with translated fields in the workflow by passing the locale to `useQueryGraphStep`.
300
301 For example:
302
303 export const workflowHighlights = [
304 ["5", "locale", "Pass the locale to the workflow"],
305 ["17", "locale", "Pass the request locale to the query options."],
306 ]
307
308 ```ts title="src/workflows/handle-posts.ts" highlights={workflowHighlights}
309 import { createWorkflow, WorkflowResponse } from "@medusajs/framework/workflows-sdk"
310 import { useQueryGraphStep } from "@medusajs/medusa/core-flows"
311
312 type WorkflowInput = {
313 locale: string;
314 }
315
316 export const handlePostsWorkflow = createWorkflow(
317 "handle-posts",
318 (input: WorkflowInput) => {
319 // do something...
320
321 const { data: posts } = useQueryGraphStep({
322 entity: "post",
323 fields: ["id", "title"],
324 options: {
325 locale: input.locale,
326 },
327 })
328
329 return new WorkflowResponse({
330 posts,
331 })
332 }
333 )
334 ```
335
336 In this example, the workflow accepts a `locale` input parameter. You then retrieve the posts with translated titles by passing the `locale` input to `useQueryGraphStep`.
337
338 The returned posts will have their `title` field set to the translated value if a translation is available for the requested locale.
339
340 You can execute the workflow in your custom API routes and pass the request locale as an input parameter:
341
342 ```ts title="src/api/routes/store/posts.ts"
343 import { MedusaRequest, MedusaResponse } from "@medusajs/framework/http"
344 import { handlePostsWorkflow } from "../../workflows/handle-posts"
345
346 export async function GET(
347 req: MedusaRequest,
348 res: MedusaResponse
349 ) {
350 const { result } = await handlePostsWorkflow(req.scope)
351 .run({
352 input: {
353 locale: req.locale,
354 },
355 })
356
357 res.json(result)
358 }
359 ```
360
361 The returned posts in the API response will have their `title` field set to the translated value if a translation is available for the requested locale.
362
363 ---
364
365 ## Manage Translations with the Translation Module Service
366
367 For more complex cases, you can manage translations for your custom data models programmatically using the Translation Module's service. You can create, update, delete, and retrieve translations of any resource using the service's methods.
368
369 Refer to the [Translation Module service reference](/references/translation) for a full list of available methods and how to use them.