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] I18n.exists? ignores scope on "Simple" backend #670

Closed
nakwa opened this issue Jun 20, 2023 · 3 comments · Fixed by #671
Closed

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

nakwa opened this issue Jun 20, 2023 · 3 comments · Fixed by #671

Comments

@nakwa
Copy link

nakwa commented Jun 20, 2023

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!

@fatkodima
Copy link
Contributor

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

@radar
Copy link
Collaborator

radar commented Jun 21, 2023

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

nakwa commented Jun 21, 2023

Awesome, thanks!

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 a pull request may close this issue.

3 participants