You are here

ip_language_negotiation.module in IP Language Negotiation 7

Same filename and directory in other branches
  1. 8 ip_language_negotiation.module

Main module file for the IP Language Negotiation.

File

ip_language_negotiation.module
View source
<?php

/**
 * @file
 * Main module file for the IP Language Negotiation.
 */

/**
 * Implements hook_language_negotiation_info().
 */
function ip_language_negotiation_language_negotiation_info() {
  return array(
    'ip_language_negotiation' => array(
      'callbacks' => array(
        'language' => 'ip_language_negotiation_from_ip',
      ),
      'name' => t('Country'),
      'description' => t("IP-based Determination of a Visitor's Country"),
      'weight' => -1,
      'file' => drupal_get_path('module', 'ip_language_negotiation') . '/ip_language_negotiation.module',
      'config' => 'admin/config/regional/language/configure/ip',
    ),
  );
}

/**
 * Implements hook_menu().
 */
function ip_language_negotiation_menu() {
  $items['admin/config/regional/language/configure/ip'] = array(
    'title' => 'IP Country detection configuration',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ip_language_negotiation_form',
    ),
    'access arguments' => array(
      'administer languages',
    ),
    'file' => 'ip_language_negotiation.admin.inc',
    'type' => MENU_VISIBLE_IN_BREADCRUMB,
  );
  return $items;
}

/**
 * Implements hook_form_alter().
 */
function ip_language_negotiation_form_system_performance_settings_alter(&$form, &$form_state) {
  $form['caching']['cache']['#description'] = t('Please note: page caching is disabled on the front page by the IP Language Negotiation module.');
}

/**
 * Callback function for ip_language_negotiation_language_negotiation_info().
 */
function ip_language_negotiation_from_ip($languages) {

  // Disable caching for this page. This only happens when negotiating
  // based on IP. Once the redirect took place to the correct domain
  // or language prefix, this function is not reached anymore and
  // caching works as expected.
  drupal_page_is_cacheable(FALSE);
  $current_ip = ip_address();
  $language_settings = variable_get('ip_language_negotiation_countries', array());
  drupal_load('module', 'ip2country');

  // Check for debug settings. If enabled, use it.
  if (variable_get('ip2country_debug', 0) == 1) {
    if (variable_get('ip2country_test_type', 0) == 0) {

      // Debug Country entered.
      $country_code = variable_get('ip2country_test_country', 'US');
    }
    else {

      // Debug IP entered.
      $ip = variable_get('ip2country_test_ip_address', $current_ip);
      $country_code = ip2country_get_country($ip);
    }
  }
  else {

    // Check if the country code can be determined by the IP.
    $country_code = ip2country_get_country($current_ip);
  }
  if (!empty($country_code)) {

    // Check if a language is set for the determined country.
    if (!empty($language_settings[$country_code])) {
      return $language_settings[$country_code];
    }
  }
}