Index: ip2locale.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/ip2locale/ip2locale.module,v
retrieving revision 1.1.2.19
diff -u -p -r1.1.2.19 ip2locale.module
--- ip2locale.module	7 May 2010 12:19:02 -0000	1.1.2.19
+++ ip2locale.module	17 Nov 2010 12:51:09 -0000
@@ -15,6 +15,7 @@
 
 define('IP2LOCALE_FIRST_REQUEST_ONLY', 1);
 define('IP2LOCALE_ALL_REQUESTS', 2);
+define('IP2LOCALE_SWITCHER_CALLBACK', 'ip2locale/switcher');
 
 /**
  * Implementation of hook_help().
@@ -83,6 +84,13 @@ function ip2locale_menu() {
     'access arguments' => array('administer site configuration'),
     'type' => MENU_CALLBACK,
   );
+  $items[IP2LOCALE_SWITCHER_CALLBACK.'/%'] = array(
+    'title' => 'IP to Locale language switcher override',
+    'page callback' => 'ip2locale_switcher_callback',
+    'page arguments' => array(2),
+    'access arguments' => array('access content'),
+    'type' => MENU_CALLBACK,
+  );
   return $items;
 }
 
@@ -151,9 +159,14 @@ function ip2locale_boot() {
     return FALSE;
   }
 
+  // Identify ip2locale language switcher callback.
+  if (preg_match('/^\/'.preg_replace('/\//', '\/', IP2LOCALE_SWITCHER_CALLBACK).'/', request_uri())) {
+    return FALSE;
+  }
+
   // load in the code required to deal with paths properly.
   drupal_bootstrap(DRUPAL_BOOTSTRAP_PATH); 
-  
+
   // Should we attempt to perform a redirect on this path?
   $page_match = ip2locale_match();
   if (!$page_match) return FALSE;
@@ -182,10 +195,10 @@ function ip2locale_boot() {
       $lc = $_SESSION['ip2locale_lc'] = ip2locale_get_locale($ip);
     }
     else {
+      $lc = $_SESSION['ip2locale_lc'];
       if (variable_get('ip2locale_debug', FALSE)) {
         drupal_set_message("IP to Locale debug: Got language code '". $lc ."' from users session.");
       }
-      $lc = $_SESSION['ip2locale_lc'];
     }
   }
 
@@ -507,3 +520,40 @@ function ip2locale_ip_address() {
   }
   return $ip;
 }
+
+/**
+ * Implementation of hook_translation_link_alter().
+ */
+function ip2locale_translation_link_alter(&$links, $path) {
+  // Alter links in the language switcher only.
+  if (count($links)) {
+    if ($first_link = array_values(array_slice($links, 0, 1))) {
+      if ($first_link[0]['attributes']['class'] == 'language-link') {
+        foreach($links as $key => &$link) {
+          $link['query'] = array(
+            'destination' => ltrim(url($link['href'], array('language' => $link['language'])), '/'),
+          );
+          $link['href'] = IP2LOCALE_SWITCHER_CALLBACK.'/'.$key;
+          unset($link['language']->prefix);
+        }
+      }
+    }
+  }
+}
+
+/**
+ * Menu callback for intercepting the language switcher and updating session.
+ */
+function ip2locale_switcher_callback($lang) {
+  // Set session variable to prevent drupal_goto() being intercepted again.
+  $_SESSION['ip2locale_lc_processed'] = TRUE;
+
+  // Override session variable.
+  if ($lang) {
+    $_SESSION['ip2locale_lc'] = $lang;
+    if (variable_get('ip2locale_debug', FALSE)) {
+      drupal_set_message("IP to Locale debug: Got language code '". $lang ."' from langage switcher.");
+    }
+  }
+  drupal_goto();
+}
