112

I have seen the HTTP headers of Content-Language and Accept-Language, could someone explain what these are for and the difference between them? I have a multilingual site and wondering should I be setting both to the sites current selected language, by the user.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
amateur
  • 43,371
  • 65
  • 192
  • 320

6 Answers6

135

Content-Language, an entity header, is used to describe the language(s) intended for the audience, so that it allows a user to differentiate according to the users' own preferred language. Entity headers are used in both, HTTP requests and responses.1

Accept-Language, a request HTTP header, advertises which languages the client is able to understand, and which locale variant is preferred.2 There can be multiple languages, each with an optional weight or 'quality' value. For example:

Accept-Language: da, en-GB;q=0.8, en;q=0.7

(The default weight is 1, so this is equivalent to da;q=1, en-GB;q=0.8, en;q=0.7).

You're going to have to parse the values and weights to see if an appropriate translation is available, and provide the user the translation in the highest preferred language weight.

It is recommended you give the users an alternative, such as a cookie set value, to force a certain language for your site. This is because some users may want to see your site in a certain language, without changing their language acceptance preferences.

Leo
  • 10,407
  • 3
  • 45
  • 62
onteria_
  • 68,181
  • 7
  • 71
  • 64
  • 14
    Slight tweak to your answer: Content-Language is a content header, so it applies to content whether from server to client or from client to server. – james.garriss Jan 18 '12 at 18:19
  • 1
    RFC2616: “The Content-Language entity-header field describes the natural language(s) of the intended audience for the enclosed entity. However, just because multiple languages are present within an entity does not mean that it is intended for multiple linguistic audiences. An example would be a beginner's language primer, such as "A First Lesson in Latin," which is clearly intended to be used by an English-literate audience. In this case, the Content-Language would properly only include "en".” R. Fielding, UC Irvine, J. Gettys, J. Mogul, H. Frystyk, L. Masinter, P. Leach, T. Berners-Lee; 1999. – Yarl Apr 04 '17 at 12:59
  • So that are not definitely languages on page. See [RFC 2616 Section 14.12](http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.12). – Yarl Apr 04 '17 at 13:06
27

Content-Language is the language of the page you're serving. Accept-Language is a list of languages you PREFER to accept.

24

Content-Language describes the language that a particular piece of content is intended for. Accept-Language is the list of languages that a user agent wants content in. The best way to think of this is that Content-Language describes content and Accept-Language conveys a preference.

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
5

The Content-Language entity-header field describes the natural language(s) of the intended audience for the enclosed entity. Note that this might not be equivalent to all the languages used within the entity-body.

The Accept-Language request-header field restricts the set of natural languages that are preferred as a response to the request

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Rana Prathap
  • 83
  • 1
  • 11
1

The Content-Language entity header is used to describe the language(s) intended for the audience, so that it allows a user to differentiate according to the users' own preferred language.

Header type                     Entity header
Forbidden header name         no
CORS-safelisted response-header   yes
CORS-safelisted request-header    yes

MDN Web Reference - HTTP Headers - Content-Language

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.)

Header type                     Request header
Forbidden header name         no
CORS-safelisted request-header    yes

MDN Web Reference - HTTP Headers - Accept-Language

georgeawg
  • 48,608
  • 13
  • 72
  • 95
1

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. Source

In Accept-Language client informs the server in what language wants to get a response.

Content-Language contains information about response culture.

The service is trying to convert the response into a given language in the Accept-Language header. However, there can be a situation where the server is not able to answer in a given language. In that case, the response can be in the default language. Information about the response language will be in the Content-Language header.