PDA

View Full Version : Detect country and set language? Javascript?



heato112
December 30th, 2009, 02:17 PM
Hello,

I wanted to set the language of my flash website based on the users location country wise. I know there are some methods with php and searching a database but I am trying to avoid that route.

I found one script in javascript that links to a webpage and produces a document.write if you go to that link. It returns the users country i believe. but there seems to be no documentation.

<!--webbot bot="HTMLMarkup" startspan -->
<script language="Javascript" src="http://map.geoup.com/geoup?template=CountryName">
</script>
<!--webbot bot="HTMLMarkup" endspan -->
Is it possible for me to capture that input from the link and pass it in as a flashvar? I am using as3 and I have no clue how to pass that through. If anyone can provide assistance that would be awesome.

theCodeBot
December 31st, 2009, 09:55 AM
This (http://www.justin-cook.com/wp/2006/11/29/determine-a-visitors-location-by-their-ip-address/) may be of use to you. It details how you could use PHP, ASP, or .NET to learn the user's IP address, and then do some math and search a simple CSV file to learn which country they are in.

Using this, you could have PHP ONLY get the IP address of the user, and then have your javascript do the parsing and pass a flashVar or use ExternalInterface, or even have your ActionScript directly ask for the IP address from the server and parse the CSV itself, skipping ExternalInterface entirely.

However, there seem to be two problems with this CSV: One, it's a 4.9MB file, which you do NOT want to parse real-time every single page load, and as such, you would be better off translating it into a database table and having PHP search that, but problem two is that this CSV does not have languages, only country names and codes.

Now, if you only support a few languages, for example English, Swedish, and Russian like my last project, you could easily only take the records from this CSV that matter so you have a much smaller file. Then you are only loading a kilobyte or two. If an IP address visits that is not in your shrunken CSV, then you can default them to English or bring them to the language selection screen.

EDIT: You could also have flash just query This page (http://www.ipgp.net/) via PHP, and then just keep a list of country names in your ActionScript to match country with language. I need to look past the first three results on google more often :)

nithan
January 14th, 2011, 04:56 PM
Use the following code

<div id="DivContent">
<!--webbot bot="HTMLMarkup" startspan -->
<script language="Javascript" src="http://map.geoup.com/geoup?template=CountryName">
</script>
</div>
<!--webbot bot="HTMLMarkup" endspan -->
<script type="text/javascript">
function getHTML(){
alert(document.getElementById("DivContent").innerHTML);
}
</script>
<input name="button" type="button" id="button" onClick="getHTML()" value="Button">

Swooter
January 15th, 2011, 10:24 AM
What about using the built-in language detection?

import flash.system.Capabilities;
var isEnglish:Boolean = (Capabilities.language == "en");

trace(isEnglish);

baseter
April 20th, 2011, 05:23 AM
This (http://www.justin-cook.com/wp/2006/11/29/determine-a-visitors-location-by-their-ip-address/) may be of use to you. It details how you could use PHP, ASP, or .NET to learn the user's IP address, and then do some math and search a simple CSV file to learn which country they are in.

Using this, you could have PHP ONLY get the IP address of the user, and then have your javascript do the parsing and pass a flashVar or use ExternalInterface, or even have your ActionScript directly ask for the IP address from the server and parse the CSV itself, skipping ExternalInterface entirely.

However, there seem to be two problems with this CSV: One, it's a 4.9MB file, which you do NOT want to parse real-time every single page load, and as such, you would be better off translating it into a database table and having PHP search that, but problem two is that this CSV does not have languages, only country names and codes.

Now, if you only support a few languages, for example English, Swedish, and Russian like my last project, you could easily only take the records from this CSV that matter so you have a much smaller file. Then you are only loading a kilobyte or two. If an IP address visits that is not in your shrunken CSV, then you can default them to English or bring them to the language selection screen.

EDIT: You could also have flash just query This page (http://www.ipgp.net/) via PHP, and then just keep a list of country names in your ActionScript to match country with language. I need to look past the first three results on google more often :)

Querying the whole page with results, including the html code and all other scripts will cause a high loat both to your app and the website servers. Instead you can look on that page and find info about the API provided so you can only query a small xml file.