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 -p -r1.33 country_code.admin.inc
--- country_code.admin.inc	24 Oct 2008 11:45:49 -0000	1.33
+++ country_code.admin.inc	27 Oct 2008 21:26:46 -0000
@@ -216,12 +216,6 @@ function country_code_admin_country_lang
     $form['languages'][$lang_code] = _country_code_country_languages_language($country, $language);
   }
 
-  $form['buttons']['submit'] = array(
-    '#type' => 'submit',
-    '#value' => t('Save changes'),
-    '#disabled' => empty($languages),
-  );
-
   return $form;
 }
 
@@ -242,7 +236,7 @@ function _country_code_country_languages
   );
   $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),
   );
 
   return $form;
@@ -291,7 +285,7 @@ function theme_country_code_admin_countr
 /**
  * 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);
@@ -305,12 +299,9 @@ function country_code_admin_language_for
   $form['country'] = array(
     '#type' => 'value',
     '#value' => $country['country'],
+    '#description' => t("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'])))),
   );
 
-  if (isset($language)) {
-    drupal_set_title(t('Editing language %language', array('%language' => $language['name'])));
-  }
-
   $form['language'] = array(
     '#type' => 'select',
     '#title' => t('Language'),
@@ -320,18 +311,9 @@ function country_code_admin_language_for
     '#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'])))),
-  );
-
   $form['submit'] = array(
     '#type' => 'submit',
-    '#value' => isset($language['language']) ? t('Save') : t('Add language'),
+    '#value' => t('Add language'),
   );
 
   return $form;
@@ -341,31 +323,24 @@ function country_code_admin_language_for
  * 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.
+
+  $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';
Index: country_code.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/country_code/country_code.install,v
retrieving revision 1.14
diff -u -p -r1.14 country_code.install
--- country_code.install	23 Oct 2008 09:14:40 -0000	1.14
+++ country_code.install	27 Oct 2008 21:26:46 -0000
@@ -45,34 +45,6 @@ function country_code_schema() {
     '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;
 }
 
Index: country_code.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/country_code/country_code.module,v
retrieving revision 1.68
diff -u -p -r1.68 country_code.module
--- country_code.module	25 Oct 2008 01:18:12 -0000	1.68
+++ country_code.module	27 Oct 2008 21:26:48 -0000
@@ -104,16 +104,16 @@ function _country_code_init_language() {
 
   $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
@@ -123,8 +123,11 @@ function _country_code_init_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];
     }
   }
 }
@@ -718,62 +721,6 @@ function country_code_country_delete($co
 }
 
 /**
- * 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 +732,7 @@ function country_code_country_name($code
 }
 
 /**
- * Given a country code, return all languages.
+ * Given a country code, return all languages for that country.
  *
  * @return
  *   Array of language objects.
@@ -803,23 +750,24 @@ function country_code_languages($country
     // @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 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 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 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 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 +800,16 @@ function country_code_country_list() {
  */
 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();
