[ActiveSupport::Duration] Use the current locale in .inspect


Currently, the .inspect method of ActiveSupport::Duration handles the i18n-ization like this:

    def inspect #:nodoc:
        reduce(::Hash.new(0)) { |h,(l,r)| h[l] += r; h }.
        sort_by {|unit,  _ | [:years, :months, :days, :minutes, :seconds].index(unit)}.
        map     {|unit, val| "#{val} #{val == 1 ? unit.to_s.chop : unit.to_s}"}.
        to_sentence(locale: ::I18n.default_locale)


Which can be frustrating if the user’s current locale is different from the default one.

In my case, a french user would read english versions of inspected durations instead of french ones (ie: “12 hours” instead of “12 heures”)

What I propose is to change this line of code:

to_sentence(locale: ::I18n.default_locale)


To this:

to_sentence(locale: ::I18n.locale)



Your proposed change wouldn’t actually make the change offered up your example (“hours” to “heures”).

The “locale” option here is being passed to to_sentence specifically, so the only thing that would likely change is the word connectors, so “10 years, 1 month and 1 day” would end up as “10 years, 1 month et 1 day”.

You can see this behavior explicitly in the tests added along with the change to use default_locale in the first place here: https://github.com/rails/rails/pull/18462/files