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

Support Intl.RelativeTimeFormat for relativeTime formatting #1056

Closed
kvnxiao opened this issue May 6, 2024 · 3 comments · Fixed by #1057
Closed

Support Intl.RelativeTimeFormat for relativeTime formatting #1056

kvnxiao opened this issue May 6, 2024 · 3 comments · Fixed by #1057
Labels
enhancement New feature or request unconfirmed Needs triage.

Comments

@kvnxiao
Copy link

kvnxiao commented May 6, 2024

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

@amannn
Copy link
Owner

amannn commented May 7, 2024

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: #765.

See also:

return new Intl.RelativeTimeFormat(locale, {
// `numeric: 'auto'` can theoretically produce output like "yesterday",
// but it only works with integers. E.g. -1 day will produce "yesterday",
// but -1.1 days will produce "-1.1 days". Rounding before formatting is
// not desired, as the given dates might cross a threshold were the
// output isn't correct anymore. Example: 2024-01-08T23:00:00.000Z and
// 2024-01-08T01:00:00.000Z would produce "yesterday", which is not the
// case. By using `always` we can ensure correct output. The only exception
// is the formatting of times <1 second as "now".
numeric: unit === 'second' ? 'auto' : 'always'
}).format(value, unit);

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

I've added #1057 to look into this.

@kvnxiao
Copy link
Author

kvnxiao commented May 7, 2024

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!

@amannn
Copy link
Owner

amannn commented May 8, 2024

Sounds good, thanks for the feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request unconfirmed Needs triage.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants