Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add lazy look up in abstract controller's translate method #7082

Merged
merged 1 commit into from Jul 18, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion actionpack/lib/abstract_controller/translation.rb
@@ -1,6 +1,12 @@
module AbstractController
module Translation
def translate(*args)
key = args.first
if key.is_a?(String) && (key[0] == '.')
key = "#{ controller_path.gsub('/', '.') }.#{ action_name }#{ key }"
args[0] = key
end

I18n.translate(*args)
end
alias :t :translate
Expand All @@ -10,4 +16,4 @@ def localize(*args)
end
alias :l :localize
end
end
end
13 changes: 13 additions & 0 deletions actionpack/test/abstract/translation_test.rb
Expand Up @@ -23,4 +23,17 @@ def test_action_controller_base_responds_to_localize
def test_action_controller_base_responds_to_l
assert_respond_to @controller, :l
end

def test_lazy_lookup
expected = 'bar'
@controller.stubs(:action_name => :index)
I18n.stubs(:translate).with('action_controller.base.index.foo').returns(expected)
assert_equal expected, @controller.t('.foo')
end

def test_default_translation
key, expected = 'one.two' 'bar'
I18n.stubs(:translate).with(key).returns(expected)
assert_equal expected, @controller.t(key)
end
end