-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[data grid] how to update column header titles when switching language without reseting data-grid state #13055
Comments
well I found something instead of doing this const { t } = useTranslation();
const columns = useMemo(() => [
{
field: "first_name",
headerName: t("first_name")
},
{
field: "last_name",
headerName: t("last_name")
},
], [t]); I can do this const columns = useMemo(() => [
{
field: "first_name",
renderHeader: () => <Trans i18nKey="first_name"/>
},
{
field: "last_name",
renderHeader: () => <Trans i18nKey="last_name"/>
},
], []); |
Title of a custom filter operator also doesn't update when switching languages. const { t } = useTranslation();
const columns = useMemo(() => [
{
field: "first_name",
renderHeader: () => <Trans i18nKey="first_name"/>,
filterable: true,
filterOperators: [
{
value: "contains",
getApplyFilterFn: () => null,
// @ts-ignore
label: <Trans i18nKey="header-operators.mode.contains"/>, // <-- it will update when switching languages
// @ts-ignore
headerLabel: <Trans i18nKey="header-operators.mode.contains"/>, // <-- it will update when switching languages
},
{
value: "exact",
getApplyFilterFn: () => null,
label: t("header-operators.mode.exact"), // <-- it won't update when switching languages
headerLabel: t("header-operators.mode.exact"), // <-- it won't update when switching languages
},
// ... other filter operators
]
},
// ... other column definitions
], []); I had to use |
Hey @layerok ... |
@michelengelen I am not satisfied with my solution renderHeader: () => <HeaderCell><Trans i18nKey="some-key"/></HeaderCell> I think this problem requires solution at the library level const columns = useMemo([
{
field: 'title',
headerName: () => t('title'),
filterOperators: [
{
label: () => t('contains'),
headerLabel: () => t('contains')
}
]
}
], []);
const apiRef = useGridApiRef();
useLayoutEffect(() => {
const forceUpdate = () => {
apiRef.current.forceUpdate();
apiRef.current.updateColumns(columns)
};
i18n.on("languageChanged", forceUpdate);
return () => i18n.off("languageChanged", forceUpdate);
}, [apiRef, columns]);
return <DataGrid columns={columns} /> but right now this is impossible because I guess this issue is becoming more of a feature request than a question |
Workaround 2const columnsRef = useRef<ExtendedGridColDef[]>([
{
field: 'title',
lazyHeaderName: () => t('title'),
headerName: "",
}
], []);
const apiRef = useGridApiRef();
useLayoutEffect(() => {
const forceUpdate = () => {
apiRef.current.forceUpdate();
apiRef.current.updateColumns(columnsRef.current.map((column) => ({
...column,
headerName: column.lazyHeaderName()
}));
};
i18n.on("languageChanged", forceUpdate);
return () => i18n.off("languageChanged", forceUpdate);
}, [apiRef, columnsRef.current]);
return <DataGrid columns={columnsRef.current}/> |
I am not sure where the problem is: const translations = {
first_name: {
en: 'First Name',
de: 'Vorname',
},
last_name: {
en: 'Last Name',
de: 'Nachname',
},
};
export default function DataGridDemo() {
const [lang, setLang] = React.useState<'en' | 'de'>('en');
// dummy function to just return some values from an object
const t = (key: keyof typeof translations) => translations[key][lang];
const HeaderCell = ({ labelKey }: { labelKey: keyof typeof translations }) => (
<div>{t(labelKey)}</div>
);
const columns = React.useMemo(
() => [
{ field: 'id', headerName: 'ID', width: 90 },
{
field: 'firstName',
width: 150,
editable: true,
renderHeader: () => <HeaderCell labelKey="first_name" />,
},
{
field: 'lastName',
width: 150,
editable: true,
renderHeader: () => <HeaderCell labelKey="last_name" />,
},
],
[t],
);
... Wouldn't this be sufficient? This does work like as well when using: const columns = React.useMemo(
() => [
{ field: 'id', headerName: 'ID', width: 90 },
{
field: 'firstName',
headerName: t('first_name'),
width: 150,
editable: true,
},
{
field: 'lastName',
headerName: t('last_name'),
width: 150,
editable: true,
},
],
[t],
); Is i18n not updating the |
I would need setup a dev environment with next for this. This might take until tomorrow though. Would that be ok? |
Yes, and you can use my sandbox. next is already there |
The problem is not with i18next but with the const columns = React.useMemo(() => [{ field: 'id', headerName: t('id'), width: 90 }], [t]); If you don't put the |
OK, thanks ... got it. |
You could probably use apiRef.updateColumns and pass the new headers, but it's not the prettier solution imaginable. |
The problem in depth
I know that
columns
prop should keep the same reference between two rerenders to avoid loosing data grid state.But If I don't update
columns
prop when switching language then column headers aren't translated. If I updatecolumns
prop then data grid state is reset. What should I do in this situation?Link to sandbox
Steps:
datagrid-state-reset-when-switching-language.mov
Your environment
No response
Search keywords: i18next translation column language data grid
Order ID: 74777
The text was updated successfully, but these errors were encountered: