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

refactor(RSC): Refactor async APIs to make the locale optional #600

Merged
merged 10 commits into from
Nov 7, 2023

Conversation

amannn
Copy link
Owner

@amannn amannn commented Nov 6, 2023

Change (tldr)

Before:

import {getTranslator} from 'next-intl/server';

const t = await getTranslator(locale, namespace?);

After:

import {getTranslations} from 'next-intl/server';

// Simplified usage in async components
const t = await getTranslations(namespace?);

// Optionally integrate with static rendering in the 
// Metadata API by passing an explicit locale.
const t = await getTranslations({locale, namespace}); 

Apart from getTranslations, also the other async APIs have been streamlined to accept an optional object with the locale:

  • 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 the locale.

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 with useTranslations().rich and therefore made getTranslator 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:

  1. Rename getTranslator to getTranslations for API similarity with useTranslations (and to make this change non-breaking for users of previous release candidate versions)
  2. Switch getTranslator to an API where the locale is optional, making the consumption in components easier.
  3. Allow to pass an explicit locale to getTranslations via an object, for easier static rendering support in the Metadata API.
  4. Update the docs to include documentation both for async as well as non-async components (see the current draft).

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 and namespace.

Copy link

vercel bot commented Nov 6, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
next-intl-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 6, 2023 9:02pm
next-intl-example-next-13 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 6, 2023 9:02pm

@amannn amannn changed the title refactor(RSC): Refactor async APIs refactor(RSC): Refactor async APIs to make the locale optional Nov 6, 2023
@amannn amannn marked this pull request as ready for review November 7, 2023 08:40
@amannn amannn merged commit 633e07d into feat/next-13-rsc Nov 7, 2023
16 checks passed
@amannn amannn deleted the feat/next-13-rsc-async-apis branch November 7, 2023 08:41
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