You are here

function geoip_language_negotiation in GeoIP API 7

Same name and namespace in other branches
  1. 6 geoip_language/geoip_language.module \geoip_language_negotiation()

Identify language via URL prefix or domain excluding administrative paths. Try to keep as light weight as possible

Return value

string Language as ISO 639-1 Code

1 call to geoip_language_negotiation()
geoip_language_init in geoip_language/geoip_language.module
Implements hook_language_negotiation_info().

File

geoip_language/geoip_language.module, line 81
Language negotiation based on GeoIP detection.

Code

function geoip_language_negotiation() {

  // Get a list of enabled languages.
  $languages = language_list('enabled');
  $languages = $languages[1];

  // Get the original q, as it is before path un-aliasing.  This is important
  // because a path alias which is the same as a language prefix will have been
  // improperly un-aliased and the language prefix will be lost.
  $query = array();
  parse_str($_SERVER['QUERY_STRING'], $query);
  $q = $query['q'];

  // Mostly copied from language_initialize(). Do a basic path prefix lookup to
  // determine the language.
  $args = explode('/', $q);
  $prefix = array_shift($args);

  // Search prefix within enabled languages.
  foreach ($languages as $language) {
    if (!empty($language->prefix) && $language->prefix == $prefix) {

      // Set the global language object.
      $GLOBALS['language'] = $language;

      // Rebuild $_GET['q'] with the language removed.
      $_GET['q'] = implode('/', $args);

      // Re-initialize the path.
      drupal_init_path();

      // Make PressFlow happy by only storing the language code if there's an
      // existing session.
      if (count($_SESSION)) {
        $_SESSION['geoip_language'] = $language->language;
      }
      return;
    }
  }

  // Begin fallbacks. At this point, we know that there is not a valid path
  // prefix, so we must first determine a language, and then do a redirect to
  // the current path, in that language.
  global $user;

  // First check to see if a language was previously determined.
  if (isset($_SESSION['geoip_language']) && isset($languages[$_SESSION['geoip_language']])) {
    $language = $languages[$_SESSION['geoip_language']];
  }
  elseif ($user->uid && isset($languages[$user->language])) {
    $language = $languages[$user->language];
  }
  else {
    $language = geoip_language_detect_language();
  }

  // Set the global language object.
  $GLOBALS['language'] = $language;
  global $base_path;

  // Abort the redirect if bootstrap happened outside of index.php or if the
  // requested path is inside the files directory
  if ($_SERVER['SCRIPT_NAME'] != $base_path . 'index.php' || strpos($_GET['q'], file_directory_path()) === 0 || $_SERVER['REQUEST_METHOD'] == 'POST') {
    return;
  }

  // Now that the language is detected, do an absolute redirect to avoid page
  // caching in the wrong language.
  $url = url($_GET['q'], array(
    'language' => $language,
    'absolute' => TRUE,
    'query' => drupal_query_string_encode($_GET, array(
      'q',
    )),
  ));
  drupal_goto($url, NULL, NULL, 301);
  exit;
}