Index: legal.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/legal/legal.module,v
retrieving revision 1.22
diff -u -p -r1.22 legal.module
--- legal.module	8 Aug 2008 22:51:47 -0000	1.22
+++ legal.module	10 Sep 2008 15:40:40 -0000
@@ -77,6 +77,77 @@ function legal_theme() {
   );
 }
 
+/**
+ * Implementation of hook_locale().
+ */
+function legal_locale($op = 'groups', $group = NULL) {
+  switch ($op) {
+    case 'groups':
+      return array('legal' => t('Legal'));
+    case 'refresh':
+      if ($group == 'legal') {
+        return legal_locale_refresh();
+      }      
+  }
+}
+
+/**
+ * Read all existing legal strings into the local system.
+ */
+function legal_locale_refresh() {
+  $result = db_query("SELECT * FROM {legal_conditions}");
+  while ($conditions = db_fetch_array($result)) {
+    $conditions['extras'] = empty($conditions['extras']) ? array() : unserialize($conditions['extras']);
+    legal_locale_process($conditions);
+  }
+}
+
+/**
+ * Send all strings in a terms and conditions version for translation.
+ */
+function legal_locale_process($conditions) {
+  if (module_exists('i18nstrings')) {
+    // Store conditions strings to translate.
+    // Don't use to because we're passing an array.
+    tt("legal:version-{$conditions['tc_id']}:conditions", $conditions['conditions'], NULL, TRUE);
+    if (!empty($conditions['changes'])) {
+      tt("legal:version-{$conditions['tc_id']}:changes", $conditions['changes'], NULL, TRUE);
+    }
+    foreach ($conditions['extras'] as $key => $label) {
+      // Store each label.
+      if (!empty($label)) {
+        tt("legal:version-{$conditions['tc_id']}:extras:$key:", $label, NULL, TRUE);
+      }
+    }
+  }
+}
+
+/**
+ * Fetch a translation.
+ */
+function legal_localize($location, $string) {
+  if (module_exists('i18nstrings')) {
+    return tt($location, $string);
+  }
+  return $string;
+}
+
+/**
+ * Translate a set of legal conditions.
+ */
+function legal_localize_conditions($conditions) {
+  $conditions['conditions'] = legal_localize("legal:version-{$conditions['tc_id']}:conditions", $conditions['conditions']);
+  if (!empty($conditions['changes'])) {
+    $conditions['changes'] = legal_localize("legal:version-{$conditions['tc_id']}:changes", $conditions['changes']);
+  }
+  foreach ($conditions['extras'] as $key => $label) {
+    if (!empty($label)) {
+      $conditions['extras'][$key] = legal_localize("legal:version-{$conditions['tc_id']}:extras:$key", $label);
+    }
+  }
+  return $conditions;
+}
+
 function legal_display_fields($conditions) {
     
     $form = array();
@@ -158,7 +229,7 @@ function legal_display_fields($condition
 
 function legal_page() {
     
-    $conditions = legal_get_conditions();
+    $conditions = legal_get_conditions(TRUE);
     $output = '';
     
     switch(variable_get('legal_display', '0')) {
@@ -329,6 +400,9 @@ function legal_administration_submit($fo
   // If new conditions are different from current permisions - enter in database
   if ( legal_conditions_updated($form) ) {
       db_query("INSERT INTO {legal_conditions} (tc_id, conditions, date, extras, changes) VALUES (NULL, '%s', %d, '%s', '%s')", $values['conditions'], time(), serialize($values['extras']), $values['changes']);
+      // The tc_id is needed to correctly register data for translation.
+      $values['tc_id'] = db_last_insert_id('legal', 'tc_id');
+      legal_locale_process($values);
       drupal_set_message (t('Terms & Conditions have been saved.'));
   }
   
@@ -394,7 +468,7 @@ function legal_user($op, &$edit, &$accou
         
       case 'register':
         
-        $conditions = legal_get_conditions();
+        $conditions = legal_get_conditions(TRUE);
         if (empty($conditions['conditions'])) return;
         
         $form_fields = legal_display_fields($conditions);
@@ -425,7 +499,7 @@ function legal_user($op, &$edit, &$accou
 
       case 'login':
 
-        $conditions = legal_get_conditions();
+        $conditions = legal_get_conditions(TRUE);
         if (empty($conditions['conditions'])) return;
 
         $accepted = legal_version_check($user->uid, $conditions['tc_id']);
@@ -448,7 +522,7 @@ function legal_user($op, &$edit, &$accou
         
       case 'form':
         
-        $conditions = legal_get_conditions();
+        $conditions = legal_get_conditions(TRUE);
         if (empty($conditions['conditions'])) return;
         
         if ($category == 'account') {
@@ -572,7 +646,7 @@ function legal_user($op, &$edit, &$accou
 // require registered users to accept new T&C  
 function legal_login($constructor, $uid, $id_hash = NULL) {
 	
-	$conditions = legal_get_conditions();
+	$conditions = legal_get_conditions(TRUE);
 	$form = legal_display_fields($conditions);
 
 	$form['uid'] = array(
@@ -669,10 +743,14 @@ function legal_save_accept($uid, $tc_id)
   return;
 }
 
-function legal_get_conditions() {
+function legal_get_conditions($localize = FALSE) {
 
     $conditions = db_fetch_array(db_query("SELECT * FROM {legal_conditions} ORDER BY tc_id DESC LIMIT 1"));
     $conditions['extras'] = empty($conditions['extras']) ? array() : unserialize($conditions['extras']);
+    // If we're localizing, pass conditions and extra labels through localization.
+    if ($localize) {
+      $conditions = legal_localize_conditions($conditions);
+    }
     return $conditions;
 }
 
@@ -710,13 +788,15 @@ function legal_display_changes($form, $u
         '#tree' => TRUE,
     );
     
-    while ($conditions = db_fetch_object($results)) { 
+    while ($conditions = db_fetch_array($results)) {
+      // We're displaying to the end user, so localize.
+      $conditions['changes'] = legal_localize("legal:version-{$conditions['tc_id']}:changes", $conditions['changes']);
        
        unset($changes);
         
-        if (!empty($conditions->changes)) {
+        if (!empty($conditions['changes'])) {
         
-            $changes = explode("\r\n", $conditions->changes);
+            $changes = explode("\r\n", $conditions['changes']);
             
             foreach($changes as $change) {
                 $form['changes'][] = array(
