connect-locale is a configurable Express/Connect middleware designed specifically for detecting and storing user locale preferences within web applications. It is not a comprehensive i18n solution for managing resource files, but rather focuses on establishing a flexible strategy for locale identification and persistence. The library allows developers to define a sequence of strategies (e.g., path, cookie, accept-language header) to determine the user's preferred locale and then store it in various locations (e.g., cookie, session, request object, `res.locals`) for subsequent use in other middleware or templating engines. The current stable version is 1.3.3. Given its last update several years ago, it is in maintenance mode, providing a stable, albeit not actively developed, solution for locale detection. Its key differentiators are its highly configurable detection and storage mechanisms, allowing for custom i18n strategies.
npm install connect-localeVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic integration of connect-locale with Express. It configures locale detection from path, cookies, and the Accept-Language header, storing the result in cookies, the request object, and `res.locals`. It includes the necessary `cookie-parser` middleware and shows how to access the detected locale within a route handler.
Ensure `app.use(cookieParser())` is called before `app.use(Locale(options))`.
Use `const Locale = require('connect-locale');` for CommonJS environments or configure your build system to handle CJS imports in ESM projects.Review the source code for potential vulnerabilities or consider alternative actively maintained libraries if long-term support and modern features are critical.
Adjust your application's routing to match the expected URL structure for the 'path' strategy, or choose other `getLocaleFrom` strategies more aligned with your URL design.
Ensure `Locale` middleware is called with a valid options object, e.g., `app.use(Locale({ locales: ['en'], getLocaleFrom: ['default'] }))`.Install `cookie-parser` (`npm install cookie-parser`) and ensure `app.use(cookieParser())` is called before `app.use(Locale(options))`.
Change the import statement to `const Locale = require('connect-locale');`.Ensure you are using `const Locale = require('connect-locale');` and then correctly invoking it as `app.use(Locale(options));`.