From 1c53816dd4280db78bfbf8d4fac2abe7b6d7d2d3 Mon Sep 17 00:00:00 2001 From: Ondruch Vit Date: Fri, 20 Feb 2009 18:44:22 +0100 Subject: [PATCH] number_to_human_size with default fallback to english --- .../lib/action_view/helpers/number_helper.rb | 22 +++++++++++--- actionpack/test/template/number_helper_test.rb | 29 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index e622f97..618a55b 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -254,8 +254,8 @@ module ActionView options = args.extract_options! options.symbolize_keys! - defaults = I18n.translate(:'number.format', :locale => options[:locale], :raise => true) rescue {} - human = I18n.translate(:'number.human.format', :locale => options[:locale], :raise => true) rescue {} + defaults = translate_with_default_fallback(:'number.format', :locale => options[:locale]) + human = translate_with_default_fallback(:'number.human.format', :locale => options[:locale]) defaults = defaults.merge(human) unless args.empty? @@ -268,10 +268,10 @@ module ActionView separator ||= (options[:separator] || defaults[:separator]) delimiter ||= (options[:delimiter] || defaults[:delimiter]) - storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true) + storage_units_format = translate_with_default_fallback(:'number.human.storage_units.format', :locale => options[:locale]) if number.to_i < 1024 - unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i, :raise => true) + unit = translate_with_default_fallback(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => number.to_i) storage_units_format.gsub(/%n/, number.to_i.to_s).gsub(/%u/, unit) else max_exp = STORAGE_UNITS.size - 1 @@ -281,7 +281,7 @@ module ActionView number /= 1024 ** exponent unit_key = STORAGE_UNITS[exponent] - unit = I18n.translate(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number, :raise => true) + unit = translate_with_default_fallback(:"number.human.storage_units.units.#{unit_key}", :locale => options[:locale], :count => number) begin escaped_separator = Regexp.escape(separator) @@ -296,6 +296,18 @@ module ActionView end end end + + private + + def translate_with_default_fallback(key, options) + options[:raise] = true + begin + I18n.translate(key, options) + rescue + options[:locale] = :en + I18n.translate(key, options) + end + end end end end diff --git a/actionpack/test/template/number_helper_test.rb b/actionpack/test/template/number_helper_test.rb index 9c9f549..d7a116d 100644 --- a/actionpack/test/template/number_helper_test.rb +++ b/actionpack/test/template/number_helper_test.rb @@ -111,6 +111,35 @@ class NumberHelperTest < ActionView::TestCase assert_nil number_to_human_size(nil) end + def test_number_to_human_size_with_nonenglish_locale + I18n.locale = :wokie + assert_equal '0 Bytes', number_to_human_size(0) + assert_equal '1 Byte', number_to_human_size(1) + assert_equal '3 Bytes', number_to_human_size(3.14159265) + assert_equal '123 Bytes', number_to_human_size(123.0) + assert_equal '123 Bytes', number_to_human_size(123) + assert_equal '1.2 KB', number_to_human_size(1234) + assert_equal '12.1 KB', number_to_human_size(12345) + assert_equal '1.2 MB', number_to_human_size(1234567) + assert_equal '1.1 GB', number_to_human_size(1234567890) + assert_equal '1.1 TB', number_to_human_size(1234567890123) + assert_equal '1025 TB', number_to_human_size(1025.terabytes) + assert_equal '444 KB', number_to_human_size(444.kilobytes) + assert_equal '1023 MB', number_to_human_size(1023.megabytes) + assert_equal '3 TB', number_to_human_size(3.terabytes) + assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2) + assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4) + assert_equal("123 Bytes", number_to_human_size("123")) + assert_equal '1.01 KB', number_to_human_size(1.0123.kilobytes, :precision => 2) + assert_equal '1.01 KB', number_to_human_size(1.0100.kilobytes, :precision => 4) + assert_equal '10 KB', number_to_human_size(10.000.kilobytes, :precision => 4) + assert_equal '1 Byte', number_to_human_size(1.1) + assert_equal '10 Bytes', number_to_human_size(10) + #assert_nil number_to_human_size('x') # fails due to API consolidation + assert_nil number_to_human_size(nil) + I18n.locale = :en + end + def test_number_to_human_size_with_options_hash assert_equal '1.18 MB', number_to_human_size(1234567, :precision => 2) assert_equal '3 Bytes', number_to_human_size(3.14159265, :precision => 4) -- 1.5.6