I want to find my player’s location (or at least their country) I guess from their ip address, but how do I go about doing this. My mission is to automate language selection to some degree.
Thanks
I want to find my player’s location (or at least their country) I guess from their ip address, but how do I go about doing this. My mission is to automate language selection to some degree.
Thanks
If your program is on a mobile device you could use the GPS to find out this information.
If it’s on PC, then you could retrieve the systems date and time, and then compare it with a service such as the NIST Internet Time Service to find out what time zone they are in.
Although for other devices, there are API’s designed for this intention, you can use a web service to download the raw source to the HTML page returned when querying an IP. One such example is whatismyipaddress.com. GPS coordinates cost a number of phones a service charge to keep online, and re-downloading the weblink again and again can be taxing over time. Grab it once, store it in hash, and just match the current ip with the saved one for changes before re-grabbing information. Since you’re looking for language automation, you won’t even need to bother. Just grab it once on first-boot, and leave it up to the user to correct it.
I found this in some of the answers. This was a resolved question. And they asked the same thing you did basically. Hope it helps.
As you’ve identified, step 1 is to get the user’s IP, which you can get as follows:
var hostName = System.Net.Dns.GetHostName();
var hostEntry = System.Net.Dns.GetHostEntry(hostName);
var addressList = hostEntry.AddressList;
var ipAddress = addressList[addressList.Length-1].ToString()
Then you need to lookup that IP address in a geocoded database. There are various webservices that offer this facility, such as http://freegeoip.net/
This is a far from perfect way of determining the user’s location, but assuming you offer them the facility to override the automatically chosen language, it should serve as a start.
No need to know location, if language is your primary concern.
On standalone you have regional settings in Windows, and I guess some similar settings in Mac/Linux. On mobiles, you can select language chosen by user. In web browsers, you can use navigator.userLanguage (you have to call it in context of web page, not in Unity).
In your case regional settings is a best option, but as far as I know, there was some bug in retrieving this in Mono. Please read this question and answer for additional info and workaround.