Permalink
Browse files

Fixed i18n bulk translate issues in NumberHelper

Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information...
1 parent 177a35e commit dc66469e6464bbb6d7bd6f242731395b6574aca2 @clemens clemens committed with josh Aug 5, 2008
@@ -71,9 +71,9 @@ def number_to_phone(number, options = {})
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 @@ def number_to_currency(number, options = {})
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 @@ def number_with_delimiter(number, *args)
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 @@ def number_with_precision(number, *args)
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 @@ def number_with_precision(number, *args)
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 @@ def number_to_human_size(number, *args)
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 @@ def number_to_human_size(number, *args)
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
@@ -18,35 +18,35 @@ def setup
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

0 comments on commit dc66469

Please sign in to comment.