9951 explained code solutions for 126 technologies


expressjsHow can I set up localization in Express.js?


Localization in Express.js is a way of setting up different language versions of your website. It can be done by using the accept-language module. Here is an example of setting up localization in Express.js:

// Set up Express.js
const express = require('express');
const app = express();

// Set up the accept-language module
const acceptLanguage = require('accept-language');

// Set up the language versions
acceptLanguage.languages(['en-US', 'es-ES']);

// Set the language on the request
app.use(acceptLanguage.setLocaleFromQuery());

// Use the language in your routes
app.get('/', (req, res) => {
  res.send(`The language is ${req.locale}`);
});

Output example

The language is en-US

The code above sets up localization in Express.js by:

  1. Requiring the express and accept-language modules.
  2. Setting up the language versions with acceptLanguage.languages().
  3. Setting the language on the request with acceptLanguage.setLocaleFromQuery().
  4. Using the language in your routes.

Helpful links

Edit this code on GitHub