Index: country_code.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/country_code/country_code.install,v
retrieving revision 1.14
diff -u -r1.14 country_code.install
--- country_code.install	23 Oct 2008 09:14:40 -0000	1.14
+++ country_code.install	28 Oct 2008 03:47:45 -0000
@@ -45,37 +45,17 @@
     'primary key' => array('country'),
   );
 
-  $schema['country_code_country_language'] = array(
-    'description' => t('List of all available languages indexed by country.'),
-    'fields' => array(
-      'country' => array(
-        'type' => 'varchar',
-        'length' => 12,
-        'not null' => TRUE,
-        'default' => '',
-        'description' => t("Country code, e.g. 'us' or 'br'."),
-      ),
-      'language' => array(
-        'type' => 'varchar',
-        'length' => 12,
-        'not null' => TRUE,
-        'default' => '',
-        'description' => t("Language code, e.g. 'de' or 'en-US'."),
-      ),
-      'weight' => array(
-        'type' => 'int',
-        'not null' => TRUE,
-        'default' => 0,
-        'size' => 'tiny',
-        'description' => t('The weight of this record.'),
-      ),
-    ),
-    'primary key' => array('country', 'language'),
-  );
-
   return $schema;
 }
 
+function country_code_update_1() {
+  $return = array();
+
+  db_drop_table($return, 'country_code_country_language');
+  
+  return $return;
+}
+
 /**
  * Implementation of hook_uninstall().
  */
