I have a client who wants IP detection but also wants the language switcher to be available. The action of the language switcher appears to be overridden by ip2locale because when ip2locale detects the language it is stored in a session variable. If the user clicks the language switcher, ip2locale reads the session variable and redirects to the language-specific path dictated by the session variable's value, not the path of the switcher link.

I have built a patch which allows the language switcher to override the session variable setting if the user clicks a specific language. It does this by introducing a menu callback and altering the language switcher links to reference this callback path, passing along the actual page destination (which should be the page the user currently sees, but for the different language they want to browse). The callback takes a language code argument and writes this code into the ip2locale session variable before performing a drupal_goto() to redirect to the required translation of the current page.

If the language switcher block is not active, there should be no change in the way ip2locale behaves.

I have included a debug message to show when the language switcher override is performed.

I have also fixed a small bug whereby an existing debug message was output before the setting of a variable it needed.

CommentFileSizeAuthor
language-switcher-ip2locale.patch3.13 KBsp3boy
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alexku’s picture

The patch works. Thank you.

However following code will not work with http://drupal.org/project/lang_dropdown module, because it doesn't set the class attribute

    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);
        }
      }
    }