Improve set_locale

We discard session[:locale] as valid locale when it is no longer include in
the :available_locales
This commit is contained in:
taitus
2020-05-06 13:45:16 +02:00
parent 002e9239d0
commit ee5ac25cb1
2 changed files with 23 additions and 3 deletions

View File

@@ -46,9 +46,11 @@ class ApplicationController < ActionController::Base
session[:locale] = params[:locale] session[:locale] = params[:locale]
end end
session[:locale] ||= I18n.default_locale if session[:locale] && I18n.available_locales.include?(session[:locale].to_sym)
locale = session[:locale]
locale = session[:locale] else
locale = I18n.default_locale
end
if current_user && current_user.locale != locale.to_s if current_user && current_user.locale != locale.to_s
current_user.update(locale: locale) current_user.update(locale: locale)

View File

@@ -88,4 +88,22 @@ describe "Localization" do
end end
end end
end end
scenario "uses default locale when session locale has disappeared" do
default_locales = I18n.available_locales
visit root_path(locale: :es)
expect(page).to have_content "Entrar"
begin
I18n.available_locales = default_locales - [:es]
visit root_path
expect(page).to have_content "Sign in"
ensure
I18n.available_locales = default_locales
end
end
end end