Permalink
Browse files

add nested tabbed interface (not terribly proud of the code, but it w…

…orks fine).
  • Loading branch information...
1 parent 86cbfdc commit 877d8adf367bc55ace57491d75c8b6fd562a206e Josh Adams committed Dec 28, 2010
@@ -1,13 +1,30 @@
module GoTranslateYourself
class SiteTranslationsController < ApplicationController
before_filter :auth
+ helper 'go_translate_yourself/translation_tabs'
+ helper 'go_translate_yourself/translation_table'
def edit
@translations = hash_class[
*GoTranslateYourself.current_store.keys_without_prefix.collect do |key|
[key, GoTranslateYourself.current_store.default_translation("dev.#{key}")]
end.flatten
]
+ # NOTE: This is not your typical ruby-esque way to do things, what with the passing by reference,
+ # but it totally works. I'm getting a nested hash of the namespaces.
+ @translations_split = {}
+ @translations.each do |key, default_translation|
+ tran_parts = key.split('.')
+ current_position = @translations_split
+ while tran_parts.length > 1
+ next_part = tran_parts.shift
+ unless current_position[next_part]
+ current_position[next_part] = {}
+ end
+ current_position = current_position[next_part]
+ end
+ current_position[tran_parts.first] = key
+ end
@locales = GoTranslateYourself.locales
render :layout => GoTranslateYourself.layout_name
end
@@ -11,7 +11,7 @@ def translation_text_field(locale, key, default_translation)
end
text_field_tag("translations[#{locale}]#{key_parts}",
translation_text || "",
- :size => (default_translation.size + 20))
+ :size => (default_translation.size))
end
end
end
@@ -0,0 +1,12 @@
+module GoTranslateYourself
+ module TranslationTableHelper
+ def translation_table_for(translations, locales)
+ render :partial => 'go_translate_yourself/site_translations/translation_table', :locals => { :translations => translations, :locales => locales }
+ end
+
+ def get_translations_under(child, translations)
+ array_representation = translations.select{|k, v| child.values.include?(k)}
+ hash_var = HashFromArray.hash_from_array(array_representation)
+ end
+ end
+end
@@ -0,0 +1,11 @@
+module GoTranslateYourself
+ module TranslationTabsHelper
+ def tabs_for(hash)
+ render :partial => 'go_translate_yourself/site_translations/tabs', :locals => { :hash => hash }
+ end
+
+ def tabs_full_for(hash)
+ render :partial => 'go_translate_yourself/site_translations/tabs_full', :locals => { :hash => hash }
+ end
+ end
+end
@@ -0,0 +1,23 @@
+<% id = "tabs-#{hash.object_id}" -%>
+<div id=<%= id -%>>
+ <ul>
+ <% hash.each do |key, child| %>
+ <li><a href="#tabs-<%= hash.object_id -%>-<%= key.object_id -%>"><%= key -%></a></li>
+ <% end %>
+ </ul>
+ <% hash.each do |key, child| %>
+ <% child_translations = get_translations_under(child, @translations) -%>
+ <% child_hash_array_representation = child.select{|k, v| v.is_a?(Hash) } -%>
+ <div id="tabs-<%= hash.object_id -%>-<%= key.object_id -%>">
+ <%= translation_table_for(child_translations, @locales) -%>
+ <% unless child_hash_array_representation.empty? -%>
+ <%= tabs_for(HashFromArray.hash_from_array(child_hash_array_representation)) -%>
+ <% end -%>
+ </div>
+ <% end %>
+</div>
+<script>
+ $(function() {
+ $('#<%= id -%>').tabs();
+ });
+</script>
@@ -0,0 +1,8 @@
+<% hash.each do |key, child| %>
+ <h3><%= key -%></h3>
+ <% child_translations = get_translations_under(child, @translations) -%>
+ <% child_hash_array_representation = child.select{|k, v| v.is_a?(Hash) } -%>
+ <% child_hash = HashFromArray.hash_from_array(child_hash_array_representation) -%>
+ <%= tabs_full_for(child_hash) -%>
+ <%= translation_table_for(child_translations, @locales) -%>
+<% end %>
@@ -0,0 +1,29 @@
+<table class="gt_site_translations">
+ <tr class="gt_ths">
+ <th>Key</th>
+ <th>Value</th>
+ </tr>
+ <% translations.each do |key, default_translation| %>
+ <tr class="gt_separator">
+ <td colspan="2"></td>
+ </tr>
+ <tr class="gt_default_value">
+ <td class="gt_key">
+ <%= key %>
+ </td>
+ <td class="gt_value">
+ <%= default_translation %>
+ </td>
+ <% locales.each do |locale| %>
+ <tr>
+ <td>
+ <%= locale %>
+ </td>
+ <td>
+ <%= translation_text_field locale, key, default_translation %>
+ </td>
+ </tr>
+ <% end %>
+ </tr>
+ <% end %>
+</table>
@@ -1,33 +1,5 @@
+<h2>Translations Editor</h2>
<%= form_tag update_site_translations_path, :method => :post do %>
- <table class="gt_site_translations">
- <tr class="gt_ths">
- <th>Key</th>
- <th>Value</th>
- </tr>
- <% @translations.each do |key, default_translation| %>
- <tr class="gt_separator">
- <td colspan="2"></td>
- </tr>
- <tr class="gt_default_value">
- <td class="gt_key">
- <%= key %>
- </td>
- <td class="gt_value">
- <%= default_translation %>
- </td>
- <% @locales.each do |locale| %>
- <tr>
- <td>
- <%= locale %>
- </td>
- <td>
- <%= translation_text_field locale, key, default_translation %>
- </td>
- </tr>
- <% end %>
- </tr>
- <% end %>
- </table>
+ <%= tabs_for(@translations_split) -%>
<%= submit_tag "Save" %>
<% end %>
-
@@ -1,3 +1,4 @@
+require 'go_translate_yourself/hash_from_array'
module GoTranslateYourself
class Engine < Rails::Engine
end
@@ -0,0 +1,5 @@
+class HashFromArray
+ def self.hash_from_array(array)
+ array.inject({}) {|h, elem| h[elem[0]]=elem[1]; h}
+ end
+end

0 comments on commit 877d8ad

Please sign in to comment.