|
1
|
--- |
|
2
|
generate_toc: true |
|
3
|
--- |
|
4
|
|
|
5
|
import { CodeTabs, CodeTab, ChildDocs, Prerequisites, Table } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Translation Module`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
In this section of the documentation, you will find resources to learn more about the Translation Module and how to use it in your application. |
|
14
|
|
|
15
|
<Prerequisites |
|
16
|
items={[ |
|
17
|
{ |
|
18
|
text: "Medusa v2.12.3 or later", |
|
19
|
link: "https://github.com/medusajs/medusa/releases/tag/v2.12.3" |
|
20
|
}, |
|
21
|
{ |
|
22
|
text: "Translation Feature Flag Enabled", |
|
23
|
link: "#configure-translation-module" |
|
24
|
} |
|
25
|
]} |
|
26
|
/> |
|
27
|
|
|
28
|
<Note title="Looking for no-code docs?"> |
|
29
|
|
|
30
|
Refer to the [Medusa Admin User Guide](!user-guide!/settings/translations) to learn how to manage translations in the dashboard. |
|
31
|
|
|
32
|
</Note> |
|
33
|
|
|
34
|
Medusa has translation features available out-of-the-box through the Translation Module. A [module](!docs!/learn/fundamentals/modules) is a standalone package that provides features for a single domain. Each of Medusa's commerce features is provided in Commerce Modules, such as the Translation Module. |
|
35
|
|
|
36
|
<Note> |
|
37
|
|
|
38
|
Refer to the [Module Isolation](!docs!/learn/fundamentals/modules/isolation) guide to learn more about why modules are isolated. |
|
39
|
|
|
40
|
</Note> |
|
41
|
|
|
42
|
## Translation Features |
|
43
|
|
|
44
|
- [Translation and Locale Management](./concepts/page.mdx): Manage locales and add translations for different resources in your store. |
|
45
|
- [Multi-Language Support](./storefront/page.mdx): Manage and serve resources like products in multiple languages to cater to a diverse customer base. |
|
46
|
- [Translation for Custom Models](./custom-data-models/page.mdx): Extend translation capabilities to custom data models in your Medusa application. |
|
47
|
- [Manage Translatable Resources from Medusa Admin](!user-guide!/settings/translations#manage-translatable-resources): Configure which resources and fields are translatable directly from the Medusa Admin dashboard. |
|
48
|
|
|
49
|
--- |
|
50
|
|
|
51
|
## Configure Translation Module |
|
52
|
|
|
53
|
The Translation Module is currently behind a feature flag. To use it in your Medusa application, add it to the `modules` array and enable the `translation` feature flag. |
|
54
|
|
|
55
|
In your `medusa-config.ts` file, add the Translation Module to the `modules` array and enable the `translation` feature flag: |
|
56
|
|
|
57
|
```ts title="medusa-config.ts" |
|
58
|
module.exports = defineConfig({ |
|
59
|
// ... |
|
60
|
modules: [ |
|
61
|
// other modules... |
|
62
|
{ |
|
63
|
resolve: "@medusajs/medusa/translation", |
|
64
|
}, |
|
65
|
], |
|
66
|
featureFlags: { |
|
67
|
translation: true, |
|
68
|
}, |
|
69
|
}) |
|
70
|
``` |
|
71
|
|
|
72
|
Then, run the following command to make the necessary database changes for the Translation Module: |
|
73
|
|
|
74
|
```bash npx2yarn |
|
75
|
npx medusa db:migrate |
|
76
|
``` |
|
77
|
|
|
78
|
You can then use the Translation Module in your Medusa application. |
|
79
|
|
|
80
|
--- |
|
81
|
|
|
82
|
## How to Use the Translation Module |
|
83
|
|
|
84
|
In your Medusa application, you build flows around Commerce Modules. A flow is built as a [Workflow](!docs!/learn/fundamentals/workflows), which is a special function composed of a series of steps that guarantees data consistency and a reliable rollback mechanism. |
|
85
|
|
|
86
|
You can build custom workflows and steps. You can also re-use Medusa's workflows and steps, which are provided by the `@medusajs/medusa/core-flows` package. |
|
87
|
|
|
88
|
For example: |
|
89
|
|
|
90
|
export const highlights = [ |
|
91
|
["12", "Modules.TRANSLATION", "Resolve the module in a step."] |
|
92
|
] |
|
93
|
|
|
94
|
```ts title="src/workflows/create-translation.ts" highlights={highlights} |
|
95
|
import { |
|
96
|
createWorkflow, |
|
97
|
WorkflowResponse, |
|
98
|
createStep, |
|
99
|
StepResponse, |
|
100
|
} from "@medusajs/framework/workflows-sdk" |
|
101
|
import { Modules } from "@medusajs/framework/utils" |
|
102
|
|
|
103
|
const createTranslationStep = createStep( |
|
104
|
"create-translation", |
|
105
|
async ({}, { container }) => { |
|
106
|
const translationModuleService = container.resolve(Modules.TRANSLATION) |
|
107
|
|
|
108
|
const translation = await translationModuleService.createTranslations({ |
|
109
|
reference_id: "product_123", |
|
110
|
reference: "product", |
|
111
|
locale_code: "fr-FR", |
|
112
|
translations: { |
|
113
|
title: "Produit Exemple", |
|
114
|
description: "Ceci est une description en français.", |
|
115
|
}, |
|
116
|
}) |
|
117
|
|
|
118
|
return new StepResponse({ translation }, translation.id) |
|
119
|
}, |
|
120
|
async (translationId, { container }) => { |
|
121
|
const translationModuleService = container.resolve(Modules.TRANSLATION) |
|
122
|
|
|
123
|
await translationModuleService.deleteTranslations([translationId]) |
|
124
|
} |
|
125
|
) |
|
126
|
|
|
127
|
export const createTranslationWorkflow = createWorkflow( |
|
128
|
"create-translation", |
|
129
|
() => { |
|
130
|
const { translation } = createTranslationStep() |
|
131
|
|
|
132
|
return new WorkflowResponse({ |
|
133
|
translation, |
|
134
|
}) |
|
135
|
} |
|
136
|
) |
|
137
|
``` |
|
138
|
|
|
139
|
You can then execute the workflow in your custom API routes, scheduled jobs, or subscribers: |
|
140
|
|
|
141
|
<CodeTabs group="resource-types"> |
|
142
|
<CodeTab label="API Route" value="api-route"> |
|
143
|
|
|
144
|
```ts title="src/api/workflow/route.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
145
|
import type { |
|
146
|
MedusaRequest, |
|
147
|
MedusaResponse, |
|
148
|
} from "@medusajs/framework/http" |
|
149
|
import { createTranslationWorkflow } from "../../workflows/create-translation" |
|
150
|
|
|
151
|
export async function GET( |
|
152
|
req: MedusaRequest, |
|
153
|
res: MedusaResponse |
|
154
|
) { |
|
155
|
const { result } = await createTranslationWorkflow(req.scope) |
|
156
|
.run() |
|
157
|
|
|
158
|
res.send(result) |
|
159
|
} |
|
160
|
``` |
|
161
|
|
|
162
|
</CodeTab> |
|
163
|
<CodeTab label="Subscriber" value="subscriber"> |
|
164
|
|
|
165
|
```ts title="src/subscribers/user-created.ts" highlights={[["11"], ["12"]]} collapsibleLines="1-6" expandButtonLabel="Show Imports" |
|
166
|
import { |
|
167
|
type SubscriberConfig, |
|
168
|
type SubscriberArgs, |
|
169
|
} from "@medusajs/framework" |
|
170
|
import { createTranslationWorkflow } from "../workflows/create-translation" |
|
171
|
|
|
172
|
export default async function handleUserCreated({ |
|
173
|
event: { data }, |
|
174
|
container, |
|
175
|
}: SubscriberArgs<{ id: string }>) { |
|
176
|
const { result } = await createTranslationWorkflow(container) |
|
177
|
.run() |
|
178
|
|
|
179
|
console.log(result) |
|
180
|
} |
|
181
|
|
|
182
|
export const config: SubscriberConfig = { |
|
183
|
event: "user.created", |
|
184
|
} |
|
185
|
``` |
|
186
|
|
|
187
|
</CodeTab> |
|
188
|
<CodeTab label="Scheduled Job" value="scheduled-job"> |
|
189
|
|
|
190
|
```ts title="src/jobs/run-daily.ts" highlights={[["7"], ["8"]]} |
|
191
|
import { MedusaContainer } from "@medusajs/framework/types" |
|
192
|
import { createTranslationWorkflow } from "../workflows/create-translation" |
|
193
|
|
|
194
|
export default async function myCustomJob( |
|
195
|
container: MedusaContainer |
|
196
|
) { |
|
197
|
const { result } = await createTranslationWorkflow(container) |
|
198
|
.run() |
|
199
|
|
|
200
|
console.log(result) |
|
201
|
} |
|
202
|
|
|
203
|
export const config = { |
|
204
|
name: "run-once-a-day", |
|
205
|
schedule: `0 0 * * *`, |
|
206
|
} |
|
207
|
``` |
|
208
|
|
|
209
|
</CodeTab> |
|
210
|
</CodeTabs> |
|
211
|
|
|
212
|
Refer to the [Workflows](!docs!/learn/fundamentals/workflows) documentation to learn more. |
|
213
|
|
|
214
|
--- |
|
215
|
|
|
216
|
## Supported Module Translations |
|
217
|
|
|
218
|
The Translation Module currently supports translations for the following data models: |
|
219
|
|
|
220
|
<Table> |
|
221
|
<Table.Header> |
|
222
|
<Table.Row> |
|
223
|
<Table.HeaderCell> |
|
224
|
Data Model |
|
225
|
</Table.HeaderCell> |
|
226
|
<Table.HeaderCell> |
|
227
|
Translatable Fields |
|
228
|
</Table.HeaderCell> |
|
229
|
</Table.Row> |
|
230
|
</Table.Header> |
|
231
|
<Table.Body> |
|
232
|
<Table.Row> |
|
233
|
<Table.Cell> |
|
234
|
`CustomerGroup` ([Customer Module](../customer/page.mdx)) ([+v2.12.4](https://github.com/medusajs/medusa/releases/tag/v2.12.4)) |
|
235
|
</Table.Cell> |
|
236
|
<Table.Cell> |
|
237
|
`name` |
|
238
|
</Table.Cell> |
|
239
|
</Table.Row> |
|
240
|
<Table.Row> |
|
241
|
<Table.Cell> |
|
242
|
`Product` ([Product Module](../product/page.mdx)) |
|
243
|
</Table.Cell> |
|
244
|
<Table.Cell> |
|
245
|
- `title` |
|
246
|
- `description` |
|
247
|
- `material` |
|
248
|
- `subtitle` |
|
249
|
</Table.Cell> |
|
250
|
</Table.Row> |
|
251
|
<Table.Row> |
|
252
|
<Table.Cell> |
|
253
|
`ProductCategory` ([Product Module](../product/page.mdx)) |
|
254
|
</Table.Cell> |
|
255
|
<Table.Cell> |
|
256
|
- `name` |
|
257
|
- `description` |
|
258
|
</Table.Cell> |
|
259
|
</Table.Row> |
|
260
|
<Table.Row> |
|
261
|
<Table.Cell> |
|
262
|
`ProductCollection` ([Product Module](../product/page.mdx)) |
|
263
|
</Table.Cell> |
|
264
|
<Table.Cell> |
|
265
|
`title` |
|
266
|
</Table.Cell> |
|
267
|
</Table.Row> |
|
268
|
<Table.Row> |
|
269
|
<Table.Cell> |
|
270
|
`ProductOption` ([Product Module](../product/page.mdx)) |
|
271
|
</Table.Cell> |
|
272
|
<Table.Cell> |
|
273
|
`title` |
|
274
|
</Table.Cell> |
|
275
|
</Table.Row> |
|
276
|
<Table.Row> |
|
277
|
<Table.Cell> |
|
278
|
`ProductOptionValue` ([Product Module](../product/page.mdx)) |
|
279
|
</Table.Cell> |
|
280
|
<Table.Cell> |
|
281
|
`value` |
|
282
|
</Table.Cell> |
|
283
|
</Table.Row> |
|
284
|
<Table.Row> |
|
285
|
<Table.Cell> |
|
286
|
`ProductTag` ([Product Module](../product/page.mdx)) |
|
287
|
</Table.Cell> |
|
288
|
<Table.Cell> |
|
289
|
`value` |
|
290
|
</Table.Cell> |
|
291
|
</Table.Row> |
|
292
|
<Table.Row> |
|
293
|
<Table.Cell> |
|
294
|
`ProductType` ([Product Module](../product/page.mdx)) |
|
295
|
</Table.Cell> |
|
296
|
<Table.Cell> |
|
297
|
`value` |
|
298
|
</Table.Cell> |
|
299
|
</Table.Row> |
|
300
|
<Table.Row> |
|
301
|
<Table.Cell> |
|
302
|
`ProductVariant` ([Product Module](../product/page.mdx)) |
|
303
|
</Table.Cell> |
|
304
|
<Table.Cell> |
|
305
|
- `title` |
|
306
|
- `material` |
|
307
|
</Table.Cell> |
|
308
|
</Table.Row> |
|
309
|
<Table.Row> |
|
310
|
<Table.Cell> |
|
311
|
`Region` ([Region Module](../region/page.mdx)) ([+v2.12.4](https://github.com/medusajs/medusa/releases/tag/v2.12.4)) |
|
312
|
</Table.Cell> |
|
313
|
<Table.Cell> |
|
314
|
`name` |
|
315
|
</Table.Cell> |
|
316
|
</Table.Row> |
|
317
|
<Table.Row> |
|
318
|
<Table.Cell> |
|
319
|
`ShippingOption` ([Fulfillment Module](../fulfillment/page.mdx)) ([+v2.12.4](https://github.com/medusajs/medusa/releases/tag/v2.12.4)) |
|
320
|
</Table.Cell> |
|
321
|
<Table.Cell> |
|
322
|
`name` |
|
323
|
</Table.Cell> |
|
324
|
</Table.Row> |
|
325
|
<Table.Row> |
|
326
|
<Table.Cell> |
|
327
|
`ShippingOptionType` ([Fulfillment Module](../fulfillment/page.mdx)) ([+v2.12.4](https://github.com/medusajs/medusa/releases/tag/v2.12.4)) |
|
328
|
</Table.Cell> |
|
329
|
<Table.Cell> |
|
330
|
- `label` |
|
331
|
- `description` |
|
332
|
</Table.Cell> |
|
333
|
</Table.Row> |
|
334
|
<Table.Row> |
|
335
|
<Table.Cell> |
|
336
|
`TaxRate` ([Tax Module](../tax/page.mdx)) ([+v2.12.4](https://github.com/medusajs/medusa/releases/tag/v2.12.4)) |
|
337
|
</Table.Cell> |
|
338
|
<Table.Cell> |
|
339
|
`name` |
|
340
|
</Table.Cell> |
|
341
|
</Table.Row> |
|
342
|
</Table.Body> |
|
343
|
</Table> |
|
344
|
|
|
345
|
Future versions of the Translation Module will include support for all Commerce Modules. |
|
346
|
|
|
347
|
--- |
|
348
|
|
|
349
|
<CommerceModuleSections /> |