diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-01-20 16:37:48 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-01-20 16:37:48 -0300 |
commit | 977f6b497843971419baa815f03f43a0e239a0ac (patch) | |
tree | 6fd49c0b94a3a3425bbc3ace9b42f659e09aac2d | |
parent | f170453493dd7c2d62f7b866867f7a3c8ec623d7 (diff) | |
download | rails-977f6b497843971419baa815f03f43a0e239a0ac.tar.gz rails-977f6b497843971419baa815f03f43a0e239a0ac.tar.bz2 rails-977f6b497843971419baa815f03f43a0e239a0ac.zip |
Refactor translations retrieval
-rw-r--r-- | actionpack/lib/action_view/helpers/number_helper.rb | 33 |
1 files changed, 15 insertions, 18 deletions
diff --git a/actionpack/lib/action_view/helpers/number_helper.rb b/actionpack/lib/action_view/helpers/number_helper.rb index d4f1c22c14..b736f4424c 100644 --- a/actionpack/lib/action_view/helpers/number_helper.rb +++ b/actionpack/lib/action_view/helpers/number_helper.rb @@ -125,11 +125,10 @@ module ActionView options.symbolize_keys! - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - currency = I18n.translate(:'number.currency.format', :locale => options[:locale], :default => {}) + currency = translations_for('currency', options[:locale]) currency[:negative_format] ||= "-" + currency[:format] if currency[:format] - defaults = DEFAULT_CURRENCY_VALUES.merge(defaults).merge!(currency) + defaults = DEFAULT_CURRENCY_VALUES.merge(defaults_translations(options[:locale])).merge!(currency) defaults[:negative_format] = "-" + options[:format] if options[:format] options = defaults.merge!(options) @@ -152,7 +151,6 @@ module ActionView e.number.to_s.html_safe? ? formatted_number.html_safe : formatted_number end end - end # Formats a +number+ as a percentage string (e.g., 65%). You can customize the format in the +options+ hash. @@ -190,9 +188,7 @@ module ActionView options.symbolize_keys! - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - percentage = I18n.translate(:'number.percentage.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(percentage) + defaults = defaults_translations(options[:locale]).merge(translations_for('percentage', options[:locale])) options = options.reverse_merge(defaults) @@ -241,8 +237,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - options = options.reverse_merge(defaults) + options = options.reverse_merge(defaults_translations(options[:locale])) parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") @@ -287,9 +282,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - precision_defaults = I18n.translate(:'number.precision.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(precision_defaults) + defaults = defaults_translations(options[:locale]).merge(translations_for('precision', options[:locale])) options = options.reverse_merge(defaults) # Allow the user to unset default values: Eg.: :significant => false precision = options.delete :precision @@ -357,9 +350,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - human = I18n.translate(:'number.human.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(human) + defaults = defaults_translations(options[:locale]).merge(translations_for('human', options[:locale])) options = options.reverse_merge(defaults) #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files @@ -470,9 +461,7 @@ module ActionView return number end - defaults = I18n.translate(:'number.format', :locale => options[:locale], :default => {}) - human = I18n.translate(:'number.human.format', :locale => options[:locale], :default => {}) - defaults = defaults.merge(human) + defaults = defaults_translations(options[:locale]).merge(translations_for('human', options[:locale])) options = options.reverse_merge(defaults) #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files @@ -512,6 +501,14 @@ module ActionView private + def defaults_translations(locale) + I18n.translate(:'number.format', :locale => locale, :default => {}) + end + + def translations_for(namespace, locale) + I18n.translate(:"number.#{namespace}.format", :locale => locale, :default => {}) + end + def parse_float_number(number, raise_error) Float(number) rescue ArgumentError, TypeError |