admin管理员组

文章数量:1327684

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

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

Share Improve this question asked Mar 10, 2010 at 9:24 agente_secretoagente_secreto 8,07916 gold badges60 silver badges83 bronze badges 1
  • Don't use Geolocalization, never! 1) You travel to another country, that does not makes you speak that language. 2) In countries with multiple languages (India, Canada, Switzerland...), this is not going to work. 3) VPNs 4) Complex tracking and data-protection issues. – Adrian Maire Commented Jan 28 at 14:17
Add a ment  | 

4 Answers 4

Reset to default 4

Use the Accept-Language HTTP header.

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.

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.

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

本文标签: javascriptMost effective way to auto detectselect language in a websiteStack Overflow