react-i18next SSR Not Working — Fix Server Rendering
In this fix, you'll learn how to properly configure react-i18next for SSR. This matters for server-rendered multilingual pages, and appears in Next.js and custom SSR setups.
The Problem
The following code demonstrates the typical mistake:
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
i18n.use(initReactI18next).init({
resources,
lng: 'en',
});
// SSR output shows translation keys, not values
The Fix
Here is the corrected version:
export const createInstance = (lng) => {
const instance = createInstance();
instance.use(initReactI18next).init({
resources, lng, fallbackLng: 'en',
interpolation: { escapeValue: false },
});
return instance;
};
// SSR output shows translated values
Prevention
- Create a fresh i18n instance per request to avoid cross-request state leakage.
- Detect language from request (cookie, Accept-Language, or URL path).
- Use react: { useSuspense: false } in SSR to avoid Suspense issues.
- Load only needed namespaces for the current page.
- For Next.js 13+, use app directory patterns with server-only/client-only imports.
Common Mistakes with i18n ssr
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro