From 6d83ead9aaafb6797adf58033c11e1f3080dec57 Mon Sep 17 00:00:00 2001
From: Sascha Grossenbacher <saschagros@gmail.com>
Date: Fri, 13 May 2011 17:49:10 +0200
Subject: [PATCH] Issue #1154364 by Berdir: Added ability to create translate tab automatically through hook_i18n_object_info().

---
 i18n_string/i18n_string.module        |   31 +++++++++++++++++++
 i18n_string/i18n_string.pages.inc     |   37 ++++++++++++++++++++++
 i18n_taxonomy/i18n_taxonomy.admin.inc |   54 ++-------------------------------
 i18n_taxonomy/i18n_taxonomy.module    |   54 ++++++++++-----------------------
 4 files changed, 87 insertions(+), 89 deletions(-)

diff --git a/i18n_string/i18n_string.module b/i18n_string/i18n_string.module
index 9e046f2..4432a1f 100644
--- a/i18n_string/i18n_string.module
+++ b/i18n_string/i18n_string.module
@@ -146,6 +146,37 @@ function i18n_string_menu() {
     'file' => 'i18n_string.inc',
     'type' => MENU_CALLBACK,
   );
+
+  foreach (i18n_object_info() as $type => $info) {
+    if (isset($info['string translation']['translate tab'])) {
+      $translate_tab = $info['string translation']['translate tab'];
+      $argument = array_search($translate_tab['argument placeholder'], explode('/', $translate_tab['path']));
+      $items[$translate_tab['path'] . '/translate'] = array(
+        'title' => 'Translate',
+        'page callback' => isset($translate_tab['page callback']) ? $translate_tab['page callback'] : 'i18n_string_translate_tab_default_page',
+        'page arguments' => array($type, $argument),
+        'access callback' => $translate_tab['access callback'],
+        'access arguments' => array($argument),
+        'type' => MENU_LOCAL_TASK,
+        'weight' => isset($translate_tab['weight']) ? $translate_tab['weight'] : 0,
+        'file' => isset($translate_tab['file']) ? $translate_tab['file'] : 'i18n_string.pages.inc',
+        'file path' => isset($translate_tab['file path']) ? $translate_tab['file path'] : drupal_get_path('module', 'i18n_string'),
+      );
+      $items[$translate_tab['path'] . '/translate/%language'] = array(
+        'title' => 'Translate',
+        'page callback' => 'i18n_string_translate_tab_default_page',
+        'page arguments' => array($type, $argument, substr_count($translate_tab['path'], '/') + 2),
+        'access callback' => $translate_tab['access callback'],
+        'access arguments' => array($argument),
+        'type' => MENU_CALLBACK,
+        'file' => isset($translate_tab['file']) ? $translate_tab['file'] : 'i18n_string.pages.inc',
+        'file path' => isset($translate_tab['file path']) ? $translate_tab['file path'] : drupal_get_path('module', 'i18n_string'),
+      );
+    }
+  }
+
+  dpm($items);
+
   return $items;
 }
 
diff --git a/i18n_string/i18n_string.pages.inc b/i18n_string/i18n_string.pages.inc
index 20756e9..6aec458 100644
--- a/i18n_string/i18n_string.pages.inc
+++ b/i18n_string/i18n_string.pages.inc
@@ -169,3 +169,40 @@ function i18n_string_validate_submission($formkey, $value) {
     watchdog('locale', 'Attempted submission of a translation string with disallowed HTML: %string', array('%string' => $value), WATCHDOG_WARNING);
   }
 }
+
+/**
+ * Generic menu callback that provides a translate tab based on the i18n object
+ * information.
+ */
+function i18n_string_translate_tab_default_page($type, $object, $language = NULL) {
+  $info = i18n_object_info($type);
+  if (!$info) {
+    return MENU_NOT_FOUND;
+  }
+
+  $string_translation = $info['string translation'];
+  $tab_info = $string_translation['translate tab'];
+  $id = $object->{$info['key']};
+
+  // The first property is the default.
+  $default_property_key = key($string_translation['properties']);
+
+  $form_meta = array(
+    '#page_title' => t('Translate @title', array('@title' => $info['title'])),
+    '#item_title_key' => array($string_translation['textgroup'], $string_translation['type'], $id, $default_property_key),
+    '#item_title_default' => $object->{$default_property_key},
+    '#edit' => str_replace($tab_info['argument placeholder'], $object->{$tab_info['argument property']}, $tab_info['path']) . '/edit',
+    '#translate' => str_replace($tab_info['argument placeholder'], $object->{$tab_info['argument property']}, $tab_info['path']) . '/translate',
+    '#items' => array(),
+  );
+  foreach ($string_translation['properties'] as $key => $title) {
+    if (!empty($object->{$key})) {
+      $form_meta['#items'][] = array(
+        '#title' => is_array($title) ? $title['title'] : $title,
+        '#string_key' => array($string_translation['textgroup'], $string_translation['type'], $id, $key),
+        '#default_value' => $object->{$key},
+      );
+    }
+  }
+  return i18n_string_translate_page($form_meta, $language);
+}
\ No newline at end of file
diff --git a/i18n_taxonomy/i18n_taxonomy.admin.inc b/i18n_taxonomy/i18n_taxonomy.admin.inc
index 2c0fc02..868e73a 100644
--- a/i18n_taxonomy/i18n_taxonomy.admin.inc
+++ b/i18n_taxonomy/i18n_taxonomy.admin.inc
@@ -30,7 +30,7 @@ function i18n_taxonomy_page_vocabulary($vocabulary, $op = NULL, $tid = NULL) {
 /**
  * Callback for term translation tab.
  */
-function i18n_taxonomy_term_translation_tab_page($term, $language = NULL) {
+function i18n_taxonomy_term_translation_tab_page($type, $term, $language = NULL) {
   if (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_TRANSLATE)) {
     $vocabulary = taxonomy_vocabulary_load($term->vid);
     $translation_set = !empty($term->i18n_tsid) ? i18n_translation_set_load($term->i18n_tsid) : NULL;
@@ -38,27 +38,7 @@ function i18n_taxonomy_term_translation_tab_page($term, $language = NULL) {
   }
   elseif (i18n_taxonomy_vocabulary_mode($term->vid, I18N_MODE_LOCALIZE)) {
     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(),
-    );
-    $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);
+    return i18n_string_translate_tab_default_page($type, $term, $language);
   }
 }
 
