Skip to content

Conversation

@valeryq
Copy link

@valeryq valeryq commented Sep 11, 2025

Add support for default formatters

This PR introduces automatic default formatter handling for ICU message format placeholders and function-based formatters that don't specify a format.

Changes

Automatic default format injection: Messages with placeholders like {value, number} are automatically transformed to {value, number, default} before parsing.
Function formatter support: The useFormatter hook and createFormatter function now apply default formats when no specific format is provided.
Generic type support: Works with any ICU message format or function-based fromat type (number, date, time, plural, etc.)
Unified configuration: Both ICU types and function formatters can define default formats in the IntlProvider's formats prop

// Before: Required explicit format specification
<IntlProvider
  formats={{
    number: {
      default: { useGrouping: false },        // Applied to both ICU and functions
      currency: { style: 'currency', currency: 'USD' }
    },
    dateTime: {
      default: { year: 'numeric' },          // Applied to both ICU and functions
      full: { dateStyle: 'full' }
    }
  }}
>
  {/* ICU message formatting with defaults */}
  {t('price: {value, number}', {value: 10000})}  {/* → "price: 10000" */}
  
  {/* Function formatting with defaults */}
  {format.number(10000)}                         {/* → "10000" */}
  {format.dateTime(new Date())}                  {/* → "2024" */}
</IntlProvider>

Implementation

The transformation is handled by a regex pattern /\{([^,}]+),\s*(\w+)\s*}/g that:

  • Matches ICU placeholders with exactly 2 parts (variable, type)
  • Transforms them to 3 parts (variable, type, default)
  • Preserves existing 3-part placeholders unchanged
  • Function formatter defaults: Both useFormatter and createFormatter check for default format configurations when no explicit format is provided

@vercel
Copy link

vercel bot commented Sep 11, 2025

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

Project Deployment Preview Comments Updated (UTC)
next-intl-example-app-router Ready Ready Preview Comment Sep 11, 2025 9:10am
next-intl-example-app-router-without-i18n-routing Ready Ready Preview Comment Sep 11, 2025 9:10am

@vercel
Copy link

vercel bot commented Sep 11, 2025

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

A member of the Team first needs to authorize it.

@valeryq
Copy link
Author

valeryq commented Sep 12, 2025

The implementation doesn't fit well with the roadmap of next-intl.

@valeryq valeryq closed this Sep 12, 2025
@valeryq valeryq deleted the feat/support-for-default-formatters branch September 12, 2025 09:39
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.

1 participant