rails/rails — Issue #4420: I18n fallbacks for the number helpers
Author: mitio
Created: 2012-01-12 13:47:25+00:00
State: open

Possibly related issues:

* #2526 (closed for reasons unknown to me)
* #940 (incorrectly marked as invalid)
* #2802 (still open)
* #2890 (pull request, still open)

I've just stumbled upon the following odd behavior of `NumberHelper#number_with_precision` which has been around for a while:

`NumberHelper#number_with_precision` raises an exception if used when the current `I18n.locale` has no translations for `'number.format'` and `'number.precision.format'`. The exception is a `TypeError: wrong argument type nil (expected Fixnum)` which backtraces to `lib/action_view/helpers/number_helper.rb:318:in 'round'` (for 3-2-stable). Of course, as a result of this, all other helpers which call `number_with_precision` internally, raise the same exception (e.g. `number_to_human_size`).

I think the main problem here is that it's not clear what is the intended behavior in this case. **We need a decision what should happen.** I would suggest a fallback to `:en`. If, however, we would keep the current behavior, we have the following problems:

1. The exception is misleading; as suggested in #2802, maybe it is better to have the option `:raise => true` in the `I18n.translate` calls, so that the developer can see that he's missing translations.
3. [the docs are misleading](https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_view/helpers/number_helper.rb#L260), stating that the default precision is 3 and also not mentioning that the precision actually depends on the current locale. Docs should be updated.

For those who would like to try, here are the steps to reproduce (I've tested this with Rails 3.1.3 and Rails 3-2-stable):

1. Create a blank Rails 3.1+ app
2. Go to `bundle exec rails c` and run the following:
3. `I18n.locale = :de; helper.number_with_precision 1`

The relevant portion of code is at:
https://github.com/rails/rails/blob/3-2-stable/actionpack/lib/action_view/helpers/number_helper.rb#L298

I would vote for an actual fallback to the defaults in `:en`, but at least the docs should be updated. I could also do a pull request on the subject, provided that an agreement on what the behavior should be, is reached.