You are here

function geoip_language_mappings in GeoIP API 7

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

API function to return the country-language mapping.

TODO: Serializing to a text file is insane. This should go into the cache table instead.

Return value

array Associative array key=countrycode, value= Language as ISO 639-1 Code

7 calls to geoip_language_mappings()
geoipLanguageTestCase::testMappingCrud in geoip_language/geoip_language.test
geoip_language_detect_language in geoip_language/geoip_language.module
Return the language object mapped to the current GeoIP detected country.
geoip_language_flush_caches in geoip_language/geoip_language.module
Implements hook_flush_caches().
geoip_language_mapping_create in geoip_language/geoip_language.module
API function to create a new mapping.
geoip_language_mapping_delete in geoip_language/geoip_language.module
API function to delete a mapping.

... See full list

File

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

Code

function geoip_language_mappings($reset = FALSE) {
  static $mapping = NULL;
  if ($reset || !isset($mapping)) {
    $file = file_directory_path() . '/geoip_language.txt';
    if (file_exists($file)) {
      $data = unserialize(file_get_contents($file));
      $mapping = isset($data['geoip']) ? $data['geoip'] : NULL;
    }

    // Build the mapping array and cache it to the filesystem.
    if ($reset || !$mapping) {
      $mapping = array();
      $data = array(
        'geoip' => array(),
        'default' => language_default('prefix'),
      );
      $result = db_query('SELECT g.*, l.prefix FROM {geoip_language} g INNER JOIN {languages} l on g.language = l.language ORDER BY g.country ASC');
      while ($row = db_fetch_object($result)) {
        $mapping[$row->country] = $row->language;
        $data['geoip'][$row->country] = $row;
      }

      // Add default
      $file = file_save_data(serialize($data), 'geoip_language.txt', FILE_EXISTS_REPLACE);
    }
  }
  return $mapping;
}