[BUG] I18n.exists? ignores scope on "Simple" backend
Closed this issue · 3 comments
What I tried to do
Hi there,
I am trying to validate a translation key existence using I18n.exists?
running on I18n::Backend::Simple
config/application.rb (Rails)
config.i18n.default_locale = :en
config.i18n.available_locales = [:en, :fr]
config.i18n.fallbacks = true
en:
commons:
hello: Hello
No other language file is defined, hence using fr
should fallback to en
.
What I expected to happen
rails c
I18n.exists?(:"commons.hello", :en)
=> true # it works
I18n.exists?(:"commons.hello", :fr)
=> true # it works
I18n.exists?(:"hello", :fr, scope: [:commons])
=> true # I guess it should work
What actually happened
rails c
I18n.exists?(:"commons.hello", :fr)
=> true # it works
I18n.translate(:"hello", locale: :fr, scope: [:commons])
=> "Hello" # it works
I18n.exists?(:"hello", :fr, scope: [:commons])
=> false # it returns false
I suspect the reason to be this line in the I18n::Backend::Fallbacks
class where exists?
calls super
without passing down **options
:
return true if super(fallback, key)
Patching the method seems to produce the intended result :
return true if super(fallback, key, **options)
I18n.exists?(:"hello", :fr, scope: [:commons])
=> true # it works
However I am not sure to understand if this behavior was implemented on purpose or not.
Versions of i18n, rails, and anything else you think is necessary
Rails 7.0.5
Ruby ruby 3.1.3p185
Gem i18n 1.14.1
For reference: ActiveRecord (ActiveModel) is notably using I18n scope to translate model names
https://github.com/rails/rails/blob/main/activemodel/lib/active_model/naming.rb#L204
(I was tweaking around model_name, that how I ran into this issue).
Also probably worth referencing: issue #365 and PR #482
- if this indeed qualifies as a bug, I can of course take care of the PR.
Thanks!
Looks like definitely a bug for me. Please, do open a PR.
Thanks for opening this. Easier for me to keep track of the conversation with a number next to the title. I remember numbers.
Agree that this looks like a bug to me. The #671 PR will probably fix it. I'm waiting for that to run through CI before I merge.
Awesome, thanks!