Skip to content

Commit a834fa7

Browse files
authored
Merge pull request #8924 from marmelab/fix-emptytext-translation
Fix `<TextField>` and `<RichTextField>` don't translate the `emptyText`
2 parents e7bde0a + 8e625b2 commit a834fa7

File tree

4 files changed

+76
-7
lines changed

4 files changed

+76
-7
lines changed

packages/ra-ui-materialui/src/field/RichTextField.spec.tsx

+34-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from 'react';
22
import expect from 'expect';
33
import { render, screen, fireEvent } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { RichTextField, removeTags } from './RichTextField';
78
import { Secure } from './RichTextField.stories';
@@ -148,4 +149,36 @@ describe('<RichTextField />', () => {
148149
(container.querySelector('#stolendata') as HTMLInputElement)?.value
149150
).toEqual('none');
150151
});
152+
153+
it('should translate emptyText', () => {
154+
const i18nProvider = polyglotI18nProvider(
155+
_locale =>
156+
({
157+
resources: {
158+
books: {
159+
name: 'Books',
160+
fields: {
161+
id: 'Id',
162+
title: 'Title',
163+
author: 'Author',
164+
year: 'Year',
165+
},
166+
not_found: 'Not found',
167+
},
168+
},
169+
} as any),
170+
'en'
171+
);
172+
render(
173+
<I18nContextProvider value={i18nProvider}>
174+
<RichTextField
175+
record={{ id: 123 }}
176+
source="foo.bar"
177+
emptyText="resources.books.not_found"
178+
/>
179+
</I18nContextProvider>
180+
);
181+
182+
expect(screen.getByText('Not found')).not.toBeNull();
183+
});
151184
});

packages/ra-ui-materialui/src/field/RichTextField.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { FC, memo } from 'react';
33
import PropTypes from 'prop-types';
44
import get from 'lodash/get';
55
import Typography, { TypographyProps } from '@mui/material/Typography';
6-
import { useRecordContext } from 'ra-core';
6+
import { useRecordContext, useTranslate } from 'ra-core';
77
import purify from 'dompurify';
88

99
import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
@@ -35,6 +35,7 @@ export const RichTextField: FC<RichTextFieldProps> = memo<RichTextFieldProps>(
3535
} = props;
3636
const record = useRecordContext(props);
3737
const value = get(record, source);
38+
const translate = useTranslate();
3839

3940
return (
4041
<Typography
@@ -44,7 +45,7 @@ export const RichTextField: FC<RichTextFieldProps> = memo<RichTextFieldProps>(
4445
{...sanitizeFieldRestProps(rest)}
4546
>
4647
{value == null && emptyText ? (
47-
emptyText
48+
translate(emptyText, { _: emptyText })
4849
) : stripTags ? (
4950
removeTags(value)
5051
) : (

packages/ra-ui-materialui/src/field/TextField.spec.tsx

+35-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as React from 'react';
22
import expect from 'expect';
3-
import { render, getNodeText } from '@testing-library/react';
4-
import { RecordContextProvider } from 'ra-core';
3+
import { screen, render, getNodeText } from '@testing-library/react';
4+
import { RecordContextProvider, I18nContextProvider } from 'ra-core';
5+
import polyglotI18nProvider from 'ra-i18n-polyglot';
56

67
import { TextField } from './TextField';
78

@@ -79,4 +80,36 @@ describe('<TextField />', () => {
7980
);
8081
expect(queryByText('NA')).not.toBeNull();
8182
});
83+
84+
it('should translate emptyText', () => {
85+
const i18nProvider = polyglotI18nProvider(
86+
_locale =>
87+
({
88+
resources: {
89+
books: {
90+
name: 'Books',
91+
fields: {
92+
id: 'Id',
93+
title: 'Title',
94+
author: 'Author',
95+
year: 'Year',
96+
},
97+
not_found: 'Not found',
98+
},
99+
},
100+
} as any),
101+
'en'
102+
);
103+
render(
104+
<I18nContextProvider value={i18nProvider}>
105+
<TextField
106+
record={{ id: 123 }}
107+
source="foo.bar"
108+
emptyText="resources.books.not_found"
109+
/>
110+
</I18nContextProvider>
111+
);
112+
113+
expect(screen.getByText('Not found')).not.toBeNull();
114+
});
82115
});

packages/ra-ui-materialui/src/field/TextField.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22
import { memo, FC, ElementType } from 'react';
33
import get from 'lodash/get';
44
import Typography, { TypographyProps } from '@mui/material/Typography';
5-
import { useRecordContext } from 'ra-core';
5+
import { useRecordContext, useTranslate } from 'ra-core';
66

77
import { sanitizeFieldRestProps } from './sanitizeFieldRestProps';
88
import { PublicFieldProps, InjectedFieldProps, fieldPropTypes } from './types';
@@ -11,6 +11,7 @@ export const TextField: FC<TextFieldProps> = memo(props => {
1111
const { className, source, emptyText, ...rest } = props;
1212
const record = useRecordContext(props);
1313
const value = get(record, source);
14+
const translate = useTranslate();
1415

1516
return (
1617
<Typography
@@ -21,7 +22,8 @@ export const TextField: FC<TextFieldProps> = memo(props => {
2122
>
2223
{value != null && typeof value !== 'string'
2324
? JSON.stringify(value)
24-
: value || emptyText}
25+
: value ||
26+
(emptyText ? translate(emptyText, { _: emptyText }) : null)}
2527
</Typography>
2628
);
2729
});

0 commit comments

Comments
 (0)