Skip to content

Commit

Permalink
Merge pull request #2626 from jhawthorn/frontend_locale_set
Browse files Browse the repository at this point in the history
Make frontend's LocaleController compatible with solidus_i18n
  • Loading branch information
jhawthorn authored Apr 4, 2018
2 parents 5dd03bc + 7d5e383 commit 2fb28fb
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 17 deletions.
14 changes: 8 additions & 6 deletions frontend/app/controllers/spree/locale_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@
module Spree
class LocaleController < Spree::StoreController
def set
if request.referer && request.referer.starts_with?('http://' + request.host)
session['user_return_to'] = request.referer
end
if params[:locale] && I18n.available_locales.map(&:to_s).include?(params[:locale])
session[:locale] = I18n.locale = params[:locale]
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[:locale] = requested_locale
I18n.locale = requested_locale
flash.notice = t('spree.locale_changed')
else
flash[:error] = t('spree.locale_not_changed')
end
redirect_back_or_default(spree.root_path)

redirect_to spree.root_path
end
end
end
1 change: 1 addition & 0 deletions frontend/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
resources :products, only: [:index, :show]

get '/locale/set', to: 'locale#set'
post '/locale/set', to: 'locale#set', as: :select_locale

# non-restful checkout stuff
patch '/checkout/update/:state', to: 'checkout#update', as: :update_checkout
Expand Down
57 changes: 57 additions & 0 deletions frontend/spec/controllers/locale_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Spree::LocaleController, type: :controller do
include_context "fr locale"

context 'switch_to_locale specified' do
context "available locale" do
it 'sets locale and redirects' do
get :set, params: { switch_to_locale: 'fr' }
expect(I18n.locale).to eq :fr
expect(response).to redirect_to('/')
expect(session[:locale]).to eq('fr')
expect(flash[:notice]).to eq(I18n.t("spree.locale_changed"))
end
end

context "unavailable locale" do
it 'does not change locale and redirects' do
get :set, params: { switch_to_locale: 'klingon' }
expect(I18n.locale).to eq :en
expect(response).to redirect_to('/')
expect(flash[:error]).to eq(I18n.t("spree.locale_not_changed"))
end
end
end

context 'locale specified' do
context "available locale" do
it 'sets locale and redirects' do
get :set, params: { locale: 'fr' }
expect(I18n.locale).to eq :fr
expect(response).to redirect_to('/')
expect(flash[:notice]).to eq(I18n.t("spree.locale_changed"))
end
end

context "unavailable locale" do
it 'does not change locale and redirects' do
get :set, params: { locale: 'klingon' }
expect(I18n.locale).to eq :en
expect(response).to redirect_to('/')
expect(flash[:error]).to eq(I18n.t("spree.locale_not_changed"))
end
end
end

context 'both locale and switch_to_locale specified' do
it 'uses switch_to_locale value' do
get :set, params: { locale: 'en', switch_to_locale: 'fr' }
expect(I18n.locale).to eq :fr
expect(response).to redirect_to('/')
expect(flash[:notice]).to eq(I18n.t("spree.locale_changed"))
end
end
end
12 changes: 1 addition & 11 deletions frontend/spec/features/locale_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@ def with_locale(locale)
end

context 'shopping cart link and page' do
before do
I18n.backend.store_translations(:fr, spree: {
i18n: { this_file_language: "Français" },
cart: 'Panier',
shopping_cart: 'Panier'
})
end

after do
I18n.reload!
end
include_context "fr locale"

it 'should be in french' do
with_locale('fr') do
Expand Down
15 changes: 15 additions & 0 deletions frontend/spec/support/shared_contexts/locales.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

shared_context 'fr locale' do
before do
I18n.backend.store_translations(:fr, spree: {
i18n: { this_file_language: "Français" },
cart: 'Panier',
shopping_cart: 'Panier'
})
end

after do
I18n.reload!
end
end

0 comments on commit 2fb28fb

Please sign in to comment.