ruby-i18n/i18n

[BUG] Fallbacks do not work for Rails STI models

Closed this issue · 6 comments

What I tried to do

In application.rb:
config.i18n.fallbacks = true

In en.yml:

en:
  activerecord:
    models:
      product:
        one: Product
      product/ticket:
        one: Ticket

In console:

I18n.locale # => :en
Product::Ticket.model_name.human # => Ticket
I18n.locale = :'en-US'
Product::Ticket.model_name.human # => Product

What I expected to happen

Product::Ticket.model_name.human returns Ticket in any other language, i.e. uses the en fallback

The lookup chain is wrong (using I18n-debug gem):

D, [2022-02-07T17:43:21.409149 #26221] DEBUG -- : [i18n-debug] es.activerecord.models.product/ticket => nil
D, [2022-02-07T17:43:21.409224 #26221] DEBUG -- : [i18n-debug] es.activerecord.models.product => nil
D, [2022-02-07T17:43:21.409277 #26221] DEBUG -- : [i18n-debug] en.activerecord.models.product => {:one=>"Product", :other=>"Products"}

Versions of i18n, rails, and anything else you think is necessary

i18n (1.9.1)
i18n-debug (1.2.0)
i18n-tasks (0.9.37)
rails-i18n (7.0.1)

Bonus points for providing an application or a small code example which reproduces the issue.

Thanks! ❤️

radar commented

Hey @jeffblake, sorry for the troubles you're encountering here.

I've just tried to reproduce this in a new Rails application, and I wasn't able to successfully do it:

irb(main):001:0> I18n.locale # => :en
=> :en
irb(main):002:0> Product::Ticket.model_name.human # => Ticket
[i18n-debug] en.activerecord.models.product/ticket => {:one=>"Ticket"}
=> "Ticket"
irb(main):003:0> I18n.locale = :'en-US'
irb(main):004:0> Product::Ticket.model_name.human # => Product
[i18n-debug] en-US.activerecord.models.product/ticket => nil
[i18n-debug] en.activerecord.models.product/ticket => {:one=>"Ticket"}
=> "Ticket"
irb(main):005:0> I18n.fallbacks
=> {:en=>[:en], :"en-US"=>[:"en-US", :en]}

I can confirm I've enabled fallbacks and the translation file is the same. In terms of gems, I've got these relevant ones installed:

  * i18n (1.9.1)
  * i18n-debug (1.2.0)
  * rails-i18n (7.0.1)
  * rails (6.1.4.4)

Could you please put an app up on GitHub that reproduces this issue? I think that would help me to track this down.

Thanks!

radar commented

Thank you. I was now able to reproduce this issue. The script I'm using for this reproduction is this:

p I18n.locale # => :en
puts "Expecting 'Ticket'"
puts "Actual: #{Product::Ticket.model_name.human}" # => Product
p I18n.locale = :'en-US'
puts "Expecting 'Ticket'"
puts "Actual: #{Product::Ticket.model_name.human}" # => Product
p I18n.fallbacks

And I run it with:

bundle exec rails runner repro.rb

It looks like it was a regression in 1.9, as I am unable to reproduce the failure with 1.8.11. I'll try to find that commit that broke this.

radar commented

Looks like this might be the commit: fe72c0f.

commit fe72c0f5bfdb9a91039bede931a1652208dd63ec
Author: Michael Overmeyer <michael.overmeyer@shopify.com>
Date:   Wed Dec 15 15:55:02 2021 -0500

    Resolve `Symbol`s using the original fallback locale

 lib/i18n.rb                          |  1 +
 lib/i18n/backend/fallbacks.rb        | 13 +++++++++-
 lib/i18n/tests/localization/procs.rb |  1 +
 lib/i18n/tests/procs.rb              |  8 +++++-
 test/backend/fallbacks_test.rb       | 48 ++++++++++++++++++++++++++++++++++++

Thanks for finding it, hopefully we can get a fix soon, it's causing a headache for a lot of my customers.

radar commented

Hey @movermeyer 👋 I'm going to revert the above commit (from #591), and add a regression test in for #617 in a separate PR.

Could you please investigate a way that we could make #617 and your work on #591 work in harmony?