Skip to content

Commit

Permalink
Merge pull request #5303 from solidusio/elia/admin/locale-fix
Browse files Browse the repository at this point in the history
[Admin] Fix user locale handling in SolidusAdmin
  • Loading branch information
elia authored Aug 4, 2023
2 parents 7cd6dd5 + 9ce3bc6 commit 7eba7c1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
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
32 changes: 32 additions & 0 deletions admin/app/controllers/solidus_admin/controller_helpers/locale.rb
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

0 comments on commit 7eba7c1

Please sign in to comment.