# Accept-Language

> The `Accept-Language` HTTP header tells the server which languages the user prefers.

## How does the server detect the user's language?

The request header with the user's preferred languages in order, for example 'de-CH, de;q=0.9, en;q=0.8'. Servers use it to pick the initial language, but the user must always be able to switch it manually.

- Topic: Internationalization
- Type: concept

## Example

Wrong:

```js
const header = req.headers['accept-language'];
const lang = header.slice(0, 2);
```

Right:

```js
const header = req.headers['accept-language'] ?? '';
const prefs = header.split(',')
  .map((part) => part.trim().split(';q='))
  .sort((a, b) => Number(b[1] ?? 1) - Number(a[1] ?? 1))
  .map(([tag]) => tag.split('-')[0]);
const lang = req.cookies.lang ?? prefs.find((l) => SUPPORTED.includes(l)) ?? 'en';
```

The header may be missing and holds a weighted list; a saved user choice wins, then the best supported match, then a default.

## Common mistakes

- Redirecting users by IP location and locking them out of other languages.

## Key points

- The detected language can be changed by the user, and the choice is remembered.
- Language is not detected from IP address or country alone.

## Interview questions

### What does q=0.9 mean in Accept-Language?

It is a weight from 0 to 1 that ranks the preference. Languages without `q` have weight 1, so the list is ordered from most to least preferred.

### How do I read the preferred languages in the browser?

Use `navigator.languages`, which returns the same ordered list the browser sends in the header.

### Should the response include Vary: Accept-Language?

Yes, when the content depends on the header. Without it, a cache can serve one language to everyone.

## Related cards

- [HTTP Header](https://wonderium.dev/web/http-header.md)
- [navigator.languages](https://wonderium.dev/js/navigator-languages.md)
- [Locale](https://wonderium.dev/web/locale.md)

---

- Level: [Web Platform](https://wonderium.dev/web.md)
- HTML version: https://wonderium.dev/web/accept-language
- Last updated: 2026-11-25
