Skip to content

Commit

Permalink
arrays can now be supported
Browse files Browse the repository at this point in the history
  • Loading branch information
ichiro101 committed Nov 23, 2011
1 parent 2eb1e34 commit 199dfcf
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 35 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Authors
- Joakim Westerlund (web design)
- Milan Novota (initial Rails 3 support)
- Roman Shterenzon (Rails 3 cleanup and gem packaging)
- Ichiro Yamamoto

Many thanks to http://newsdesk.se for sponsoring the development of this plugin.

Expand Down
20 changes: 17 additions & 3 deletions app/controllers/translate_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def index
end

def translate
I18n.backend.store_translations(@to_locale, Translate::Keys.to_deep_hash(params[:key]))
processed_parameters = process_array_parameters(params[:key])
I18n.backend.store_translations(@to_locale, Translate::Keys.to_deep_hash(processed_parameters))
Translate::Storage.new(@to_locale).write_to_file
Translate::Log.new(@from_locale, @to_locale, params[:key].keys).write_to_file
force_init_translations # Force reload from YAML file
Expand All @@ -39,8 +40,8 @@ def initialize_keys
@keys.reject! do |key|
from_text = lookup(@from_locale, key)
# When translating from one language to another, make sure there is a text to translate from.
# Always exclude non string translation objects as we don't support editing them in the UI.
(@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String))
# The only supported formats are String and Array. We don't support other formats
(@from_locale != @to_locale && !from_text.present?) || (from_text.present? && !from_text.is_a?(String) && !from_text.is_a?(Array))
end
end

Expand Down Expand Up @@ -162,4 +163,17 @@ def old_from_text(key)
def log_hash
@log_hash ||= Translate::Log.new(@from_locale, @to_locale, {}).read
end

def process_array_parameters(parameter)
reconstructed_hash = Hash.new

parameter.each do |key, value|
if value.is_a?(String)
reconstructed_hash[key] = value
elsif value.is_a?(Hash)
reconstructed_hash[key] = Translate::Keys.arraylize(value)
end
end
reconstructed_hash
end
end
15 changes: 15 additions & 0 deletions app/helpers/translate_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
module TranslateHelper

def render_translate_form(from_locale, to_locale, key)
from_text = lookup(from_locale, key)
if from_text.is_a?(String)
render :partial => 'string_form', :locals =>
{:from_locale => from_locale,
:to_locale => to_locale,
:key => key}
elsif from_text.is_a?(Array)
render :partial => 'array_form', :locals =>
{:from_locale => from_locale,
:to_locale => to_locale,
:key => key}
end
end

def from_locales
# Attempt to get the list of locale from configuration
from_loc = Rails.application.config.from_locales if Rails.application.config.respond_to?(:from_locales)
Expand Down
42 changes: 42 additions & 0 deletions app/views/translate/_array_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<%
from_text = lookup(from_locale, key)
to_text = lookup(to_locale, key)
field_name = "key[#{key}]"
%>

<div class="translation">
<% if from_text.present? %>
<p class="translation-text">
<ol>
<% from_text.each_with_index do |from_text_section, index| %>
<%
from_text_section = from_text_section.to_s
line_size = 100
n_lines = n_lines(from_text_section, line_size)
# this is needed so the controller doesn't freak out when there is no translations found
# for this element yet...
to_text = Array.new if to_text.blank?
%>
<li><%= from_text_section %></li>
<p class="edit-form">
<% if n_lines > 1 %>
<%= text_area_tag("#{field_name}[#{index}]", to_text[index], :size => "#{line_size}x#{n_lines}", :id => "#{key}[#{index}]") %>
<% else %>
<%= text_field_tag("#{field_name}[#{index}]", to_text[index], :size => line_size, :id => "#{key}[#{index}]") %>
<% end %>
<%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}[#{index}]', \"#{escape_javascript(from_text_section)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
</p>
<% end %>
</ol>
</p>
<% end %>
<p>
<em>
<br/>
<strong>Key:</strong><%=h key %><br/>
<% if @files[key] %>
<strong>File:</strong><%= @files[key].join("<br/>") %>
<% end %>
</em>
</p>
</div>
2 changes: 1 addition & 1 deletion app/views/translate/_pagination.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
<% end %>
</ul>
</div>
<% end %>
<% end %>
32 changes: 32 additions & 0 deletions app/views/translate/_string_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<%
from_text = lookup(from_locale, key)
to_text = lookup(to_locale, key)
line_size = 100
n_lines = n_lines(from_text, line_size)
field_name = "key[#{key}]"
%>

<div class="translation">
<% if from_text.present? %>
<p class="translation-text">
<%= simple_format(h(from_text)) %>
</p>
<% end %>
<p class="edit-form">
<% if n_lines > 1 %>
<%= text_area_tag(field_name, to_text, :size => "#{line_size}x#{n_lines}", :id => key) %>
<% else %>
<%= text_field_tag(field_name, to_text, :size => line_size, :id => key) %>
<% end %>
</p>
<p>
<em>
<%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}', \"#{escape_javascript(from_text)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
<br/>
<strong>Key:</strong><%=h key %><br/>
<% if @files[key] %>
<strong>File:</strong><%= @files[key].join("<br/>") %>
<% end %>
</em>
</p>
</div>
37 changes: 6 additions & 31 deletions app/views/translate/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,13 @@
<p class="translate">
<%= submit_tag "Save Translations" %>
</p>
<% @paginated_keys.each do |key|
from_text = lookup(@from_locale, key)
to_text = lookup(@to_locale, key)
line_size = 100
n_lines = n_lines(from_text, line_size)
field_name = "key[#{key}]"
%>
<div class="translation">
<% if from_text.present? %>
<p class="translation-text">
<%= simple_format(h(from_text)) %>
</p>
<% @paginated_keys.each do |key| %>
<%=
# this is a helper method, we need to determine if the thing we are trying to translate is
# a string or an array, and it would be very messey if i put everything in this view
render_translate_form(@from_locale, @to_locale, key)
%>
<% end %>
<p class="edit-form">
<% if n_lines > 1 %>
<%= text_area_tag(field_name, to_text, :size => "#{line_size}x#{n_lines}", :id => key) %>
<% else %>
<%= text_field_tag(field_name, to_text, :size => line_size, :id => key) %>
<% end %>
</p>
<p>
<em>
<%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}', \"#{escape_javascript(from_text)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
<br/>
<strong>Key:</strong><%=h key %><br/>
<% if @files[key] %>
<strong>File:</strong><%= @files[key].join("<br/>") %>
<% end %>
</em>
</p>
</div>
<% end %>
<p class="translate">
<%= submit_tag "Save Translations" %>
</p>
Expand Down
15 changes: 15 additions & 0 deletions lib/translate/keys.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ def self.deep_merge!(hash1, hash2)
hash1.merge!(hash2, &merger)
end

# Convert something like:
#
# {'0' => "elem 1", '1' => "elem 2"}
#
# to:
#
# ["elem 1", "elem 2"]
#
def self.arraylize(input_hash)
input_hash.inject([]) do |constructed_array, (key, value)|
constructed_array << value
constructed_array
end
end

private

def extract_files
Expand Down

0 comments on commit 199dfcf

Please sign in to comment.