Skip to content

Commit

Permalink
Merge pull request romanbsd#2 from ichiro101/master
Browse files Browse the repository at this point in the history
Added support for arrays, and provided interface for users to explicitly set their from and to locale
  • Loading branch information
Roman Shterenzon committed Nov 23, 2011
2 parents 0b5fa7e + 199dfcf commit e58322b
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 37 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ Add to your Gemfile:

Now visit /translate in your web browser to start translating.

Configuration
-------------

(Optional) You can configure from_locales and to_locales explicitly through your environments/development.rb by adding

config.from_locales = [:en]
config.to_locales = [:ja, :es, :fr]

Where [:en] and [:ja, :es, :fr] could be replaced by locale list of your choice.

Dependencies
------------

Expand All @@ -56,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
31 changes: 31 additions & 0 deletions app/helpers/translate_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
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)
return I18n.available_locales if from_loc.blank?
raise StandardError, "from_locale expected to be an array" if from_loc.class != Array
from_loc
end

def to_locales
to_loc = Rails.application.config.to_locales if Rails.application.config.respond_to?(:to_locales)
return I18n.available_locales if to_loc.blank?
raise StandardError, "to_locales expected to be an array" if to_loc.class != Array
to_loc
end

def simple_filter(labels, param_name = 'filter', selected_value = nil)
selected_value ||= params[param_name]
filter = []
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>
41 changes: 8 additions & 33 deletions app/views/translate/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<%= hidden_field_tag(:filter, params[:filter]) %>
<%= hidden_field_tag(:sort_by, params[:sort_by]) %>
<label>Translate from</label>
<%= select_tag(:from_locale, options_for_select(I18n.available_locales, @from_locale.to_sym)) %> <span>to</span>
<%= select_tag(:to_locale, options_for_select(I18n.available_locales, @to_locale.to_sym)) %>
<%= select_tag(:from_locale, options_for_select(from_locales, @from_locale.to_sym)) %> <span>to</span>
<%= select_tag(:to_locale, options_for_select(to_locales, @to_locale.to_sym)) %>
<%= submit_tag "Display" %>
</p>
</div>
Expand Down 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 e58322b

Please sign in to comment.