Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make frontend's LocaleController compatible with solidus_i18n #2626

Merged
merged 5 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing magic comment # frozen_string_literal: true.


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