admin管理员组文章数量:1394740
I am trying to implement internationalization using react-i18n in my frontend application. The application uses TypeScript and Next.js.
I have set up two languages, English and Finnish, along with a language switcher.
When I refresh the page with English (default) selected, all is well, but when I refresh with Finnish selected, I get a HydrationError saying that Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client. This can happen if a SSR-ed Client Component used
as shown below:
I assume this is because the i18n library modifies the language on the client side, disrupting the server-side rendering checks.
My i18n.ts is as follows:
"use client"
import i18next from "i18next";
import { initReactI18next } from "react-i18next";
import LanguageDetector from "i18next-browser-languagedetector";
import resourcesToBackend from "i18next-resources-to-backend"
import enNs from "./public/locales/en.json";
import fiNs from "./public/locales/fi.json";
export const defaultNS = "ns1";
i18next
.use(initReactI18next)
.use(LanguageDetector)
.use(resourcesToBackend((language: any, namespace: any) => import(`./public/locales/${language}.json`)))
.init({
debug: true,
lng: undefined,
fallbackLng: "en",
defaultNS,
resources: {
en: {
ns1: enNs
},
fi: {
ns1: fiNs
},
},
});
export default i18next;
The next-i18next library does not work since that is only for pages applications and I am using an app application.
How do I make it so that either the i18n library does the modifications on the server side, or otherwise so that the HydrationError is resolved?
本文标签: javascriptHydrationError when using nexti18n with Nextjs and TypeScriptStack Overflow
版权声明:本文标题:javascript - HydrationError when using next-i18n with Next.js and TypeScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744629369a2616446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论