Navigation Menu

Skip to content

Commit

Permalink
Add i18n for number_to_human_size() helper storage units. Translation…
Browse files Browse the repository at this point in the history
… key is number.human.storage_units.

[#1448 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information
yaroslav authored and jeremy committed Nov 23, 2008
1 parent 9d4ae40 commit d361587
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 4 deletions.
7 changes: 3 additions & 4 deletions actionpack/lib/action_view/helpers/number_helper.rb
Expand Up @@ -220,8 +220,6 @@ def number_with_precision(number, *args)
end
end

STORAGE_UNITS = %w( Bytes KB MB GB TB ).freeze

# Formats the bytes in +size+ into a more understandable representation
# (e.g., giving it 1500 yields 1.5 KB). This method is useful for
# reporting file sizes to users. This method returns nil if
Expand Down Expand Up @@ -257,6 +255,7 @@ def number_to_human_size(number, *args)
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)
storage_units = I18n.translate(:'number.human.storage_units', :locale => options[:locale], :raise => true)

unless args.empty?
ActiveSupport::Deprecation.warn('number_to_human_size takes an option hash ' +
Expand All @@ -268,12 +267,12 @@ def number_to_human_size(number, *args)
separator ||= (options[:separator] || defaults[:separator])
delimiter ||= (options[:delimiter] || defaults[:delimiter])

max_exp = STORAGE_UNITS.size - 1
max_exp = storage_units.size - 1
number = Float(number)
exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
number /= 1024 ** exponent
unit = STORAGE_UNITS[exponent]
unit = storage_units[exponent]

begin
escaped_separator = Regexp.escape(separator)
Expand Down
1 change: 1 addition & 0 deletions actionpack/lib/action_view/locale/en.yml
Expand Up @@ -44,6 +44,7 @@
# separator:
delimiter: ""
precision: 1
storage_units: [Bytes, KB, MB, GB, TB]

# Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
datetime:
Expand Down
3 changes: 3 additions & 0 deletions actionpack/test/template/number_helper_i18n_test.rb
Expand Up @@ -10,6 +10,7 @@ def setup
@number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' }
@currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 }
@human_defaults = { :precision => 1 }
@human_storage_units_defaults = %w(Bytes KB MB GB TB)
@percentage_defaults = { :delimiter => '' }
@precision_defaults = { :delimiter => '' }

Expand Down Expand Up @@ -47,6 +48,8 @@ def test_number_to_human_size_translates_human_formats
I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
I18n.expects(:translate).with(:'number.human.format', :locale => 'en',
:raise => true).returns(@human_defaults)
I18n.expects(:translate).with(:'number.human.storage_units', :locale => 'en',
:raise => true).returns(@human_storage_units_defaults)
# can't be called with 1 because this directly returns without calling I18n.translate
number_to_human_size(1025, :locale => 'en')
end
Expand Down

0 comments on commit d361587

Please sign in to comment.