Merge pull request #4001 from rockandror/check-session-locale

Discard session[:locale] when is not valid
This commit is contained in:
Javier Martín
2020-06-25 22:00:37 +02:00
committed by GitHub
5 changed files with 34 additions and 14 deletions

View File

@@ -18,8 +18,9 @@ class Admin::SiteCustomization::InformationTextsController < Admin::SiteCustomiz
next
else
text = I18nContent.find_or_create_by!(key: content[:id])
Globalize.locale = locale
text.update!(value: value)
Globalize.with_locale(locale) do
text.update!(value: value)
end
end
end
end

View File

@@ -42,20 +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
session[:locale] ||= I18n.default_locale
session[:locale] = I18n.locale
end
locale = session[:locale]
if current_user && current_user.locale != locale.to_s
current_user.update(locale: locale)
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
I18n.default_locale
end
I18n.locale = locale
Globalize.locale = I18n.locale
end
def set_layout

View File

@@ -46,7 +46,6 @@ class Management::BaseController < ActionController::Base
session[:locale] ||= I18n.default_locale
I18n.locale = session[:locale]
Globalize.locale = I18n.locale
end
def current_budget

View File

@@ -26,7 +26,6 @@ RSpec.configure do |config|
config.before do |example|
I18n.locale = :en
Globalize.locale = nil
Globalize.set_fallbacks_to_all_available_locales
Setting["feature.user.skip_verification"] = nil
end

View File

@@ -88,4 +88,22 @@ describe "Localization" do
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