最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Most effective way to auto detectselect language in a website? - Stack Overflow

programmeradmin3浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论