Skip to content

Store locale in cookies #865

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

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
include ParamConverters

before_action :authenticate_auth_user!
around_action :switch_locale

default_form_builder SkillsFormBuilder

def switch_locale(&)
locale = params[:locale] || I18n.default_locale
I18n.with_locale(locale, &)
param_locale = params[:locale]
redirect_to(locale: cookies[:locale] || I18n.default_locale) unless param_locale
cookies.permanent[:locale] = param_locale if true?(params[:set_by_user])
I18n.with_locale(param_locale, &)
end

def authenticate_auth_user!
Expand Down Expand Up @@ -43,6 +47,6 @@ def render_error(title_key, body_key, status = :bad_request)
end

def default_url_options
{ locale: I18n.locale == I18n.default_locale ? nil : I18n.locale }
{ locale: I18n.locale }
end
end
4 changes: 2 additions & 2 deletions app/helpers/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def devise?

def language_selector
languages = I18n.available_locales.map { |e| e.to_s }.map do |lang_code|
[language(lang_code).capitalize, url_for(locale: lang_code)]
[language(lang_code).capitalize, url_for(locale: lang_code, set_by_user: true)]
end
options_for_select(languages, url_for(locale: I18n.locale))
options_for_select(languages, url_for(locale: I18n.locale, set_by_user: true))
end

def language(lang_code)
Expand Down
16 changes: 16 additions & 0 deletions spec/controllers/people_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,20 @@
expect(people(:bob).reload.nationality2).to eql("DE")
end
end

describe 'handling of locale' do
it 'it should correctly set locale cookie' do
# Check if locale cookie is not set by default
get :index, params: {locale: :fr}
expect(response.cookies['locale']).to be_nil

# Check if locale cookie is set if it is changed by user
get :index, params: {locale: :fr, set_by_user: true}
expect(response.cookies['locale']).to eql('fr')

# Cookie should not be changed if user requests page with other locale
get :index, params: {locale: :en}
expect(response.cookies['locale']).to be_nil
end
end
end
25 changes: 18 additions & 7 deletions spec/features/routing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

describe "Check auto rerouting" do
ROUTES = {
"/": "/people",
"/": "/de/people",
"/de/": "/de/people",
"/people": "/people",
"/people_skills": "/people_skills",
"/people": "/de/people",
"/people_skills": "/de/people_skills",
"/en": "/en/people",
}

Expand All @@ -29,14 +29,25 @@
context "Set locale via dropdown" do
before(:each) do
visit people_path
select 'Italiano', from: "i18n_language"
default_url_options[:locale] = :it
end

it "Should open profile with correct language" do
it "Should open profile with correct language and preserve language in cookie" do
select 'Italiano', from: "i18n_language"

select_from_slim_select("#person_id_person", bob.name)
expect(page).to have_text("Dati personali")

# Visiting page with locale explicitly defined in route should use locale from route
visit '/en/'
select_from_slim_select("#person_id_person", bob.name)
expect(page).to have_text("Personal details")

# Simulate revisit without a locale in the url, which should use locale from cookie
visit '/'

select_from_slim_select("#person_id_person", bob.name)
expect(page).to have_text("Dati personali")
click_link(href: person_people_skills_path(bob))
click_link(href: person_people_skills_path(bob, locale: :it))
expect(page).to have_text("Nuove competenze per la valutazione")
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/features/tabbar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

before(:each) do
I18n.locale = locale
default_url_options[:locale] = I18n.locale == I18n.default_locale ? nil : I18n.locale
default_url_options[:locale] = I18n.locale
end

after(:each) do
expect(current_path.start_with?("/#{locale}")).to eq(I18n.locale != I18n.default_locale)
expect(current_path).to start_with("/#{locale}")
end

describe 'Global' do
Expand Down
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@
end

config.before(:each, type: :feature) do
default_url_options[:locale] = I18n.locale == I18n.default_locale ? nil : I18n.locale
default_url_options[:locale] = I18n.locale
end
end