Skip to content
Browse files

Merge pull request #7082 from jalkoby/lazy_lookup_in_abstract_controller

add lazy look up in abstract controller's translate method
  • Loading branch information...
2 parents 58ccc9f + 1b5298e commit 1b39d39af26ae44e4aecb0b48063f08bc5a9a500 @josevalim josevalim committed Jul 18, 2012
Showing with 20 additions and 1 deletion.
  1. +7 −1 actionpack/lib/abstract_controller/translation.rb
  2. +13 −0 actionpack/test/abstract/translation_test.rb
View
8 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
@@ -10,4 +16,4 @@ def localize(*args)
end
alias :l :localize
end
-end
+end
View
13 actionpack/test/abstract/translation_test.rb
@@ -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

0 comments on commit 1b39d39

Please sign in to comment.
Something went wrong with that request. Please try again.