Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Fallbacks do not work for Rails STI models #617

Closed
jeffblake opened this issue Feb 7, 2022 · 6 comments · Fixed by #622
Closed

[BUG] Fallbacks do not work for Rails STI models #617

jeffblake opened this issue Feb 7, 2022 · 6 comments · Fixed by #622
Labels

Comments

@jeffblake
Copy link

jeffblake commented Feb 7, 2022

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
Copy link
Collaborator

radar commented Feb 8, 2022

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 radar added the no-repro label Feb 8, 2022
@jeffblake
Copy link
Author

Here is the repro: https://github.com/jeffblake/i18n-bug
Screen Shot 2022-02-09 at 8 55 22 AM

@radar
Copy link
Collaborator

radar commented Feb 9, 2022

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
Copy link
Collaborator

radar commented Feb 9, 2022

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 ++++++++++++++++++++++++++++++++++++

@jeffblake
Copy link
Author

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

@radar
Copy link
Collaborator

radar commented Feb 11, 2022

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants