From 5775e2a1aa8c4826d1b7578d300f5e321ceb7ceb Mon Sep 17 00:00:00 2001 From: Frederic Bahr Date: Thu, 9 Jan 2025 20:05:02 +0100 Subject: [PATCH] feat: add language localization support for converting numbers to local string This explicitly tells the `toLocaleString` function which language to use based on the provided localization instead of relying on the browser language. This allows a correct formatting of numbers within a multi language app. --- .../pages/docs/guides/localization.mdx | 4 +++- .../toolbar/MRT_TablePagination.tsx | 8 +++++--- .../toolbar/MRT_ToolbarAlertBanner.tsx | 4 ++-- .../material-react-table/src/locales/ar.ts | 1 + .../material-react-table/src/locales/az.ts | 1 + .../material-react-table/src/locales/bg.ts | 1 + .../material-react-table/src/locales/cs.ts | 1 + .../material-react-table/src/locales/da.ts | 1 + .../material-react-table/src/locales/de.ts | 1 + .../material-react-table/src/locales/el.ts | 1 + .../material-react-table/src/locales/en.ts | 1 + .../material-react-table/src/locales/es.ts | 1 + .../material-react-table/src/locales/et.ts | 1 + .../material-react-table/src/locales/fa.ts | 1 + .../material-react-table/src/locales/fi.ts | 1 + .../material-react-table/src/locales/fr.ts | 1 + .../material-react-table/src/locales/he.ts | 1 + .../material-react-table/src/locales/hr.ts | 1 + .../material-react-table/src/locales/hu.ts | 1 + .../material-react-table/src/locales/hy.ts | 1 + .../material-react-table/src/locales/id.ts | 1 + .../material-react-table/src/locales/it.ts | 1 + .../material-react-table/src/locales/ja.ts | 1 + .../material-react-table/src/locales/ko.ts | 1 + .../material-react-table/src/locales/nl.ts | 1 + .../material-react-table/src/locales/no.ts | 1 + .../material-react-table/src/locales/np.ts | 1 + .../material-react-table/src/locales/pl.ts | 1 + .../material-react-table/src/locales/pt-BR.ts | 1 + .../material-react-table/src/locales/pt.ts | 1 + .../material-react-table/src/locales/ro.ts | 1 + .../material-react-table/src/locales/ru.ts | 1 + .../material-react-table/src/locales/sk.ts | 1 + .../src/locales/sr-Cyrl-RS.ts | 1 + .../src/locales/sr-Latn-RS.ts | 1 + .../material-react-table/src/locales/sv.ts | 1 + .../material-react-table/src/locales/tr.ts | 1 + .../material-react-table/src/locales/uk.ts | 1 + .../material-react-table/src/locales/vi.ts | 1 + .../src/locales/zh-Hans.ts | 1 + .../src/locales/zh-Hant.ts | 1 + packages/material-react-table/src/types.ts | 2 ++ .../stories/features/Pagination.stories.tsx | 19 +++++++++++++++++++ 43 files changed, 69 insertions(+), 6 deletions(-) diff --git a/apps/material-react-table-docs/pages/docs/guides/localization.mdx b/apps/material-react-table-docs/pages/docs/guides/localization.mdx index 9b9987094..a1be99f26 100644 --- a/apps/material-react-table-docs/pages/docs/guides/localization.mdx +++ b/apps/material-react-table-docs/pages/docs/guides/localization.mdx @@ -44,6 +44,7 @@ const table = useMaterialReactTable({ columns, data, localization: { + language: 'pt', // BCP 47 language tag for number formatting actions: 'Ações', and: 'e', cancel: 'Cancelar', @@ -60,6 +61,7 @@ const table = useMaterialReactTable({ return ; ``` -For a full list of all available translation keys, see [here](https://github.com/KevinVandy/material-react-table/blob/v3/packages/material-react-table/src/locales/en.ts) +For a full list of all available translation keys, see [here](https://github.com/KevinVandy/material-react-table/blob/v3/packages/material-react-table/src/locales/en.ts). +Please note that each localization object should include a `language` property containing a valid BCP 47 language tag to ensure proper number formatting. If you end up fully translating MRT into another language that is not yet supported, please consider making a PR to add it to the library so that everyone can use it! diff --git a/packages/material-react-table/src/components/toolbar/MRT_TablePagination.tsx b/packages/material-react-table/src/components/toolbar/MRT_TablePagination.tsx index 1bc28e1a8..70018613c 100644 --- a/packages/material-react-table/src/components/toolbar/MRT_TablePagination.tsx +++ b/packages/material-react-table/src/components/toolbar/MRT_TablePagination.tsx @@ -174,10 +174,12 @@ export const MRT_TablePagination = ({ sx={{ m: '0 4px', minWidth: '8ch' }} variant="body2" >{`${ - lastRowIndex === 0 ? 0 : (firstRowIndex + 1).toLocaleString() - }-${lastRowIndex.toLocaleString()} ${ + lastRowIndex === 0 + ? 0 + : (firstRowIndex + 1).toLocaleString(localization.language) + }-${lastRowIndex.toLocaleString(localization.language)} ${ localization.of - } ${totalRowCount.toLocaleString()}`} + } ${totalRowCount.toLocaleString(localization.language)}`} {showFirstButton && ( diff --git a/packages/material-react-table/src/components/toolbar/MRT_ToolbarAlertBanner.tsx b/packages/material-react-table/src/components/toolbar/MRT_ToolbarAlertBanner.tsx index 47f918bdf..778f9f6d6 100644 --- a/packages/material-react-table/src/components/toolbar/MRT_ToolbarAlertBanner.tsx +++ b/packages/material-react-table/src/components/toolbar/MRT_ToolbarAlertBanner.tsx @@ -65,8 +65,8 @@ export const MRT_ToolbarAlertBanner = ({ selectedRowCount > 0 ? ( {localization.selectedCountOfRowCountRowsSelected - ?.replace('{selectedCount}', selectedRowCount.toLocaleString()) - ?.replace('{rowCount}', totalRowCount.toString())} + ?.replace('{selectedCount}', selectedRowCount.toLocaleString(localization.language)) + ?.replace('{rowCount}', totalRowCount.toLocaleString(localization.language))}