From ddceb6585788507a1d51c07cbea58e72261387b5 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Sat, 14 May 2011 12:14:25 +0200
Subject: [PATCH] Issue #1154364 by Berdir: Create form_meta items information based on hook_i18n_object_info().

---
 i18n_string/i18n_string.pages.inc     |   22 +++++++++++++++++++++
 i18n_taxonomy/i18n_taxonomy.admin.inc |   34 +++-----------------------------
 2 files changed, 26 insertions(+), 30 deletions(-)

diff --git a/i18n_string/i18n_string.pages.inc b/i18n_string/i18n_string.pages.inc
index 20756e9..f7e254b 100644
--- a/i18n_string/i18n_string.pages.inc
+++ b/i18n_string/i18n_string.pages.inc
@@ -15,6 +15,28 @@ function i18n_string_translate_page($form_meta, $langcode = NULL) {
     '#item_title_header' => t('Title'),
   );
 
+  // If there is an object property, get the items from there.
+  if (!empty($form_meta['#type']) && !empty($form_meta['#object'])) {
+    $info = i18n_object_info($form_meta['#type']);
+
+    foreach ($info['string translation']['properties'] as $key => $title) {
+      if (!empty($form_meta['#object']->{$key})) {
+        $form_meta['#items'][] = array(
+          '#title' => is_array($title) ? $title['title'] : $title,
+          '#string_key' => array($info['string translation']['textgroup'], $info['string translation']['type'], $form_meta['#object']->{$info['key']}, $key),
+          '#default_value' => $form_meta['#object']->{$key},
+        );
+      }
+    }
+  }
+
+  // If not set, assume that the default item is the first one.
+  if ((empty($form_meta['#item_title_key']))) {
+    $default_item = reset($form_meta['#items']);
+    $form_meta['#item_title_key'] = $default_item['#string_key'];
+    $form_meta['#item_title_default'] = $default_item['#default_value'];
+  }
+
   if (empty($langcode)) {
     drupal_set_title($form_meta['#page_title']);
     return i18n_string_translate_page_overview($form_meta);
diff --git a/i18n_taxonomy/i18n_taxonomy.admin.inc b/i18n_taxonomy/i18n_taxonomy.admin.inc
index a9b0b9d..646da22 100644
--- a/i18n_taxonomy/i18n_taxonomy.admin.inc
+++ b/i18n_taxonomy/i18n_taxonomy.admin.inc
@@ -45,24 +45,11 @@ function i18n_taxonomy_term_translation_tab_page($term, $language = NULL) {
     module_load_include('inc', 'i18n_string', 'i18n_string.pages');
     $form_meta = array(
       '#page_title' => t('Translate term'),
-      '#item_title_key' => array('taxonomy', 'term', $term->tid, 'name'),
-      '#item_title_default' => $term->name,
       '#edit' => 'taxonomy/term/' . $term->tid . '/edit',
       '#translate' => 'taxonomy/term/' . $term->tid . '/translate',
-      '#items' => array(),
+      '#type' => 'taxonomy_term',
+      '#object' => $term,
     );
-    $form_meta['#items'][] = array(
-      '#title' => t('Name'),
-      '#string_key' => array('taxonomy', 'term', $term->tid, 'name'),
-      '#default_value' => $term->name,
-    );
-    if (!empty($term->description)) {
-      $form_meta['#items'][] = array(
-	'#title' => t('Description'),
-	'#string_key' => array('taxonomy', 'term', $term->tid, 'description'),
-	'#default_value' => $term->description
-      );
-    }
     return i18n_string_translate_page($form_meta, $language);
   }
 }
@@ -308,23 +295,10 @@ function i18n_taxonomy_translation_vocabulary_page($vocabulary, $language = NULL
   module_load_include('inc', 'i18n_string', 'i18n_string.pages');
   $form_meta = array(
     '#page_title' => t('Translate vocabulary'),
-    '#item_title_key' => array('taxonomy', 'vocabulary', $vocabulary->vid, 'name'),
-    '#item_title_default' => $vocabulary->name,
     '#edit' => 'admin/structure/taxonomy/' . $vocabulary->machine_name . '/edit',
     '#translate' => 'admin/structure/taxonomy/' . $vocabulary->machine_name . '/translate',
-    '#items' => array(),
+    '#type' => 'taxonomy_vocabulary',
+    '#object' => $vocabulary,
   );
-  $form_meta['#items'][] = array(
-    '#title' => t('Name'),
-    '#string_key' => array('taxonomy', 'vocabulary', $vocabulary->vid, 'name'),
-    '#default_value' => $vocabulary->name,
-  );
-  if (!empty($vocabulary->description)) {
-    $form_meta['#items'][] = array(
-      '#title' => t('Description'),
-      '#string_key' => array('taxonomy', 'vocabulary', $vocabulary->vid, 'description'),
-      '#default_value' => $vocabulary->description
-    );
-  }
   return i18n_string_translate_page($form_meta, $language);
 }
-- 
1.7.4.1

