diff --git a/app/controllers/admin/site_customization/information_texts_controller.rb b/app/controllers/admin/site_customization/information_texts_controller.rb index 6d91ab5fa..fc8a2b834 100644 --- a/app/controllers/admin/site_customization/information_texts_controller.rb +++ b/app/controllers/admin/site_customization/information_texts_controller.rb @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c560ee5ed..c35bc5b22 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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 diff --git a/app/controllers/management/base_controller.rb b/app/controllers/management/base_controller.rb index 6d4070e09..6a2788718 100644 --- a/app/controllers/management/base_controller.rb +++ b/app/controllers/management/base_controller.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 808707a9b..39266a920 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/system/localization_spec.rb b/spec/system/localization_spec.rb index 4056cb8dd..94801ae5d 100644 --- a/spec/system/localization_spec.rb +++ b/spec/system/localization_spec.rb @@ -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