Skip to content

Conversation

devongovett
Copy link
Member

@devongovett devongovett commented Jul 11, 2022

This reduces our bundle size by ~40 KB minified, or around ~11 KB min+gzip by removing the intl-messageformat dependency. This is done by pre-compiling messages during the build rather than parsing them at runtime. Basically, a message like this:

"{count, plural, =0 {No items selected} one {# item selected} other {# items selected}}."

is compiled to a function like this, which returns a template literal:

(args, formatter) => `${formatter.plural(args.count, {"=0": `No items selected`, one: () => `${formatter.number(args.count)} item selected`, other: () => `${formatter.number(args.count) items selected}`})}.`

As you can see, the MessageFormatter class includes some helpers used by the messages, such as plural formatting.

This example is kinda advanced, but in many cases, there are only basic interpolations without fancy formatting.

(args) => `${args.item} selected`

And if there are no interpolations at all, then only a string is returned rather than a function.

This is a breaking change to @internationalized/message and therefore @react-aria/i18n since the input message format changed. We will need to decide how to handle this, especially with the mono package.

@adobe-bot
Copy link

@adobe-bot
Copy link

@adobe-bot
Copy link

@devongovett
Copy link
Member Author

Discussed with @dannify about how to handle the breaking change. We would like to avoid bumping react-aria to v4 until we have a few more breaking changes ready. But we also don't want to block this until then. Instead, we propose the following:

  • Keep the existing useMessageFormatter hook for backward compatibility, but deprecate it, and remove from the sidebar in the docs.
  • Introduce a new useLocalizedStringFormatter hook for the new version.
  • Create a new @internationalized/string package and deprecate @internationalized/message. This would contain LocalizedStringFormatter and LocalizedStringDictionary objects to replace the existing MessageFormatter and MessageDictionary. This way the names of these objects match the hook.

Alternative would be to only bump a major on @internationalized/message, but then we'd need to move the old implementation into @react-aria/i18n in order to keep useMessageFormatter around. Plus, it might be nice to have the names match across the board.

@adobe-bot
Copy link

@devongovett devongovett marked this pull request as ready for review July 14, 2022 01:43
…ormat

# Conflicts:
#	packages/@react-aria/calendar/src/useCalendarBase.ts
#	packages/@react-aria/calendar/src/useCalendarCell.ts
#	packages/@react-aria/datepicker/src/useDateRangePicker.ts
#	packages/@react-aria/i18n/src/index.ts
@adobe-bot
Copy link

snowystinger
snowystinger previously approved these changes Jul 22, 2022
@adobe-bot
Copy link

LFDanLu
LFDanLu previously approved these changes Jul 25, 2022
Copy link
Member

@LFDanLu LFDanLu left a comment

Choose a reason for hiding this comment

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

Heads up, Field had an instance of useMessageFormatter that was added recently so I went ahead and updated that. Other than that, I assume we'll be updating internationalized.mdx to point to useLocalizedStringFormatter and adding a deprecation notice to useMessageFormatter.mdx after this goes in?

snowystinger
snowystinger previously approved these changes Jul 25, 2022
@adobe-bot
Copy link

@devongovett
Copy link
Member Author

Yes, will update the docs separately.

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.

4 participants