I18n.backend.translate error when default is empty #120

Closed
vraravam opened this Issue Nov 10, 2011 · 6 comments

Comments

Projects
None yet
4 participants

Rails' ActionView::Helpers::NumberHelper methods (number_to_currency, number_to_percentage) call to its own number_with_precision - which in turn call i18n's I18n::Backend::Base.translate.
The calling example is:

    I18n.translate(:'number.format', :locale => options[:locale], :default => {})

As you can see, the last parameter is an empty hash. Tracing this into the I18n::Backend::Base.translate will result in:

        if options.empty?
          entry = resolve(locale, key, entry, options)
        else
          count, default = options.values_at(:count, :default)
          values = options.except(*RESERVED_KEYS)
          # ************ START BUG FIX ************
          entry = entry.nil? && default.present? && !default.empty? ?
          # ************ END BUG FIX ************
          # ************ START ORIGINAL CODE ************
          # entry = entry.nil? && default ?
          # ************ END ORIGINAL CODE ************
            default(locale, key, default, options) : resolve(locale, key, entry, options)
        end

The fix is in the above lines of code. Could you please include it in the next version?

To test this out in rails console:
include ActionView::Helpers::NumberHelper
I18n.locale = :"en-IN"
number_with_precision(65.445555, :precision => 2) # will result in "6545" without my patch

but with my patch will result in "65.45"

Gist with the full method: https://gist.github.com/1224170

gamov commented Mar 22, 2012

Why this is not included....

ping

Collaborator

tigrish commented Jul 6, 2012

@vraravam can you write a test and submit a pull request?

Collaborator

radar commented Nov 3, 2016

With a config/locales/pl.yml file in my Rails app, calling I18n.translate(:'number.format', :locale => :pl, :default => {}) returns {} on i18n 0.7.0.

If there's still a bug here, please let me know how I can reproduce it.

radar closed this Nov 3, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment