next-intl icon indicating copy to clipboard operation
next-intl copied to clipboard

Support Intl.RelativeTimeFormat for `relativeTime` formatting

Open kvnxiao opened this issue 1 year ago • 2 comments

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

Currently, the formatter for Formatting date and time ranges does not support additional configuration other than specifying the exact unit to display the output in.

The relative time formatting should support all the options from Intl.RelativeTimeFormat and not just RelativeTimeFormatUnit

Describe the solution you'd like

Instead of:

type RelativeTimeFormatOptions = {
    now?: number | Date;
    unit?: Intl.RelativeTimeFormatUnit;
};
export default RelativeTimeFormatOptions;

The RelativeTimeFormatOptions type should also accept the options from the Intl.RelativeTimeFormat constructor:

  • numberingSystem
  • style
  • numeric

Describe alternatives you've considered

The only alternative would be to use the native Intl API directly, but this should be integrated into the formatter in next-intl IMO

kvnxiao avatar May 06 '24 22:05 kvnxiao

Hey, thanks for the report! I agree, we could definitely support numberingSystem and style.

As for numeric I'm a bit hesitant though, as it can lead to issues when values are rounded: https://github.com/amannn/next-intl/pull/765.

See also:

https://github.com/amannn/next-intl/blob/3eefe6f5f12b49ca0237c96ac84a1abdce6dbab3/packages/use-intl/src/core/createFormatter.tsx#L247-L257

Do you need numeric for your use case? If so, can you include further details?

I've added #1057 to look into this.

amannn avatar May 07 '24 09:05 amannn

At the moment, I only have a need for the style to specify long, short, or narrow.

I think numberingSystem and style is sufficient for now!

kvnxiao avatar May 07 '24 23:05 kvnxiao

Sounds good, thanks for the feedback!

amannn avatar May 08 '24 05:05 amannn