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:
@@ -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)
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user