ruby-i18n / i18n

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

[BUG] I18n.exists? ignores scope on "Simple" backend #670

Closed nakwa closed 1 year ago

nakwa commented 1 year ago

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 https://github.com/ruby-i18n/i18n/issues/365 and PR https://github.com/ruby-i18n/i18n/pull/482

- if this indeed qualifies as a bug, I can of course take care of the PR.

Thanks!

fatkodima commented 1 year ago

Looks like definitely a bug for me. Please, do open a PR.

radar commented 1 year ago

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.

nakwa commented 1 year ago

Awesome, thanks!