From 3b5a96bdfd96ef3a52b7b8fdc5b0680eaf277d5a Mon Sep 17 00:00:00 2001 From: taitus Date: Thu, 25 Jun 2020 19:53:48 +0200 Subject: [PATCH] Refactor set_locale Add new current_locale method to simplify logic --- app/controllers/application_controller.rb | 24 ++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index fc0430d05..c35bc5b22 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -42,21 +42,23 @@ class ApplicationController < ActionController::Base end def set_locale - if params[:locale] && I18n.available_locales.include?(params[:locale].to_sym) - session[:locale] = params[:locale] + I18n.locale = current_locale + + if current_user && current_user.locale != I18n.locale.to_s + current_user.update(locale: I18n.locale) end - if session[:locale] && I18n.available_locales.include?(session[:locale].to_sym) - locale = session[:locale] + session[:locale] = I18n.locale + end + + def current_locale + if I18n.available_locales.include?(params[:locale]&.to_sym) + params[:locale] + elsif I18n.available_locales.include?(session[:locale]&.to_sym) + session[:locale] else - locale = I18n.default_locale + I18n.default_locale end - - if current_user && current_user.locale != locale.to_s - current_user.update(locale: locale) - end - - I18n.locale = locale end def set_layout