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.

Bring back Locale controller from Spree I18n

Damian Legawiec743f8d360eef
diff --git a/core/app/helpers/spree/locale_helper.rb b/core/app/helpers/spree/locale_helper.rb
index 28c890d4bf..5e8c589e31 100644
--- a/core/app/helpers/spree/locale_helper.rb
+++ b/core/app/helpers/spree/locale_helper.rb
@@ -8,8 +8,14 @@ module Spree
available_locales.map { |locale| locale_presentation(locale) }
end
+ def supported_locales_options
+ return if current_store.nil?
+
+ current_store.supported_locales_list.map { |locale| locale_presentation(locale) }
+ end
+
def locale_presentation(locale)
- if defined?(SpreeI18n)
+ if I18n.exists?('spree.i18n.this_file_language', locale: locale)
[Spree.t('i18n.this_file_language', locale: locale), locale]
else
locale.to_s == 'en' ? ['English (US)', :en] : [locale, locale]
diff --git a/frontend/app/controllers/spree/locale_controller.rb b/frontend/app/controllers/spree/locale_controller.rb
index 1d84195d3f..882abbca0d 100644
--- a/frontend/app/controllers/spree/locale_controller.rb
+++ b/frontend/app/controllers/spree/locale_controller.rb
@@ -1,14 +1,12 @@
module Spree
class LocaleController < Spree::StoreController
+ def index
+ render :index, layout: false
+ end
+
def set
- session['user_return_to'] = request.referer if request.referer&.starts_with?('http://' + request.host)
- if params[:locale] && I18n.available_locales.map(&:to_s).include?(params[:locale])
- session[:locale] = I18n.locale = params[:locale]
- flash.notice = Spree.t(:locale_changed)
- else
- flash[:error] = Spree.t(:locale_not_changed)
- end
- redirect_back_or_default(spree.root_path)
+ session[:locale] = params[:locale]
+ redirect_back_or_default(root_path(locale: current_locale))
end
end
end
diff --git a/frontend/app/views/spree/locale/index.html.erb b/frontend/app/views/spree/locale/index.html.erb
new file mode 100644
index 0000000000..3eab0ab5ce
--- /dev/null
+++ b/frontend/app/views/spree/locale/index.html.erb
@@ -0,0 +1,15 @@
+<% if current_store.supported_locales_list.size > 1 %>
+ <li id="locale-select" data-hook>
+ <%= form_tag spree.set_locale_path, class: 'navbar-form' do %>
+ <div class="form-group">
+ <label for="switch_to_locale" class="sr-only">
+ <%= Spree.t(:'i18n.language') %>
+ </label>
+ <%= select_tag(:switch_to_locale,
+ options_for_select(supported_locales_options, I18n.locale),
+ class: 'form-control') %>
+ <noscript><%= submit_tag %></noscript>
+ </div>
+ <% end %>
+ </li>
+<% end %>
diff --git a/frontend/config/routes.rb b/frontend/config/routes.rb
index e7bbdec1c0..778b2d21b7 100644
--- a/frontend/config/routes.rb
+++ b/frontend/config/routes.rb
@@ -3,7 +3,9 @@ Spree::Core::Engine.add_routes do
resources :products, only: [:index, :show]
- get '/locale/set', to: 'locale#set'
+ get '/locales', to: 'locale#index', as: :locales
+ get '/locale/set', to: 'locale#set', as: :set_locale
+ get '/currency/set', to: 'currency#set', as: :set_currency
# non-restful checkout stuff
patch '/checkout/update/:state', to: 'checkout#update', as: :update_checkout
@@ -34,6 +36,4 @@ Spree::Core::Engine.add_routes do
get '/api_tokens', to: 'store#api_tokens'
post '/ensure_cart', to: 'store#ensure_cart'
get '/products/:id/related', to: 'products#related'
-
- get '/currency/set', to: 'currency#set', defaults: { format: :json }, as: :set_currency
end
diff --git a/frontend/spec/features/locale_spec.rb b/frontend/spec/features/locale_spec.rb
index 70278d8a10..ee2f4afb7c 100644
--- a/frontend/spec/features/locale_spec.rb
+++ b/frontend/spec/features/locale_spec.rb
@@ -11,6 +11,9 @@ describe 'setting locale', type: :feature do
cart_page: {
header: 'Votre panier',
empty_info: 'Votre panier est vide'
+ },
+ i18n: {
+ this_file_language: 'Français (FR)'
}
})
end
@@ -67,4 +70,32 @@ describe 'setting locale', type: :feature do
it_behaves_like 'translates cart page'
end
+
+ context 'via set_locale endpoint' do
+ before do
+ store.update(default_locale: 'en', supported_locales: 'en,fr')
+ end
+
+ after do
+ I18n.locale = 'en'
+ end
+
+ before { visit spree.set_locale_path(locale: 'fr') }
+
+ it_behaves_like 'translates cart page'
+ end
+
+ context 'locales list endpoint' do
+ before do
+ store.update(default_locale: 'en', supported_locales: 'en,fr')
+ visit spree.locales_path
+ end
+
+ it { expect(page).to have_http_status(:ok) }
+
+ it 'renders the list' do
+ expect(page).to have_text(Spree.t(:'i18n.language'))
+ expect(page).to have_select('switch_to_locale', with_options: ['English (US)', 'Français (FR)'])
+ end
+ end
end