Skip to content

Commit

Permalink
Merge pull request #4126 from RyanofWoods/refactor-locale-controllers
Browse files Browse the repository at this point in the history
Refactor frontend and backend locale_controllers
  • Loading branch information
kennyadsl authored Jul 7, 2021
2 parents ac63060 + 6a9fb15 commit 673b4fb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
23 changes: 12 additions & 11 deletions backend/app/controllers/spree/admin/locale_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@ module Spree
module Admin
class LocaleController < Spree::Admin::BaseController
def set
locale = params[:switch_to_locale].to_s.presence
requested_locale = params[:switch_to_locale].to_s.presence

if locale && I18n.available_locales.include?(locale.to_sym)
I18n.locale = locale
session[set_user_language_locale_key] = locale

respond_to do |format|
format.json { render json: { locale: locale, location: spree.admin_url } }
end
if locale_is_available?(requested_locale)
I18n.locale = requested_locale
session[set_user_language_locale_key] = requested_locale
render json: { locale: requested_locale, location: spree.admin_url }
else
respond_to do |format|
format.json { render json: { locale: I18n.locale }, status: 404 }
end
render json: { locale: I18n.locale }, status: 404
end
end

private

def locale_is_available?(locale)
locale && I18n.available_locales.include?(locale.to_sym)
end
end
end
end
11 changes: 8 additions & 3 deletions frontend/app/controllers/spree/locale_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,23 @@
module Spree
class LocaleController < Spree::StoreController
def set
available_locales = Spree.i18n_available_locales
requested_locale = params[:switch_to_locale] || params[:locale]

if requested_locale && available_locales.map(&:to_s).include?(requested_locale)
session[set_user_language_locale_key] = requested_locale
if locale_is_available?(requested_locale)
I18n.locale = requested_locale
session[set_user_language_locale_key] = requested_locale
flash.notice = t('spree.locale_changed')
else
flash[:error] = t('spree.locale_not_changed')
end

redirect_to spree.root_path
end

private

def locale_is_available?(locale)
locale && Spree.i18n_available_locales.include?(locale.to_sym)
end
end
end

0 comments on commit 673b4fb

Please sign in to comment.