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	20 Oct 2008 13:12:59 -0000
@@ -14,7 +14,14 @@ function legal_help($path, $arg) {
       break;
      case 'admin/settings/legal':
       $output .= t('Display a Terms & Conditions statement on the registration page, require visitor to accept the T&C to register. ');
-      $output .= t("A !page displaying your T&C will be automatically created, access to this page can be set via the !access administration page.", array('!page' => l('page', 'legal'), '!access' => l('permissions', 'admin/user/permissions')));
+      $output .= t('A <a href="@page">page</a> displaying your T&C will be automatically created, access to this page can be set via the <a href="@access">permissions</a> administration page.', array('@page' => url('legal'), '@access' => url('admin/user/permissions')));
+      if (!module_exists('i18nstrings')) {
+        $output .= ' '. t('To make terms and conditions translatable, install and enable Internationalization module, then install and enable the <a href="@i18n" >Internationalization</a> package\'s Strings module.', array('@i18n' => url('http://drupal.org/project/i18n', array('absolute' => TRUE))));
+      }
+      elseif (user_access('translate interface')) {
+        $output .= ' '. t('Legal terms and conditions may be translated using the <a href="@translate">Translate interface</a> page" .', array('@translate' => url('admin/build/translate/search')));
+      }
+      break;
   }
   return $output;
 }
@@ -77,6 +84,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();
@@ -115,7 +193,7 @@ function legal_display_fields($condition
             $form['legal']['conditions'] = array(
               '#value' => ' ',
             );
-						$accept_label = t('<strong>Accept</strong> !terms of Use', array('!terms' => l('Terms & Conditions', 'legal')));
+						$accept_label = t('<strong>Accept</strong> <a href="@terms">Terms & Conditions</a> of Use', array('@terms' => url('legal')));
             break;  
 
         default: // scroll box (HTML)
@@ -158,7 +236,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')) {
@@ -278,7 +356,7 @@ function legal_preview($form, $form_valu
             $form['legal']['conditions'] = array(
               '#value' => ' ',
             );
-				    $form['legal']['legal_accept']['#title'] = t('<strong>Accept</strong> !terms of Use', array('!terms' => l('Terms & Conditions', 'legal')));
+				    $form['legal']['legal_accept']['#title'] = t('<strong>Accept</strong> <a href="@terms">Terms & Conditions</a> of Use', array('@terms' => url('legal')));
             break;  
         default: // scroll box (HTML)
             $form['legal']['conditions'] = array(
@@ -329,6 +407,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 +475,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 +506,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 +529,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 +653,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,11 +750,14 @@ function legal_save_accept($uid, $tc_id)
   return;
 }
 
-function legal_get_conditions() {
-
-    $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']);
-    return $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;
 }
 
 function legal_conditions_updated($new) {
@@ -710,13 +794,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(
Index: tests/legal_i18n.test
===================================================================
RCS file: tests/legal_i18n.test
diff -N tests/legal_i18n.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ tests/legal_i18n.test	20 Oct 2008 13:12:59 -0000
@@ -0,0 +1,69 @@
+<?php
+// $Id$
+
+class LegalInternationalisationTestCase extends DrupalWebTestCase {
+  /**
+   * Implementation of getInfo().
+   */
+  function getInfo() {
+    return array(
+      'name' => t('Legal internationalisation'),
+      'description' => t('Legal module integration with i18nstrings.'),
+      'group' => t('Legal')
+    );
+  }
+
+  /**
+   * Implementation of setUp().
+   */
+  function setUp() {
+    parent::setUp('legal', 'locale', 'i18n', 'i18nstrings', 'checkbox_validate');
+    $this->admin_user = $admin_user = $this->drupalCreateUser(array('administer site configuration', 'translate interface', 'administer Terms and Conditions', 'administer languages'));
+    $this->drupalLogin($this->admin_user);
+    // Set path negotiation.
+    variable_set('language_negotiation', LANGUAGE_NEGOTIATION_PATH);
+  }
+
+  /**
+   * Test that strings from legal module can be translated by i18nstrings.
+   */
+  function testLegali18n() {
+    // Enable an additional language.
+    $edit = array();
+    $edit['langcode'] = 'fr';
+    $this->drupalPost('admin/settings/language/add', $edit, t('Add language'));
+    $this->drupalGet('admin/settings/language');
+    $this->assertText(t('French'), t('Language added successfully'));
+
+    // Save initial terms and conditions in English.
+    $edit = array(
+      'conditions' => 'Conditions in English.',
+    );
+    $this->drupalPost('admin/settings/legal', $edit, t('Save'));
+
+    // Search for the string to translate.
+    $this->drupalGet('admin/build/translate/search');
+    $this->assertText(t('Legal'), t('Legal group is available for interface translation'));
+    $edit = array(
+      'group' => 'legal',
+    );
+    $this->drupalPost('admin/build/translate/search', $edit, t('Search'));
+    $this->assertText('Conditions in English', t('Conditions string returned by search'));
+    // There should be only one row in the text group, so click the first edit link we find.
+    $this->clickLink('edit');
+
+    // Translate the conditions into French.
+    $edit = array(
+      'translations[fr]' => 'Conditions en Francais.',
+    );
+    $this->drupalPost(NULL, $edit, t('Save translations'));
+
+    // Confirm strings are available on user/register.
+    $this->drupalLogout();
+    $this->drupalGet('user/register');
+    $this->assertRaw('Conditions in English.', t('English conditions text found on user registration'));
+    $this->drupalGet('fr/user/register');
+    $this->assertResponse('200');
+    $this->assertRaw('Conditions en Francais.', t('French conditions text found on user registration'));
+  }
+}
