6 test "lookup: given a translation is a proc it calls the proc with the key and interpolation values" do
7 I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
8 assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(:a_lambda, :foo => 'foo')
11 test "defaults: given a default is a Proc it calls it with the key and interpolation values" do
12 proc = lambda { |*args| filter_args(*args) }
13 assert_equal '[nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
16 test "defaults: given a default is a key that resolves to a Proc it calls it with the key and interpolation values" do
17 I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
18 assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => :a_lambda, :foo => 'foo')
19 assert_equal '[:a_lambda, {:foo=>"foo"}]', I18n.t(nil, :default => [nil, :a_lambda], :foo => 'foo')
22 test "interpolation: given an interpolation value is a lambda it calls it with key and values before interpolating it" do
23 proc = lambda { |*args| filter_args(*args) }
24 assert_match %r(\[\{:foo=>#<Proc.*>\}\]), I18n.t(nil, :default => '%{foo}', :foo => proc)
27 test "interpolation: given a key resolves to a Proc that returns a string then interpolation still works" do
28 proc = lambda { |*args| "%{foo}: " + filter_args(*args) }
29 assert_equal 'foo: [nil, {:foo=>"foo"}]', I18n.t(nil, :default => proc, :foo => 'foo')
32 test "pluralization: given a key resolves to a Proc that returns valid data then pluralization still works" do
33 proc = lambda { |*args| { :zero => 'zero', :one => 'one', :other => 'other' } }
34 assert_equal 'zero', I18n.t(:default => proc, :count => 0)
35 assert_equal 'one', I18n.t(:default => proc, :count => 1)
36 assert_equal 'other', I18n.t(:default => proc, :count => 2)
39 test "lookup: given the option :resolve => false was passed it does not resolve proc translations" do
40 I18n.backend.store_translations(:en, :a_lambda => lambda { |*args| filter_args(*args) })
41 assert_equal Proc, I18n.t(:a_lambda, :resolve => false).class
44 test "lookup: given the option :resolve => false was passed it does not resolve proc default" do
45 assert_equal Proc, I18n.t(nil, :default => lambda { |*args| filter_args(*args) }, :resolve => false).class
50 def filter_args(*args)
51 args.map {|arg| arg.delete(:fallback) if arg.is_a?(Hash) ; arg }.inspect