Next.js Discord

Discord Forum

How can I use next-intl translation my client components?

Answered
sh1ro posted this in #help-forum
Open in Discord
Avatar
I followed the set-up guide on next-intl setup document. There is a problem, when I'm making a navbar which is a client component, the ui have to change at the same time with the locale, but it appears this error message? Any suggestion for fixing and optimizing?
Image
Image
Image
Answered by B33fb0n3
no you can't, as the useTranslations need to know what kind of data you want to access. And without the mentioned provider it doesn't know that. And inside a constants file there is no react that could provide it. So make sure you reuse it when putting it in a variable like you did. If even more components of this three need it, you either want to move it one level up the tree (and pass it as props down to the needed components) or call the method inside the next react component again (I prefer the latter)
View full answer

8 Replies

Avatar
Bonus:
Image
Avatar
@sh1ro I followed the set-up guide on next-intl setup document. There is a problem, when I'm making a navbar which is a client component, the ui have to change at the same time with the locale, but it appears this error message? Any suggestion for fixing and optimizing?
Avatar
you need to make sure to call the useTranslations inside your component and not outside your component. So shift the navLinks and the useTranslations into your component and you solved the error
Avatar
@sh1ro So I can't use it in a `constant` file?
Avatar
no you can't, as the useTranslations need to know what kind of data you want to access. And without the mentioned provider it doesn't know that. And inside a constants file there is no react that could provide it. So make sure you reuse it when putting it in a variable like you did. If even more components of this three need it, you either want to move it one level up the tree (and pass it as props down to the needed components) or call the method inside the next react component again (I prefer the latter)
Answer
Avatar
@sh1ro How to close this help?
Avatar
I maked it as solution. You can leave this thread if you want to. Happy to help 🙂