Community

Redirect user to regional language site version by knowing the ip of users

Hello,
We are basically developing a Drupal 7 site with multiple language support.
The task is that, user should be redirected to regional translated pages from the regional IP fetched.
For instance, www.fr.domainname.com/page1/ OR www.domainname.com/fr/page1/

I have followed the guide's,
Internalization:

IP to Locale:

But didn't succeed. Also didnt found any guide for the same.

Please guide us in this situation.

Thanks . . .

Comments

Possible using http://ipinfodb.com/ APIS.

I have added a module for the same... just adding the code. With little modifications it can be configured for any no of languages and options.

1. I am using APIs from http://ipinfodb.com/.
2. ip2locationlite.class.php -> file can be downloaded from this link "http://ipinfodb.com/ip_location_api.php". The file consists of a very simple class, just used for calling webservice provided by http://ipinfodb.com/ and can be avoided by using your own class with file_get_contents.

3. I used for only chinese.

  <?php

/** Author : Sunny Jhunjhunwala (sunny.jhunjhunwala@sourcen.com)
* @file - the file basically uses the php api provided by ipinfodb to get user's country
*/

/**
* Implements hook_init().
* Only for the first time checks if user is from china or not, if yes then redirect user to chinese version of the site.
*/
function dw_ip2location_init() {
   
  if (!(array_key_exists('visited', $_COOKIE)) && empty($GLOBALS['user']->uid)) {
    // Set the cookie to check if the user is new or existing.
    setcookie("visited", 1, time() + (10 * 365 * 24 * 60 * 60));
    include_once ('ip2locationlite.class.php');
    $ipLite = new ip2location_lite;
    $ipLite->setKey('a00740f71ae6ed8db250cb4c3a8ecdac672b5eae4d2a28c06284aa5461d0636d');
    // @TODO : remove the below if-else while moving to production.
    if (isset($_GET['ip2location'])) {
      $locations = $ipLite->getCountry($_GET['ip2location']);
    } else {
      $locations = $ipLite->getCountry($_SERVER['REMOTE_ADDR']);
    }
    if (!empty($locations) && is_array($locations)) {
      // Checking if the user if from china and redirecting.
      if ($locations['countryCode'] == 'CN' || $locations['countryName'] == 'china') {       
        $path = $_GET['q']; 
        $translations = i18n_get_path_translations($path); 
        $language = i18n_language_interface(); 
        if($language->prefix != 'cn' ) {
          global $base_url;
          if(!drupal_is_front_page() && array_key_exists('zh-hans', $translations)) {
            drupal_goto( $base_url . '/cn/' . $translations['zh-hans']['href'], array('absolute' => true));
          }
          else {
            drupal_goto( $base_url . '/cn/', array('absolute' => true));
          }         
        }
      }
    }
  }
}

Thanks Sunny, Would

Thanks Sunny,
Would definitely check it out.
:)