Skip to content

Commit

Permalink
Merge pull request #10787 from mzazrivec/restore_i18n_in_toolbars
Browse files Browse the repository at this point in the history
Restore internationalization functionality in toolbars
  • Loading branch information
Martin Povolny committed Aug 30, 2016
2 parents 01f74b4 + b43ca55 commit b3d92e3
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
4 changes: 3 additions & 1 deletion app/helpers/application_helper/button/basic.rb
Expand Up @@ -14,7 +14,9 @@ def initialize(view_context, view_binding, instance_data, props)

# Return content under key such as :text or :confirm run through gettext or
# evalated in the context of controller variables and helper methods.
def localized(key)
def localized(key, value = nil)
self[key] = value if value

case self[key]
when NilClass then ''
when Proc then instance_eval(&self[key])
Expand Down
14 changes: 9 additions & 5 deletions app/helpers/application_helper/toolbar_builder.rb
Expand Up @@ -107,10 +107,12 @@ def apply_common_props(button, input)
:data => input[:data]
)

button[:enabled] = input[:enabled]
button[:title] = input[:title] unless input[:title].blank?
button[:text] = input[:text] unless input[:text].blank?
button[:confirm] = input[:confirm] unless input[:confirm].blank?
button[:enabled] = input[:enabled]
%i(title text confirm).each do |key|
unless input[key].blank?
button[key] = button.localized(key, input[key])
end
end
button[:url_parms] = update_url_parms(safer_eval(input[:url_parms])) unless input[:url_parms].blank?

if input[:popup] # special behavior: button opens window_url in a new window
Expand All @@ -125,7 +127,9 @@ def apply_common_props(button, input)
dis_title = build_toolbar_disable_button(button[:child_id] || button[:id])
if dis_title
button[:enabled] = false
button[:title] = dis_title if dis_title.kind_of? String
if dis_title.kind_of? String
button[:title] = button.localized(:title, dis_title)
end
end
button
end
Expand Down
29 changes: 29 additions & 0 deletions spec/helpers/application_helper/toolbar_builder_spec.rb
Expand Up @@ -2476,6 +2476,7 @@ def setup_firefox_with_linux
}
@tb_buttons = {}
@button = {:id => "custom_#{btn_num}"}
@button = ApplicationHelper::Button::Basic.new(nil, nil, {}, {:id => "custom_#{btn_num}"})
allow_any_instance_of(Object).to receive(:query_string).and_return("")
allow_message_expectations_on_nil
end
Expand Down Expand Up @@ -2516,6 +2517,34 @@ def setup_firefox_with_linux
expect(subject).to have_key(:onwhen)
end
end

context "internationalization" do
it "does translation of text title and confirm strings" do
%i(text title confirm).each do |key|
@input[key] = 'Configuration' # common button string, translated into Japanese
end
FastGettext.locale = 'ja'
apply_common_props(@button, @input)
%i(text title confirm).each do |key|
expect(@button[key]).not_to match('Configuration')
end
FastGettext.locale = 'en'
end

it "does delayed translation of text title and confirm strings" do
%i(text title confirm).each do |key|
@input[key] = proc do
_("Add New %{model}") % {:model => 'Model'}
end
end
FastGettext.locale = 'ja'
apply_common_props(@button, @input)
%i(text title confirm).each do |key|
expect(@button[key]).not_to match('Add New Model')
end
FastGettext.locale = 'en'
end
end
end

describe "#build_toolbar_save_button" do
Expand Down

0 comments on commit b3d92e3

Please sign in to comment.