Language Switcher Based On Ip Location

If you have a website with multi language and you want to switch website language based on visitor ip location, below is the tips to make language switcher based on visitor ip location.

1. You need to install language switcher wordpress plugin to your site. Follow the instruction on http://www.poplarware.com/languageplugin.html

2. Read about sample script to change URL based on IP on http://www.ip2nation.com/ip2nation/Sample_Scripts/Country_Based_Redirect

3. Read about FAQ in http://www.poplarware.com/languageplugin_faq.html#faq. Please notice on number 21. I copy paste below. Remember the position of “// use default language if user did not set” from source code langswitch.php because you will edit the source code on that position.

I can program a little in PHP, and I want to know if there is a way I can do something in PHP that detects what language the site viewer has chosen in the Language Switcher.
Yes, you can. There is a global variable in the Language Switcher called $langswitch_lang_pref, which gets set to the 2-letter language code currently in effect. So you can do something like this:
global $langswitch_lang_pref;
if( $langswitch_lang_pref == ‘ru’ ) {
// do something if the language is Russian
} else {
// do something different if the language is not Russian
}

4. Download data to map IP to country from http://www.ip2nation.com/ip2nation/Download then add it to your database.

5. Test the script from number 2 above on your server.

6. Join the knowledge you get from number 2 and 3 above. This is the important step. Open the langswitch.php you get from number 1. Then edit the source code. Below is an example after you edit it:

// use default language if user did not set

//this line is commented to make language switcher based on ip location to work — if( !$langswitch_lang_pref ) {
//this line is commented to make language switcher based on ip location to work — $langswitch_lang_pref = get_option( $langSwitchDefLangOpt );

// The lines below added by plugie to make language switcher based on ip location to work
$server = ‘localhost’; // MySQL hostname
$username = ‘languangeswitch’; // MySQL username
$password = ‘thisisapassword’; // MySQL password
$dbname = ‘db_languangeswitch’; // MySQL db name

$db = mysql_connect($server, $username, $password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());

$sql = ‘SELECT
country
FROM
ip2nation
WHERE
ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") ORDER BY ip DESC LIMIT 0,1'; list($country) = mysql_fetch_row(mysql_query($sql)); switch ($country) { case 'id': $langswitch_lang_pref='id'; break; default: $langswitch_lang_pref='en'; break; } //============================ //this line is commented to make language switcher based on ip location to work -- }

7. Try to browse your site from any of country using anonymous proxy. You can try http://www.hrmovie.com

8. After you have installed it successfully, you can remove language switcher plugin from your sidebar. So customer can’t choose language they preferred. Your site just give the language based on visitor ip location.

The sample of multi language site that use Language switcher can be found on http://syrup.plugie.com. That website using two language: indonesian and english. If the ip visitor comes from indonesia the website will give indonesian languange. If comes from other countries besides Indonesia the website will give english language

Good luck and tell me your experience.

Be Sociable, Share!
CategoriesIT

2 Replies to “Language Switcher Based On Ip Location”

  1. Interesting post. But lets say I want to have an auction site that detects the IP, shows that countries language and only displays “items” being auction in that specific country ?

Leave a Reply

Your email address will not be published. Required fields are marked *