Skip to content

Commit a60969a

Browse files
committed
[Doc] Mention localization and translated values in i18n guide
1 parent 25574b9 commit a60969a

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

docs/Translation.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,3 +262,82 @@ When building an internationalized app with react-admin, the usual workflow is t
262262

263263
Check [the translation setup documentation](./TranslationSetup.md) to understand how to build your own translation file, the [list of available translations](./TranslationLocales.md) to find a translation for your language, and [Translating the UI](./TranslationTranslating.md) to understand how to translate react-admin commponents.
264264

265+
## Translating Values
266+
267+
Beyond the UX, you may need to translate values in your data. For instance, you may want to display a translated label for a status field, or a translated name for a category.
268+
269+
This implies that your data model stores the translations in a way that can be used by the UI. The advised solution is to store the translations as a JSON object in the data itself. For example, a Category resource could have a `name` field that contains the translations for each locale:
270+
271+
```json
272+
{
273+
"id": 1,
274+
"name": {
275+
"en": "Shoes",
276+
"fr": "Chaussures"
277+
}
278+
}
279+
```
280+
281+
If you follow this data structure, you can use special fields and inputs to display and edit the translated values.
282+
283+
- [`<TranslatableField>`](./TranslatableFields.md) lets you display all the translations for a field in a single component.
284+
285+
<video controls autoplay playsinline muted loop>
286+
<source src="./img/translatable-fields-basic.webm" type="video/webm" />
287+
<source src="./img/translatable-fields-basic.webm" type="video/mp4" />
288+
Your browser does not support the video tag.
289+
</video>
290+
291+
```jsx
292+
<TranslatableFields locales={['en', 'fr']}>
293+
<TextField source="title" />
294+
<TextField source="description" />
295+
</TranslatableFields>
296+
```
297+
298+
- [`<TranslatableInputs>`](./TranslatableInputs.md) lets you edit all the translations for a field in a single component.
299+
300+
<video controls autoplay playsinline muted loop>
301+
<source src="./img/translatable-input.webm" type="video/webm"/>
302+
<source src="./img/translatable-input.mp4" type="video/mp4"/>
303+
Your browser does not support the video tag.
304+
</video>
305+
306+
```jsx
307+
<TranslatableInputs locales={['en', 'fr']}>
308+
<TextInput source="name" />
309+
<RichTextInput source="description" />
310+
</TranslatableInputs>
311+
```
312+
313+
Check out the documentation for [Translatable Fields](./TranslatableFields.md) and [Translatable Inputs](./TranslatableInputs.md) for more details.
314+
315+
## Localization
316+
317+
For numeric and temporal values, react-admin benefits from the Single-Page Application architecture. As the application executes in the browser, it uses the browser's locale by default to format numbers and dates.
318+
319+
For instance, the `<DateField>` renders the date in the user's locale, using the `Intl.DateTimeFormat` API.
320+
321+
```tsx
322+
<DateField source="published_at" />
323+
// renders the record { id: 1234, published_at: new Date('2017-04-23') } as
324+
<span>4/23/2017</span>
325+
```
326+
327+
You can force a specific locale by passing the `locale` prop to the field:
328+
329+
```tsx
330+
<DateField source="published_at" locale="fr-FR" />
331+
// renders the record { id: 1234, published_at: new Date('2017-04-23') } as
332+
<span>23/04/2017</span>
333+
```
334+
335+
The following components take advantage of browser localization:
336+
337+
- [`<DateField>`](./DateField.md)
338+
- [`<DateInput>`](./DateInput.md)
339+
- [`<DateTimeInput>`](./DateTimeInput.md)
340+
- [`<DateRangeInput>`](./DateRangeInput.md)
341+
- [ `<NumberField>`](./NumberField.md)
342+
- [`<NumberInput>`](./NumberInput.md)
343+
- [`<TimeInput>`](./TimeInput.md)

0 commit comments

Comments
 (0)