Skip to content

Commit

Permalink
add nested tabbed interface (not terribly proud of the code, but it w…
Browse files Browse the repository at this point in the history
…orks fine).
  • Loading branch information
Josh Adams committed Dec 28, 2010
1 parent 86cbfdc commit 877d8ad
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 31 deletions.
@@ -1,13 +1,30 @@
module GoTranslateYourself module GoTranslateYourself
class SiteTranslationsController < ApplicationController class SiteTranslationsController < ApplicationController
before_filter :auth before_filter :auth
helper 'go_translate_yourself/translation_tabs'
helper 'go_translate_yourself/translation_table'


def edit def edit
@translations = hash_class[ @translations = hash_class[
*GoTranslateYourself.current_store.keys_without_prefix.collect do |key| *GoTranslateYourself.current_store.keys_without_prefix.collect do |key|
[key, GoTranslateYourself.current_store.default_translation("dev.#{key}")] [key, GoTranslateYourself.current_store.default_translation("dev.#{key}")]
end.flatten 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 @locales = GoTranslateYourself.locales
render :layout => GoTranslateYourself.layout_name render :layout => GoTranslateYourself.layout_name
end end
Expand Down
Expand Up @@ -11,7 +11,7 @@ def translation_text_field(locale, key, default_translation)
end end
text_field_tag("translations[#{locale}]#{key_parts}", text_field_tag("translations[#{locale}]#{key_parts}",
translation_text || "", translation_text || "",
:size => (default_translation.size + 20)) :size => (default_translation.size))
end end
end end
end end
12 changes: 12 additions & 0 deletions app/helpers/go_translate_yourself/translation_table_helper.rb
@@ -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
11 changes: 11 additions & 0 deletions app/helpers/go_translate_yourself/translation_tabs_helper.rb
@@ -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
23 changes: 23 additions & 0 deletions app/views/go_translate_yourself/site_translations/_tabs.html.erb
@@ -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>
32 changes: 2 additions & 30 deletions app/views/go_translate_yourself/site_translations/edit.html.erb
@@ -1,33 +1,5 @@
<h2>Translations Editor</h2>
<%= form_tag update_site_translations_path, :method => :post do %> <%= form_tag update_site_translations_path, :method => :post do %>
<table class="gt_site_translations"> <%= tabs_for(@translations_split) -%>
<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>
<%= submit_tag "Save" %> <%= submit_tag "Save" %>
<% end %> <% end %>

1 change: 1 addition & 0 deletions lib/go_translate_yourself/engine.rb
@@ -1,3 +1,4 @@
require 'go_translate_yourself/hash_from_array'
module GoTranslateYourself module GoTranslateYourself
class Engine < Rails::Engine class Engine < Rails::Engine
end end
Expand Down
5 changes: 5 additions & 0 deletions lib/go_translate_yourself/hash_from_array.rb
@@ -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.