Replies: 4 comments 8 replies
-
Hello @hqw567, i am working with next.js or i have implement translation both side client side or server side component but i am calling Api's outside my components and need to handle error or other things in multi language Thank you |
Beta Was this translation helpful? Give feedback.
-
@amannn Can you help us out? |
Beta Was this translation helpful? Give feedback.
-
Please see https://next-intl-docs.vercel.app/blog/translations-outside-of-react-components |
Beta Was this translation helpful? Give feedback.
-
Just to add to the discussion here. I've found a use case for using translations outside of components using yup (validation library for forms). I've been trying to create a schema like so :
which uses the translated labels for error messages based on locale. Now this schema is used in multiple forms, and is concatenated with other schema as needed. So to keep my component code clean and DRY, I extract the individual schema into separate files which are not React components and then fetch them from the React components :
As you can see, I need to pass the t function to the getContactSchema method in order to specify translated labels, as I am unable to use useTranslations nor getTranslations. Besides having to type the t function correctly (which I have been unable to do so far 😢 ), I don't think that this is scalable for larger applications. Other libraries expose the i18n object so that we can call i18n.t function outside of components and use translations where needed. @amannn I understand your approach, but I find that there are also use cases where translations outside of components are really useful. Is there any way you can reconsider allowing translations outside of components ? |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
How to use translation outside of components? I have some special circumstances where I need to use translation outside of the component. Is there a compatible method to use translation outside of the component? Although it is troublesome and terrible, I still need to consider this situation.
Beta Was this translation helpful? Give feedback.
All reactions