views:

70

answers:

4

Hello,

I need to implement an autodetect feature for a simple, plain html website I am working on. It has two languages and the client wants it to automatically select the language. This could be done either via the browser's language or geolocalization, both options are good.

Can someone point me to a good script or solution to do this? ive been searching for a while and cant seem to find anything...

Thank you

+3  A: 

Use the Accept-Language HTTP header.

Sjoerd
hmmm, sounds good, but how would one go about such a thing...?
0al0
http://stackoverflow.com/questions/2352205/how-to-redirect-users-based-on-browser-language/2352250#2352250
Sinan
+1  A: 

You could use the UserAgent of the Browser.

I wouldn't use geo-localization...I'm in Austria but I prefer websites in english, that's why I changed my UserAgent to resemble that, and most websites stick to it.

Bobby
Not to mention tourists, travellers and immigrants. And regions where two or more languages are spoken.
Álvaro G. Vicario
A: 

in javascript, language info is stored in the global navigator object (navigator.language in FF and navigator.browserLanguage/systemLanguage in MSIE).

stereofrog
A: 

The Accept-Language HTTP header is usually the best way, but is only available on the server. Since your using a plain (I read static) html page and JavaScript, you'll need a different approach.

Different browsers expose language information in different ways:

document.navigator.language; // firefox
document.navigator.browserLanguage;  // IE

I'm not sure what other browsers do. I think the values will be things like "en" or "en-us", so you should experiment. For the browsers you target, set the language setting to the ones your interested in, and see what the above values pop out of the above expressions.

olooney