Permalink
Browse files

Fix localization when the Proc gets the object and extra options.

Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information...
lucasmncastro authored and josevalim committed May 3, 2010
1 parent 7f93f13 commit 4a8cd9fa660daaa3078e24c5851353ca377d9213
View
@@ -63,7 +63,7 @@ def localize(locale, object, format = :default, options = {})
if Symbol === format
key = format
type = object.respond_to?(:sec) ? 'time' : 'date'
- format = I18n.t(:"#{type}.formats.#{key}", :locale => locale, :raise => true)
+ format = I18n.t(:"#{type}.formats.#{key}", options.merge(:raise => true, :object => object, :locale => locale))
end
# format = resolve(locale, object, format, options)
@@ -162,7 +162,8 @@ def resolve(locale, object, subject, options = nil)
when Symbol
I18n.translate(subject, (options || {}).merge(:locale => locale, :raise => true))
when Proc
- resolve(locale, object, subject.call(object, options), options = {})
+ date_or_time = options.delete(:object) || object
+ resolve(locale, object, subject.call(date_or_time, options), options = {})
else
subject
end
@@ -40,11 +40,6 @@ def setup
assert_equal 'Mar', I18n.l(@date, :format => '%b', :locale => :de)
end
- # TODO fails, but something along these lines probably should pass
- # define_method "test localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
- # assert_equal '[Sat Mar 01 06:00:00 UTC 2008, {:foo=>"foo"}]', I18n.l(@time, :format => :proc, :foo => 'foo', :locale => :de)
- # end
-
define_method "test localize Date: given an unknown format it does not fail" do
assert_nothing_raised { I18n.l(@date, :format => '%x') }
end
@@ -47,19 +47,6 @@ def setup
assert_equal 'pm', I18n.l(@other_datetime, :format => '%p', :locale => :de)
end
- # TODO fails, but something along these lines probably should pass
- # define_method "test localize DateTime: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
- # if can_store_procs?
- # store_translations :de, :time => { :formats =>
- # :long_ordinalized => lambda { |date, options|
- # tz = " (#{options[:timezone]})" if options[:timezone]
- # "#{date.day}ter %B %Y, %H:%M Uhr#{tz}"
- # }
- # }
- # assert_equal '1ter März 2008, 06:00 Uhr', I18n.l(@datetime, :long_ordinalized)
- # end
- # end
-
define_method "test localize DateTime: given an unknown format it does not fail" do
assert_nothing_raised { I18n.l(@datetime, :format => '%x') }
end
@@ -35,23 +35,47 @@ module Procs
if can_store_procs?
setup_time_proc_translations
date = ::Date.new(2008, 3, 1, 6)
- assert_equal '[:"date.formats.proc", {}]', I18n.l(date, :format => :proc, :locale => :ru)
+ assert_equal '[Sat, 01 Mar 2008, {}]', I18n.l(date, :format => :proc, :locale => :ru)
+ end
+ end
+
+ define_method "test localize Date: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
+ if can_store_procs?
+ setup_time_proc_translations
+ date = ::Date.new(2008, 3, 1, 6)
+ assert_equal '[Sat, 01 Mar 2008, {:foo=>"foo"}]', I18n.l(date, :format => :proc, :foo => 'foo', :locale => :ru)
end
end
define_method "test localize DateTime: given a format that resolves to a Proc it calls the Proc with the object" do
if can_store_procs?
setup_time_proc_translations
datetime = ::DateTime.new(2008, 3, 1, 6)
- assert_equal '[:"time.formats.proc", {}]', I18n.l(datetime, :format => :proc, :locale => :ru)
+ assert_equal '[Sat, 01 Mar 2008 06:00:00 +0000, {}]', I18n.l(datetime, :format => :proc, :locale => :ru)
+ end
+ end
+
+ define_method "test localize DateTime: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
+ if can_store_procs?
+ setup_time_proc_translations
+ datetime = ::DateTime.new(2008, 3, 1, 6)
+ assert_equal '[Sat, 01 Mar 2008 06:00:00 +0000, {:foo=>"foo"}]', I18n.l(datetime, :format => :proc, :foo => 'foo', :locale => :ru)
end
end
define_method "test localize Time: given a format that resolves to a Proc it calls the Proc with the object" do
if can_store_procs?
setup_time_proc_translations
time = ::Time.parse('2008-03-01 6:00 UTC')
- assert_equal '[:"time.formats.proc", {}]', I18n.l(time, :format => :proc, :locale => :ru)
+ assert_equal '[Sat Mar 01 06:00:00 UTC 2008, {}]', I18n.l(time, :format => :proc, :locale => :ru)
+ end
+ end
+
+ define_method "test localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
+ if can_store_procs?
+ setup_time_proc_translations
+ time = ::Time.parse('2008-03-01 6:00 UTC')
+ assert_equal '[Sat Mar 01 06:00:00 UTC 2008, {:foo=>"foo"}]', I18n.l(time, :format => :proc, :foo => 'foo', :locale => :ru)
end
end
@@ -47,11 +47,6 @@ def setup
assert_equal 'pm', I18n.l(@other_time, :format => '%p', :locale => :de)
end
- # TODO fails, but something along these lines probably should pass
- # define_method "test localize Time: given a format that resolves to a Proc it calls the Proc with the object and extra options" do
- # assert_equal '[Sat Mar 01 06:00:00 UTC 2008, {:foo=>"foo"}]', I18n.l(@time, :format => :proc, :foo => 'foo', :locale => :de)
- # end
-
define_method "test localize Time: given an unknown format it does not fail" do
assert_nothing_raised { I18n.l(@time, :format => '%x') }
end

11 comments on commit 4a8cd9f

Collaborator

knapo replied May 11, 2010

I'm getting 18 errors when running tests with this commit :(

I just to clone this project and run the tests and all are ok.
What are the tests that are breaking?

Collaborator

josevalim replied May 11, 2010

I can reproduce. I'm using ruby2ruby and active record (2.3.5).

Collaborator

knapo replied May 11, 2010

It's related to crappy alias_method_chain on interpolate method in activerecord-2.3.5/lib/active_record/i18n_interpolation_deprecation.rb

Collaborator

josevalim replied May 11, 2010

Does anyone have a fix? Otherwise I will need to revert it!

Collaborator

knapo replied May 11, 2010

Well...I would throw those crappy i18n_interpolation_deprecation.rb out from AR and release AR 2.3.6 ;)

Collaborator

josevalim replied May 11, 2010

Done.

Collaborator

josevalim replied May 11, 2010

Btw, 2.3.6 should be released shortly.

Collaborator

knapo replied May 11, 2010

I wasn't serious;) But that's great! It will also simplify the interpolation fix discussed in http://github.com/svenfuchs/i18n/commit/9d12bfa71d06ffbd42d9bb76172e6f0ca1fb474e.

Collaborator

knapo replied May 11, 2010

Are you going to upgrade bundled i18n to 0.4.0 as well?

Collaborator

josevalim replied May 22, 2010

Can you please provide a patch? Bundling the new version and replacing the deprecated syntax {{}} by %{}? In the meanwhile, I will get I18n 0.4.0 release!

Please sign in to comment.