re-add locale switching logic

based on https://guides.rubyonrails.org/i18n.html\#managing-the-locale-across-requests
This commit is contained in:
Georg Gadinger 2023-01-05 15:20:24 +01:00
parent 6dbed2a43a
commit 8323f39ecd
1 changed files with 11 additions and 5 deletions

View File

@ -6,17 +6,23 @@ class ApplicationController < ActionController::Base
before_action :sentry_user_context before_action :sentry_user_context
before_action :configure_permitted_parameters, if: :devise_controller? before_action :configure_permitted_parameters, if: :devise_controller?
before_action :check_locale around_action :switch_locale
before_action :banned? before_action :banned?
before_action :find_active_announcements before_action :find_active_announcements
# check if user wants to read # check if user wants to read
def check_locale def switch_locale(&action)
return I18n.locale = "en" if Rails.env.test? return I18n.with_locale("en", &action) if Rails.env.test?
I18n.locale = "en" locale = params[:lang] || current_user&.locale || cookies[:lang] || "en"
if params[:lang] && current_user.present?
current_user.locale = locale
current_user.save
end
cookies[:lang] = I18n.locale cookies[:lang] = locale
I18n.with_locale(locale, &action)
end end
# check if user got hit by the banhammer of doom # check if user got hit by the banhammer of doom