devnotes.

req.acceptsLanguages() Function

req.acceptsLanguages() checks whether a given language is acceptable to the client, based on the Accept-Language header, and returns the first match or false.

Syntax

req.acceptsLanguages(lang)

Example

const express = require("express");
const app = express();
const PORT = 4700;

app.get("/greeting", (req, res) => {
    const lang = req.acceptsLanguages(["bn", "en-US"]);

    if (lang === "bn") {
        return res.send("স্বাগতম!");
    }
    res.send("Welcome!");
});

app.listen(PORT, () => console.log(`Listening on ${PORT}`));

Use cases

  • Serving localized content based on client preference.
  • Building multilingual APIs and websites.
  • Choosing a default language when no strong preference is given.