ruby-i18n / i18n

Internationalization (i18n) library for Ruby
MIT License
988 stars 411 forks source link

Use localized date and time formats in `interpolate_hash` #674

Open davidgumberg opened 1 year ago

davidgumberg commented 1 year ago

Interpolated Time's and Date's should respect default formatting options and locale.

Rails, for example, passes Date and Time ActiveRecord attributes to I18n::t.

(This may also avoid triggering some deprecation warnings in Rails eg: https://github.com/rails/rails/issues/48960)

radar commented 1 year ago

@davidgumberg Would you mind adding a test for this please?

davidgumberg commented 1 year ago

@radar Thanks for the review, I've added some tests.

Is this behavior desired?

test "interpolation: given a Date with no default format set it raises I18n::MissingTranslationData" do
    assert_raises(I18n::MissingTranslationData) do
        date = Date.new(2008, 3, 1)
        interpolate(:default => '%{date}', :date => date)
    end
end

I am not really sure how much code there is in the wild that passes Date's and Time's to I18n::t, but this will break anything that does so without a :default format set for Date/Time which might be too aggressive?

The gentler alternative would be to default to sprintf as before if a format is not present.

If you have a chance, let me know what you think.