i18next-http-middleware is a versatile, framework-agnostic middleware for integrating i18next into Node.js web applications (Express, Fastify, Hapi) and Deno. Currently at version 3.9.5, it is actively maintained with regular updates, with the last major update to 3.x series being 2 months ago. This package serves as a modern drop-in replacement for the deprecated `i18next-express-middleware`, offering broader compatibility beyond just Express. It facilitates language detection from various request sources (path, cookie, header, querystring, session) and injects the i18next instance directly onto the request object, providing methods like `req.t()`, `req.language`, and `req.languages` for seamless internationalization within server-side handlers and views. Its primary differentiator is its flexibility across different HTTP frameworks and its close integration with the i18next ecosystem for robust server-side internationalization.
npm install i18next-http-middlewareVerified import paths — ran on the pinned version, not inferred.
This example sets up `i18next-http-middleware` with an Express server, initializing i18next with multiple languages and preloaded resources. It demonstrates applying the middleware globally, accessing translation functions (`req.t`) and language properties (`req.language`, `req.languages`) from the request object, and handles basic routing for language switching based on URL path or query parameters. It also includes an ignored health check route.
Upgrade to `i18next-http-middleware` version 3.9.3 or higher immediately. This version introduces robust sanitization of control characters and a strengthened XSS regex to mitigate these vulnerabilities.
Migrate from `i18next-express-middleware` to `i18next-http-middleware`. Ensure you update your imports and middleware registration according to the new package's documentation, taking advantage of its broader framework support.
Ensure all necessary languages are either `preload`ed in your `i18next.init()` configuration or that you have an i18next backend (e.g., `i18next-fs-backend`, `i18next-http-backend`) properly configured to fetch missing translations. To persist language changes across requests, you typically need to update a cookie or session via client-side or server-side logic outside of `req.i18n.changeLanguage()`.
If experiencing this issue, upgrade to `i18next-http-middleware` version 3.8.2 or later, which includes a fix for this bug. Alternatively, configure `fallbackLng` as a simple array (e.g., `['en']`) to avoid the error in affected versions.
Upgrade `i18next-http-middleware` to version 3.8.2 or newer. As a workaround for affected versions, configure `fallbackLng` as an array (e.g., `fallbackLng: ['en']`) instead of an object.
Verify that the language you are switching to is included in `preload` in your `i18next.init()` configuration, or ensure your i18next setup includes a backend that can asynchronously load resources for new languages. To persist language changes across requests, you typically need to update a user's language preference in a cookie or session, which the `LanguageDetector` can then pick up on subsequent requests.
Ensure you have called `app.use(handle(i18next, ...))` for Express, `app.register(i18nextMiddleware.plugin, ...)` for Fastify, or `server.register({ plugin: i18nextMiddleware.hapiPlugin, ...})` for Hapi, and that `i18next` itself is initialized with `i18next.use(LanguageDetector).init(...)` before applying the middleware to your application's request pipeline.