Reputation: 51
I need to get language of a user who have visiting my website by their IP address using PHP. how can I do it, there is any API to do this, please advise
Upvotes: 4
Views: 7522
Reputation: 71
Use the GeoIP module as has previously been suggested and then insert this code somewhere in your project:
if($country = geoip_country_code_by_name($host))
{
switch($country)
{
case "DJ":
case "ER":
case "ET":
$lang = "aa";
break;
case "AE":
case "BH":
case "DZ":
case "EG":
case "IQ":
case "JO":
case "KW":
case "LB":
case "LY":
case "MA":
case "OM":
case "QA":
case "SA":
case "SD":
case "SY":
case "TN":
case "YE":
$lang = "ar";
break;
case "AZ":
$lang = "az";
break;
case "BY":
$lang = "be";
break;
case "BG":
$lang = "bg";
break;
case "BD":
$lang = "bn";
break;
case "BA":
$lang = "bs";
break;
case "CZ":
$lang = "cs";
break;
case "DK":
$lang = "da";
break;
case "AT":
case "CH":
case "DE":
case "LU":
$lang = "de";
break;
case "MV":
$lang = "dv";
break;
case "BT":
$lang = "dz";
break;
case "GR":
$lang = "el";
break;
case "AG":
case "AI":
case "AQ":
case "AS":
case "AU":
case "BB":
case "BW":
case "CA":
case "GB":
case "IE":
case "KE":
case "NG":
case "NZ":
case "PH":
case "SG":
case "US":
case "ZA":
case "ZM":
case "ZW":
$lang = "en";
break;
case "AD":
case "AR":
case "BO":
case "CL":
case "CO":
case "CR":
case "CU":
case "DO":
case "EC":
case "ES":
case "GT":
case "HN":
case "MX":
case "NI":
case "PA":
case "PE":
case "PR":
case "PY":
case "SV":
case "UY":
case "VE":
$lang = "es";
break;
case "EE":
$lang = "et";
break;
case "IR":
$lang = "fa";
break;
case "FI":
$lang = "fi";
break;
case "FO":
$lang = "fo";
break;
case "BE":
case "FR":
case "SN":
$lang = "fr";
break;
case "IL":
$lang = "he";
break;
case "IN":
$lang = "hi";
break;
case "HR":
$lang = "hr";
break;
case "HT":
$lang = "ht";
break;
case "HU":
$lang = "hu";
break;
case "AM":
$lang = "hy";
break;
case "ID":
$lang = "id";
break;
case "IS":
$lang = "is";
break;
case "IT":
$lang = "it";
break;
case "JP":
$lang = "ja";
break;
case "GE":
$lang = "ka";
break;
case "KZ":
$lang = "kk";
break;
case "GL":
$lang = "kl";
break;
case "KH":
$lang = "km";
break;
case "KR":
$lang = "ko";
break;
case "KG":
$lang = "ky";
break;
case "UG":
$lang = "lg";
break;
case "LA":
$lang = "lo";
break;
case "LT":
$lang = "lt";
break;
case "LV":
$lang = "lv";
break;
case "MG":
$lang = "mg";
break;
case "MK":
$lang = "mk";
break;
case "MN":
$lang = "mn";
break;
case "MY":
$lang = "ms";
break;
case "MT":
$lang = "mt";
break;
case "MM":
$lang = "my";
break;
case "NP":
$lang = "ne";
break;
case "AW":
case "NL":
$lang = "nl";
break;
case "NO":
$lang = "no";
break;
case "PL":
$lang = "pl";
break;
case "AF":
$lang = "ps";
break;
case "AO":
case "BR":
case "PT":
$lang = "pt";
break;
case "RO":
$lang = "ro";
break;
case "RU":
case "UA":
$lang = "ru";
break;
case "RW":
$lang = "rw";
break;
case "AX":
$lang = "se";
break;
case "SK":
$lang = "sk";
break;
case "SI":
$lang = "sl";
break;
case "SO":
$lang = "so";
break;
case "AL":
$lang = "sq";
break;
case "ME":
case "RS":
$lang = "sr";
break;
case "SE":
$lang = "sv";
break;
case "TZ":
$lang = "sw";
break;
case "LK":
$lang = "ta";
break;
case "TJ":
$lang = "tg";
break;
case "TH":
$lang = "th";
break;
case "TM":
$lang = "tk";
break;
case "CY":
case "TR":
$lang = "tr";
break;
case "PK":
$lang = "ur";
break;
case "UZ":
$lang = "uz";
break;
case "VN":
$lang = "vi";
break;
case "CN":
case "HK":
case "TW":
$lang = "zh";
break;
default:break;
}
}
Upvotes: 5
Reputation: 17573
While you can do a lookup on an IP address to get an idea of the general physical location, the physical location may not have much to do with the language of the person whose IP address you are looking up.
For instance while the IP location may be New York area of the United States the person may speak Mandarin as their primary language because they are visiting the area.
Here is a service that you could use for IP address lookup http://ipinfodb.com/ip_location_api.php
Here is a stackoverflow discussion Get user location by IP address in C# that might be helpful as well.
However you will need to provide a mechanism for allowing the language choice to change. And use cookies or something similar to remember the language choice.
The most common use of location information is to provide targeted advertising which is more likely to be of use to the person at that location.
EDIT: Use of mobile devices
Since mobile devices and smart phones accessing the internet over high-speed 3G and 4G cellular networks are becoming increasing common, a question is whether IP address geolocation works for those types of devices.
The short answer is, not very well. See this article, Where's that Phone?: Geolocating IP Addresses on 3G Networks from Microsoft Research.
Also, here is a stackoverflow question on mobile phone location from a HTTP request.
Here is an article, Geolocating IP addressesin Cellular Data Networks that provides some information on the problem as well.
Upvotes: 1
Reputation: 2719
You can use any geoIP module. It allow you detect country by IP. But this is not very correct way. For example I now in Thailand (and I have thai IP), but my language is russian :)
This is not very good when google show me page in thai language.
For detect language you can use Headers from browser. Preferred languages are listed in this Headers.
Upvotes: 4