@@ -240,32 +220,4 @@ function i18n_taxonomy_translation_sets_overview($vocabulary) {
     $build['message']['#markup'] = t('No translations defined for this vocabulary.');
   }
   return $build;
-}
-
-/**
- * Callback for term translation tab.
- */
-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(),
-  );
-  $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);
-}
+}
\ No newline at end of file
diff --git a/i18n_taxonomy/i18n_taxonomy.module b/i18n_taxonomy/i18n_taxonomy.module
index db19800..ed7ba7f 100644
--- a/i18n_taxonomy/i18n_taxonomy.module
+++ b/i18n_taxonomy/i18n_taxonomy.module
@@ -70,25 +70,6 @@ function _i18n_taxonomy_vocabulary_options() {
  * Implements hook_menu().
  */
 function i18n_taxonomy_menu() {
-  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/translate'] = array(
-    'title' => 'Translate',
-    'page callback' => 'i18n_taxonomy_translation_vocabulary_page',
-    'page arguments' => array(3),
-    'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_access',
-    'access arguments' => array(3),
-    'type' => MENU_LOCAL_TASK,
-    //'parent' => 'admin/content/taxonomy/%taxonomy_vocabulary',
-    'file' => 'i18n_taxonomy.admin.inc',
-  );
-  $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/translate/%language'] = array(
-    'title' => 'Translate',
-    'page callback' => 'i18n_taxonomy_translation_vocabulary_page',
-    'page arguments' => array(3, 5),
-    'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_access',
-    'access arguments' => array(3),
-    'type' => MENU_CALLBACK,
-    'file' => 'i18n_taxonomy.admin.inc',
-  );
   $items['admin/structure/taxonomy/%taxonomy_vocabulary_machine_name/list/list'] = array(
     'title' => 'Terms',
     'type' => MENU_DEFAULT_LOCAL_TASK,
@@ -123,25 +104,6 @@ function i18n_taxonomy_menu() {
     //'parent' => 'admin/content/taxonomy/%taxonomy_vocabulary',
     'file' => 'i18n_taxonomy.admin.inc',
   );
-  $items['taxonomy/term/%taxonomy_term/translate'] = array(
-    'title' => 'Translate',
-    'page callback' => 'i18n_taxonomy_term_translation_tab_page',
-    'page arguments' => array(2),
-    'access callback' => 'i18n_taxonomy_term_translation_tab_access',
-    'access arguments' => array(2),
-    'type' => MENU_LOCAL_TASK,
-    'weight' => 10,
-    'file' => 'i18n_taxonomy.admin.inc',
-  );
-  $items['taxonomy/term/%taxonomy_term/translate/%language'] = array(
-    'title' => 'Translate',
-    'page callback' => 'i18n_taxonomy_term_translation_tab_page',
-    'page arguments' => array(2, 4),
-    'access callback' => 'i18n_taxonomy_term_translation_tab_access',
-    'access arguments' => array(2),
-    'type' => MENU_CALLBACK,
-    'file' => 'i18n_taxonomy.admin.inc',
-  );
   $items['i18n/taxonomy/autocomplete/vocabulary/%taxonomy_vocabulary_machine_name/%'] = array(
     'title' => 'Autocomplete taxonomy',
     'page callback' => 'i18n_taxonomy_autocomplete_language',
@@ -1102,6 +1064,16 @@ function i18n_taxonomy_i18n_object_info() {
           'format' => 'format',
         ),
       ),
+      'translate tab' => array(
+        'path' => 'taxonomy/term/%taxonomy_term',
+        'page callback' => 'i18n_taxonomy_term_translation_tab_page',
+        'file' => 'i18n_taxonomy.admin.inc',
+        'file path' => drupal_get_path('module', 'i18n_taxonomy'),
+        'access callback' => 'i18n_taxonomy_term_translation_tab_access',
+        'argument placeholder' => '%taxonomy_term',
+        'argument property' => 'tid',
+        'weight' => 10,
+      ),
     )
   );
   $info['taxonomy_vocabulary'] = array(
@@ -1115,6 +1087,12 @@ function i18n_taxonomy_i18n_object_info() {
         'name' => t('Name'),
         'description' => t('Description'),
       ),
+      'translate tab' => array(
+        'path' => 'admin/structure/taxonomy/%taxonomy_vocabulary_machine_name',
+        'access callback' => 'i18n_taxonomy_vocabulary_translation_tab_access',
+        'argument placeholder' => '%taxonomy_vocabulary_machine_name',
+        'argument property' => 'machine_name',
+      ),
     )
   );
   return $info;
-- 
1.7.4.1

