-
-
Notifications
You must be signed in to change notification settings - Fork 247
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
Cannot use translations in async server components #179
Comments
Hi! Are you using the Server Components preview? This is a known bug upstream in React / Next.js. Did you read the error message? It should suggest an alternative how you can fix this. |
I am having the same issue! I use the Server Components (beta) of next-intl. I receive the following error (no suggestion how to solve it)
Any ideas? Edit:
|
Can you sure a reproduction? You can use this as a template: https://github.com/amannn/next-intl-example-next-13 |
As a solution for the issue I am using the following now let messages;
try {
messages = (await import(`@/dictionaries/${locale}.json`)).default;
} catch (error) {
notFound();
}
const t = createTranslator({ locale, messages }); |
Thanks for providing more details! There was indeed some additional handling missing in the latest beta to display a more helpful error message.
I still have hope that this will be resolved upstream in React / Next.js, but hopefully this serves as a workaround in the meantime. |
While there is a resolution for the upstream React/Next.js issue, you can use this one that's a drop-in replacement for (It uses the deprecated import { type NamespaceKeys, type NestedKeyOf } from "next-intl";
import { getLocale, getTranslator } from "next-intl/server";
type IntlMessages = typeof import("@/messages/en.json");
export function getTranslations<
NestedKey extends NamespaceKeys<
IntlMessages,
NestedKeyOf<IntlMessages>
> = never,
>(namespace?: NestedKey) {
const locale = getLocale();
return getTranslator(locale, namespace);
} You might not need the Here's how yo use it,
|
I found this solution which is simple and working fine for me:
|
There's some more discussion on this topic in #406 btw.! |
Description
Hi, I found that if a server component is async (to fetch data) I cannot use anything from this package. UseTranslations will not work, other hooks exported by this package also fail (Im sure that useLocale also causes a crash).
Reproduction URL (CodeSandbox)
Don't have time but its easy to reproduce
Reproduction description
Steps to reproduce:
Expected behaviour
I expect it to work
The text was updated successfully, but these errors were encountered: