Http Accept Language
Accept-Language – HTTP – MDN Web Docs
The Accept-Language request HTTP header indicates the natural language and locale that the client prefers.
The server uses content negotiation to select one of the proposals and informs the client of the choice with the Content-Language response header. Browsers set required values for this header according to their active user interface language. Users rarely change it, and such changes are not recommended because they may lead to fingerprinting.
This header serves as a hint when the server cannot determine the target content langauge otherwise (for example, use a specific URL that depends on an explicit user decision). The server should never override an explicit user language choice. The content of Accept-Language is often out of a user’s control (when traveling, for instance). A user may also want to visit a page in a language different from the user interface language.
The server possibly can send back a 406 (Not Acceptable) error code when unable to serve content in a matching language. However, such a behavior is rarely implemented for a better user experience, and servers often ignore the Accept-Language header in such cases.
Header type
Request header
Forbidden header name
no
CORS-safelisted request header
yes, with the additional restriction that values can only be
0-9, A-Z, a-z, space or
*, -. ;=.
SyntaxAccept-Language:
Accept-Language: *
// Multiple types, weighted with the quality value syntax:
Accept-Language: fr-CH, fr;q=0. 9, en;q=0. 8, de;q=0. 7, *;q=0. 5
Directives
A language tag (which is sometimes referred to as a “locale identifier”). This
consists of a 2-3 letter base language tag that indicates a language, optionally
followed by additional subtags separated by ‘-‘. The most common extra
information is the country or region variant (like ‘en-US’ or
‘fr-CA’) or the type of alphabet to use (like ‘sr-Latn’).
Other variants, like the type of orthography (‘de-DE-1996’), are usually
not used in the context of this header.
*
Any language; ‘*’ is used as a wildcard. ;q= (q-factor weighting)
Any value placed in an order of preference expressed using a relative
quality value called weight.
ExamplesAccept-Language: de
Accept-Language: de-CH
Accept-Language: en-US, en;q=0. 5
SpecificationsSpecificationHypertext Transfer Protocol (HTTP/1. 1): Semantics and Content (HTTP/1. 1)# compatibilityBCD tables only load in the browserSee also
HTTP content negotiation
A header with the result of the content negotiation:
Content-Language
Other similar headers: TE, Accept-Encoding, Accept
Accept-Language – HTTP – W3cubDocs
The Accept-Language request HTTP header advertises which languages the client is able to understand, and which locale variant is preferred. (By languages, we mean natural languages, such as English, and not programming languages. ) Using content negotiation, the server then selects one of the proposals, uses it and informs the client of its choice with the Content-Language response header. Browsers set adequate values for this header according to their user interface language and even if a user can change it, this happens rarely (and is frowned upon as it leads to fingerprinting). This header is a hint to be used when the server has no way of determining the language via another way, like a specific URL, that is controlled by an explicit user decision. It is recommended that the server never overrides an explicit decision. The content of the Accept-Language is often out of the control of the user (like when traveling and using an Internet Cafe in a different country); the user may also want to visit a page in another language than the locale of their user interface. If the server cannot serve any matching language, it can theoretically send back a 406 (Not Acceptable) error code. But, for a better user experience, this is rarely done and more common way is to ignore the Accept-Language header in this case. Syntax Accept-Language:
Accept-Language: *
// Multiple types, weighted with the quality value syntax:
Accept-Language: fr-CH, fr;q=0. 9, en;q=0. 8, de;q=0. 7, *;q=0. 5 Directives
Accept-Language: de-CH
Accept-Language: en-US, en;q=0. 5
Specifications Browser compatibilityUpdate compatibility data on GitHub
Desktop
Chrome
Edge
Firefox
Internet Explorer
Opera
Safari
Accept-Language
Yes
12
Mobile
Android webview
Chrome for Android
Firefox for Android
Opera for Android
Safari on iOS
Samsung Internet
See also HTTP content negotiation
Header with the result of the content negotiation: Content-Language
Other similar headers: TE, Accept-Encoding, Accept-Charset, Accept
Accept-Language used for locale setting – W3C
Accept-Language used for locale setting
Intended audience:
script developers (PHP, JSP, etc. ), Web project managers, and anyone who wants to use locale information.
Question
Is it a good idea to use the HTTP Accept-Language header to determine the locale of the user?
Background
For a number of perfectly valid reasons some web applications would like to associate a locale with each user that visits the site. This
locale would enable them to provide information in the local format. Some of this information is common to traditional software locales, such as:
What numeric formats does the user expect?
How should dates and times be formatted?
In other cases, other information may be derived from the locale information plus additional knowledge, such as:
Should measurements be metric (centimeters, kilometers, liters) or imperial (inches, miles, gallons)?
What is the user’s time zone?
Does the user use Letter size paper, or A4?
What shoe and clothing sizing systems should be used?
What is the user’s physical location?
Since none of these are included in the HTTP protocol many web developers have used the Accept-Language header to make inferences about
the user’s locale.
The Accept-Language header is information about the user’s language preferences that is passed via HTTP when a document is requested.
Mainstream browsers allow these language preferences to be modified by the user. The
value itself is a defined by BCP 47, typically as a two or three letter language code (eg. fr for French), followed by
optional subcodes representing such things as country (eg. fr-CA represents French as spoken in Canada).
The question is about whether this information is appropriate for determining the locale of the user.
Answer
The HTTP Accept-Language header was originally only intended to specify the user’s language. However, since many applications need to
know the locale of the user, common practice has used Accept-Language to determine this information. It is not a good idea to use the HTTP
Accept-Language header alone to determine the locale of the user. If you use Accept-Language exclusively, you may handcuff the user
into a set of choices not to his liking.
For a first contact, using the Accept-Language value to infer regional settings may be a good starting point, but be sure to allow them
to change the language as needed and specify their cultural settings more exactly if necessary. Store the results in a database or a cookie for later
visits.
Some of the potential issues include the following:
Many users never change the defaults for Accept-Language. They are set when the user agent is installed. Unless they are multilingual
or have some other reason to adjust language preferences they may not even know such settings exist. Hence, the user may not have ever ratified the
Accept-Language setting.
A user agent may send a request that specifies only a language and not a region, for example you may not get a header with de-DE, de-CH or de-AT to indicate German as spoken in Germany, Switzerland or Austria, respectively. On the other hand, you
might only get de indicating a preference for German. If you were planning to use the region to decide what currency to use you are now
in a bind. Your particular circumstances might allow you to make assumptions such as “Germany has 83 million people, Switzerland has 7 million
but only 63% speak German, Austria has 8 million, so this user probably uses the Euro. If we’re wrong we’ll only offend 4. 6% of our German speaking
customers or just over 4 million people. ” Feel free to make such an assumption, if you can accept the risk. Its a lot simpler to ask the user
for more information. Also, the math gets more difficult for Spanish or English, for instance.
People borrow machines from friends, they rent them from internet cafes. In these cases the inferred locale may be inappropriate, and
care should be taken to allow the user to select the appropriate language and locale from whatever page they are looking at.
By the way
Using the Accept-Language header is also a good starting point for determining the language of the user, rather than the
locale, but even then you must know its limitations and give the user some way to override the assumptions you make. Many of the potential issues
listed above apply here too.
Even if you aren’t intentionally making assumptions about locale or region, you should be aware that your programming environment may be
making such assumptions on your behalf. Many Web servers, server side scripting languages, and operating environments, by default, parse and infer
their native locale objects from Accept-Language. For example, uses the Accept-Language to determine the default CultureInfo, Java Servlet
provides a getLocale() and getLocales() pair of methods that parse Accept-Language, and so forth. These objects are very useful in obtaining
resources and other “language preference” material. They are less useful, as pointed out above, in determining many of the fine grained attributes of
users or in designing the international behavior of a site. A language preference of es-MX doesn’t necessarily mean that a postal address form should
be formatted or validated for Mexican addresses. The user might still live in the USA (or elsewhere).
Further reading
What the HTTP specification says about Accept-Language
Other W3C I18N resources relating to language
Frequently Asked Questions about http accept language
What is accept-language in HTTP?
The Accept-Language request HTTP header advertises which languages the client is able to understand, and which locale variant is preferred. … This header is a hint to be used when the server has no way of determining the language via another way, like a specific URL, that is controlled by an explicit user decision.
What is the accept-language header?
The Accept-Language header is information about the user’s language preferences that is passed via HTTP when a document is requested. Mainstream browsers allow these language preferences to be modified by the user. The value itself is a defined by BCP 47, typically as a two or three letter language code (eg.
How do I accept-language in header?
HTTP headers | Accept-Language<language>: This consists of a 2-3 letter base language tag representing the language, followed by sub-tags separated by ‘-‘ . The extra information is the region and country variant (like ‘en-US’ or ‘fr-CA’)*: It is used as a wildcard for any language present.Oct 31, 2019