|
1
|
import { Prerequisites } from "docs-ui" |
|
2
|
|
|
3
|
export const metadata = { |
|
4
|
title: `Translation Concepts`, |
|
5
|
} |
|
6
|
|
|
7
|
# {metadata.title} |
|
8
|
|
|
9
|
In this guide, you'll learn about the key concepts related to the Translation Module, including locales and translations. |
|
10
|
|
|
11
|
<Prerequisites |
|
12
|
items={[ |
|
13
|
{ |
|
14
|
text: "Translation Module Configured", |
|
15
|
link: "/commerce-modules/translation#configure-translation-module", |
|
16
|
}, |
|
17
|
]} |
|
18
|
/> |
|
19
|
|
|
20
|
## Locales |
|
21
|
|
|
22
|
The [Locale data model](/references/translation/models/Locale) represents a language that resources can be translated into. |
|
23
|
|
|
24
|
Its `code` property 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). |
|
25
|
|
|
26
|
Locales are automatically populated in your Medusa application the first time the Translation Module is used. |
|
27
|
|
|
28
|
### Default Locale |
|
29
|
|
|
30
|
The Translation Module doesn't enforce a default locale. If a resource doesn't have a translation in the requested locale, it will fall back to the original value stored in the resource's data model. |
|
31
|
|
|
32
|
--- |
|
33
|
|
|
34
|
## Translations |
|
35
|
|
|
36
|
The [Translation data model](/references/translation/models/Translation) represents a translated value for a specific resource in a specific locale. |
|
37
|
|
|
38
|
The data model has the following properties to associate the translation with the resource: |
|
39
|
|
|
40
|
- `reference_id`: The ID of the resource being translated (for example, the ID of the product being translated). |
|
41
|
- `reference`: The name of the table where the resource is stored (for example, `product` when translating a product). |
|
42
|
|
|
43
|
The locale of the translation is indicated by the `locale_code` property, which follows the same [IETF BCP 47 standard](https://gist.github.com/typpo/b2b828a35e683b9bf8db91b5404f1bd1) as the [Locale](#locales) data model. |
|
44
|
|
|
45
|
A resource may have only one translation per locale. For example, a product can have only one translation in `fr-FR` and another translation in `es-ES`. |
|
46
|
|
|
47
|
The actual translated values are stored in the `translations` property, which is a JSON object where each key represents a field of the resource being translated, and the value is the translated text. |
|
48
|
|
|
49
|
For example, a translation for a product to French (`fr-FR`) may look like this: |
|
50
|
|
|
51
|
```json |
|
52
|
{ |
|
53
|
"reference_id": "prod_123", |
|
54
|
"reference": "product", |
|
55
|
"locale_code": "fr-FR", |
|
56
|
"translations": { |
|
57
|
"title": "Produit Exemple", |
|
58
|
"description": "Ceci est une description en français." |
|
59
|
} |
|
60
|
} |
|
61
|
``` |
|
62
|
|
|
63
|
Each key in the `translations` object corresponds to a property in the product resource, such as `title` and `description`, with their respective translated values. |
|
64
|
|
|
65
|
### Update Translations |
|
66
|
|
|
67
|
When you update a translation, for example, with the Translation Module's `updateTranslations` method or the [batchTranslationsWorkflow](/references/medusa-workflows/batchTranslationsWorkflow), Medusa merges the `translations` object you pass into the existing `translations` object. It doesn't replace the entire object. |
|
68
|
|
|
69
|
So, the update only changes the keys you pass. Keys that you omit keep their existing translated values. |
|
70
|
|
|
71
|
For example, if a translation has the `title` and `description` keys, the following update only changes the `title`, and keeps the `description` as is: |
|
72
|
|
|
73
|
```ts |
|
74
|
await translationModuleService.updateTranslations({ |
|
75
|
id: "trans_123", |
|
76
|
translations: { |
|
77
|
title: "Nouveau titre", |
|
78
|
}, |
|
79
|
}) |
|
80
|
``` |
|
81
|
|
|
82
|
To remove the translated value of a field, set its key to `null`: |
|
83
|
|
|
84
|
```ts |
|
85
|
await translationModuleService.updateTranslations({ |
|
86
|
id: "trans_123", |
|
87
|
translations: { |
|
88
|
description: null, |
|
89
|
}, |
|
90
|
}) |
|
91
|
``` |
|
92
|
|
|
93
|
Medusa ignores translated values that are `null` or empty, and falls back to the original value stored in the resource's data model. |
|
94
|
|
|
95
|
<Note type="warning"> |
|
96
|
|
|
97
|
If a workflow that updates translations fails, such as the `batchTranslationsWorkflow`, the rollback restores the previous values of existing keys. However, since the rollback also merges the previous `translations` object, it doesn't remove keys that the update added. |
|
98
|
|
|
99
|
</Note> |
|
100
|
|
|
101
|
--- |
|
102
|
|
|
103
|
## Retrieve Translations in Medusa |
|
104
|
|
|
105
|
<Note> |
|
106
|
|
|
107
|
To retrieve translations for the storefront, refer to the [Serve Translations in the Storefront](../storefront/page.mdx) guide. |
|
108
|
|
|
109
|
</Note> |
|
110
|
|
|
111
|
You can retrieve translations for a resource on the Medusa server using [Query](!docs!/learn/fundamentals/query). You can filter translations by `reference_id` and `locale_code` to get the specific translation you need. |
|
112
|
|
|
113
|
For example: |
|
114
|
|
|
115
|
```ts |
|
116
|
const { data: translations } = await query.graph({ |
|
117
|
entity: "translation", |
|
118
|
fields: ["reference_id", "locale_code", "translations"], |
|
119
|
filters: { |
|
120
|
reference_id: "prod_123", |
|
121
|
locale_code: "fr-FR", |
|
122
|
}, |
|
123
|
}) |
|
124
|
|
|
125
|
console.log(translations) |
|
126
|
// Output: |
|
127
|
// [{ |
|
128
|
// reference_id: "prod_123", |
|
129
|
// locale_code: "fr-FR", |
|
130
|
// translations: { |
|
131
|
// title: "Produit Exemple", |
|
132
|
// description: "Ceci est une description en français." |
|
133
|
// } |
|
134
|
// }] |
|
135
|
``` |
|
136
|
|
|
137
|
In this example, you retrieve the translation records of a product with the ID `prod_123` in French (`fr-FR`). |
|
138
|
|
|
139
|
If you've enabled the [Caching Module](../../../infrastructure-modules/caching/page.mdx) in your Medusa application, you can also cache translation queries to improve performance: |
|
140
|
|
|
141
|
```ts |
|
142
|
const { data: translations } = await query.graph({ |
|
143
|
entity: "translation", |
|
144
|
fields: ["reference_id", "locale_code", "translations"], |
|
145
|
filters: { |
|
146
|
reference_id: "prod_123", |
|
147
|
locale_code: "fr-FR", |
|
148
|
}, |
|
149
|
}, { |
|
150
|
cache: { |
|
151
|
enable: true, |
|
152
|
}, |
|
153
|
}) |
|
154
|
|
|
155
|
console.log(translations) |
|
156
|
// Output: |
|
157
|
// [{ |
|
158
|
// reference_id: "prod_123", |
|
159
|
// locale_code: "fr-FR", |
|
160
|
// translations: { |
|
161
|
// title: "Produit Exemple", |
|
162
|
// description: "Ceci est une description en français." |
|
163
|
// } |
|
164
|
// }] |
|
165
|
``` |