Search code examples
httpgeolocationhttp-accept-language

Accuracy of accept_language to detect country


I need to detect the country of users but I'm trying to avoid the whole call to an external service to get the location based on IP or to an internal database. I need to make this service really responsive and it takes almost half a second to query any external service so I'm trying to avoid that. Also any call to a db would be costly for this approach. I need the page to respond in less than 20ms.

I'm thinking of get the country locale based on Accept_language header.

My question is. Does anyone know how accurate may this be? I'm aware country is not always present or users may change default language or be present on a different country with their computers.

Does anyone had previous experience or is there any documentation (I couldn't find one googling or in w3 page) that specifies a percent of accuracy or error using this approach?


Solution

    • Detecting the users country by it language settings is just a bad and stupid idea. I am from Germany and I know a lot of people who use their browser/mobile phone or OS in an other language then German. There are a few delivery services I know which check for the language transmitted by the browser and do not work if it isn't German. Guess where I order never again.
    • Simple detect the language by their IP. There a few (even free) services available which would convert IP into a country.
      For example: http://www.maxmind.com/app/geolite. They do have a web API, a server you could install or just grab the whole database and implement it on your own. Also, there are plenty of modules/helpers for various web servers, which won't require any extensive coding. (See: http://www.maxmind.com/app/mod_geoip for Apache, this embeds the country of the visitor as a HTTP Header.)
      The DB results are cached and should be fast enough for you needs. 20ms is very fast, if you think about all the time is wasted for the packages to get to your server and then whole back ;-)