Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Admin] Fix user locale handling in SolidusAdmin #5303

Merged
merged 3 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 3 additions & 17 deletions admin/app/controllers/solidus_admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,15 @@
module SolidusAdmin
class BaseController < ApplicationController
include ActiveStorage::SetCurrent
include ::SolidusAdmin::Auth
include Spree::Core::ControllerHelpers::Store
include Spree::Admin::SetsUserLanguageLocaleKey
include GearedPagination::Controller

include SolidusAdmin::ControllerHelpers::Auth
include SolidusAdmin::ControllerHelpers::Locale
include SolidusAdmin::AuthAdapters::Backend if defined?(Spree::Backend)

include ::GearedPagination::Controller

before_action :set_locale

layout 'solidus_admin/application'
helper 'solidus_admin/container'
helper 'solidus_admin/layout'

private

def set_locale
if params[:switch_to_locale].to_s != session[set_user_language_locale_key].to_s
session[set_user_language_locale_key] = params[:switch_to_locale]
flash[:notice] = t('spree.locale_changed')
end

I18n.locale = session[set_user_language_locale_key] ? session[set_user_language_locale_key].to_sym : I18n.default_locale
end
end
end
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module SolidusAdmin::Auth
module SolidusAdmin::ControllerHelpers::Auth
extend ActiveSupport::Concern

included do
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module SolidusAdmin::ControllerHelpers::Locale
extend ActiveSupport::Concern
include Spree::Admin::SetsUserLanguageLocaleKey

included do
before_action :set_locale
before_action :update_user_locale
end

private

def update_user_locale
requested_locale = params[:switch_to_locale] or return

if requested_locale.to_sym != user_locale
session[set_user_language_locale_key] = requested_locale

Check warning on line 18 in admin/app/controllers/solidus_admin/controller_helpers/locale.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/controller_helpers/locale.rb#L17-L18

Added lines #L17 - L18 were not covered by tests

flash[:notice] = t('spree.locale_changed')
redirect_to url_for(request.params.except(:switch_to_locale))

Check warning on line 21 in admin/app/controllers/solidus_admin/controller_helpers/locale.rb

View check run for this annotation

Codecov / codecov/patch

admin/app/controllers/solidus_admin/controller_helpers/locale.rb#L20-L21

Added lines #L20 - L21 were not covered by tests
end
end

def user_locale
session[set_user_language_locale_key] || I18n.default_locale
end

def set_locale
I18n.locale = user_locale
end
end
2 changes: 1 addition & 1 deletion admin/lib/solidus_admin/preview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ module ControllerHelper
extend ActiveSupport::Concern

included do
include SolidusAdmin::Auth
include SolidusAdmin::ControllerHelpers::Auth
helper ActionView::Helpers
helper SolidusAdmin::ContainerHelper
helper_method :current_component
Expand Down