Skip to content

feat: Add displayName to useFormatter#2285

Merged
amannn merged 7 commits into
amannn:mainfrom
roderickhsiao:feat/display-names-formatter
Apr 28, 2026
Merged

feat: Add displayName to useFormatter#2285
amannn merged 7 commits into
amannn:mainfrom
roderickhsiao:feat/display-names-formatter

Conversation

@roderickhsiao

@roderickhsiao roderickhsiao commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Description

Exposes Intl.DisplayNames through the useFormatter hook as a new singular displayName formatter, consistent with existing formatter APIs while matching the underlying Intl.DisplayNames naming.

Why

Intl.DisplayNames was already wired in the internal formatter cache (createIntlFormatters) but was never surfaced via useFormatter. This PR adds the missing public API and keeps naming/docs aligned.

What changed

  • packages/use-intl/src/core/createFormatter.tsx
    • Adds displayName(value, options) and displayName(value, format, options?) overloads.
    • Reuses formatters.getDisplayNames cache-backed instances.
  • packages/use-intl/src/core/Formats.tsx
    • Adds displayName?: Record<string, Intl.DisplayNamesOptions> to global formats.
  • packages/use-intl/src/core/AppConfig.tsx
    • Adds displayName format-name typing support in FormatNames augmentation.
  • packages/use-intl/src/react/useFormatter.test.tsx
    • Adds coverage for region/language/currency formatting, named global formats, and Intl.DisplayNames instance caching.
  • Documentation updates:
    • docs/src/pages/docs/usage/display-name.mdx (renamed from display-names.mdx) with singular API examples.
    • docs/src/pages/docs/usage/_meta.tsx route key updated to display-name.
    • docs/src/pages/docs/usage/configuration.mdx and docs/src/pages/docs/workflows/typescript.mdx updated to singular key/API usage.
    • docs/src/pages/docs/environments/runtime-requirements.mdx now explicitly lists Intl.DisplayNames and corresponding polyfill entries.

Test plan

  • format.displayName('US', {type: 'region'}) -> "United States"
  • format.displayName('en', {type: 'language'}) -> "English"
  • format.displayName('USD', {type: 'currency'}) -> "US Dollar"
  • Named global format: format.displayName('US', 'region') with formats.displayName.region = {type: 'region'}
  • Intl.DisplayNames instances are cached for equivalent options

Verification run

  • pnpm --filter use-intl test -- run src/react/useFormatter.test.tsx (pass)
  • Docs formatting checked with Prettier on changed docs files

@vercel

vercel Bot commented Mar 15, 2026

Copy link
Copy Markdown

@roderickhsiao is attempting to deploy a commit to the next-intl Team on Vercel.

A member of the Team first needs to authorize it.

@roderickhsiao roderickhsiao marked this pull request as ready for review March 15, 2026 22:26
@amannn amannn mentioned this pull request Apr 28, 2026
15 tasks

@amannn amannn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @roderickhsiao, thanks a lot for this PR—this looks really great!

There's only one API consideration I'd raise, let me know what you think.

Comment thread docs/src/pages/docs/usage/display-names.mdx Outdated
Comment thread docs/src/pages/docs/workflows/typescript.mdx
@roderickhsiao roderickhsiao changed the title feat: add displayNames formatter to useFormatter feat: add displayName formatter to useFormatter Apr 28, 2026
@roderickhsiao

Copy link
Copy Markdown
Contributor Author

@amannn thanks for the review, love the work! updated PR.

Cheers

@amannn amannn changed the title feat: add displayName formatter to useFormatter feat: Add displayName to useFormatter Apr 28, 2026
@vercel

vercel Bot commented Apr 28, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
next-intl-docs Ready Ready Preview, Comment Apr 28, 2026 6:27pm
next-intl-example-app-router Ready Ready Preview, Comment Apr 28, 2026 6:27pm
next-intl-example-app-router-without-i18n-routing Ready Ready Preview, Comment Apr 28, 2026 6:27pm

Request Review

@amannn

amannn commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Awesome, thanks!

One last thing: CI is failing because size budgets for use-intl and next-intl need to be adjusted.

See .size-limit.ts files in the relevant directories.

Can you have a quick look? I think maintainer edits aren't turned on for this PR.

@roderickhsiao

Copy link
Copy Markdown
Contributor Author

Awesome, thanks!

One last thing: CI is failing because size budgets for use-intl and next-intl need to be adjusted.

See .size-limit.ts files in the relevant directories.

Can you have a quick look? I think maintainer edits aren't turned on for this PR.

I got you, one sec

@amannn

amannn commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Regarding 597c3e4, I think I'd prefer a sorting in the docs based on how often the APIs are needed (dates and numbers more than display names). Can you revert that part?

In code, I'm fine with either sorting.

@roderickhsiao

Copy link
Copy Markdown
Contributor Author

I'll let you decide, I think its in general the usage is kind of bias (if number is more than dates?) but I also get your point

@amannn

amannn commented Apr 28, 2026

Copy link
Copy Markdown
Owner

Yeah, I honestly don't know if date or number formatting is more popular (would guess for dates), but having these come first makes sense IMO.

Thanks a lot!

@roderickhsiao roderickhsiao force-pushed the feat/display-names-formatter branch from 7b3f768 to 98b3e26 Compare April 28, 2026 16:47

@amannn amannn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor thing I noticed on a final look

Comment thread packages/use-intl/src/core/createFormatter.tsx Outdated
Comment thread packages/use-intl/src/core/createFormatter.tsx Outdated

@amannn amannn left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks! 🙌

@amannn amannn merged commit 3666aa8 into amannn:main Apr 28, 2026
5 of 6 checks passed
@roderickhsiao roderickhsiao deleted the feat/display-names-formatter branch April 28, 2026 18:29
@roderickhsiao

Copy link
Copy Markdown
Contributor Author

Thank you for the patient review :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants