Hydrate i18n on the client
mainTo prevent hydration mismatches, the client-side i18next instance must be initialized with the same language the server used. Use the <html lang> attribute as the source of truth for detection. If using a backend for on-demand loading, configure the loadPath to match your locale serving route.
import Fetch from "i18next-fetch-backend";
import i18next from "i18next";
import I18nextBrowserLanguageDetector from "i18next-browser-languagedetector";
import { startTransition, StrictMode } from "react";
import { hydrateRoot } from "react-dom/client";
import { I18nextProvider, initReactI18next } from "react-i18next";
import { HydratedRouter } from "react-router/dom";
async function main() {
await i18next
.use(initReactI18next)
.use(Fetch)
.use(I18nextBrowserLanguageDetector)
.init({
fallbackLng: "en",
detection: { order: ["htmlTag"], caches: [] },
backend: { loadPath: "/api/locales/{{lng}}/{{ns}}" },
});
startTransition(() => {
hydrateRoot(
document,
<I18nextProvider i18n={i18next}>
<StrictMode>
<HydratedRouter />
</StrictMode>
</I18nextProvider>,
);
});
}
main().catch(console.error);