Index: country_code.test
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/country_code/country_code.test,v
retrieving revision 1.18
diff -u -r1.18 country_code.test
--- country_code.test	24 Oct 2008 18:32:25 -0000	1.18
+++ country_code.test	28 Oct 2008 03:47:46 -0000
@@ -220,7 +220,6 @@
   function addCountryLanguage($language_code, $country_code, $create_country_language = TRUE) {
     $edit = array();
     $edit['language'] = $language_code;
-    $edit['create'] = $create_country_language ? 1 : 0;
     $this->drupalPost('admin/settings/country-code-country/' . $country_code . '/languages/add', $edit, t('Add language'));
     $country_language = $language_code . '-' . strtoupper($country_code);
     $this->assertTrue(country_code_language_exists($country_language), t('New country-language was enabled.'));
Index: country_code.admin.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/country_code/country_code.admin.inc,v
retrieving revision 1.33
diff -u -r1.33 country_code.admin.inc
--- country_code.admin.inc	24 Oct 2008 11:45:49 -0000	1.33
+++ country_code.admin.inc	28 Oct 2008 03:47:45 -0000
@@ -195,7 +195,6 @@
 function country_code_admin_country_delete_submit($form, &$form_state) {
   if ($form_state['values']['confirm']) {
     country_code_country_delete($form_state['values']['country']);
-    country_code_language_delete($form_state['values']['country']['country']);
     drupal_set_message(t('The country %country has been removed.', array('%country' => $form_state['values']['country']['name'])));
   }
   $form_state['redirect'] = 'admin/settings/country-code';
@@ -228,21 +227,19 @@
 function _country_code_country_languages_language($country, $language) {
   $form['language'] = array(
     '#type' => 'hidden',
-    '#value' => $language->language,
+    '#value' => $language->country_language,
   );
-
   $form['name'] = array(
     '#type' => 'markup',
     '#value' => $language->name,
   );
-
   $form['weight'] = array(
     '#type' => 'weight',
     '#default_value' => $language->weight,
   );
   $form['operations'] = array(
     '#type' => 'markup',
-    '#value' => l(t('delete'), 'admin/settings/country-code-country/' . $country['country'] . '/languages/delete/' . $language->language),
+    '#value' => l(t('delete'), 'admin/settings/language/delete/' . $language->country_language, array('query' => drupal_get_destination())),
   );
 
   return $form;
@@ -250,8 +247,7 @@
 
 function country_code_admin_country_languages_submit($form, &$form_state) {
   foreach ($form_state['values']['languages'] as $language) {
-    $language['country'] = $form_state['values']['country'];
-    country_code_language_save($language);
+    db_query("UPDATE {languages} SET weight = %d WHERE language = '%s'", $language['weight'], $language['language']);
   }
   $form_state['redirect'] = 'admin/settings/country-code-country/' . $form_state['values']['country'] . '/languages';
   return;
@@ -291,7 +287,7 @@
 /**
  * Add language form.
  */
-function country_code_admin_language_form(&$form_state, $country = NULL, $language = NULL) {
+function country_code_admin_language_form(&$form_state, $country = NULL) {
   $form = array();
 
   $language_codes = country_code_languages($country['country'], FALSE);
@@ -307,9 +303,7 @@
     '#value' => $country['country'],
   );
 
-  if (isset($language)) {
-    drupal_set_title(t('Editing language %language', array('%language' => $language['name'])));
-  }
+  $default_language = language_default();
 
   $form['language'] = array(
     '#type' => 'select',
@@ -317,21 +311,12 @@
     '#default_value' => isset($country) ? $language['language'] : NULL,
     '#options' => $options,
     '#required' => TRUE,
-    '#description' => t('Select the desired language from the list.'),
-  );
-
-  $default_language = language_default();
-
-  $form['create'] = array(
-    '#type' => 'checkbox',
-    '#title' => t('Create a new country language'),
-    '#default_value' => 1,
-    '#description' => t("Check this option to create a new language for this country, if one doesn't exist already. For example, if you add %language_name, you will get a language %country_language_code (%country_language_name).", array('%language_name' => $default_language->name, '%country_language_code' => country_code_get_country_language($default_language->language, $country['country']), '%country_language_name' => t('@language_name (@country_name)', array('@language_name' => $default_language->name, '@country_name' => $country['name'])))),
+    '#description' => t("Select the desired language from the list. When you add a language, a new language will be created. For example, if you add %language_name, you will get a language %country_language_code (%country_language_name).", array('%language_name' => $default_language->name, '%country_language_code' => country_code_get_country_language($default_language->language, $country['country']), '%country_language_name' => t('@language_name (@country_name)', array('@language_name' => $default_language->name, '@country_name' => $country['name'])))),
   );
 
   $form['submit'] = array(
     '#type' => 'submit',
-    '#value' => isset($language['language']) ? t('Save') : t('Add language'),
+    '#value' => t('Add language'),
   );
 
   return $form;
@@ -341,61 +326,26 @@
  * Add language form submit.
  */
 function country_code_admin_language_form_submit($form, &$form_state) {
-  $flag = country_code_language_save($form_state['values']);
-  $variables = array('%language' => locale_language_name($form_state['values']['language']));
-  $message = ($flag == SAVED_NEW ? t('Language %language successfully added.', $variables) : t('Language %language successfully updated.', $variables));
-  drupal_set_message($message);
-
-  if (isset($form_state['values']['create']) && $form_state['values']['create']) {
-    // Custom language creation.
-
-    $langcode = country_code_get_country_language($form_state['values']['language'], $form_state['values']['country'], FALSE);
-    // Only create if the language doesn't exist.
-    if (!country_code_language_exists($langcode)) {
-      $languages = language_list();
-      $language = $languages[$form_state['values']['language']];
-      // Load the language this language is based on.
-      $custom_language = array(
-        'langcode' => $langcode,
-        'name' => $language->name . ' (' . country_code_country_name($form_state['values']['country']) . ')',
-        'native' => $language->native . ' (' . $form_state['values']['country'] . ')',
-        'direction' => $language->direction,
-        'domain' => '',
-        'prefix' => strtolower($langcode),
-      );
-      locale_inc_callback('locale_add_language', $custom_language['langcode'], $custom_language['name'], $custom_language['native'], $custom_language['direction'], $custom_language['domain'], $custom_language['prefix']);
-      drupal_set_message(t('The language %language has been created and can now be used. You may customize this language on the <a href="@language-edit">language edit screen</a>.', array('%language' => t($custom_language['name']), '@language-edit' => url('admin/settings/language/edit/' . $langcode))));
-    }
-  }
+  // Custom language creation.
 
-  $form_state['redirect'] = 'admin/settings/country-code-country/' . $form_state['values']['country'] . '/languages';
-  return;
-}
-
-/**
- * Confirm language deletion.
- */
-function country_code_admin_language_delete(&$form_state, $country, $language) {
-  $form['country'] = array(
-    '#type' => 'value',
-    '#value' => $country['country'],
-  );
-  $form['language'] = array(
-    '#type' => 'value',
-    '#value' => $language['language'],
-  );
-
-  return confirm_form($form, t('Are you sure you want to remove %language from country %country?', array('%language' => locale_language_name($language['language']), '%country' => country_code_country_name($country['country']))), 'admin/settings/country-code', t('This action cannot be undone.'), t('Delete'), t('Cancel'));
-}
-
-/**
- * Process country deletion submissions.
- */
-function country_code_admin_language_delete_submit($form, &$form_state) {
-  if ($form_state['values']['confirm']) {
-    country_code_language_delete($form_state['values']['country'], $form_state['values']['language']);
-    drupal_set_message(t('The language %language has been removed.', array('%language' => locale_language_name($form_state['values']['language']))));
+  $langcode = country_code_get_country_language($form_state['values']['language'], $form_state['values']['country'], FALSE);
+  // Only create if the language doesn't exist.
+  if (!country_code_language_exists($langcode)) {
+    $languages = language_list();
+    $language = $languages[$form_state['values']['language']];
+    // Load the language this language is based on.
+    $custom_language = array(
+      'langcode' => $langcode,
+      'name' => $language->name . ' (' . country_code_country_name($form_state['values']['country']) . ')',
+      'native' => $language->native . ' (' . $form_state['values']['country'] . ')',
+      'direction' => $language->direction,
+      'domain' => '',
+      'prefix' => strtolower($langcode),
+    );
+    locale_inc_callback('locale_add_language', $custom_language['langcode'], $custom_language['name'], $custom_language['native'], $custom_language['direction'], $custom_language['domain'], $custom_language['prefix']);
+    drupal_set_message(t('The language %language has been created and can now be used. You may customize this language on the <a href="@language-edit">language edit screen</a>.', array('%language' => t($custom_language['name']), '@language-edit' => url('admin/settings/language/edit/' . $langcode))));
   }
+
   $form_state['redirect'] = 'admin/settings/country-code-country/' . $form_state['values']['country'] . '/languages';
   return;
 }
Index: country_code.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/country_code/country_code.module,v
retrieving revision 1.68
diff -u -r1.68 country_code.module
--- country_code.module	25 Oct 2008 01:18:12 -0000	1.68
+++ country_code.module	28 Oct 2008 03:47:46 -0000
@@ -104,16 +104,17 @@
 
   $languages = language_list('enabled');
   $languages = $languages[1];
-  $languages = array_intersect_key($languages, country_code_languages());
+  $country_languages = country_code_languages();
 
   // Recognize a language passed through GET or a language set in the session.
   // Only accept an incoming language if it's one of the current country's languages.
-  if (isset($_GET['language']) && isset($languages[$_GET['language']])) {
+  if (isset($_GET['language']) && isset($country_languages[$_GET['language']])) {
     $_SESSION['country_code_language'] = $_GET['language'];
   }
+
   // Only accept a session language if it's one of the current country's languages.
-  if (isset($_SESSION['country_code_language']) && isset($languages[$_SESSION['country_code_language']])) {
-    $language = $languages[$_SESSION['country_code_language']];
+  if (isset($_SESSION['country_code_language']) && $country_language_long = country_code_get_country_language($_SESSION['country_code_language'])) {
+    $language = $languages[$country_language_long];
     return;
   }
   // If a language is in the session but it's invalid for the current country, drop it
@@ -121,10 +122,12 @@
   else {
     unset($_SESSION['country_code_language']);
   }
-
   if ($country_language = country_code_language()) {
-    if (isset($languages[$country_language])) {
-      $language = $languages[$country_language];
+    $country_language_long = country_code_get_country_language($country_language);
+    // The long form is not associated directly with the country, so we look for it
+    // in the full set of enabled languages.
+    if (isset($languages[$country_language_long])) {
+      $language = $languages[$country_language_long];
     }
   }
 }
@@ -219,16 +222,6 @@
     'type' => MENU_LOCAL_TASK,
     'file' => 'country_code.admin.inc',
   );
-  $items['admin/settings/country-code-country/%country_code_country/languages/delete/%country_code_language'] = array(
-    'title' => 'Confirm',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('country_code_admin_language_delete', 3, 6),
-    'load arguments' => array(3),
-    'access arguments' => array('administer site configuration'),
-    'type' => MENU_CALLBACK,
-    'file' => 'country_code.admin.inc',
-  );
-
 
   // Delete country.
   $items['admin/settings/country-code/delete/%country_code_country'] = array(
@@ -280,7 +273,7 @@
  * Implementation of hook_user().
  */
 function country_code_user($op, $edit, &$account, $category = NULL) {
-  global $language, $user;
+  global $user;
 
   // If the site is set to enable user country selection and we have more
   // than one country and are either creating a user on the admin
@@ -538,7 +531,7 @@
       // If the link's language is not the current one, prepend a prefix
       // and pass a language key. The prepended prefix ensures the links
       // don't get the 'active' class.
-      if ($links[$key]['language']->language != $language->language) {
+      if ($links[$key]['language']->language != substr($language->language, 0, 2)) {
         $links[$key]['href'] = $country_code . '/' . $links[$key]['href'];
         $links[$key]['query'] = array('language' => $key);
       }
@@ -718,62 +711,6 @@
 }
 
 /**
- * Fetch and individual language -> country.
- *
- * @param $language
- *   An language code.
- * @param $country
- *   An country code.
- *
- * @return
- *   A single record in array format, or FALSE if none matched the incoming
- *   country code/language.
- */
-function country_code_language_load($language, $country) {
-  $sql = "SELECT * FROM {country_code_country_language} WHERE country = '%s' AND language = '%s'";
-  $result = db_query($sql, $country, $language);
-  if ($record = db_fetch_array($result)) {
-    return $record;
-  }
-  else {
-    return FALSE;
-  }
-}
-
-/**
- * Inserts a new language for a country, or updates an existing one.
- *
- * @param $record
- *   A language record to be saved. If language already exists for the given
- *   country in database, the record will be updated. Otherwise, a new record
- *   will be inserted into the database.
- * @return
- *   The flag SAVED_NEW or SAVED_UPDATE based on the operation performed.
- */
-function country_code_language_save($record) {
-  return country_code_language_load($record['language'], $record['country']) ? drupal_write_record('country_code_country_language', $record, array('country', 'language')) : drupal_write_record('country_code_country_language', $record);
-}
-
-/**
- * Deletes a language for a given country, or all languages for that country.
- *
- * @param $country
- *   The country code of the country to be referenced.
- * @param $language
- *   The language code of the language to be deleted. If none is given, all languages
- *   for the given country are deleted.
- */
-function country_code_language_delete($country, $language = NULL) {
-  if ($language) {
-    db_query("DELETE FROM {country_code_country_language} WHERE country = '%s' AND language = '%s'", $country, $language);
-  }
-  else {
-    db_query("DELETE FROM {country_code_country_language} WHERE country = '%s'", $country);
-  }
-}
-
-
-/**
  * Given a country code, return the country name.
  *
  * @return
@@ -785,7 +722,7 @@
 }
 
 /**
- * Given a country code, return all languages.
+ * Given a country code, return all languages for that country.
  *
  * @return
  *   Array of language objects.
@@ -803,23 +740,24 @@
     // @TODO: use the MIN weight?
     if ($country == COUNTRY_CODE_GLOBAL) {
       if ($enabled) {
-        $result = db_query("SELECT DISTINCT(l.language), l.weight FROM {country_code_country_language} l INNER JOIN {country_code_country} c ON l.country = c.country AND c.enabled = 1 ORDER BY l.weight");
+        $result = db_query("SELECT DISTINCT(SUBSTRING(c.language, 1, 2)) AS language, g.name, g.native, c.weight FROM {languages} c INNER JOIN {languages} g ON SUBSTRING(c.language, 1, 2) = g.language WHERE c.enabled = 1 ORDER BY c.weight, c.name");
       }
       else {
-        $result = db_query("SELECT DISTINCT(language), weight FROM {country_code_country_language} ORDER BY weight");
+        $result = db_query("SELECT DISTINCT(SUBSTRING(c.language, 1, 2)) AS language, g.name, g.native, c.weight FROM {languages} c INNER JOIN {languages} g ON SUBSTRING(c.language, 1, 2) = g.language ORDER BY c.weight, c.name");
       }
     }
     // Otherwise, return a country's languages.
     else {
+      // Upper case is  used for countries in the language codes.
+      $country_upper = strtoupper($country);
       if ($enabled) {
-        $result = db_query("SELECT DISTINCT(l.language), l.weight FROM {country_code_country_language} l INNER JOIN {country_code_country} c ON l.country = c.country AND c.enabled = 1 WHERE l.country = '%s' ORDER BY l.weight", $country);
+        $result = db_query("SELECT c.language AS country_language, SUBSTRING(c.language, 1, 2) AS language, g.name, g.native, c.weight FROM {languages} c INNER JOIN {languages} g ON SUBSTRING(c.language, 1, 2) = g.language WHERE SUBSTRING(c.language, 4) = '%s' AND c.enabled = 1 ORDER BY c.weight, c.name", $country_upper);
       }
       else {
-        $result = db_query("SELECT language, weight FROM {country_code_country_language} WHERE country = '%s' ORDER BY weight", $country);
+        $result = db_query("SELECT c.language AS country_language, SUBSTRING(c.language, 1, 2) AS language, g.name, g.native, c.weight FROM {languages} c INNER JOIN {languages} g ON SUBSTRING(c.language, 1, 2) = g.language WHERE SUBSTRING(c.language, 4) = '%s' ORDER BY c.weight, c.name", $country_upper);
       }
     }
     while ($language = db_fetch_object($result)) {
-      $language->name = locale_language_name($language->language);
       $languages[$country][$language->language] = $language;
     }
   }
@@ -852,12 +790,16 @@
  */
 function country_code_get_country_language($language_code = NULL, $country_code = NULL, $enabled = TRUE) {
   global $language;
-  if (empty($language_code)) {
-    $language_code = $language->language;
-  }
+
   if (empty($country_code)) {
     $country_code = country_code();
   }
+  if (empty($language_code)) {
+    if (strtolower(substr($language->language, 3)) == $country_code) {
+      $language_code = substr($language->language, 0, 2);
+    }
+  }
+
   $country_language = $language_code . '-' . strtoupper($country_code);
   if ($enabled) {
     $enabled_languages = locale_language_list();
@@ -927,13 +869,13 @@
 
   if ($tnid) {
     $translations = translation_node_get_translations($tnid);
-    $languages = array();
-    // Priorize country-specific language, if one is found.
-    if ($country_language = country_code_get_country_language()) {
-      $languages[] = $country_language;
-    }
-    // Always include the current language.
-    $languages[] = $language->language;
+
+    // Priorize country-specific language.
+    $languages = array($language->language);
+
+    // Also search for 'generic', non country specific language.
+    $languages[] = substr($language->language, 0, 2);
+
     // Return the first found translation.
     foreach ($languages as $language) {
       if (isset($translations[$language])) {
