Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the browser accept-language

If the user has their browser set to fr-CA, for instance but my site has the option to view the page in another available language (like English). How can I override the accept-language header so that I can reload using the specified language?

I was attempting to simply change the Accept-Language header and then reload the page, but I'm not sure how to do this. Any thoughts?

UPDATE: Ok, so I'm getting people explaining to me what localization is so I must not have asked this properly.

My site currently has globalization set to auto in the web.config so it will automatically set the thread culture to whichever language was negotiated at app start. By default, the user's browser will send the accept-language header based on the language settings for the browser which, like someone pointed out below, average users have no clue what those are, where those are, or how to change them. In any case, let's call this default behavior that the browser will handle the language headers first. However, as a FEATURE, I want to allow the user to change this accept-language header from the page. For instance, in the application, the language settings will normally be determined by cookie or by user preference (via profile settings), but on the landing/login page (particularly if this your first time logging in on a certain computer), I have no idea who you are so I only have your browser settings to go off of. But let's say that you're traveling on business and accessing this site from an american computer, the page loads in English and you can't read it and you have no idea how to change the browser language. Would it not be nice to have the option to choose your language from a drop down menu or icon or something? I think it would be.

So in order to do that, I need to be able to change that accept-language header and reload the page. This is where I'm not sure how to proceed.

I've tried navigator.language = <selected language> and xhr.setRequestHeader but these don't seem to work.

like image 439
Sinaesthetic Avatar asked Aug 30 '13 00:08

Sinaesthetic


People also ask

Can I change accept-language header?

The user can change the Accept-Language header sent by the browser using the browser's preference settings. E.g., in Chrome, go to “Settings”, click on “Show advanced settings...”, scroll down to “Languages”, click on “Language and input settings...”, and then add languages and drag to order them.

How do I accept a language header?

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.

Why did my browser change languages?

Like most browsers, Chrome automatically selects a language. But it might not always be the right one for you. In some cases, Chrome can also switch language without warning. This occurs primarily after running updates.


1 Answers

The Accept-Language header is a request header, so it is sent by the browser (or other client software). On your server, you can ignore it by doing nothing. That is, it has no effect as such; all depends on whether your server-side code recognizes it and takes some action. So you cannot and need not change the header sent by the browser.

The user can change the Accept-Language header sent by the browser using the browser’s preference settings. E.g., in Chrome, go to “Settings”, click on “Show advanced settings...”, scroll down to “Languages”, click on “Language and input settings...”, and then add languages and drag to order them.

This is not something people do often, and most people have no idea of the existence of such settings. So what the browser sends is usually some factory defaults. They can be rather strange, e.g. Accept-Language: fr-CA would mean, by the protocol, “if you, dear server, have different language versions of the resource I’m requesting for, please give me Canadian French; if you don’t have it, I don’t care which version you give”. This means that if the resource exists in generic French (of French French), or in English, these would have no preference over, say, Swahili or Volapük version. So a more sensible header would normally be Accept-Language: fr-CA, fr, en, and this is in practice how you should deal with Accept-Language: fr-CA.

like image 64
Jukka K. Korpela Avatar answered Oct 21 '22 11:10

Jukka K. Korpela