The Accept-Language
HTTP header is a critical element of the HTTP protocol, enabling a client to specify the preferred language(s) for the requested resource. It plays a fundamental role in shaping a more personalized and user-friendly browsing experience.
The Purpose of Accept-Language
This request header is primarily used for content negotiation, where the client specifies the preferred languages for the response. The server can then provide the resource in the appropriate language if such a version is available.
Syntax
The Accept-Language
header follows the syntax:
Accept-Language: <language>
Where <language>
signifies the desired languages. You can specify multiple languages separated by commas and use quality factors (q) to indicate preference:
Accept-Language: <language>, <language>;q=<quality_factor>
Possible Values
The Accept-Language
header supports a wide variety of languages, specified using language tags as defined by BCP 47. Some commonly used values include:
en
: Englishes
: Spanishfr
: Frenchde
: German*
: Any language
How it Works: Request and Response
To illustrate the use of the Accept-Language
header, let’s look at a standard request-response cycle:
Request
Suppose a client prefers responses in French but also understands English, it might send a request like this:
GET /document HTTP/1.1
Host: example.com
Accept-Language: fr, en;q=0.9
In this request, the client indicates a preference for French (fr
) but can also accept English (en
), albeit with a lower preference.
Response
The server uses the Accept-Language
header to select the appropriate language for the response. If possible, it will provide the resource in the client’s preferred language.
A response to the above request could look like this:
HTTP/1.1 200 OK
Content-Language: fr
Content-Type: text/html
<!DOCTYPE html>
...
</html>
In this response, the server indicates that it’s sending HTML content in French. If the server cannot deliver a response in a language acceptable to the client, it may deliver the content in a default language or return a 406 Not Acceptable
status code.
Compatibility
The Accept-Language
header is part of the HTTP/1.1 specification and is widely supported across all modern browsers and servers.
Summary
The Accept-Language
HTTP header is a key instrument for delivering personalized content, allowing clients to express their language preferences for the response. Its correct usage facilitates better user experiences, especially in multi-language environments. Understanding Accept-Language
is essential when building web applications intended for global reach.