diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-06-24 20:02:52 -0300 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-08-11 00:20:19 -0300 |
commit | 47b4d13c8d7602fc19229dd8cb70974e401b13b2 (patch) | |
tree | d30cfb8970d63e53ecf745f54687395803915507 /activesupport/lib | |
parent | a9dccda936cbd3ead6d43997e6c7990f8bd92055 (diff) | |
download | rails-47b4d13c8d7602fc19229dd8cb70974e401b13b2.tar.gz rails-47b4d13c8d7602fc19229dd8cb70974e401b13b2.tar.bz2 rails-47b4d13c8d7602fc19229dd8cb70974e401b13b2.zip |
Ensure I18n format values always have precedence over defaults
Always merge I18n format values, namespaced or not, over the default
ones, to ensure I18n format defaults will have precedence over our
namespaced values.
Precedence should happen like this:
default :format
default :namespace :format
i18n :format
i18n :namespace :format
Because we cannot allow our namespaced default to override a I18n
:format config - ie precision in I18n :format should always have higher
precedence than our default precision for a particular :namespace.
Also simplify default format options logic.
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/number_helper.rb | 82 |
1 files changed, 40 insertions, 42 deletions
diff --git a/activesupport/lib/active_support/number_helper.rb b/activesupport/lib/active_support/number_helper.rb index 89a58f3701..3849f94a31 100644 --- a/activesupport/lib/active_support/number_helper.rb +++ b/activesupport/lib/active_support/number_helper.rb @@ -8,21 +8,6 @@ module ActiveSupport extend self DEFAULTS = { - # Used in number_to_currency - currency: { - format: { - # Where is the currency sign? %u is the currency unit, %n the number (default: $5.00) - format: "%u%n", - unit: "$", - # These five are to override number.format and are optional - separator: ".", - delimiter: ",", - precision: 2, - significant: false, - strip_insignificant_zeros: false - } - }, - # Used in number_to_delimited # These are also the defaults for 'currency', 'percentage', 'precision', and 'human' format: { @@ -39,6 +24,21 @@ module ActiveSupport strip_insignificant_zeros: false }, + # Used in number_to_currency + currency: { + format: { + format: "%u%n", + negative_format: "-%u%n", + unit: "$", + # These five are to override number.format and are optional + separator: ".", + delimiter: ",", + precision: 2, + significant: false, + strip_insignificant_zeros: false + } + }, + # Used in number_to_percentage percentage: { format: { @@ -202,10 +202,10 @@ module ActiveSupport return unless number options = options.symbolize_keys - currency = translations_for(:currency, options[:locale]) + currency = i18n_format_options(options[:locale], :currency) currency[:negative_format] ||= "-" + currency[:format] if currency[:format] - defaults = defaults_translations(options[:locale]).merge(currency) + defaults = default_format_options(:currency).merge!(currency) defaults[:negative_format] = "-" + options[:format] if options[:format] options = defaults.merge!(options) @@ -256,7 +256,7 @@ module ActiveSupport return unless number options = options.symbolize_keys - defaults = format_translations(:percentage, options[:locale]) + defaults = format_options(options[:locale], :percentage) options = defaults.merge!(options) format = options[:format] || "%n%" @@ -293,7 +293,7 @@ module ActiveSupport return number unless valid_float?(number) - options = defaults_translations(options[:locale]).merge(options) + options = format_options(options[:locale]).merge!(options) parts = number.to_s.to_str.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{options[:delimiter]}") @@ -344,7 +344,7 @@ module ActiveSupport number = Float(number) options = options.symbolize_keys - defaults = format_translations(:precision, options[:locale]) + defaults = format_options(options[:locale], :precision) options = defaults.merge!(options) precision = options.delete :precision @@ -424,7 +424,7 @@ module ActiveSupport return number unless valid_float?(number) number = Float(number) - defaults = format_translations(:human, options[:locale]) + defaults = format_options(options[:locale], :human) options = defaults.merge!(options) #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files @@ -554,7 +554,7 @@ module ActiveSupport return number unless valid_float?(number) number = Float(number) - defaults = format_translations(:human, options[:locale]) + defaults = format_options(options[:locale], :human) options = defaults.merge!(options) #for backwards compatibility with those that didn't add strip_insignificant_zeros to their locale files @@ -598,33 +598,31 @@ module ActiveSupport end private_class_method :private_module_and_instance_method - def format_translations(namespace, locale) #:nodoc: - defaults_translations(locale).merge!(translations_for(namespace, locale)) + def format_options(locale, namespace = nil) #:nodoc: + default_format_options(namespace).merge!(i18n_format_options(locale, namespace)) end - private_module_and_instance_method :format_translations + private_module_and_instance_method :format_options - def defaults_translations(locale) #:nodoc: - defaults = DEFAULTS[:format].dup - i18n_defaults = I18n.translate(:'number.format', :locale => locale, :default => {}) - defaults.merge!(i18n_defaults) + def default_format_options(namespace = nil) #:nodoc: + options = DEFAULTS[:format].dup + options.merge!(DEFAULTS[namespace][:format]) if namespace + options end - private_module_and_instance_method :defaults_translations + private_module_and_instance_method :default_format_options - def translations_for(namespace, locale) #:nodoc: - defaults = DEFAULTS[namespace][:format].dup - i18n_defaults = I18n.translate(:"number.#{namespace}.format", :locale => locale, :default => {}) - defaults.merge!(i18n_defaults) + def i18n_format_options(locale, namespace = nil) #:nodoc: + options = I18n.translate(:'number.format', locale: locale, default: {}).dup + if namespace + options.merge!(I18n.translate(:"number.#{namespace}.format", locale: locale, default: {})) + end + options end - private_module_and_instance_method :translations_for + private_module_and_instance_method :i18n_format_options - def translate_number_value_with_default(key, i18n_options = {}) - defaults_keys = key.split('.') - default = DEFAULTS - while default_key = defaults_keys.shift - default = default[default_key.to_sym] - end + def translate_number_value_with_default(key, i18n_options = {}) #:nodoc: + default = key.split('.').reduce(DEFAULTS) { |defaults, k| defaults[k.to_sym] } - I18n.translate(key, { :default => default, scope: :number }.merge!(i18n_options)) + I18n.translate(key, { default: default, scope: :number }.merge!(i18n_options)) end private_module_and_instance_method :translate_number_value_with_default |