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

I18n::InvalidLocale (:en is not a valid locale): #549

Closed
brentgreeff opened this issue Apr 4, 2022 · 8 comments
Closed

I18n::InvalidLocale (:en is not a valid locale): #549

brentgreeff opened this issue Apr 4, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@brentgreeff
Copy link

We have an application with config.i18n.available_locales = [:fr, :nl]

Crazy but we dont support English - as important as it is in the world :)

I might have some time to hack on this if you want me to try?

Processing by GoodJob::ExecutionsController#index as HTML
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.0ms | Allocations: 901)

I18n::InvalidLocale (:en is not a valid locale):
  
i18n (1.10.0) lib/i18n.rb:357:in `enforce_available_locales!'
i18n (1.10.0) lib/i18n/config.rb:15:in `locale='
actionview (6.1.4.3) lib/action_view/lookup_context.rb:296:in `locale='
actionview (6.1.4.3) lib/action_view/rendering.rb:21:in `locale='
i18n (1.10.0) lib/i18n.rb:74:in `locale='
i18n (1.10.0) lib/i18n.rb:326:in `with_locale'
good_job (2.11.3) engine/app/controllers/good_job/base_controller.rb:30:in `switch_locale'
@morgoth
Copy link
Collaborator

morgoth commented Apr 4, 2022

I guess GJ should use I18n.default_locale. Not sure if this would have any negative effects that are not visible right now.

The language switch was added to ensure consistent UI, vide #407

@bensheldon
Copy link
Owner

Interesting! Maybe I18n.default_locale || :en. There are some changes in #497 around locale. I'll make a repro app and get this sorted.

@bensheldon
Copy link
Owner

bensheldon commented Apr 6, 2022

@bensheldon bensheldon added the bug Something isn't working label Apr 6, 2022
@brentgreeff
Copy link
Author

Fetching good_job 2.12.0 (was 2.11.3)

I get the same error

Processing by GoodJob::ExecutionsController#index as HTML
  Parameters: {"poll"=>"value value value value value value value value value ", "job_class"=>"", "state"=>"finished", "queue_name"=>"", "query"=>"fail@example.com"}
Completed 500 Internal Server Error in 30ms (ActiveRecord: 0.0ms | Allocations: 3322)  

I18n::InvalidLocale (:en is not a valid locale):
  
i18n (1.10.0) lib/i18n.rb:357:in `enforce_available_locales!'
i18n (1.10.0) lib/i18n/config.rb:15:in `locale='
actionview (6.1.4.3) lib/action_view/lookup_context.rb:296:in `locale='
actionview (6.1.4.3) lib/action_view/rendering.rb:21:in `locale='
i18n (1.10.0) lib/i18n.rb:74:in `locale='
i18n (1.10.0) lib/i18n.rb:326:in `with_locale'
good_job (2.12.0) engine/app/controllers/good_job/application_controller.rb:34:in `switch_locale'
activesupport (6.1.4.3) lib/active_support/callbacks.rb:126:in `block in run_callbacks'
active_decorator (1.4.0) lib/active_decorator/view_context.rb:37:in `block (3 levels) in <module:Filter>'
active_decorator (1.4.0) lib/active_decorator/view_context.rb:25:in `run_with'
active_decorator (1.4.0) lib/active_decorator/view_context.rb:36:in `block (2 levels) in <module:Filter>'
activesupport (6.1.4.3) lib/active_support/callbacks.rb:126:in `instance_exec'

I might be doing something wrong, but my config/application.rb looks like this:

module Website
  class Application < Rails::Application
    config.load_defaults 6.1

    config.i18n.available_locales = [:fr, :nl]
  end
end

adding :en back in - config.i18n.available_locales = [:fr, :nl, :en]
that works.

I could have some time to try fix this on the weekend if you like?

@bensheldon
Copy link
Owner

Are you setting a default_locale?

I think I assumed that there would be a default locale. I guess maybe in the absence of a default locale it should default to the first of available_locales. Does that seem right?

@brentgreeff
Copy link
Author

I don't think I am setting a default_locale anywhere.

In my case - yes the first locale in config.i18n.available_locales is correct when it comes to the good_job_dashboard.

I didn't actually code the locale specific stuff in my app - not that experienced with it. - but am digging into it now.

@bensheldon
Copy link
Owner

bensheldon commented Apr 7, 2022

From my poking at it, here's what I think is happening:

If the application doesn't set a default_locale, the default locale becomes :en even when available_locales does not include :en and I18n.enforce_available_locales is true. e.g. the default_locale is initialized with an unavailable value.

GoodJob is using I18n.with_locale(tmp_locale, &block) and something like this is happening

original_locale = I18n.locale #=> :en, which is not in available_locales
I18n.locale = tmp_locale
block.call
I18n.locale = original_locale #=> raises I18n::InvalidLocale because :en is not an available locale

You can see that happening here:

[1] pry(main)> I18n.default_locale
=> :en
[2] pry(main)> I18n.locale
=> :en
[3] pry(main)> I18n.available_locales
=> [:nl, :fr]
[4] pry(main)> I18n.with_locale(:fr) { puts "current locale is #{I18n.locale}" }
current locale is fr
I18n::InvalidLocale: :en is not a valid locale
from /Users/bensheldon/.rbenv/versions/3.0.3/lib/ruby/gems/3.0.0/gems/i18n-1.10.0/lib/i18n.rb:357:in `enforce_available_locales!'

My conclusion is I think you should be setting an available default_locale within your application because the underlying problem is that I18n is trying to re-assign the original locale, and the original locale was invalid to begin with (and not that GoodJob is assigning an invalid value).

@brentgreeff
Copy link
Author

Thanks - I will do that. - thanks for putting time into this - I thought it must be somewhere in your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

3 participants