Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Wrong locale gets randomly loaded #3140

Closed
sedubois opened this issue May 15, 2019 · 3 comments
Closed

Wrong locale gets randomly loaded #3140

sedubois opened this issue May 15, 2019 · 3 comments

Comments

@sedubois
Copy link
Contributor

sedubois commented May 15, 2019

I have followed the instructions in #2837 (after bumping into the symptoms described in #3134) as follows:

# initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.parent_controller = "Admin::BaseController"
end

# controllers/admin/base_controller.rb
class Admin::BaseController < ApplicationController
  ...
end

# controllers/application_controller.rb
class ApplicationController < ActionController::Base
  before_action :set_locale

  def set_locale
    
  def set_locale
    if [RailsAdmin].include?(self.class.parent)
      I18n.locale = :en
    else
      I18n.locale = get_locale_from_params || get_locale_from_user || get_locale_from_session || get_locale_from_accept || I18n.default_locale
      session[:locale] = I18n.locale
      current_user.update(locale: I18n.locale) if user_signed_in? and current_user.locale != I18n.locale
      I18n.locale
    end
  end
end

Nevertheless, the locale still randomly loads into French in RailsAdmin. Would you have any idea what I am missing? Thank you very much 🙏

Screenshot 2019-05-15 at 12 26 31

@sorenwiz
Copy link

Hi @sedubois, did you resolve this? This issue is killing me as well..

@sorenwiz
Copy link

I think I solved this by setting I18n.default_locale = :en in application.rb

@codealchemy
Copy link
Contributor

Adding an around_action in your custom controller should address this, ie.

# initializers/rails_admin.rb
RailsAdmin.config do |config|
  config.parent_controller = "Admin::BaseController"
end

# controllers/admin/base_controller.rb
class Admin::BaseController < ActionController::Base
  around_action :use_default_locale
  
  private
  
  def use_default_locale(&block)
    # Executes the request with the I18n.default_locale.
    # https://github.com/ruby-i18n/i18n/commit/9b14943d5e814723296cd501283d9343985fca4e
    I18n.with_locale(I18n.default_locale, &block)
  end
end

I18n.default_locale= is a global setting, so I can see that causing some issues if it's being assigned dynamically. Setting I18n.locale can be problematic as well, from the Rails guides:

I18n.locale can leak into subsequent requests served by the same thread/process if it is not consistently set in every controller. For example executing I18n.locale = :es in one POST requests will have effects for all later requests to controllers that don't set the locale, but only in that particular thread/process. For that reason, instead of I18n.locale = you can use I18n.with_locale which does not have this leak issue.

A couple other options for configuring the locale for a request (with examples / suggested libraries) can be found in the Rails guides.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants