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

Revert #591, add a regression test for #617 #620

Closed
wants to merge 2 commits into from
Closed

Revert #591, add a regression test for #617 #620

wants to merge 2 commits into from

Conversation

radar
Copy link
Collaborator

@radar radar commented Feb 11, 2022

After #591, and with I18n v1.9.x versions, the fallbacks logic has broken. In this PR, I include a simple test to reproduce the issue as well as reverting #591.

cc @movermeyer / @jeffblake

@radar
Copy link
Collaborator Author

radar commented Feb 11, 2022

@jeffblake I've just confirmed in that test application that:

gem 'i18n', github: "ruby-i18n/i18n", branch: "617"

No longer makes this issue appear.

As it's currently the weekend (or soon to be) in most places of the world, and I am cautious about releasing a new version and disrupting people's weekends, I will be holding off on committing + releasing this one until Tuesday morning Australian Eastern Time.

@radar
Copy link
Collaborator Author

radar commented Feb 11, 2022

It appears that the issue is with defaults here being a single object, and not being duplicated. So when defaults.shift is called, the defaults in the options hash is being changed also. If it was a duplicated object, we wouldn't have this problem.

This change to ActiveModel would also fix this issue, and allow for #591 to be added back into I18n:

diff --git a/activemodel/lib/active_model/naming.rb b/activemodel/lib/active_model/naming.rb
index 72a3e43e71..2b7f02fd0b 100644
--- a/activemodel/lib/active_model/naming.rb
+++ b/activemodel/lib/active_model/naming.rb
@@ -205,7 +205,7 @@ def human(options = {})
       defaults << options[:default] if options[:default]
       defaults << @human

-      options = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults }.merge!(options.except(:default))
+      options = { scope: [@klass.i18n_scope, :models], count: 1, default: defaults.dup }.merge!(options.except(:default))
       I18n.translate(defaults.shift, **options)
     end

However, I no longer make contributions to Rails code itself, after bad interactions with a particular person on the core team.

@jeffblake
Copy link

Thanks @radar ! That branch fixes it.

@movermeyer
Copy link
Contributor

However, I no longer make contributions to Rails code itself, after bad interactions with a particular person on the core team.

@radar Understandable. Sorry you had to go through that.
I'll test out the fix and open a PR against Rails with that change.

Comment on lines +211 to +213
defaults = [:"product/ticket", :product, "Ticket"]
options = {:scope=>[:activerecord, :models], :count=>1, :default=> defaults}
assert_equal("Ticket", I18n.t(defaults.shift, **options))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defaults = [:"product/ticket", :product, "Ticket"]
options = {:scope=>[:activerecord, :models], :count=>1, :default=> defaults}
assert_equal("Ticket", I18n.t(defaults.shift, **options))
defaults = [:product, "Ticket"]
options = {:scope=>[:activerecord, :models], :count=>1, :default=> defaults}
assert_equal("Ticket", I18n.t(:"product/ticket", **options))

@movermeyer
Copy link
Contributor

movermeyer commented Feb 13, 2022

@radar To be clear before I say other things: I'm fine with the revert, but disagree with that regression test as written. I've added a suggestion to fix it.


I reviewed the proposed fix to Rails, and while I agree that works as a workaround, I disagree that's the solution to the problem.

I think it makes sense that they don't provide the original Symbol as part of the defaults (i.e., the shift modifying options['defaults'] is desired; even though their code could better express their intent. Indeed, they have since refactored that code and it's somewhat clearer now). They don't want to do the lookup of the initial value twice, since they shouldn't expect it to be any different the second time around.


#591 is incorrect, since I didn't check/realize that resolve is called in three different cases:

  1. When the value found in the translation file is a Symbol (Code)
  2. When a value is not found, then values from defaults are resolved. (Code)
    • When resolving defaults, you want to do it only at the level of the current locale, before falling back.
    • i.e., Go through all defaults before falling back through fallbacks locales, keeping the behaviour as it has always been.
  3. When the value found in the translation file is a Proc (Code)
    • I haven't yet given enough thought to convince myself about which locale should be used when the Proc returns a Symbol (current locale or the original locale)

Prior to #591, there was no distinction between the cases. #591 should have introduced that distinction.


I'll look into what can be done and open a replacement to #591 (Update: replacement PR is here)
But it won't require a change in the caller's defaults parameters.

@movermeyer
Copy link
Contributor

@radar I've opened an alternative PR to this revert: #622

@radar
Copy link
Collaborator Author

radar commented Feb 14, 2022

Looks like #622 is the go here. I'll be closing this one, and doing a release tomorrow AM including #622.

@radar radar closed this Feb 14, 2022
@radar radar deleted the 617 branch February 14, 2022 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants