From e22e78545112eaad857ab1e02119e20ce10065d0 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Fri, 19 Oct 2012 16:43:42 -0300 Subject: [PATCH] Add i18n scope to disance_of_time_in_words. This fixes #733. --- .../lib/action_view/helpers/date_helper.rb | 6 +++- .../test/template/date_helper_i18n_test.rb | 29 ++++++++++++------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/actionpack/lib/action_view/helpers/date_helper.rb b/actionpack/lib/action_view/helpers/date_helper.rb index 5464437e42..f43d20c6ed 100644 --- a/actionpack/lib/action_view/helpers/date_helper.rb +++ b/actionpack/lib/action_view/helpers/date_helper.rb @@ -73,13 +73,17 @@ def distance_of_time_in_words(from_time, to_time = 0, include_seconds_or_options options[:include_seconds] ||= !!include_seconds_or_options end + options = { + scope: :'datetime.distance_in_words' + }.merge!(options) + from_time = from_time.to_time if from_time.respond_to?(:to_time) to_time = to_time.to_time if to_time.respond_to?(:to_time) from_time, to_time = to_time, from_time if from_time > to_time distance_in_minutes = ((to_time - from_time)/60.0).round distance_in_seconds = (to_time - from_time).round - I18n.with_options :locale => options[:locale], :scope => :'datetime.distance_in_words' do |locale| + I18n.with_options :locale => options[:locale], :scope => options[:scope] do |locale| case distance_in_minutes when 0..1 return distance_in_minutes == 0 ? diff --git a/actionpack/test/template/date_helper_i18n_test.rb b/actionpack/test/template/date_helper_i18n_test.rb index 63066d40cd..495a9d3f9d 100644 --- a/actionpack/test/template/date_helper_i18n_test.rb +++ b/actionpack/test/template/date_helper_i18n_test.rb @@ -36,16 +36,13 @@ def test_distance_of_time_in_words_calls_i18n end end - def assert_distance_of_time_in_words_translates_key(passed, expected) - diff, passed_options = *passed - key, count = *expected - to = @from + diff - - options = {:locale => 'en', :scope => :'datetime.distance_in_words'} - options[:count] = count if count - - I18n.expects(:t).with(key, options) - distance_of_time_in_words(@from, to, passed_options.merge(:locale => 'en')) + def test_distance_of_time_in_words_calls_i18n_with_custom_scope + { + [30.days, { scope: :'datetime.distance_in_words_ago' }] => [:'about_x_months', 1], + [60.days, { scope: :'datetime.distance_in_words_ago' }] => [:'x_months', 2], + }.each do |passed, expected| + assert_distance_of_time_in_words_translates_key(passed, expected, scope: :'datetime.distance_in_words_ago') + end end def test_time_ago_in_words_passes_locale @@ -74,6 +71,18 @@ def test_distance_of_time_pluralizations assert_equal expected, I18n.t(key, :count => count, :scope => 'datetime.distance_in_words') end end + + def assert_distance_of_time_in_words_translates_key(passed, expected, expected_options = {}) + diff, passed_options = *passed + key, count = *expected + to = @from + diff + + options = { locale: 'en', scope: :'datetime.distance_in_words' }.merge!(expected_options) + options[:count] = count if count + + I18n.expects(:t).with(key, options) + distance_of_time_in_words(@from, to, passed_options.merge(locale: 'en')) + end end class DateHelperSelectTagsI18nTests < ActiveSupport::TestCase