How to get user region/country information in web-applicaiton?
My main purpose is to show different page to different country(language) people. I think it should be based on the user region/country, which I guess can fetch out from ip address?
example: *For EN country people, they would see this. www.exam开发者_如何学Gople.com
*For Japanese, they would dispatch to this. www.example.com/jp
Also I saw some people said to accomplish these purpose, the best way is based on the web-browser language, which can get from http header. I don't know which is better, what is the common way and why..?
Thanks for your advices!
I'am using Flex and Java.
Zolo, you can use something like the geoip database from maxmind to easily find region/country/province from IP addresses, but it has a few drawbacks:
- VPNs and caches can yield false values
- User's can't easily override it.
Going with the browser's Accept: headers is generally preferred because a Spanish speaking person in the US can set their language settings to get 'es' instead of 'en' even if their IP would get them 'en'.
When I've had to do this in the past I ended up with an ordered preference list within the language selection logic, with each level overriding the subsequent ones if present:
- Expressed user preference by slicking a flag icon on site, stored in session or cookie
- The TLD of the domain, with .com signifying nothing (ex: mycompany.jp gets you Japanese site, but mycomany.com is a "no vote" and passes through to the next level
- Browser language setting from the accept header
- GeoIP best guess
- Fallback to English
The goal, of course being to base the selection on things that most accurately reflect the preferences of the user.
IP2Location offers several different ways to get this information (they have the data necessary to do an IP address lookup). This includes a web service which costs $49 per year, or you can buy the data directly from them for the same price.
Figuring out the language of choice for a user of a web application is...
tricky business !
See this related SO Question or also this one.
Another useful hint is to use the keyword GeoLocation
, although finding the location and the language of choice are two related but distinct problems.
For example if geolocation indicates downtown Geneva, French might be the language of choice, but, German, English and several other languages are almost as probable.
I got the following URLs from the SO questions afore mentioned:
Free IP geolocation services
- IPGeo: www.ipgeo.com
- MaxMind GeoIP:http://www.maxmind.com/
Commercial IP geolocation services
- Quova: http://quova.com/
- NetAcuity by Digital Element/Envoy: http://www.digital-element.com/
For language identification (rather than location), it is convenient and typically more accurate to use the HTTP Header's "accept-language" value, but that too can be misleading. Although someone may have a Japanese version of the browser installed he/she may prefer reading in English...
No matter what language you offer the users, by default, be sure to allow them to easily switch to another version of the site (and if possible, to remember this choice for when they return to your site)
精彩评论