I'm having trouble with the useTranslation hook from the next-translate package in my Next.js application. While all my languages are being recognized, I encounter a hydration error whenever I change the language and refresh the page.
Here's the setup I have in my i18n.js file located at the root:
module.exports = {
locales: ["ar", "en","es"],
defaultLocale: "en",
pages: {
"*": ["common"],
},
};
I'm using useTranslation in my components as follows:
import useTranslation from "next-translate/useTranslation";
const ComponentName = () => {
const { t } = useTranslation("common");
// rest of the component
}
I've placed all my translations inside the public folder. When I change the language, I store the new language in local storage.
The issue arises when I change the language and then refresh the page. I get a hydration error because the server side language is detected as English, while the client has the custom language I had selected and stored in local storage.
Is there a way to solve this problem such that the server side recognizes the selected language upon refreshing? I have tried all the ways. Any help or guidance is greatly appreciated. https://drive.google.com/file/d/1WSBKdXKTpOlQv6g2v1c2KpnZaZjecHhf/view?usp=sharing - here is my folder strucutre
I implemented multi-language support using the next-translate/useTranslation hook in my Next.js application. I stored the selected language in local storage when changing languages. I expected the application to maintain the selected language even after a page refresh, seamlessly translating the page to the chosen language.On refreshing the page after changing the language, I encountered a hydration error. The server side detected English (the default language) while the client had the custom language I selected, causing a mismatch.