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

[question] How to set the Devise locale for current request (in a thread safe manner)? #282

Closed
collimarco opened this issue May 26, 2020 · 5 comments

Comments

@collimarco
Copy link

Currently I use this code as suggested by the Rails guide:

  # application_controller.rb

  around_action :switch_locale
  def switch_locale(&action)
    locale = params[:locale] || I18n.default_locale
    I18n.with_locale(locale, &action)
  end

This is thread safe and works perfectly for the application... but not for Devise! Devise keeps using the default language (even if the translated YML files are present).

What should I do in order to tell Devise the language for the current user?

I know that there is I18n.locale but it is not thread safe...

@collimarco
Copy link
Author

It seems that some flash messages from devise are translated and others are not.

It may be related to this:
#145

In order to reproduce the issue:

  1. try login with an invalid password
  2. you get an error message (flash) in the wrong language (the default, not the current)

If you log in successfully, instead, the flash message is in the correct language.

@JasonBarnabe
Copy link
Collaborator

I doubt the issue is in this project. Can you take a look at heartcombo/devise#4823 to see if anything helps in there?

@JasonBarnabe
Copy link
Collaborator

I did some testing...

  • If you set locale in before_action with I18n.locale =, then both your app and Devise's error messages are translated properly.
  • If you set locale in around_action with I18n.with_locale, then your app's messages are translated properly, but Devise's error messages are not.

Presumably in the around_action case, the error message is being generated outside the I18n.with_locale block. I'm not sure if this would be considered a bug, but I found wardencommunity/warden#180 that seems to reference the issue.

@excid3
Copy link

excid3 commented Jul 16, 2020

I got looking into this today.

Setting I18n.locale= is thread safe.

https://github.com/ruby-i18n/i18n/blob/3269c01edf3bb9537602bf0467a6bab38b3d9ba6/lib/i18n.rb#L41-L62

From this thread where they discussed changing it in the Rails guide. The change was not made for thread safety as Xavier points out. That's lead to some confusion, but it's safe to use a before_action with I18n.locale=

@JasonBarnabe
Copy link
Collaborator

I've updated the readme to warn against using around_action and I18n.with_locale as described in the Rails guide. There isn't anything to fix in this project - the issue looks like it's in wardencommunity/warden#180.

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

No branches or pull requests

3 participants