Skip to content
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

[i18n] Format numbers according to locale #24495

Open
benknight opened this issue Jan 19, 2021 · 3 comments
Open

[i18n] Format numbers according to locale #24495

benknight opened this issue Jan 19, 2021 · 3 comments
Labels
component: table This is the name of the generic UI component, not the React module! component: TablePagination The React component. i18n internationalization new feature New feature or request package: base-ui Specific to @mui/base

Comments

@benknight
Copy link

benknight commented Jan 19, 2021

Very large numbers are left unformatted in TablePagination, for example:

image

<TablePagination
  rowsPerPageOptions={[25, 50, 100]}
  component="div"
  count={103177}
  rowsPerPage={25}
  page={0}
/>

Probably it should get formatted via Number.prototype.toLocaleString()

Benchmarks

@benknight benknight added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jan 19, 2021
@oliviertassinari oliviertassinari added component: table This is the name of the generic UI component, not the React module! new feature New feature or request and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Jan 19, 2021
@oliviertassinari oliviertassinari changed the title TablePagination should format large numbers according to locale [Table] TablePagination should format large numbers according to locale Jan 19, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 19, 2021

@benknight Thanks for opening this issue. You can find the same issue in Ant Design under ant-design/ant-design#19337. So far, we offer the same solution, meaning developers can customize the labelDisplayedRows prop to format the number based on their locale. It's the current workaround. It's not ideal, I have tried to work on it in the past under #20656 but it didn't fly.


In the data grid, we have partially solved the problem with the solution you are proposing:

  valueFormatter: ({ value }) => (value && isNumber(value) && value.toLocaleString()) || value,

the source of the data grid

However, this solution is not compatible with server-side rendering nor used everywhere:

SCR-20240525-rdgj

https://mui.com/x/react-data-grid/pagination/


At this point, I think that we could explore these solutions:

  1. Ignore the SSR mismatch that can come from Intl.NumberFormatter/.toLocalString(locale), with the use of the suppressHydrationWarning prop https://react.dev/reference/react-dom/components/common#common-props.
  2. Move the LocalizationProvider from the pickers to Base UI
  3. Create a useLocalization() internal hook to use it in the components
  4. Have this hook expose a formatNumber() helper.
  5. Have formatNumber() be an abstraction on top of Intl.NumberFormatter API. Similar API to toLocaleString.
  6. Encourage https://formatjs.io/docs/polyfills for developers that need SSR with a different locale than the default one (en-US). The data set used by the browser and server for Intl.NumberFormatter might not be exactly the same, leading to hydration mismatch.
  7. Cache the different instances of Intl.NumberFormatter as it's slow to initiate, probably reading in the fs to load the right data in RAM. It would be a performance killer for rendering 100 numbers in data table cells.
  8. Have useLocalization().formatNumber has a default implementation, no making LocalizationProvider required as we do when it needs date-fns or moment installed.

@benknight
Copy link
Author

Of course, I neglected to consider that localization is a much larger, nuanced issue when considering SSR. Thanks for enlightening me towards the complexities involved.

@oliviertassinari oliviertassinari added the component: TablePagination The React component. label Jan 14, 2024
@oliviertassinari oliviertassinari changed the title [Table] TablePagination should format large numbers according to locale [table] TablePagination should format large numbers according to locale Jan 14, 2024
@oliviertassinari oliviertassinari added the i18n internationalization label Jan 14, 2024
@oliviertassinari oliviertassinari changed the title [table] TablePagination should format large numbers according to locale [i18n] format large numbers according to locale Jan 14, 2024
@oliviertassinari oliviertassinari changed the title [i18n] format large numbers according to locale [i18n] Format numbers according to locale Jan 14, 2024
@oliviertassinari oliviertassinari added the package: base-ui Specific to @mui/base label Jan 14, 2024
@oliviertassinari
Copy link
Member

oliviertassinari commented Jan 14, 2024

#40589 covers a sub-problem on this, on how to format numbers for the table pagination page size picker in Persian/Arabic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: table This is the name of the generic UI component, not the React module! component: TablePagination The React component. i18n internationalization new feature New feature or request package: base-ui Specific to @mui/base
Projects
None yet
Development

No branches or pull requests

2 participants