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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Samy Kacimi
Hello,

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

    def inspect #:nodoc:
     
parts.
       
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)
   
end


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)

Regards,
Samy

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

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

T.J. Schuck
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

On Fri, Jun 22, 2018 at 9:51 AM, Samy Kacimi <[hidden email]> wrote:
Hello,

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

    def inspect #:nodoc:
     
parts.
       
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)
   
end


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)

Regards,
Samy

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.