Skip to content

Commit 22c4b2e

Browse files
committed
update i18n
1 parent 45a51fd commit 22c4b2e

File tree

7 files changed

+33
-93
lines changed

7 files changed

+33
-93
lines changed

docs_headless/src/content/docs/i18n/Translate.md

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
---
2-
layout: default
3-
title: "The Translate Component"
2+
title: "Translate"
43
---
54

6-
# `<Translate>`
7-
85
The `<Translate>` component renders a translated message based on a translation key.
96

107
## Usage
118

129
The component will look up the translation for the `i18nKey` in the `i18nProvider` and render it. If not found, it will render the `children` prop.
1310

1411
```tsx
15-
import { Translate, useRecord, useUpdate } from 'react-admin';
12+
import { Translate, useRecord, useUpdate } from 'ra-core';
1613

1714
const MarkAsUnreadButton = () => {
1815
const record = useRecord();
@@ -74,8 +71,6 @@ const messages = {
7471

7572
Use the `options` props to pass additional options to the `translate` function, e.g. for [pluralization or interpolation](./TranslationTranslating.md#interpolation-pluralization-and-default-translation).
7673

77-
{% raw %}
78-
7974
```tsx
8075
const messages = {
8176
custom: {
@@ -87,12 +82,8 @@ const messages = {
8782
// Hello, John!
8883
```
8984

90-
{% endraw %}
91-
9285
One particular option is `smart_count`, which is used for pluralization.
9386

94-
{% raw %}
95-
9687
```tsx
9788
const messages = {
9889
ra: {
@@ -105,5 +96,3 @@ const messages = {
10596
<Translate i18nKey="ra.notification.deleted" options={{ smart_count: 2 }} />
10697
// 2 items deleted
10798
```
108-
109-
{% endraw %}

docs_headless/src/content/docs/i18n/TranslationLocales.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ layout: default
33
title: "Supported Locales"
44
---
55

6-
## Supported Locales
7-
86
You can find translation packages for the following languages:
97

108
- 🇦🇪 Arabic (`ar`): [developerium/ra-language-arabic](https://github.com/developerium/ra-language-arabic)

docs_headless/src/content/docs/i18n/TranslationSetup.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
---
2-
layout: default
3-
title: "Setup"
2+
title: "Setting Up Translations"
43
---
54

6-
# Setting Up Translations
7-
85
If you want to add or update translations, you'll have to provide your own `i18nProvider`.
96

10-
Just like for the `dataProvider` and the `authProvider`, you can inject the `i18nProvider` to your react-admin app using the `<Admin i18nProvider>` prop:
7+
Just like for the `dataProvider` and the `authProvider`, you can inject the `i18nProvider` to your react-admin app using the `<CoreAdminContext i18nProvider>` prop:
118

129
```jsx
10+
import { CoreAdminContext } from 'ra-core';
1311
import { i18nProvider } from './i18nProvider';
1412

1513
const App = () => (
16-
<Admin
14+
<CoreAdminContext
1715
dataProvider={dataProvider}
1816
i18nProvider={i18nProvider}
1917
>
2018
{/* ... */}
21-
</Admin>
19+
</CoreAdminContext>
2220
);
2321
```
2422

@@ -64,33 +62,33 @@ export const i18nProvider = polyglotI18nProvider(
6462
);
6563
```
6664

67-
The second argument to the `polyglotI18nProvider` function is the default locale. The third is the list of supported locales - and is used by the [`<LocaleMenuButton>`](./LocalesMenuButton.md) component to display a list of languages.
65+
The second argument to the `polyglotI18nProvider` function is the default locale. The third is the list of supported locales - and is used by the [`<LocaleMenuButton>`](https://marmelab.com/react-admin/LocalesMenuButton.html) component to display a list of languages.
6866

69-
Next, pass the custom `i18nProvider` to your `<Admin>`:
67+
Next, pass the custom `i18nProvider` to your `<CoreAdminContext>`:
7068

7169
```jsx
72-
import { Admin } from 'react-admin';
70+
import { CoreAdminContext } from 'ra-core';
7371
import { i18nProvider } from './i18nProvider';
7472

7573
const App = () => (
76-
<Admin
74+
<CoreAdminContext
7775
i18nProvider={i18nProvider}
7876
dataProvider={dataProvider}
7977
>
8078
...
81-
</Admin>
79+
</CoreAdminContext>
8280
);
8381
```
8482

85-
That's all it takes to have a multilingual UI. As an added benefit, once a user has chosen a locale different from the default one, the react-admin app will always render using that locale (thanks to [the Store](./Store.md)).
83+
That's all it takes to have a multilingual UI. As an added benefit, once a user has chosen a locale different from the default one, the react-admin app will always render using that locale (thanks to [the Store](../guides/Store.md)).
8684

8785
## Using The Browser Locale
8886

8987
React-admin provides a helper function named `resolveBrowserLocale()`, which detects the user's browser locale. To use it, simply pass the function as the `initialLocale` argument of `polyglotI18nProvider`.
9088

9189
```jsx
9290
// in src/i18nProvider.js
93-
import { resolveBrowserLocale } from 'react-admin';
91+
import { resolveBrowserLocale } from 'ra-core';
9492
import polyglotI18nProvider from 'ra-i18n-polyglot';
9593
import en from 'ra-language-english';
9694
import fr from 'ra-language-french';

docs_headless/src/content/docs/i18n/TranslationTranslating.md

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
---
2-
layout: default
3-
title: "Translating"
2+
title: "Translating UI Components"
43
---
54

6-
# Translating UI Components
7-
85
The messages returned by the `polyglotI18nProvider` function argument should be a dictionary where the keys identify interface components, and values are the translated string. This dictionary is a simple JavaScript object looking like the following:
96

107
```js
@@ -29,7 +26,7 @@ All react-admin core components use keys starting with the `ra` prefix, to preve
2926
The default (English) messages are available in [the `ra-language-english` package source](https://github.com/marmelab/react-admin/blob/master/packages/ra-language-english/src/index.ts).
3027

3128

32-
**Tip**: You can see the raw translation keys in the UI by passing a dummy `i18nProvider` to the `<Admin>` component:
29+
**Tip**: You can see the raw translation keys in the UI by passing a dummy `i18nProvider` to the `<CoreAdminContext>` component:
3330

3431
```jsx
3532
const i18nProvider = {
@@ -39,12 +36,12 @@ const i18nProvider = {
3936
}
4037

4138
const App = () => (
42-
<Admin
39+
<CoreAdminContext
4340
dataProvider={dataProvider}
4441
i18nProvider={i18nProvider}
4542
>
4643
{/* ... */}
47-
</Admin>
44+
</CoreAdminContext>
4845
);
4946
```
5047

@@ -102,7 +99,7 @@ Imagine a translation key for the text to translate, e.g. 'myroot.hello.world' f
10299
```jsx
103100
// in src/MyHelloButton.js
104101
import * as React from "react";
105-
import { useTranslate } from 'react-admin';
102+
import { useTranslate } from 'ra-core';
106103

107104
export const MyHelloButton = () => {
108105
const translate = useTranslate();
@@ -147,7 +144,7 @@ export const en = {
147144

148145
## Translating Form Validation Errors
149146

150-
In Create and Edit views, forms can use [custom validators](./Validation.md#per-input-validation-custom-function-validator). These validator functions should return translation keys rather than translated messages. React-admin automatically passes these identifiers to the translation function.
147+
In Create and Edit views, forms can use [custom validators](../create-edit/Validation.md#per-input-validation-custom-function-validator). These validator functions should return translation keys rather than translated messages. React-admin automatically passes these identifiers to the translation function.
151148

152149
For instance, here is a validator function that only allows numbers greater than 10:
153150

@@ -160,12 +157,12 @@ const greaterThanTen = (value, allValues, props) =>
160157

161158
// in PersonEdit.js
162159
const PersonEdit = () => (
163-
<Edit>
164-
<SimpleForm>
160+
<EditBase>
161+
<Form>
165162
<TextInput source="name" />
166163
<TextInput source="age" validate={greaterThanTen} />
167-
</SimpleForm>
168-
</Edit>
164+
</Form>
165+
</EditBase>
169166
);
170167

171168
// in i18n/en.json
@@ -199,7 +196,7 @@ export default {
199196

200197
## Translating Notification Messages
201198

202-
If you use [the `useNotify` hook](./useNotify.md) to display a notification to the user, you can use a translation key for the notification text. React-admin will translate it automatically - no need to call `translate`.
199+
If you use [the `useNotify` hook](../common/useNotify.md) to display a notification to the user, you can use a translation key for the notification text. React-admin will translate it automatically - no need to call `translate`.
203200

204201
```jsx
205202
const ValidateCommentButton = ({ id }) => {
@@ -218,7 +215,7 @@ const ValidateCommentButton = ({ id }) => {
218215

219216
## Interpolation, Pluralization and Default Translation
220217

221-
If you're using [`ra-i18n-polyglot`](./Translation.md#ra-i18n-polyglot) (the default `i18nProvider`), you can leverage the advanced features of its `translate` function. [Polyglot.js](https://airbnb.io/polyglot.js/), the library behind `ra-i18n-polyglot`, provides some nice features such as interpolation and pluralization, that you can use in react-admin.
218+
If you're using [`ra-i18n-polyglot`](../guides/Translation.md#ra-i18n-polyglot) (the default `i18nProvider`), you can leverage the advanced features of its `translate` function. [Polyglot.js](https://airbnb.io/polyglot.js/), the library behind `ra-i18n-polyglot`, provides some nice features such as interpolation and pluralization, that you can use in react-admin.
222219

223220
```js
224221
const messages = {
@@ -244,41 +241,7 @@ translate('not_yet_translated', { _: 'Default translation' });
244241

245242
Check out the [Polyglot.js documentation](https://airbnb.io/polyglot.js/) for more information.
246243

247-
## Translating Record Content
248-
249-
Some of your records may contain data with multiple versions - one for each locale.
250-
251-
For instance, a product may have one reference, but several names. A `product` record would look like this:
252-
253-
```jsx
254-
{
255-
id: 123,
256-
reference: 'GURSIKSO',
257-
name: {
258-
en: 'Evening dress',
259-
fr: 'Robe du soir',
260-
}
261-
}
262-
```
263-
264-
React-admin provides a specialized component to display such translatable data ([`<TranslatableFields>`](./TranslatableFields.md)), and another specialized component to edit it ([`<TranslatableInputs>`](./TranslatableInputs.md)):
265-
266-
```jsx
267-
import { Edit, SimpleForm, TextInput, TranslatableInputs } from 'react-admin';
268-
269-
export const ProductEdit = () => (
270-
<Edit>
271-
<SimpleForm>
272-
<TextInput source="reference" />
273-
<TranslatableInputs locales={['en', 'fr']}>
274-
<TextInput source="name" />
275-
</TranslatableInputs>
276-
</SimpleForm>
277-
</Edit>
278-
);
279-
```
280244

281-
Check the documentation for each of these components for details.
282245

283246
## Forcing The Case in Confirm messages and Empty Page
284247

docs_headless/src/content/docs/i18n/TranslationWriting.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ layout: default
33
title: "Writing An I18nProvider"
44
---
55

6-
# Writing An I18nProvider
7-
86
An `i18nProvider` should be an object with three required methods and one optional method:
97

108
```ts
Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
---
2-
layout: default
32
title: "useLocaleState"
43
---
54

6-
# `useLocaleState`
7-
85
The `useLocaleState` hook allows to read and update the locale. It uses a syntax similar to react's `useState` hook.
96

107
## Syntax
@@ -19,31 +16,30 @@ const [locale, setLocale] = useLocaleState();
1916

2017
```jsx
2118
import * as React from "react";
22-
import Button from '@mui/material/Button';
23-
import { useLocaleState } from 'react-admin';
19+
import { useLocaleState } from 'ra-core';
2420

2521
const LocaleSwitcher = () => {
2622
const [locale, setLocale] = useLocaleState();
2723
return (
2824
<div>
2925
<div>Language</div>
30-
<Button
26+
<button
3127
disabled={locale === 'fr'}
3228
onClick={() => setLocale('fr')}
3329
>
3430
English
35-
</Button>
36-
<Button
31+
</button>
32+
<button
3733
disabled={locale === 'en'}
3834
onClick={() => setLocale('en')}
3935
>
4036
French
41-
</Button>
37+
</button>
4238
</div>
4339
);
4440
};
4541

4642
export default LocaleSwitcher;
4743
```
4844

49-
As this is a very common need, react-admin provides the [`<LocalesMenuButton>`](./LocalesMenuButton.md) component.
45+

docs_headless/src/content/docs/i18n/useTranslate.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ layout: default
33
title: "useTranslate"
44
---
55

6-
# `useTranslate`
7-
86
If you need to translate messages in your own components, React-admin provides a `useTranslate` hook, which returns the `translate` function.
97

108
## Syntax
@@ -21,7 +19,7 @@ const translatedMessage = translate(translationKey, options);
2119
```jsx
2220
// in src/MyHelloButton.js
2321
import * as React from "react";
24-
import { useTranslate } from 'react-admin';
22+
import { useTranslate } from 'ra-core';
2523

2624
const MyHelloButton = () => {
2725
const translate = useTranslate();

0 commit comments

Comments
 (0)