-
-
Notifications
You must be signed in to change notification settings - Fork 250
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
refactor(RSC): Refactor async APIs to make the locale optional #600
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…ard compatibility (seems necessary)
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
amannn
changed the title
refactor(RSC): Refactor async APIs
refactor(RSC): Refactor async APIs to make the locale optional
Nov 6, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change (tldr)
Before:
After:
Apart from
getTranslations
, also the other async APIs have been streamlined to accept an optional object with thelocale
:getFormatter({locale})
getMessages({locale})
getNow({locale})
getTimeZone({locale})
In case you use dynamic rendering, or you're using
unstable_setRequestLocale
, you can omit the object argument with thelocale
.See this commit for an example of how to upgrade: 1aac2e7
Background
I've always been quite vocal about why I think the hooks-based
useTranslations
API is favorable over an async alternative for components, mainly due to the resulting components qualifying as shared components and generally to reduce the APIs you have to learn.A popular user request over the RSC beta period has always been to use translations in
async
components. We've recently aligned(await getTranslator()).rich
withuseTranslations().rich
and therefore madegetTranslator
usable in React components.I gave the topic another critical look and based on recent developments on the Next.js side like PPR, it seems like async components will likely appear also in nested, deeper places in your server component tree (see also How Next.js is delivering React’s vision for the future by Sam Selikoff). Having multiple async components in your server tree, along with suspense boundaries, can be beneficial, therefore
next-intl
should make it easy to work with this pattern.Based on this, I think there's a need for first-class support for using translations in async components.
To achieve this, this PR includes the following changes:
getTranslator
togetTranslations
for API similarity withuseTranslations
(and to make this change non-breaking for users of previous release candidate versions)getTranslator
to an API where thelocale
is optional, making the consumption in components easier.locale
togetTranslations
via an object, for easier static rendering support in the Metadata API.In case Next.js adds support for reading the locale at arbitrary places in the React tree (see facebook/react#27424 (comment)), the new API furthermore provides an opportunity to deprecate the object form altogether—this wouldn't be possible with the previous API due to ambiguity of the string arguments
locale
andnamespace
.