diff --git a/README.md b/README.md index 22fe24c..0e616ab 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/app/controllers/translate_controller.rb b/app/controllers/translate_controller.rb index d06e171..219a75e 100644 --- a/app/controllers/translate_controller.rb +++ b/app/controllers/translate_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/helpers/translate_helper.rb b/app/helpers/translate_helper.rb index 553372e..c4f3052 100644 --- a/app/helpers/translate_helper.rb +++ b/app/helpers/translate_helper.rb @@ -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) diff --git a/app/views/translate/_array_form.html.erb b/app/views/translate/_array_form.html.erb new file mode 100644 index 0000000..9385b7d --- /dev/null +++ b/app/views/translate/_array_form.html.erb @@ -0,0 +1,42 @@ +<% + from_text = lookup(from_locale, key) + to_text = lookup(to_locale, key) + field_name = "key[#{key}]" +%> + +
+
+ <% 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;' %> +
+ <% end %> +
+
+
+ Key:<%=h key %>
+ <% if @files[key] %>
+ File:<%= @files[key].join("
") %>
+ <% end %>
+
+
+ <%= simple_format(h(from_text)) %> +
+<% end %> ++<% 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 %> +
+
+
+ <%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}', \"#{escape_javascript(from_text)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
+
+ Key:<%=h key %>
+ <% if @files[key] %>
+ File:<%= @files[key].join("
") %>
+ <% end %>
+
+
<%= submit_tag "Save Translations" %>
- <% @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}]" - %> -- <%= simple_format(h(from_text)) %> -
+ <% @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 %> -- <% 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 %> -
-
-
- <%= link_to_function 'Auto Translate', "getGoogleTranslation('#{key}', \"#{escape_javascript(from_text)}\", '#{@from_locale}', '#{@to_locale}')", :style => 'padding: 0; margin: 0;' %>
-
- Key:<%=h key %>
- <% if @files[key] %>
- File:<%= @files[key].join("
") %>
- <% end %>
-
-
<%= submit_tag "Save Translations" %>
diff --git a/lib/translate/keys.rb b/lib/translate/keys.rb index f05cdf2..5388c44 100644 --- a/lib/translate/keys.rb +++ b/lib/translate/keys.rb @@ -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