Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Added ControllerHelpers::Locale

Damian Legawiec7dea8cdf2153
diff --git a/api/app/controllers/spree/api/base_controller.rb b/api/app/controllers/spree/api/base_controller.rb
index 5b355184c7..ee8522c74b 100644
--- a/api/app/controllers/spree/api/base_controller.rb
+++ b/api/app/controllers/spree/api/base_controller.rb
@@ -6,6 +6,7 @@ module Spree
include Spree::Api::ControllerSetup
include Spree::Core::ControllerHelpers::Store
include Spree::Core::ControllerHelpers::StrongParameters
+ include Spree::Core::ControllerHelpers::Locale
include Spree::Core::ControllerHelpers::Currency
attr_accessor :current_api_user
diff --git a/api/app/controllers/spree/api/v2/base_controller.rb b/api/app/controllers/spree/api/v2/base_controller.rb
index 8f8c86fc17..0df7ee4287 100644
--- a/api/app/controllers/spree/api/v2/base_controller.rb
+++ b/api/app/controllers/spree/api/v2/base_controller.rb
@@ -5,13 +5,12 @@ module Spree
include CanCan::ControllerAdditions
include Spree::Core::ControllerHelpers::StrongParameters
include Spree::Core::ControllerHelpers::Store
+ include Spree::Core::ControllerHelpers::Locale
include Spree::Core::ControllerHelpers::Currency
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
rescue_from CanCan::AccessDenied, with: :access_denied
rescue_from Spree::Core::GatewayError, with: :gateway_error
- before_action :set_user_language
-
def content_type
Spree::Api::Config[:api_v2_content_type]
end
@@ -96,10 +95,6 @@ module Spree
def gateway_error(exception)
render_error_payload(exception.message)
end
-
- def set_user_language
- I18n.locale = current_store.default_locale
- end
end
end
end
diff --git a/core/app/controllers/spree/base_controller.rb b/core/app/controllers/spree/base_controller.rb
index f79302d288..4cfbe50cd5 100644
--- a/core/app/controllers/spree/base_controller.rb
+++ b/core/app/controllers/spree/base_controller.rb
@@ -7,6 +7,7 @@ class Spree::BaseController < ApplicationController
include Spree::Core::ControllerHelpers::Search
include Spree::Core::ControllerHelpers::Store
include Spree::Core::ControllerHelpers::StrongParameters
+ include Spree::Core::ControllerHelpers::Locale
include Spree::Core::ControllerHelpers::Currency
respond_to :html
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index bae50b6306..486261cb7f 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -105,4 +105,5 @@ require 'spree/core/controller_helpers/order'
require 'spree/core/controller_helpers/search'
require 'spree/core/controller_helpers/store'
require 'spree/core/controller_helpers/strong_parameters'
+require 'spree/core/controller_helpers/locale'
require 'spree/core/controller_helpers/currency'
diff --git a/core/lib/spree/core/controller_helpers/common.rb b/core/lib/spree/core/controller_helpers/common.rb
index 0cd5fc1c83..a58f856b53 100644
--- a/core/lib/spree/core/controller_helpers/common.rb
+++ b/core/lib/spree/core/controller_helpers/common.rb
@@ -10,8 +10,6 @@ module Spree
layout :get_layout
- before_action :set_user_language
-
protected
# can be used in views as well as controllers.
@@ -43,12 +41,12 @@ module Spree
private
def set_user_language
- locale = session[:locale]
- locale = store_locale if respond_to?(:store_locale, true) && locale.blank?
- locale = config_locale if respond_to?(:config_locale, true) && locale.blank?
- locale = Rails.application.config.i18n.default_locale if locale.blank?
- locale = I18n.default_locale unless I18n.available_locales.map(&:to_s).include?(locale.to_s)
- I18n.locale = locale
+ ActiveSupport::Deprecation.warn(<<-DEPRECATION, caller)
+ ControllerHelpers::Common#set_user_language is deprecated and will be removed in Spree 5.0.
+ Please use `before_action :set_locale` instead
+ DEPRECATION
+
+ set_locale
end
# Returns which layout to render.
diff --git a/core/lib/spree/core/controller_helpers/locale.rb b/core/lib/spree/core/controller_helpers/locale.rb
new file mode 100644
index 0000000000..3b7a3fb2ee
--- /dev/null
+++ b/core/lib/spree/core/controller_helpers/locale.rb
@@ -0,0 +1,52 @@
+module Spree
+ module Core
+ module ControllerHelpers
+ module Locale
+ extend ActiveSupport::Concern
+
+ included do
+ before_action :set_locale
+
+ helper_method :supported_locales
+ helper_method :supported_locales_for_all_stores
+ helper_method :current_locale
+ helper_method :supported_locale?
+ end
+
+ def set_locale
+ I18n.locale = current_locale
+ end
+
+ def current_locale
+ # session support was previously in SpreeI18n so we would like to keep it for now
+ # for easer upgrade
+ @current_locale ||= if defined?(session) && session.key?(:locale) && supported_locale?(session[:locale])
+ session[:currency]
+ elsif params[:locale].present? && supported_locale?(params[:locale])
+ params[:locale]
+ elsif respond_to?(:config_locale, true)
+ config_locale
+ else
+ current_store.default_locale || Rails.application.config.i18n.default_locale || I18n.default_locale
+ end
+ end
+
+ def supported_locales
+ @supported_locales ||= current_store.supported_locales_list
+ end
+
+ def supported_locale?(locale_code)
+ supported_locales.include?(locale_code)
+ end
+
+ def supported_locales_for_all_stores
+ if defined?(SpreeI18n)
+ (SpreeI18n::Locale.all << :en).map(&:to_s)
+ else
+ [Rails.application.config.i18n.default_locale, I18n.locale, :en].uniq.compact
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/core/spec/lib/spree/core/controller_helpers/locale_spec.rb b/core/spec/lib/spree/core/controller_helpers/locale_spec.rb
new file mode 100644
index 0000000000..e62b82b56d
--- /dev/null
+++ b/core/spec/lib/spree/core/controller_helpers/locale_spec.rb
@@ -0,0 +1,49 @@
+require 'spec_helper'
+
+class FakesController < ApplicationController
+ include Spree::Core::ControllerHelpers::Auth
+ include Spree::Core::ControllerHelpers::Order
+ include Spree::Core::ControllerHelpers::Store
+ include Spree::Core::ControllerHelpers::Locale
+end
+
+describe Spree::Core::ControllerHelpers::Locale, type: :controller do
+ controller(FakesController) {}
+
+ describe '#current_locale' do
+ context 'store with local set' do
+ let!(:store) { create :store, default: true, default_locale: 'fr' }
+
+ it 'returns current store default locale' do
+ expect(controller.current_locale.to_s).to eq('fr')
+ end
+ end
+
+ context 'store without locale set' do
+ let!(:store) { create :store, default: true }
+
+ context 'without I18n.default_locale set' do
+ it 'fallbacks to english' do
+ expect(controller.current_locale.to_s).to eq('en')
+ end
+ end
+
+ context 'with I18n.default_locale set' do
+ before { I18n.default_locale = :de }
+ after { I18n.default_locale = nil }
+
+ it 'fallbacks to the default application locale' do
+ expect(controller.current_locale.to_s).to eq('de')
+ end
+ end
+ end
+ end
+
+ describe '#supported_locales' do
+ let!(:store) { create :store, default: true, default_locale: 'de' }
+
+ it 'returns supported currencies' do
+ expect(controller.supported_locales.to_s).to include('de')
+ end
+ end
+end