Skip to content

Commit

Permalink
Fix localization when the Proc gets the object and extra options.
Browse files Browse the repository at this point in the history
Signed-off-by: José Valim <jose.valim@gmail.com>
  • Loading branch information
lucasmncastro authored and josevalim committed May 10, 2010
1 parent 7f93f13 commit 4a8cd9f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
5 changes: 3 additions & 2 deletions lib/i18n/backend/base.rb
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
5 changes: 0 additions & 5 deletions test/api/tests/localization/date.rb
Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions test/api/tests/localization/date_time.rb
Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions test/api/tests/localization/procs.rb
Expand Up @@ -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

Expand Down
5 changes: 0 additions & 5 deletions test/api/tests/localization/time.rb
Expand Up @@ -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
Expand Down

11 comments on commit 4a8cd9f

@knapo
Copy link
Collaborator

@knapo knapo commented on 4a8cd9f May 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@lucasmncastro
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@knapo
Copy link
Collaborator

@knapo knapo commented on 4a8cd9f May 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@knapo
Copy link
Collaborator

@knapo knapo commented on 4a8cd9f May 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, 2.3.6 should be released shortly.

@knapo
Copy link
Collaborator

@knapo knapo commented on 4a8cd9f May 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@knapo
Copy link
Collaborator

@knapo knapo commented on 4a8cd9f May 11, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@josevalim
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.