Express req.acceptsLanguages() Method
What you’ll learn
- How
req.acceptsLanguages()negotiates response language. - How to choose language fallback when no exact match exists.
- How this method integrates with localization workflows.
- How to avoid common language negotiation mistakes.
Syntax
javascript
req.acceptsLanguages(lang)
req.acceptsLanguages(lang1, lang2, ...)
req.acceptsLanguages([languages])1
Basic language negotiation
javascript
app.get('/welcome', function (req, res) {
var lang = req.acceptsLanguages('en', 'fr', 'es') || 'en';
res.json({ language: lang });
});2
Fallback to default locale
javascript
app.get('/docs', function (req, res) {
var lang = req.acceptsLanguages(['en', 'de']) || 'en';
res.send('Docs language: ' + lang);
});❓ FAQ
It matches supported languages against the Accept-Language header and returns the best match.
It returns false, so you can use a default language fallback.
Yes. You can pass multiple arguments or an array like req.acceptsLanguages('en', 'fr', 'es').
Yes. It helps pick a preferred locale before rendering localized content.
No. It helps with negotiation, while i18n libraries manage translations and locale formatting.
Did you know?
req.acceptsLanguages() checks the client's Accept-Language header and helps choose an appropriate response locale.
4 people found this page helpful
