Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Website that recognizes user's location/IP & changes lang. based on that

title is pretty clear.

My websites consists of both English-written and Spanish-written versions. You can go to the main site, which is in Spanish, by clicking http://www.chrishonn.com and to the translated version, which is in English, at http://en.chrishonn.com. At the index of each page there is a link (at the bottom) which allow the user to pass from one site to the other.

However, I was wondering, how do big sites like Google, Yahoo!, and other brands' websites to recognize the user's geographical location/IP so that - depending on that - the site's language is adapted (i.e. you are from China and you visit www.google.com, you'll be redirected to www.google.cn).


I have stated on every single page of my website the language:

<meta http-equiv="content-language" content="en">

That example is of course from one of the http://en.chrishonn.com sites, which are in English.

I hope someone can give me a hand. Thank you (if I missed something, please let me know).

like image 609
cr0z3r Avatar asked Jan 10 '10 22:01

cr0z3r


People also ask

Can you find an exact location using an IP address?

IP addresses do reveal your geolocation, but not your precise location like a home address does. IP addresses will also never reveal your name, phone number, or other precise personal information.

Can website detect location?

Your IP address shares the location of your network with websites and other online entities. In other words, if someone has your IP address, they can find out your location as well.

How can I trace IP address location?

Type “ping” followed by the URL of the website to get its IP. The “tracert” command lets you see what locations your data is going through. Websites like What Is My IP Address let you search for the approximate location of any IP address, so you can trace an IP address free.


2 Answers

Rather than detecting the location of IP address (often unreliable due to NAT and proxying) you could check the default language the browser is set to. There are JQuery plugins to support this, such as http://keith-wood.name/localisation.html, or use server-side code to read the HTTP request header "HTTP_ACCEPT_LANGUAGE" to determine if you want to show the ES or EN site.

like image 147
AUSteve Avatar answered Oct 22 '22 08:10

AUSteve


As for Google, your location is determined from your IP address. A request to google.com from outside the US returns an HTTP/1.1 302 Found which redirects you to your country specific domain.

As also discussed on another post, doing these kinds of redirects can make SEO tricky and complicated. I suggest reading Matt Cutt's article (a Google software engineer) on how Google handles the 302 Redirect: SEO advice: discussing 302 redirects.

Different search engines handle the 302 redirect in a different way. With 302 redirects, you may risk having your original domain ignored by search engines.


If you want to determine your users' location from their IP address, there are many off-the-shelf services which basically map most of the IP ranges to countries. You may want to check:

  • MaxMind GeoLite Country
  • IP2Location.com

Another popular technique is to parse the Accept-Language HTTP header, which contains information about the user's language preferences. Mainstream browsers allow these language preferences to be modified by the user. You may read more about this technique from:

  • Parse Accept-Language to detect a user's language
  • W3C - Accept-Language used for locale setting

like image 43
Daniel Vassallo Avatar answered Oct 22 '22 08:10

Daniel Vassallo