i18n-express is a lightweight internationalization middleware designed for Express.js applications, currently at version 1.1.3. It facilitates basic language switching and content localization by reading language-specific JSON files from a designated directory. The middleware determines the user's preferred language based on a hierarchy: a configured cookie, a URL query parameter, browser `Accept-Language` headers, or a default language. Once determined, it exposes a `textsVarName` (default: `texts`) variable to your view engine (e.g., EJS, Handlebars) containing the translated strings for the active language, along with a `lang` variable indicating the current language. Its primary differentiator is its extreme simplicity and file-based approach, avoiding complex CLDR data or advanced pluralization rules, making it suitable for very straightforward localization needs. The package has not seen updates in approximately seven years, suggesting it is no longer actively maintained and new releases are highly unlikely.
npm install i18n-expressVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes an Express app with i18n-express, demonstrating how to set up translation files, configure the middleware, and access translated texts and the current language in an EJS view.
Consider migrating to actively maintained i18n solutions for Express, such as 'i18n' or 'express-i18n'.
Ensure `app.use(cookieParser());` and `app.use(session(...));` are called prior to `app.use(i18n(...));`.
Use `const i18n = require('i18n-express');` in CommonJS projects. In ESM, use dynamic `import()` or ensure your build system correctly handles CJS interoperability with `import i18n from 'i18n-express';`.Familiarize yourself with the language detection hierarchy and ensure your application's language switching mechanisms align with it. Test all detection methods thoroughly.
For applications requiring advanced i18n features, consider libraries like `i18n` (npm package) or `formatjs` (react-intl), which offer more comprehensive solutions. This library is best suited for simple key-value translations.
Always call `i18n` with an options object: `app.use(i18n({ /* options */ }));`Ensure `translationsPath` points to the correct directory containing valid `.json` language files (e.g., `en.json`, `es.json`) and that the `textsVarName` in your view matches the configuration.
Add `const session = require('express-session');` and configure `app.use(session(...));` before using i18n-express.Verify that `app.use(cookieParser());` (and `app.use(session(...));` if used) are placed *before* `app.use(i18n(...));` in your Express application configuration.