diff options
author | Clemens Kofler <clemens@railway.at> | 2008-08-04 22:26:14 -0500 |
---|---|---|
committer | Joshua Peek <josh@joshpeek.com> | 2008-08-04 22:26:14 -0500 |
commit | dc66469e6464bbb6d7bd6f242731395b6574aca2 (patch) | |
tree | e22679a14c22e48a922edc8b2cbae6b52f8d3446 | |
parent | 177a35e711e3b21eac0eb19f03aeae7626e490f5 (diff) | |
download | rails-dc66469e6464bbb6d7bd6f242731395b6574aca2.tar.gz rails-dc66469e6464bbb6d7bd6f242731395b6574aca2.tar.bz2 rails-dc66469e6464bbb6d7bd6f242731395b6574aca2.zip |
Fixed i18n bulk translate issues in NumberHelper
Signed-off-by: Joshua Peek <josh@joshpeek.com>
-rw-r--r-- | actionpack/lib/action_view/helpers/number_helper.rb | 56 | ||||
-rw-r--r-- | actionpack/test/template/number_helper_i18n_test.rb | 26 |
2 files changed, 44 insertions, 38 deletions
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index 8c1dea2186..77f19b36a6 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -71,9 +71,9 @@ module ActionView def number_to_currency(number, options = {}) options.symbolize_keys! - defaults, currency = I18n.translate([:'number.format', :'number.currency.format'], - :locale => options[:locale]) || [{},{}] - defaults = defaults.merge(currency) + defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} + currency = I18n.translate(:'number.currency.format', :locale => options[:locale], :raise => true) rescue {} + defaults = defaults.merge(currency) precision = options[:precision] || defaults[:precision] unit = options[:unit] || defaults[:unit] @@ -109,9 +109,9 @@ module ActionView def number_to_percentage(number, options = {}) options.symbolize_keys! - defaults, percentage = I18n.translate([:'number.format', :'number.percentage.format'], - :locale => options[:locale]) || [{},{}] - defaults = defaults.merge(percentage) + defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} + percentage = I18n.translate(:'number.percentage.format', :locale => options[:locale], :raise => true) rescue {} + defaults = defaults.merge(percentage) precision = options[:precision] || defaults[:precision] separator = options[:separator] || defaults[:separator] @@ -151,7 +151,7 @@ module ActionView options = args.extract_options! options.symbolize_keys! - defaults = I18n.translate(:'number.format', :locale => options[:locale]) || {} + defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} unless args.empty? ActiveSupport::Deprecation.warn('number_with_delimiter takes an option hash ' + @@ -195,9 +195,10 @@ module ActionView options = args.extract_options! options.symbolize_keys! - defaults, precision_defaults = I18n.translate([:'number.format', :'number.precision.format'], - :locale => options[:locale]) || [{},{}] - defaults = defaults.merge(precision_defaults) + defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} + precision_defaults = I18n.translate(:'number.precision.format', :locale => options[:locale], + :raise => true) rescue {} + defaults = defaults.merge(precision_defaults) unless args.empty? ActiveSupport::Deprecation.warn('number_with_precision takes an option hash ' + @@ -209,12 +210,14 @@ module ActionView separator ||= (options[:separator] || defaults[:separator]) delimiter ||= (options[:delimiter] || defaults[:delimiter]) - rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision - number_with_delimiter("%01.#{precision}f" % rounded_number, - :separator => separator, - :delimiter => delimiter) - rescue - number + begin + rounded_number = (Float(number) * (10 ** precision)).round.to_f / 10 ** precision + number_with_delimiter("%01.#{precision}f" % rounded_number, + :separator => separator, + :delimiter => delimiter) + rescue + number + end end STORAGE_UNITS = %w( Bytes KB MB GB TB ).freeze @@ -251,8 +254,8 @@ module ActionView options = args.extract_options! options.symbolize_keys! - defaults, human = I18n.translate([:'number.format', :'number.human.format'], - :locale => options[:locale]) || [{},{}] + defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} + human = I18n.translate(:'number.human.format', :locale => options[:locale], :raise => true) rescue {} defaults = defaults.merge(human) unless args.empty? @@ -272,13 +275,16 @@ module ActionView number /= 1024 ** exponent unit = STORAGE_UNITS[exponent] - number_with_precision(number, - :precision => precision, - :separator => separator, - :delimiter => delimiter - ).sub(/(\d)(#{Regexp.escape(separator)}[1-9]*)?0+\z/, '\1') + " #{unit}" - rescue - number + begin + escaped_separator = Regexp.escape(separator) + number_with_precision(number, + :precision => precision, + :separator => separator, + :delimiter => delimiter + ).sub(/(\d)(#{escaped_separator}[1-9]*)?0+\z/, '\1\2').sub(/#{escaped_separator}\z/, '') + " #{unit}" + rescue + number + end end end end diff --git a/actionpack/test/template/number_helper_i18n_test.rb b/actionpack/test/template/number_helper_i18n_test.rb index ce0da398cc..2ee7f43a65 100644 --- a/actionpack/test/template/number_helper_i18n_test.rb +++ b/actionpack/test/template/number_helper_i18n_test.rb @@ -18,35 +18,35 @@ class NumberHelperI18nTests < Test::Unit::TestCase end def test_number_to_currency_translates_currency_formats - I18n.expects(:translate).with( - [:'number.format', :'number.currency.format'], :locale => 'en-US' - ).returns([@number_defaults, @currency_defaults]) + I18n.expects(:translate).with(:'number.format', :locale => 'en-US', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.currency.format', :locale => 'en-US', + :raise => true).returns(@currency_defaults) number_to_currency(1, :locale => 'en-US') end def test_number_with_precision_translates_number_formats - I18n.expects(:translate).with( - [:'number.format', :'number.precision.format'], :locale => 'en-US' - ).returns([@number_defaults, @precision_defaults]) + I18n.expects(:translate).with(:'number.format', :locale => 'en-US', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.precision.format', :locale => 'en-US', + :raise => true).returns(@precision_defaults) number_with_precision(1, :locale => 'en-US') end def test_number_with_delimiter_translates_number_formats - I18n.expects(:translate).with(:'number.format', :locale => 'en-US').returns(@number_defaults) + I18n.expects(:translate).with(:'number.format', :locale => 'en-US', :raise => true).returns(@number_defaults) number_with_delimiter(1, :locale => 'en-US') end def test_number_to_percentage_translates_number_formats - I18n.expects(:translate).with( - [:'number.format', :'number.percentage.format'], :locale => 'en-US' - ).returns([@number_defaults, @percentage_defaults]) + I18n.expects(:translate).with(:'number.format', :locale => 'en-US', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.percentage.format', :locale => 'en-US', + :raise => true).returns(@percentage_defaults) number_to_percentage(1, :locale => 'en-US') end def test_number_to_human_size_translates_human_formats - I18n.expects(:translate).with( - [:'number.format', :'number.human.format'], :locale => 'en-US' - ).returns([@number_defaults, @human_defaults]) + I18n.expects(:translate).with(:'number.format', :locale => 'en-US', :raise => true).returns(@number_defaults) + I18n.expects(:translate).with(:'number.human.format', :locale => 'en-US', + :raise => true).returns(@human_defaults) # can't be called with 1 because this directly returns without calling I18n.translate number_to_human_size(1025, :locale => 'en-US') end |