Skip to content

Allow providing default formats for when no option is passed to formatter #1654

@killianhuyghe

Description

@killianhuyghe

Is your feature request related to a problem? Please describe.

I'd like to be able to control what is the default format when no option is passed to format.*.

In particular, the default number formatting options in my app are { maximumFractionDigits: 2 } and I find myself having to use a specific option string with every use of format.number (e.g. format.number(n, "default")).

I am dealing with a lot of financial data in my app and the original defaults for most locales seem to keep (maximum) three decimal digits, which doesn't make sense in my app.

Describe the solution you'd like

I wish I could pass my global defaults in my config:

import {getRequestConfig} from 'next-intl/server';
 
export default getRequestConfig(async () => {
  return {
    defaultFormats: {
      number: {
        maximumFractionDigits: 2
      }
    },
    formats: {
      precise: {
        maximumFractionDigits: 5
      }
    },
 
    // ...
  };
});

and have format.number(n) use these default formats.

Describe alternatives you've considered

The two main alternatives I have considered are:

  • use a custom global format and always call with an option string like format.number(n, "default") (note that this choice of option string both makes sense and is silly as one can argue default is when no option is provided)
  • use a wrapper like
const useNumberFormatter(() => {
  const format = useFormatter();
  return (n, options) => format.number(n, options ?? defaultOptions);
}

but then you have one of the formatter that doesn't use the format.* function, leading to potential inconsistencies (or you can wrap every formatter and just always import your wrappers).

I know the alternatives are not too bad, yet I find none of them very satisfying.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions