public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Add i18n for number_to_human_size() helper storage units. Translation key is 
number.human.storage_units.

[#1448 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
yaroslav (author)
Sun Nov 23 06:30:59 -0800 2008
jeremy (committer)
Sun Nov 23 13:11:59 -0800 2008
commit  d36158794b19ee8ea49d74061218b37d4301f0f9
tree    1e044f880a2aa931804e7d1674200fa68cafec19
parent  9d4ae40bb40b2354c4061a23ae4db9a28e3174e6
...
220
221
222
223
224
225
226
227
...
257
258
259
 
260
261
262
...
268
269
270
271
 
272
273
274
275
276
 
277
278
279
...
220
221
222
 
 
223
224
225
...
255
256
257
258
259
260
261
...
267
268
269
 
270
271
272
273
274
 
275
276
277
278
0
@@ -220,8 +220,6 @@ module ActionView
0
         end
0
       end
0
 
0
-      STORAGE_UNITS = %w( Bytes KB MB GB TB ).freeze
0
-
0
       # Formats the bytes in +size+ into a more understandable representation
0
       # (e.g., giving it 1500 yields 1.5 KB). This method is useful for
0
       # reporting file sizes to users. This method returns nil if
0
@@ -257,6 +255,7 @@ module ActionView
0
         defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {}
0
         human    = I18n.translate(:'number.human.format', :locale => options[:locale], :raise => true) rescue {}
0
         defaults = defaults.merge(human)
0
+        storage_units = I18n.translate(:'number.human.storage_units', :locale => options[:locale], :raise => true)
0
 
0
         unless args.empty?
0
           ActiveSupport::Deprecation.warn('number_to_human_size takes an option hash ' +
0
@@ -268,12 +267,12 @@ module ActionView
0
         separator ||= (options[:separator] || defaults[:separator])
0
         delimiter ||= (options[:delimiter] || defaults[:delimiter])
0
 
0
-        max_exp  = STORAGE_UNITS.size - 1
0
+        max_exp  = storage_units.size - 1
0
         number   = Float(number)
0
         exponent = (Math.log(number) / Math.log(1024)).to_i # Convert to base 1024
0
         exponent = max_exp if exponent > max_exp # we need this to avoid overflow for the highest unit
0
         number  /= 1024 ** exponent
0
-        unit     = STORAGE_UNITS[exponent]
0
+        unit     = storage_units[exponent]
0
 
0
         begin
0
           escaped_separator = Regexp.escape(separator)
...
44
45
46
 
47
48
49
...
44
45
46
47
48
49
50
0
@@ -44,6 +44,7 @@
0
         # separator: 
0
         delimiter: ""
0
         precision: 1
0
+      storage_units: [Bytes, KB, MB, GB, TB]
0
 
0
   # Used in distance_of_time_in_words(), distance_of_time_in_words_to_now(), time_ago_in_words()
0
   datetime:
...
10
11
12
 
13
14
15
...
47
48
49
 
 
50
51
52
...
10
11
12
13
14
15
16
...
48
49
50
51
52
53
54
55
0
@@ -10,6 +10,7 @@ class NumberHelperI18nTests < Test::Unit::TestCase
0
       @number_defaults = { :precision => 3, :delimiter => ',', :separator => '.' }
0
       @currency_defaults = { :unit => '$', :format => '%u%n', :precision => 2 }
0
       @human_defaults = { :precision => 1 }
0
+      @human_storage_units_defaults = %w(Bytes KB MB GB TB)
0
       @percentage_defaults = { :delimiter => '' }
0
       @precision_defaults = { :delimiter => '' }
0
 
0
@@ -47,6 +48,8 @@ class NumberHelperI18nTests < Test::Unit::TestCase
0
       I18n.expects(:translate).with(:'number.format', :locale => 'en', :raise => true).returns(@number_defaults)
0
       I18n.expects(:translate).with(:'number.human.format', :locale => 'en',
0
                                     :raise => true).returns(@human_defaults)
0
+      I18n.expects(:translate).with(:'number.human.storage_units', :locale => 'en',
0
+                                    :raise => true).returns(@human_storage_units_defaults)
0
       # can't be called with 1 because this directly returns without calling I18n.translate
0
       number_to_human_size(1025, :locale => 'en')
0
     end

Comments